1、信号灯
信号灯集合(可以包含多个信号灯),IPC对象是一个信号灯集(多个信号量)。
2、信号灯与信号量的对比
3、semget函数
int semget(key_t key, int nsems, int semflg);
例子:
#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;
}
4、semctl函数
例子:
#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;
}