Linux System V IPC 信號量
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
//獲取key值, key值是System V IPC的標識符,成功返回key,失敗返回-1設errno
//同pathname+同 proj_id==>同key_t;
key_t ftok(const char *pathname, int proj_id);
pathname :文件名
proj_id: 1~255的一個數,表示project_id
key_t key=ftok(".",100); //“.”就是一個存在且可訪問的路徑, 100是假設的proj_id
if(-1==key)
perror("ftok"),exit(-1);
//創建/獲取一個信號量集,成功返回semid,失敗返回-1
int semget(key_t key, int nsems, int semflg);
nsems: 信號量集的大小/信號量的個數,0表示獲取已經存在的信號量集
semflg
//create sem
semid=semget(key,1,IPC_CREAT|IPC_EXCL|0664);
if(-1==semid)
perror("semid"),exit(-1);
//主要用於對指定的信號量集/信號量執行指定的操作,成功返回0,失敗返回-1設errno
int semctl(int semid, int semnum, int cmd, ...);
semid: 信號量集的編號(哪個信號量集)
semnum: 信號量集的下標(這個信號量集裡的哪個信號量)
cmd:具體的操作命令
//_GNU_SOURCE
struct seminfo {
int semmap; /* Number of entries in semaphore map; unused within kernel */
int semmni; /* Maximum number of semaphore sets */
int semmns; /* Maximum number of semaphores in all semaphore sets */
int semmnu; /* System-wide maximum number of undo structures; unused within kernel */
int semmsl; /* Maximum number of semaphores in a set */
int semopm; /* Maximum number of operations for semop(2) */
int semume; /* Maximum number of undo entries per process; unused within kernel */
int semusz; /* Size of struct sem_undo */
int semvmx; /* Maximum semaphore value */
int semaem; /* Max. value that can be recorded for semaphore adjustment (SEM_UNDO) */
};
//semmsl, semmns, semopm, semmni可以通過/proc/sys/kernel/sem來設置
the fourth argument:
union semun {
int val; /* Value for SETVAL */
struct semid_ds* buf; /* Buffer for IPC_STAT, IPC_SET */
unsigned short* array; /* Array for GETALL, SETALL */
struct seminfo* __buf; /* Buffer for IPC_INFO(Linux-specific) */
};
//<sys/sem.h>
struct semid_ds {
struct ipc_perm sem_perm; /* Ownership and permissions */
time_t sem_otime; /* Last semop time */
time_t sem_ctime; /* Last change time */
unsigned short sem_nsems; /* No. of semaphores in set */
};
//<sys/ipc.h>
struct ipc_perm {
key_t __key; /* Key supplied to semget(2) */
uid_t uid; /* Effective UID of owner */
gid_t gid; /* Effective GID of owner */
uid_t cuid; /* Effective UID of creator */
gid_t cgid; /* Effective GID of creator */
unsigned short mode; /* Permissions */
unsigned short __seq; /* Sequence number */
};
int res=semctl(semid,0,SETVAL,5);
if(-1==res)
perror("semctl"),exit(-1);
int res=semctl(semid,0,IPC_RMID);
if(-1==res)
perror("semctl"),exit(-1);
//操作指定的信號量集,成功返回0,失敗返回-1設errno
int semop(int semid, struct sembuf *sops, unsigned nsops);
semid:信號集的ID(returned by semget())
sops:結構體指針, 既可以指向結構體變量, 也可以指向結構體數組信號量集本質上是若干個信號量的集合, 可以實現對信號量的批處理
struct sembuf{
unsigned short sem_num; //信號量集的下標
short sem_op; //正數表示增加, 0表示不變, 負數表示��小
short sem_flg; //操作標志,默認給0
}
nsops:結構體指針指向的元素個數, 也就是數組的大小
Sys V IPC sem
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
int main(){
//get key
key_t key=ftok(".",200);
if(-1==key)
perror("ftok"),exit(-1);
printf("key=%d\n",key);
//create sem
int semid=semget(key,0,0);
if(-1==semid)
perror("semget"),exit(-1);
printf("semid=%d\n",semid);
//creat 10 children to take the shared resource
int i=0;
for(i=0;i<10;i++){ //創建10個進程, 當然,需要只給parent或child單獨fork(), 否則就是2^10個進程
pid_t pid=fork();
if(-1==pid)
perror("fork"),exit(-1);
if(0==pid){
struct sembuf buf; //准備占用資源, sem_op-1
buf.sem_num=0; //信號量集下標
buf.sem_op=-1; //信號量-1
buf.sem_flg=0; //操作標志
int res=semop(semid,&buf,1/*結構體變量的個數*/);
if(-1==res)
perror("semop"),exit(-1);
sleep(20); //模擬正在占用共享資源
buf.sem_op=1; //占用完了, sem_op+1
res=semop(semid,&buf,1);
if(-1==res)
perror("semop"),exit(-1);
exit(0); //終止子進程, 自然也就跳出了循環,防止再fork()
// break;
}
}
return 0;
}
//出現搶占的效果, 還沒有全部釋放完畢的時候就有進程搶到了已經釋放的進程