2.将信号灯集的函数二次封装
sem.c
#include<myhead.h>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) */
};//定义给信号灯集中的指定信号灯进行赋值
int init_semno(int semid,int semno)
{union semun buf;printf("请输入要给编号为%d的灯设置的值:",semno);scanf("%d",&buf.val);//调用控制函数if(semctl(semid,semno,SETVAL,buf) == -1){perror("semctl error");return -1;}return 0;
}//创建或打开共享内存:参数为要申请的信号灯集中包含的灯的个数,返回值信号灯集的id
int open_sem(int semcount)
{//创建key值key_t key = -1;if((key = ftok("/",'s')) == -1){perror("ftok error");return -1;}//通过key值创建一个信号灯集int semid = -1;if((semid = semget(key,semcount,IPC_CREAT|IPC_EXCL|0664)) == -1){//对错误码进行判断,如果错误码为EEXIST,说明信号灯集已经存在if(errno == EEXIST){//直接打开信号灯集即可semid = semget(key ,semcount ,IPC_CREAT|0664);return semid;}perror("semget error");return -1;}//给信号灯集中的每个灯进行初始化操作for(int i=0; i<semcount; i++){init_semno(semid,i); //对编号为i的灯进行初始化}return semid;
}//进行申请资源操作:参数为要申请的信号灯id,以及灯的编码
int P(int semid ,int semno)
{//定义要进行操作的结构体变量struct sembuf buf;buf.sem_num = semno;buf.sem_op = -1; //表示申请资源buf.sem_flg = 0; //表示阻塞方式申请//执行函数if(semop(semid,&buf,1) == -1){perror("P error");return -1;}return 0;
}//进行释放资源操作:参数为要申请的信号灯id,以及灯的编号
int V(int semid,int semno)
{//定义要进行操作的结构体变量struct sembuf buf; buf.sem_num = semno;buf.sem_op = 1; //表示释放资源buf.sem_flg = 0; //表示阻塞方式释放//执行函数if(semop(semid,&buf,1) == -1){perror("V error");return -1;}return 0;}//删除信号灯集的操作:参数为要删除的信号灯集id
int del_sem(int semid)
{//删除信号灯集if(semctl(semid,0,IPC_RMID) == -1){perror("delete error");return -1;}return 0;
}
shmrcv.c
#include<myhead.h>
#include"sem.h"
#define PAGE_SIZE 4096 //一页大小
int main(int argc, const char *argv[])
{//创建信号灯集int semid = open_sem(2);//创建key值key_t key = -1;if((key = ftok("/",'k')) == -1){perror("ftok error");return -1;}printf("key = %d\n", key);//通过key值创建一个共享内存int shmid = -1;if((shmid = shmget(key ,PAGE_SIZE, IPC_CREAT|0664)) == -1){perror("shmget error");return -1;}printf("shmid = %d\n",shmid);//将共享内存段的数据映射到用户空间char *addr = shmat(shmid, NULL,0);//参数2:让系统自动选择对其页//参数3:对共享内存的操作为读写操作if(addr == (void*)-1){perror("shmat error");return -1;}printf("addr = %p\n",addr);//4.使用共享内存while(1){//申请1号灯的资源P(semid,1);printf("收到数据为:%s\n",addr);//判断输入的内容if(strcmp(addr,"quit")== 0){break;}//释放0号灯的资源V(semid,0);}//5.取消映射关系if(shmdt(addr) == -1){perror("shmdt error");return -1;}//删除信号灯集del_sem(semid);return 0;
}
shmsnd.c
#include<myhead.h>
#include"sem.h"
#define PAGE_SIZE 4096 //一页大小
int main(int argc, const char *argv[])
{//创建信号灯集int semid = open_sem(2);//创建key值key_t key = -1;if((key = ftok("/",'k')) == -1){perror("ftok error");return -1;}printf("key = %d\n", key);//通过key值创建一个共享内存int shmid = -1;if((shmid = shmget(key ,PAGE_SIZE, IPC_CREAT|0664)) == -1){perror("shmget error");return -1;}printf("shmid = %d\n",shmid);//将共享内存段的数据映射到用户空间char *addr = shmat(shmid, NULL,0);//参数2:让系统自动选择对其页//参数3:对共享内存的操作为读写操作if(addr == (void*)-1){perror("shmat error");return -1;}printf("addr = %p\n",addr);//4.使用共享内存while(1){//申请0号灯的资源P(semid,0);printf("请输入>>");fgets(addr,PAGE_SIZE,stdin);addr[strlen(addr)-1]=0;printf("数据发送成功\n");//释放1号灯的资源V(semid,1);//判断输入的内容if(strcmp(addr,"quit")== 0){break;} if(strcmp(addr,"q")== 0){break;}}//5.取消映射关系if(shmdt(addr) == -1){perror("shmdt error");return -1;}//删除共享函数if(shmctl(shmid,IPC_RMID,NULL) == -1){perror("shmctl error");return -1;}return 0;
}