[Inter-process communication]------ Binary semaphore P/V operation, packaged into dynamic/static library, and used and tested separately

1. Semaphore-related code generation static library

comm.h

 1 #include <stdio.h>                                                                                                                                                                                                                                                                                                        
  2 #include <sys/types.h>
  3 #include <sys/ipc.h>
  4 #include <sys/sem.h>
  5 
  6 
  7 #define PATHNAME "."
  8 #define PROJ_ID 0x6666
  9 
 10 union semun{
 11     int val;
 12     struct semid_ds *buf;
 13     unsigned short *arr;
 14     struct seminfo *_buf;
 15 };
 16 int cerateSemSet(int nums);
 17 int initSem (int semid,int nums,int initVal);
 18 int P(int semid,int who);
 19 int V(int semid,int who);
 20 int destroySemSet(int semid);
 21 
 22 
 23 

comm.c

  1 #include "comm.h"
  2 static int commSemSet(int nums,int flags){
  3     key_t _key=ftok(PATHNAME,PROJ_ID);
  4     if(_key<0){
  5         perror("ftok");
  6         return -1;
  7     }
  8     int semid=semget(_key,nums,flags);
  9     if(semid<0){
 10         perror("semget");
 11         return -2;
 12     }
 13     return semid;
 14 }
 15 
 16 int createSemSet(int nums){
 17     return commSemSet(nums,IPC_CREAT|IPC_EXCL|0666);
 18     }
 19 int getSemSet(int nums){
 20     return commSemSet(nums,IPC_CREAT);
 21 }
 22 int initSem(int semid,int nums,int initVal){
 23     union semun _un;
 24     _un.val=initVal;
 25     if(semctl(semid,nums,SETVAL,_un)<0){
 26         perror("semctl");
 27         return -1;
 28     }
 29     return 0;
 30 }
 31 
 32 static int commPV(int semid,int who,int op){
 33     struct sembuf _sf;
 34     _sf.sem_num=who;
 35     _sf.sem_op=op;
 36     _sf.sem_flg=0;
 37     if(semop(semid,&_sf,1)<0){
 38         perror("semop");
 39         return -1;
 40     }
 41     return 0;
 42 }
 43 
 44 int P(int semid,int who){
 45     return commPV(semid,who,-1);
 46 }
 47 int V(int semid,int who){
 48     return commPV(semid,who,1);
 49 }
 50 int destroySemSet(int semid)
 51 {
 52     if(semctl(semid,0,IPC_RMID)<0){
 53         perror("semctl");
 54         return -1;
 55     }
 56 }

sem.c

1 #include "comm.h"
  2 
  3 int main()
  4 {
  5     int semid=createSemSet(1);
  6     initSem(semid,0,1);
  7     pid_t id=fork();
  8     if(id==0){
  9         int _semid=getSemSet(0);
 10         while(1){
 11             P(_semid,0);
 12             printf("A");
 13             fflush(stdout);
 14             usleep(100000);
 15             printf("A");
 16             fflush(stdout);
 17             usleep(100000);
 18             V(_semid,0);
 19         }
 20     }
 21     else {
 22         while(1){
 23             P(semid,0);
 24             printf("B");
 25             fflush(stdout);
 26             usleep(100000);
 27             printf("B");
 28             fflush(stdout);
 29             usleep(100000);
 30             V(semid,0);
 31         }
 32             wait(NULL);
 33         }
 34         destroySemSet(semid);
 35         return 0;
 36 }                                                                                                                                                                                                                                        

When we generate a static library, we must first generate a static library. We have already implemented this before, so we will not explain it here. After our static library is generated, we can compile and run the program. In this way we have implemented a static library.
write picture description here

2. Implement dynamic library

I have written the program above, and I will not write it here.
The first thing we need is to generate a dynamic library
write picture description here
. When compiling, we found that we could not pass the compilation. In the past, we also encountered a similar situation. The following solutions are provided:
write picture description here
Finally, when compiling, we found that our file has Existed, my solution has been reflected in the following link: https://blog.csdn.net/daboluo521/article/details/80036983 ;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325480948&siteId=291194637