#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 set_semval(int semid,int semno,int Value)
{
union semun us;
us.val=Value;
if(semctl(semid,semno,SETVAL,us)==-1)
{
perror("semctl error");
return -1;
}
return 0;
}
//声明一个创建信号灯集并初始化函数
int init_sem(int semnum)
{
key_t key=ftok("/",369);
if( (key=ftok("/",369))==-1)
{
perror("ftok error");
return -1;
}
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;
}
}
for(int i=0;i<semnum;i++)
{
if(i==0)
set_semval(semid,i,1);
else
set_semval(semid,i,0);
}
return semid;
}
//声明一个p操作函数(申请)
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;
}
//声明一个V操作函数(释放)
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;
}
//声明删除
int del_sem(int semid)
{
if(semctl(semid,0,IPC_RMID,0)==-1)
{
perror("del error");
return -1;
}
return 0;
}
ubuntu@ubuntu:sem4$
#include"sem.h"
int main(int argc, const char *argv[])
{ // 创建信号灯集含四个
int semid=init_sem(4);
//创建key
key_t key;
if((key=ftok("/",369))==-1)
{
perror("ftok error");
return -1;
}
//获得shmid创建共享内存
int shmid;
if((shmid=shmget(key,4096,IPC_CREAT|0666))==-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=fork();
if(pid<0)
{
perror("fork error");
return -1;
}
else if(pid ==0)
{
while(1)
{
P(semid,3);//申请资源3
//读取内容
printf("读取内容为:%s\n",addr);
if(strcmp(addr,"quit")==0)
break;
V(semid,2);//释放资源2
}
exit(EXIT_SUCCESS);
}
else
{
while(1)
{
P(semid,0);//申请资源0
//输入内容
printf("请输入:");
fgets(addr,4096,stdin);
addr[strlen(addr)-1]=0;
V(semid,1);//释放资源1
if(strcmp(addr,"quit")==0)
break;
}
//撤销共享内存映射
if(shmdt(addr)==-1)
{
perror("shmdt error");
return -1;
}
wait(NULL);//回收进程
}
return 0;
}
#include<myhead.h>
#include"sem.h"
int main(int argc, const char *argv[])
{ int semid=init_sem(4);
//创建key
key_t key;
if((key=ftok("/",369))==-1)
{
perror("ftok error");
return -1;
}
//获得shmid创建共享内存
int shmid;
if((shmid=shmget(key,4096,IPC_CREAT|0666))==-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=fork();
if(pid<0)
{
perror("fork error");
return -1;
}
else if(pid ==0)
{
while(1)
{
P(semid,1);
//读取内容
printf("读取内容为:%s\n",addr);
if(strcmp(addr,"quit")==0)
break;
V(semid,0);
V(semid,2);
}
exit(EXIT_SUCCESS);
}
else
{
while(1)
{
P(semid,2);
//输入内容
printf("请输入:");
fgets(addr,4096,stdin);
addr[strlen(addr)-1]=0;
V(semid,3);
if(strcmp(addr,"quit")==0)
break;
}
//撤销共享内存映射
if(shmdt(addr)==-1)
{
perror("shmdt error");
return -1;
}
//删除内存映射取
if(shmctl(shmid,IPC_RMID,NULL)==-1)
{
perror("shmctl error");
return -1;
}
wait(NULL);
del_sem(semid);
}
return 0;
}