It is a counter used to describe the number of certain resources and implement process communication by controlling other communication resources. It is responsible for data mutex and synchronization in this process. Mutual exclusion means that only one process is running in processes A and B within the same period of time. Synchronization. That is, after process A is completed, Process B is completed in A certain execution order. I. What is semaphores?
It is a counter used to describe the number of certain resources and implement process communication by controlling other communication resources. It is responsible for data mutex and synchronization in this process. Mutual exclusion means that only one process is running in processes A and B within the same period of time. Synchronization. That is, after process A is completed, Process B is completed in A certain execution order.
II. Working principle
Two operation modes: P operation and V operation.
P operation (that is, to apply for resources, and to subtract one from the semaphore)
V operation (releasing resources and adding one to the Semaphore)
3. View semid in ipcs-s
Ipcrm-s id delete id
IV. main functions
Shmget create semaphores
Delete shmctl
Shmop P/V operation
Function prototype: int semop (int sem_id, struct sembuf * sops, size_t nsops );
Sem_id is created by using the shmget function.
Struct sembuf * sops parameter sops points to a struct array, and each sembuf struct corresponds to a signal operation. The struct is as follows:
Struct sembuf {unsigned short sem_num; // sem_num is the index of the signal set. 0 indicates the first, 1 indicates the second... Short sem_op; // operation type, 1 --> V operation,-1 --> P operation short sem_flg; // operation Flag };
The sem_flg flag can be set to IPC_NOWAIT or SEM_UNDO. If the operation specifies SEM_UNDO (0 is given below), it will automatically cancel the process at termination.
Nsops is the number of sops
~~ ~~~~~~~~~~ ~~~~ * ********* Man function name allows you to view function usage **********~~~~~~~~~~~~ ~~~~~~~
V. code implementation
Comm. h
#pragma once #include
#include
#include
#include
#include
#include
#define _PATH_ "." #define _PROG_ID_ 0x6675 union semun { int val; struct semid_ds *buf; unsigned short *array; struct seminfo *__buf; }; int creatSem(int nsems); int get_Sem(); int initSem(int sem_id,int Which); int destroySem(int sem_id); int V_Sem(int sem_id,int which); int P_Sem(int sem_id,int which); static int op_Sem(int sem_id,int op,int which);
Comm. c
#include"comm.h" int creatSem(int nsems) { key_t _key=ftok(_PATH_,_PROG_ID_); if(_key<0) { perror("ftok"); return -1; } umask(0); int sem_Flg=IPC_CREAT|IPC_EXCL|0666; int sem_id=semget(_key,nsems,sem_Flg); if(sem_id<0) { perror("semget"); return -1; } return sem_id; } int get_Sem() { key_t k=ftok(_PATH_,_PROG_ID_); return semget(k,0,0); } static int op_Sem(int sem_id,int op,int which) { struct sembuf sem; sem.sem_num=which; sem.sem_op=op; sem.sem_flg=0; return semop(sem_id,&sem,1); } int initSem(int sem_id,int Which) { union semun _semum; _semum.val=1; int ret= semctl(sem_id,Which,SETVAL,_semum); if(ret==-1) { perror("semctl"); return ret; } return ret; } int P_Sem(int sem_id,int which) { int ret=op_Sem(sem_id,-1,which); if(ret==-1) { perror("p_sem"); return -1; } return ret; } int V_Sem(int sem_id,int which) { int ret=op_Sem(sem_id,1,which); if(ret==-1) { perror("V_Sem"); return ret; } return ret; } int destroySem(int sem_id) { int ret=semctl(sem_id,0,IPC_RMID,NULL); if(ret==-1) { perror("semtrl"); return -1; } return ret; }
My_shm.c
#include"comm.h" int main() { int sem_id=creatSem(1); initSem(sem_id,0); pid_t id=fork(); if(id<0) { perror("for"); return -1; } else if(id==0) { int sem_id=get_Sem(); while(1) { P_Sem(sem_id,0); printf("A"); fflush(stdout); sleep(1); printf("A"); fflush(stdout); sleep(2); V_Sem(sem_id,0); } }else { while(1) { P_Sem(sem_id,0); printf("B"); fflush(stdout); sleep(1); printf("B"); fflush(stdout); sleep(1); V_Sem(sem_id,0); } waitpid(id,NULL,0); } }
You can see that the printed results are random without semaphores.
The above is the content of "Linux-inter-process communication-Semaphores". For more information, see The PHP Chinese network (www.php1.cn )!