[semaphore] semaphore

Keywords: Makefile

Semaphore


1. Semaphores and P, V primitives

Semaphores and P, V primitives proposed by Dijstra

Mutual exclusion: P and V are in the same process
Synchronization: P and V in different processes

Semaphore value meaning:
(1) S > 0: s indicates the number of available resources.
(2) S=0: no resources available, no waiting process.
(3) S < 0: | s| indicates the number of processes in the waiting queue.

P primitives:

P(s)
{
    s.value = s.value--;
    if(s.value < 0)
    {
       //The process state is set to wait
       //Insert the PCB of the process into the end of the corresponding waiting queue s.queue
    }
}

V primitives:

V(s)
{
    s.value = s.value++;
    if(s.value <= 0)
    {
       //Wake up a process waiting in the corresponding waiting queue s.queue
       //Change its status to ready and insert it into the ready queue
    }
}

2. Semaphore set function

//(1)semget function
//   Function: used to create and access a semaphore set
//Archetype:
    int semget(key_t key,int nsems,int semflg);
//Parameter: key: name of signal set
//       nsems: number of semaphores in the signal set
//       semflg: it is composed of nine permission flags. Their usage is the same as the mode flag used when creating files
//Return value: 0 for success and - 1 for failure


//(2) shmctl function
//    Function: used to control semaphore set
//Archetype:
    int semctl(int semid,int semnum,int cmd, ...);
//Parameter: semid: signal set identifier returned by semget
//       semnum: number of semaphores in signal set
//       cmd: action to be taken (there are three values)
//       The last parameter is different according to different
//Return value: 0 for success and - 1 for failure


//(3) semop function
//Function: used to create and access a semaphore set
//Archetype:
    int semop(int semid,struct sembuf* sops,unsigned nsops);
//Parameters:
//  semid: the identification code of the semaphore
//  sops: a pointer to a structure value
//  nsops: number of semaphores
//Return value: 0 for success and - 1 for failure


3. Instance code

Note:
sembuf structure

struct sembuf{
    short sem_num;       //Number of semaphore
    short sem_op;        //It is the value added and subtracted by the semaphore in one PV operation. Generally, there are only two - 1(P operation) or + 1 (V operation)
    short sem_flg;       //Two values are IPC? Nowait or SEM? Undo
}
//[comm.h]
#ifndef _COMM_H__
#define _COMM_H__

#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>

#define PATHNAME "."
#define PROJ_ID 0x6666

union semun
{
    int val;
    struct semid_ds* buf;
    unsigned short* array;
    struct seminfo* _buf;
};

int createSemSet(int nums);
int initSem(int semid,int nums,int initval);
int getSemSet(int nums);
int P(int semid,int who);
int V(int semid,int who);
int destorySemSet(int semid);

#endif



//[comm.c]
#include"comm.h"

static int commSemSet(int nums,int flags)
{
    key_t _key=ftok(PATHNAME,PROJ_ID);
    if(_key<0)
    {
        perror("ftok");
        return -1;
    }
    int semid=semget(_key,nums,flags);
    if(semid<0)
    {
        perror("semget");
        return -2;
    }

    return semid;
}

int createSemSet(int nums)
{
    return commSemSet(nums,IPC_CREAT|IPC_EXCL|0666);
}
int getSemSet(int nums)
{
    return commSemSet(nums,IPC_CREAT);
}

int initSem(int semid,int nums,int initval)
{
    union semun _un;
    _un.val=initval;
    if(semctl(semid,nums,SETVAL,_un)<0)
    {
        perror("semctl");
        return -1;
    }
    return 0;
}

static int commPV(int semid,int who,int op)
{
    struct sembuf _sf;
    _sf.sem_num=who;
    _sf.sem_op=op;
    _sf.sem_flg=0;
    if(semop(semid,&_sf,1)<0)
    {
        perror("semop");
        return -1;
    }
    return 0;
} 

int P(int semid,int who)
{
    return commPV(semid,who,-1);
}
int V(int semid,int who)
{
    return commPV(semid,who,1);
}

int destorySemSet(int semid)
{
    if(semctl(semid,0,IPC_RMID)<0)
    {
        perror("semctl");
        return -1;
    }
    return 0;
}

