Comm.h文件
#ifndef _COMM_H_
#define _COMM_H_
#include<stdio.h>
#include<stdlib.h>
#include<error.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<unistd.h>
#define PATHNAME "."
#define PROJ_ID 06666
typedef union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminfo *__buf;
void *__pad;
}my_semun;
int Creat_sems(int nsems);
int Init_sems(int semid,int which,int _val);
int Get_sems();
int Destroy_sems(int semid,int sennum);
int P(int semid);
int V(int semid);
#endif
comm.c文件
#include"comm.h"
static Comm_sems(int nsems,int flags)
{
key_t key = ftok(PATHNAME, PROJ_ID);
int semid = semget(key,nsems,flags);
if(semid < 0)
{
perror("semget");
}
return semid;
}
int Creat_sems(int nsems)//创建信号量
{
return Comm_sems(nsems,IPC_CREAT | IPC_EXCL | 0666);