#ifndef _SEM_H_
#define _SEM_H_
//声明一个创建信号灯集并初始化函数
int init_sem(int semnum);
//声明一个P操作函数
int P(int semid,int semno);
//声明一个V函数
int V(int semid,int semno);
//声明一个删除信号灯集的函数
int del_sem(int semid);
#endif
#include
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminfo *__buf;
};
int set_semno_value(int semid,int semno,int semval)
{
//定义共用体
union semun us;
us.val = semval;
//调用semctl对灯光进行初始化
if(semctl(semid,semno,SETVAL,us) == -1){
perror("semctl error");
return -1;
}
return 0;
}
//创建信号灯集并初始化函数的定义
int init_sem(int semnum)
{
//1、创建key值
key_t key;
if((key = ftok("/",'t')) == -1){
perror("ftok error");
return -1;
}
//2、创建信号灯集
int semid;
if((semid = semget(key,semnum,IPC_CREAT|IPC_EXCL|0664)) == -1){
if(errno == EEXIST){
semid = semget(key,semnum,IPC_CREAT|0664);
return semid;
}else{
perror("semget error");
}
return -1;
}
//3、给信号灯集进行初始化
for(int i=0;i
#include
#include"sem.h"
int main(int argc, const char *argv[])
{
//打开信号灯集
int semid = init_sem(4);
//创建key值
key_t key;
if((key = ftok("/",'k')) == -1){
perror("ftok error");
return -1;
}
//创建共享内存
int shmid;
if((shmid = shmget(key,4096,IPC_CREAT|0664)) == -1){
perror("shmget error");
return -1;
}
//映射内存
char *addr = NULL;
if((addr = shmat(shmid,NULL,0)) == (void*)-1){
perror("shmat error");
return -1;
}
//定义一个进程号,创建子进程
pid_t pid;
pid = fork();
if(pid < 0){
perror("fork error");
return -1;
}else if(pid == 0){
//子进程等待3号资源,释放四号资源
while(1){
P(semid, 2);
printf("请输入>>");
fgets(addr,4096,stdin);
addr[strlen(addr)-1] = '\0';
V(semid,3);
if(strcmp(addr,"quit") == 0){
break;
}
exit(EXIT_SUCCESS);
}
}else{
//父进程等待二号资源,释放一号资源
while(1){
P(semid,1);
printf("共享内存中的数据为:%s\n",addr);
V(semid,0);
if(strcmp(addr,"quit") == 0){
break;
}
}
wait(NULL);
exit(EXIT_SUCCESS);
}
//取消映射
if(shmdt(addr) == -1){
perror("shmdt error");
return -1;
}
return 0;
}
#include
#include"sem.h"
//定义共用体
union semun{
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminfo *__buf;
};
int main(int argc, const char *argv[])
{
//打开信号灯集
int semid = init_sem(4);
//创建key值
key_t key;
if((key = ftok("/",'k')) == -1){
perror("ftok error");
return -1;
}
//创建共享内存
int shmid;
if((shmid = shmget(key,4096,IPC_CREAT|0664)) == -1){
perror("shmget error");
return -1;
}
//映射内存
char *addr = NULL;
if((addr = shmat(shmid,NULL,0)) == (void*)-1){
perror("shmat error");
return -1;
}
//创建进程号,创建子进程
pid_t pid;
pid = fork();
if(pid < 0){
perror("fork error");
return -1;
}else if(pid == 0){
//子进程等待四号资源,释放三号资源
union semun us;
us.val = 1;
semctl(semid,3,SETVAL,us); //初始化四号资源
while(1){
P(semid, 3);
printf("请输入>>");
fgets(addr,4096,stdin);
addr[strlen(addr)-1] = '\0';
V(semid,2);
if(strcmp(addr,"quit") == 0){
break;
}
exit(EXIT_SUCCESS);
}
}else{
//父进程等待一号资源,释放二号资源
while(1){
P(semid,0);
printf("共享内存中的数据为:%s\n",addr);
V(semid,1);
if(strcmp(addr,"quit") == 0){
break;
}
}
wait(NULL);
exit(EXIT_SUCCESS);
}
//取消映射
if(shmdt(addr) == -1){
perror("shmdt error");
return -1;
}
//删除共享内存
if(shmctl(shmid, IPC_RMID,NULL) == -1){
perror("shmctl error");
return -1;
}
del_sem(semid);
return 0;
}