计算机操作系统实训教程实验报告
姓名王学杰专业计算机应用技术班级1362班
课程操作系统实验项目无名管道通信
【实验目的】
1了解管道通信机制的基本原理
2 掌握父子进程使用无名管道通信的方法
【实验要求】
编写程序实现多个进程基于无名管道进行通讯。
用系统调用pipe()建立一个无名管道,两个进程p1和p2分别向管道输出一句话
【实验内容】
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<string.h>
main()
{
int fd[2],p1,p2,i;
char receive[50];
char send1[50]="My name is wangxuejie";
char send2[50]="My id is 1308610203";
pipe(fd); //建立一个无名管道
p1=fork();
if(p1==0)
{
lockf(fd[1],1,0);
write(fd[1],send1,strlen(send1)); //子进程1写入
lockf(fd[1],0,0);
sleep(1);
}
else
{
wait(0);
read(fd[0],receive,50);
printf("parent read from child1:%s\n",receive);
p2=fork();
if(p2==0)
{
lockf(fd[1],1,0);
write(fd[1],send2,strlen(send2)); //子进程2写入
lockf(fd[1],0,0);
sleep(1);
}
else
{
wait(0);
read(fd[0],receive,50);
printf("parent read from child2:%s\n",receive); }
}
return 0;
}
实验相关截图
实验结果。