Linux应用开发第十次作业(进程间通信2)答案
1、有两个进程A和B,进程A读取a.txt的内容,通过命名管道发送给第二个进程B并输出显示。
//q1_w.c
//采用fopen库函数
/*#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
int main()
{
FILE *fp, *ff;
char s[1024] = {0};
mkfifo("q1_pipe", 0666);
fp = fopen("q1_pipe", "w");
ff = fopen("a.txt", "r");
while(!feof(ff))
{
memset(s, 0, sizeof(s));
fread(s, sizeof(char), sizeof(s), ff);
fwrite(s, sizeof(char), sizeof(s), fp);
}
fclose(ff);
fclose(fp);
unlink("q1_pipe");
return 0;
}*/
//以下采用open系统调用
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
void main()
{
int pd, fd;
char s[1024]={0};
int ret;
mkfifo("q1_pipe", 0666);
pd = open("q1_pipe", O_WRONLY|O_CREAT, 0666);
fd = open("a.txt", O_RDONLY);
while( (ret=read(fd, s, sizeof(s)))>0 )
{
write(pd, s, ret);
memset(s, 0, sizeof(s));
}
close(fd);
close(pd);
unlink("q1_pipe");
}
//q1_r.c
//以下采用fopen库函数
/*#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
int main()
{
FILE *fp = fopen(“q1_pipe”, “r”);
char s[1024] = {0};
while(!feof(fp))
{
memset(s, 0, sizeof(s));
fread(s, sizeof(char), sizeof(s), fp);
printf("%s", s);
}
printf("\n");
fclose(fp);
return 0;
}*/
//以下采用open系统调用
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
void main()
{
int fd = open(“q1_pipe”, O_RDONLY);
char s[1024] = {0};
int ret;
while( (ret=(read(fd, s, sizeof(s))))>0 )
{
printf("%s", s);
memset(s, 0, sizeof(s));
}
close(fd);
}
2、有两个进程A、B,进程A和B都定义了下列变量,进程A对变量进行赋值,要求将变量的值传递给进程B并输出显示。
int a=1;
float b=2.2;
double c=3.1415926;
int ar[5]={10,20,30,40.50};
(1)采用命名管道传输数据,写出程序代码。
(2)采用共享内存传递数据,写出程序代码。
(1)
//q2_message.h
#ifndef _Q2_MESSAGE_H
#define _Q2_MESSAGE_H
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
typedef struct {
int a;
float b;
double c;
int ar[5];
} message;
#endif
//q2_w.c
#include “q2_message.h”
void main()
{
int fd;
message msg;
mkfifo("q2_pipe", 0666);
fd = open("q2_pipe", O_WRONLY);
msg.a = 1;
msg.b = 2.2;
msg.c = 3.1415926;
msg.ar[0] = 10;
msg.ar[1] = 20;
msg.ar[2] = 30;
msg.ar[3] = 40;
msg.ar[4] = 50;
write(fd, &msg, sizeof(msg));
close(fd);
unlink("q2_pipe)");
}
//q2_r.c
#include “q2_message.h”
void main()
{
int fd;
message msg;
fd = open("q2_pipe", O_RDONLY);
read(fd, &msg, sizeof(msg));
printf("a = %d\n", msg.a);
printf("b = %.2f\n", msg.b);
printf("a = %.7lf\n", msg.c);
printf("ar[0] = %d\n", msg.ar[0]);
printf("ar[1] = %d\n", msg.ar[1]);
printf("ar[2] = %d\n", msg.ar[2]);
printf("ar[3] = %d\n", msg.ar[3]);
printf("ar[4] = %d\n", msg.ar[4]);
close(fd);
}
(2)
//采用mmap共享内存
//q2_w1.c
#include “q2_message.h”
#include <sys/mman.h>
#include <string.h>
void main()
{
int fd = open(“q2.txt”, O_RDWR|O_CREAT, 0666);
message msg;
ftruncate(fd, sizeof(msg));
message pmap = (message)mmap(NULL, sizeof(msg), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
msg.a = 1;
msg.b = 2.2;
msg.c = 3.1415926;
msg.ar[0] = 10;
msg.ar[1] = 20;
msg.ar[2] = 30;
msg.ar[3] = 40;
msg.ar[4] = 50;
memcpy(pmap, &msg, sizeof(msg));
sleep(10);
munmap(pmap, sizeof(msg));
}
//q2_r1.c
#include “q2_message.h”
#include <sys/mman.h>
#include <string.h>
void main()
{
int fd = open(“q2.txt”, O_RDWR);
message msg;
message pmap = (message)mmap(NULL, sizeof(message), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
memcpy(&msg, pmap, sizeof(msg));
printf("a = %d\n", msg.a);
printf("b = %.2f\n", msg.b);
printf("a = %.7lf\n", msg.c);
printf("ar[0] = %d\n", msg.ar[0]);
printf("ar[1] = %d\n", msg.ar[1]);
printf("ar[2] = %d\n", msg.ar[2]);
printf("ar[3] = %d\n", msg.ar[3]);
printf("ar[4] = %d\n", msg.ar[4]);
munmap(pmap, sizeof(msg));
}
(2)
//采用system V的shmget共享内存(不要求)
//q2_w2.c
#include “q2_message.h”
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
void main()
{
key_t key = 0x123456;
int shmid = shmget(key, sizeof(message), IPC_CREAT|0666);
message *ptr = shmat(shmid, NULL, 0);
message msg;
msg.a = 1;
msg.b = 2.2;
msg.c = 3.1415926;
msg.ar[0] = 10;
msg.ar[1] = 20;
msg.ar[2] = 30;
msg.ar[3] = 40;
msg.ar[4] = 50;
memcpy(ptr, &msg, sizeof(msg));
sleep(10);
shmctl(shmid, IPC_RMID, NULL);
}
//q2_r2.c
#include “q2_message.h”
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
void main()
{
key_t key = 0x123456;
int shmid = shmget(key, sizeof(message), 0);
message msg;
message *ptr = shmat(shmid, NULL, 0);
memcpy(&msg, ptr, sizeof(msg));
printf("a = %d\n", msg.a);
printf("b = %.2f\n", msg.b);
printf("a = %.7lf\n", msg.c);
printf("ar[0] = %d\n", msg.ar[0]);
printf("ar[1] = %d\n", msg.ar[1]);
printf("ar[2] = %d\n", msg.ar[2]);
printf("ar[3] = %d\n", msg.ar[3]);
printf("ar[4] = %d\n", msg.ar[4]);
shmctl(shmid, IPC_RMID, NULL);
}
3、有两个程序msgsendstruct.c和msgrecvstruct.c,前者将结构体struct STU{char namel[10];int age;float height;}的一个实例stu={“张三”,20,1.79}发送给后者并输出显示。
(1)采用命名管道传输数据,写出程序代码。
(2)采用共享内存传递数据,写出程序代码。
(1)命名管道
//msgstruct.h
#ifndef _MSGSTRUCT_H
#define _MSGSTRUCT_H
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
typedef struct {
char name[10];
int age;
float height;
} STU;
#endif
//msgsendstruct.c
#include “msgstruct.h”
void main()
{
mkfifo(“stupipe”, 0666);
int fd = open(“stupipe”, O_WRONLY);
STU student;
strcpy(student.name, “Zhangsan”);
student.age = 19;
student.height = 1.79;
write(fd, &student, sizeof(student));
close(fd);
unlink(“stupipe”);
}
//msgrecvstruct.c
#include “msgstruct.h”
void main()
{
int fd = open(“stupipe”, O_RDONLY);
STU student;
read(fd, &student, sizeof(student));
printf(“name = %s\n”, student.name);
printf(“age = %d\n”, student.age);
printf(“name = %.2f\n”, student.height);
close(fd);
}
(2)共享内存mmap
//msgsendstruct1.c
#include “msgstruct.h”
#include <sys/mman.h>
void main()
{
int fd = open(“q3.txt”, O_RDWR|O_CREAT, 0666);
ftruncate(fd, sizeof(STU));
STU student;
ftruncate(fd, sizeof(student));
STU *pmap = (STU *)mmap(NULL, sizeof(student), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
strcpy(student.name, “Zhangsan”);
student.age = 19;
student.height = 1.79;
memcpy(pmap, &student, sizeof(student));
munmap(pmap, sizeof(student));
}
//msgrecvstruct1.c
#include “msgstruct.h”
#include <sys/mman.h>
void main()
{
int fd = open(“q3.txt”, O_RDWR);
STU student;
STU *pmap = (STU *)mmap(NULL, sizeof(student), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
memcpy(&student, pmap, sizeof(student));
printf(“name = %s\n”, student.name);
printf(“age = %d\n”, student.age);
printf(“name = %.2f\n”, student.height);
munmap(pmap, sizeof(student));
}
(2)共享内存shmat(不要求)
//msgsendstruct2.c
#include “msgstruct.h”
#include <sys/shm.h>
void main()
{
key_t key = 0x887766; //唯一就行
STU student;
int shmid = shmget(key, sizeof(student), IPC_CREAT|0666);
STU *ptr = shmat(shmid, NULL, 0);
strcpy(student.name, “Zhangsan”);
student.age = 19;
student.height = 1.79;
memcpy(ptr, &student, sizeof(student));
printf(“send ok!\n”);
sleep(10);//这里要等待共享内存被其他进程读取,否则后面删除后其他进程会出现错误
shmctl(shmid, IPC_RMID, NULL);
}
//msgrecvstruct2.c
#include “msgstruct.h”
#include <sys/shm.h>
void main()
{
key_t key = 0x887766;
STU student;
int shmid = shmget(key, sizeof(student), 0);
STU *ptr = shmat(shmid, NULL, 0);
memcpy(&student, ptr, sizeof(student));
printf(“name = %s\n”, student.name);
printf(“age = %d\n”, student.age);
printf(“name = %.2f\n”, student.height);
shmctl(shmid, IPC_RMID, NULL);
}
4、编写两个程序msgsend.c和msgrecv.c,msgsend进程不断从键盘读入信息,传送给进程msgrecv显示出来,写出程序代码。
(1)采用命名管道传输数据,写出程序代码。
(2)采用共享内存传递数据,写出程序代码。
(1)命名管道
//msgsend.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
void main()
{
mkfifo(“msgpipe”, 0666);
int fd = open("msgpipe", O_WRONLY);
char buf[1024];
while(1)
{
memset(buf, 0, 1024);
fgets(buf, 1024, stdin);
write(fd, buf, sizeof(buf));
if( strcmp(buf, "#\n")==0 )
break;
}
close(fd);
unlink("msgpipe");
}
//msgrecv.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
void main()
{
int fd = open(“msgpipe”, O_RDONLY);
char buf[1024] = {0};
int ret;
while( (ret=read(fd, buf, 1024))>0 )
{
printf("%s", buf);
memset(buf, 0, 1024);
}
close(fd);
unlink("msgpipe");
}
(2)mmap
//msgsend1.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <stdlib.h>
void main()
{
int fd = open(“msg.txt”, O_RDWR|O_CREAT, 0666);
char buf[1024];
ftruncate(fd, sizeof(buf)); //如果文件刚创建,大小为0,后面映射会有问题,所以这里强行指定文件大小
char *pmap = (char *)mmap(NULL, sizeof(buf), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if(pmap==(void *)-1) {
perror(“mmap error”);
exit(1);
}
close(fd);
while(1)
{
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
memcpy(pmap, buf, sizeof(buf));
if( strcmp(buf, "#\n")==0 )
break;
}
munmap(pmap, sizeof(buf));
}
//msgrecv1.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <stdlib.h>
void main()
{
int fd = open(“msg.txt”, O_RDWR);
char *pmap = (char *)mmap(NULL, 1024, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (pmap==(void *)-1) {
perror(“mmap error”);
exit(1);
}
close(fd);
while(1)
{
if (strcmp(pmap, "#\n")==0)
break;
if(strlen(pmap)>0)
printf("%s", pmap);
else
continue;
memset(pmap, 0, 1024);//将共享内存置空,否则一直打印循环
}
memset(pmap, 0 ,1024);
munmap(pmap, 1024);
}
(2)共享内存 shmat (不要求)
//msgsend2.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include <stdlib.h>
void main()
{
key_t key = 0x987654;
char buf[1024];
int shmid = shmget(key, sizeof(buf), IPC_CREAT|0666);
char *ptr = shmat(shmid, NULL, 0);
while(1)
{
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
memcpy(ptr, buf, sizeof(buf));
if( strcmp(buf, "#\n")==0 )
break;
}
shmctl(shmid, IPC_RMID, NULL);
}
//msgrecv2.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include <stdlib.h>
void main()
{
key_t key = 0x987654;
int shmid= shmget(key, sizeof(1024), 0);
char *ptr = shmat(shmid, NULL, 0);
while(1)
{
if (strcmp(ptr, "#\n")==0)
break;
if(strlen(ptr)>0)
printf("%s", ptr);
else
continue;
memset(ptr, 0, 1024);//将共享内存置空,否则一直打印循环
}
memset(ptr, 0 ,1024);
shmctl(shmid, IPC_RMID, NULL);
}
5、采用有名信号量实现以下功能:两个独立进程交替向文件中写入数据,要求A进程向文件number.txt中写入1000以下的奇数,B进程向文件中写入1000以下的偶数(含1000),交替写,因此文件number.txt中内容应该为“1234567891011121314151617181920……”。
//q5_1.c
#include <stdio.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <fcntl.h>
void main()
{
sem_t *semodd = sem_open(“semodd”, O_RDWR|O_CREAT, 0666, 1); //奇数先写
sem_t *semeven = sem_open(“semeven”, O_RDWR|O_CREAT, 0666, 0); //偶数后写
FILE *f;
int i = 1;
while(i<1000) {
sem_wait(semodd);// p semodd
f = fopen(“number.txt”, “a”); //不存在就创建写
fprintf(f, “%d”, i);
fclose(f);
sem_post(semeven);// v semeven
i = i + 2;
}
sem_close(semodd);
sem_close(semeven);
}
//q5_2.c
#include <stdio.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <fcntl.h>
void main()
{
sem_t *semodd = sem_open(“semodd”, O_RDWR|O_CREAT, 0666, 1); //奇数先写
sem_t *semeven = sem_open(“semeven”, O_RDWR|O_CREAT, 0666, 0); //偶数后写
FILE *f;
int i = 2;
while(i<=1000) {
sem_wait(semeven);// p semeven
f = fopen(“number.txt”, “a”); //不存在就创建写
fprintf(f, “%d”, i);
fclose(f);
sem_post(semodd);// v semodd
i = i + 2;
}
sem_close(semodd);
sem_close(semeven);
}
6、有一个间谍,他与接头人之间依赖通风信进行沟通。需要沟通时,间谍就往一个约定地点的石头下面放通风信,接头人后去取信,当间谍放在石头下的信没有被取走,间谍就不能再放信。请采用有名信号量实现上述模型。
提示思路:
1、两个进程,分别是间谍进程、接头人进程;
2、每个进程执行时有一个参数,就是信件内容,建议增加发出时间
3、采用两个有名信号量,一个表示间谍占用石头放信;一个表示接头人占用石头取信。
//q6.h
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
typedef struct {
char data[1024]; //实际消息
time_t send_time; //发送时间
} message;
//q6_spy.c
#include “q6.h”
int main(int argc, char **argv)
{
if(argc!=2) {
printf(“usage: ./q6_spy \n”);
return 1;
}
//以下生成共享内存,用于交换数据
int fd = open("q6.txt", O_RDWR|O_CREAT, 0666);
ftruncate(fd, sizeof(message));
message *pmap = (message *)mmap(NULL, sizeof(message), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
message msg;
close(fd);
//以下生成两个信号量
sem_t *sem_spy = sem_open("sem_spy", O_RDWR|O_CREAT, 0666, 1); //间谍信号量,初始值1
sem_t *sem_leader = sem_open("sem_leader", O_RDWR|O_CREAT, 0666, 0); //接头人信号量,初始值0
//开始放信
sem_wait(sem_spy);
msg.send_time = time(NULL);
strcpy(msg.data, argv[1]);
memcpy(pmap, &msg, sizeof(msg));
sem_post(sem_leader);
sem_close(sem_spy);
sem_close(sem_leader);
munmap(pmap, sizeof(message));
return 0;
}
//q6_leader.c
#include “q6.h”
int main(int argc, char **argv)
{
//以下生成共享内存,用于交换数据
int fd = open(“q6.txt”, O_RDWR|O_CREAT, 0666);
ftruncate(fd, sizeof(message));
message *pmap = (message *)mmap(NULL, sizeof(message), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
message msg;
close(fd);
//以下生成两个信号量
sem_t *sem_spy = sem_open(“sem_spy”, O_RDWR|O_CREAT, 0666, 1); //间谍信号量,初始值1,先放信
sem_t *sem_leader = sem_open(“sem_leader”, O_RDWR|O_CREAT, 0666, 0); //接头人信号量,初始值0,后放信
//开始收信
sem_wait(sem_leader);
memcpy(&msg, pmap, sizeof(msg));
struct tm *tp = localtime(&msg.send_time);
char time_str[100];
strftime(time_str, sizeof(time_str), “%Y-%m-%d %H:%M:%S”, tp);
printf(“send time: %s\nData: %s\n”, time_str, msg.data);
sem_post(sem_spy);
sem_close(sem_spy);
sem_close(sem_leader);
munmap(pmap, sizeof(message));
return 0;
}
7、有一只铁笼子,每次只能放入一只动物,猎手向笼子里放入老虎,农民向笼子里放入猪;动物园等待取笼子里的老虎,饭店等待取笼子里的猪。下面代码实现上述模型,请将空白处填满运行。
//q7.c
#include <semaphore.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
sem_t *sempig, *semtiger, *semcage; //三个信号量,猪、老虎、笼子
void farmer()
{
sem_wait(semcage); //等笼子空
printf(“Cage: a pig\n”);
sem_post(sempig); //通知饭店取猪
}
void hunter()
{
sem_wait(semcage); //等笼子空
printf(“Cage: a tiger\n”);
sem_post(semtiger); //通知动物园取
}
void zoo()
{
while(1) {
sem_wait(semtiger); //等老虎
printf(“Zoo: a tiger\tCage: empty\n”);
sem_post(semcage); //释放笼子
}
}
void rest()
{
while(1) {
sem_wait(sempig); //等猪
printf(“Restaurant: a pig\tCage: empty\n”);
sem_post(semcage); //释放笼子
}
}
int main(int argc, char **argv)
{
if(argc!=2) {
printf(“usage: ./example8 <hunter/farmer/zoo/rest>\n”);
return -1;
}
sempig = sem_open("sempig", O_RDWR|O_CREAT, 0666, 0); //初始无pig
semtiger = sem_open("semtiger", O_RDWR|O_CREAT, 0666, 0); //初始无tiger
semcage = sem_open("semcage", O_RDWR|O_CREAT, 0666, 1); //刚开始笼子空
if(sempig==SEM_FAILED || semtiger==SEM_FAILED ||semcage==SEM_FAILED) {
perror("sem_open error");
return -1;
}
if (strcmp(argv[1],"hunter")==0)
hunter();
else if (strcmp(argv[1],"farmer")==0)
farmer();
else if (strcmp(argv[1],"zoo")==0)
zoo();
else if (strcmp(argv[1],"rest")==0)
rest();
sem_close(sempig);
sem_close(semtiger);
sem_close(semcage);
return 0;
}
运行时:
./q7 zoo 表示动物园从笼子里拿老虎,一直运行
./q7 rest 表示饭店从笼子里拿猪,一直运行
./q7 hunter 运行一次表示猎人向笼子放一只老虎
./q7 farmer 运行一次表示农民向笼子放一只猪
8、采用无名信号量实现以下功能:三个进程(父子进程)交替向文件中写入一个字节的数据,要求A进程向文件a.txt中写入“ABCDEFGHIJ”,B进程向文件中写入“==========”,C进程向文件写入“0123456789”因此文件a.txt中内容应该为“A=0B=1C=2D=3E=4F=5G=6H=7I=8J=9”。
无名信号量
//homweork8.c
#include <semaphore.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <wait.h>
#include <unistd.h>
int main()
{
sem_t *semA, *semB, *semC;
pid_t pid_B, pid_C;
FILE *f;
semA = (sem_t *)mmap(NULL, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
semB = (sem_t *)mmap(NULL, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
semC = (sem_t *)mmap(NULL, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
sem_init(semA, 1, 1);
sem_init(semB, 1, 0);
sem_init(semC, 1, 0);
pid_B = fork();
//生成两个子进程来写文件
if(pid_B==0) {
char *s = "==========";
int i = 0;
while( s[i]!='\0' ) {
sem_wait(semB);
f = fopen("a.txt", "a");
fwrite(&s[i], 1, 1, f);
fclose(f);
sem_post(semC);
i++;
}
exit(0); //结束子进程,很重要,不然还会执行下面代码
}
pid_C = fork();
if(pid_C==0) {
char *s = "0123456789";
int i = 0;
while( s[i]!='\0' ) {
sem_wait(semC);
f = fopen("a.txt", "a");
fwrite(&s[i], 1, 1, f);
fclose(f);
sem_post(semA);
i++;
}
exit(0); //结束子进程,很重要,不然还会执行下面代码
}
//父进程充当A进程
char *s = "ABCDEFGHIJ";
int i = 0;
while( s[i]!='\0' ) {
sem_wait(semA);
f = fopen("a.txt", "a");
fwrite(&s[i], 1, 1, f);
fclose(f);
sem_post(semB);
i++;
}
//父进程要等待子进程结束,防止僵尸进程
waitpid(pid_B, NULL, 0);
waitpid(pid_C, NULL, 0);
sem_close(semA);
sem_close(semB);
sem_close(semC);
munmap(semA, sizeof(sem_t));
munmap(semB, sizeof(sem_t));
munmap(semC, sizeof(sem_t));
exit(0);
}
有名信号量
//A.c
#include <semaphore.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
void main()
{
sem_t *sem_left = sem_open(“sem_left”, O_RDWR|O_CREAT, 0666, 1);
sem_t *sem_center = sem_open(“sem_center”, O_RDWR|O_CREAT, 0666, 0);
sem_t *sem_right = sem_open(“sem_right”, O_RDWR|O_CREAT, 0666, 0);
FILE *f;
char *s = “ABCDEFGHIJ”;
int i = 0;
while(s[i]!=0) {
sem_wait(sem_left);
f = fopen(“a.txt”, “a”);
fwrite(&s[i], 1, 1, f);
fclose(f);
sem_post(sem_center);
i++;
}
sem_close(sem_left);
sem_close(sem_center);
sem_close(sem_right);
}
//B.c
#include <semaphore.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
void main()
{
sem_t *sem_left = sem_open(“sem_left”, O_RDWR|O_CREAT, 0666, 1);
sem_t *sem_center = sem_open(“sem_center”, O_RDWR|O_CREAT, 0666, 0);
sem_t *sem_right = sem_open(“sem_right”, O_RDWR|O_CREAT, 0666, 0);
FILE *f;
char *s = “==========”;
int i = 0;
while(s[i]!=0) {
sem_wait(sem_center);
f = fopen(“a.txt”, “a”);
fwrite(&s[i], 1, 1, f);
fclose(f);
sem_post(sem_right);
i++;
}
sem_close(sem_left);
sem_close(sem_center);
sem_close(sem_right);
}
//C.c
#include <semaphore.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
void main()
{
sem_t *sem_left = sem_open(“sem_left”, O_RDWR|O_CREAT, 0666, 1);
sem_t *sem_center = sem_open(“sem_center”, O_RDWR|O_CREAT, 0666, 0);
sem_t *sem_right = sem_open(“sem_right”, O_RDWR|O_CREAT, 0666, 0);
FILE *f;
char *s = “0123456789”;
int i = 0;
while(s[i]!=0) {
sem_wait(sem_right);
f = fopen(“a.txt”, “a”);
fwrite(&s[i], 1, 1, f);
fclose(f);
sem_post(sem_left);
i++;
}
sem_close(sem_left);
sem_close(sem_center);
sem_close(sem_right);
}