1、信號(hào)燈
信號(hào)燈集合(可以包含多個(gè)信號(hào)燈),IPC對(duì)象是一個(gè)信號(hào)燈集(多個(gè)信號(hào)量)。
信號(hào)燈
2、信號(hào)燈與信號(hào)量的對(duì)比
信號(hào)燈與信號(hào)量的對(duì)比
3、semget函數(shù)
int semget(key_t key, int nsems, int semflg);
semget函數(shù)
例子:
#include "sys/types.h"
#include "sys/sem.h"
#include "signal.h"
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main()
{
int semid;
semid = semget(IPC_PRIVATE, 3, 0777);
if(semid < 0)
{
printf("create semaphore failure\n");
return -1;
}
printf("create semaphore success semid = %d\n", semid);
system("ipcs -s");
while(1);
return 0;
}
成功創(chuàng)建
4、semctl函數(shù)
semctl函數(shù)
例子:
#include "sys/types.h"
#include "sys/sem.h"
#include "signal.h"
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main()
{
int semid;
semid = semget(IPC_PRIVATE, 3, 0777);
if(semid < 0)
{
printf("create semaphore failure\n");
return -1;
}
printf("create semaphore success semid = %d\n", semid);
system("ipcs -s");
// delete semaphore
semctl(semid, 0, IPC_RMID);
system("ipcs -s");
return 0;
}
成功刪除
5、semop函數(shù)
semop函數(shù)