//[sem_test.c]
#include"comm.h"

int main()
{
    int semid=createSemSet(1);
    printf("se=%d\n",semid);
    initSem(semid,0,1);
    pid_t id=fork();
    if(id==0)
    {
        //child
        int _semid=getSemSet(0);
        printf("_semid=%d\n",_semid);
        while(1)
        {
            P(_semid,0);
            printf("A");
            fflush(stdout);
            usleep(200000);
            printf("A ");
            fflush(stdout);
            usleep(200000);
            V(_semid,0);
        }
    }
    else
    {
        //father
        while(1)
        {
            P(semid,0);
            printf("B");
            fflush(stdout);
            usleep(200000);
            printf("B ");
            fflush(stdout);
            usleep(200000);
            V(semid,0);
        }
        wait(NULL);
    }
    destorySemSet(semid);
    return 0;
}

Operation result:

Encapsulate comm.c as a static library:

[lize-h@localhost 0406_SignalNum]$ ls
a.out  comm.c  comm.h  comm.o  Makefile  test_sem.c
[lize-h@localhost 0406_SignalNum]$ gcc -c comm.c -o comm.o     
[lize-h@localhost 0406_SignalNum]$ ls
a.out  comm.c  comm.h  comm.o  Makefile  test_sem.c

Build static library:
[lize-h@localhost 0406_SignalNum]$ ar -rc libmycomm.a comm.o
ar yes gnu Archiving tools, rc Representation ( replace and create)

[lize-h@localhost 0406_SignalNum]$ ar -tv libmycomm.a
rw-rw-r-- 500/500   1676 May  4 21:16 2018 comm.o
t: List files in static library
v: verbose detailed information

Compilation failed without calling static library
[lize-h@localhost 0406_SignalNum]$ gcc test_sem.c
/tmp/ccvjqyzV.o: In function `main':
test_sem.c:(.text+0x11): undefined reference to `createSemSet'
test_sem.c:(.text+0x46): undefined reference to `initSem'
test_sem.c:(.text+0x66): undefined reference to `getSemSet'
test_sem.c:(.text+0x93): undefined reference to `P'
test_sem.c:(.text+0xf2): undefined reference to `V'
test_sem.c:(.text+0x108): undefined reference to `P'
test_sem.c:(.text+0x167): undefined reference to `V'
collect2: ld Return to 1

Call static library
[lize-h@localhost 0406_SignalNum]$ gcc test_sem.c -L. -lmycomm -o comm
-L : Specify library path
-l : library name
//After the test target file is generated, the static library program can still be deleted and run

[lize-h@localhost 0406_SignalNum]$ ls
comm  comm.c  comm.h  comm.o  libmycomm.a  Makefile  test_sem.c 

Encapsulate comm.c as a dynamic library:

[lize-h@localhost 0406_SignalNum]$ ls
comm.c  comm.h  comm.o  libmycomm.a  Makefile  test_sem.c

shared: Represent build shared library
fPIC: Generate position independent code
[lize-h@localhost 0406_SignalNum]$ gcc -fPIC -c comm.c
[lize-h@localhost 0406_SignalNum]$ gcc -shared -o libmycomm.so *.o

[lize-h@localhost 0406_SignalNum]$ ls
comm.c  comm.h  comm.o  libmycomm.a  libmycomm.so  Makefile  test_sem.c

//Call dynamic library for compilation
[lize-h@localhost 0406_SignalNum]$ gcc test_sem.c -L. -lmycomm
[lize-h@localhost 0406_SignalNum]$ ls
a.out  comm.c  comm.h  comm.o  libmycomm.a  libmycomm.so  Makefile  test_sem.c

[lize-h@localhost 0406_SignalNum]$ ./a.out
se=131074
B_semid=131074
B AA BB A^C
[lize-h@localhost 0406_SignalNum]$ 

//Can be .soCopy the file to the system shared library path, usually /usr/lib
//take.soIt is easier to call when the file is placed in the system share path
[lize-h@localhost 0406_SignalNum]$ gcc test_sem.c -lmycomm

Posted by crazycaddy on Fri, 20 Mar 2020 09:11:16 -0700