教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 高等教育 >

操作系统实验报告 (7)(2)

来源:网络收集 时间:2026-01-26
导读: pause(); } } else { signal(SIGINT,SIG_IGN); signal(16,Int1); pause(); } } 〈运行结果〉 请读者将上述程序输入计算机后,执行并观察。 3.进程的管道通信 〈任务〉 编制一段程序,实现进程的管道通信。使用系统

pause();

}

}

else

{

signal(SIGINT,SIG_IGN);

signal(16,Int1);

pause();

}

}

〈运行结果〉

请读者将上述程序输入计算机后,执行并观察。

3.进程的管道通信

〈任务〉

编制一段程序,实现进程的管道通信。使用系统调用pipe()建立一条管道线。两个子进程p1和p2分别向通道个写一句话:

child1 process is sending message!

child2 process is sending message!

而父进程则从管道中读出来自两个进程的信息,显示在屏幕上。

〈程序〉

#include <unistd.h>

#include <signal.h>

#include <stdio.h>

int pid1,pid2;

main( )

{

int fd[2];

char outpipe[100],inpipe[100];

pipe(fd); /*创建一个管道*/

while ((pid1=fork( ))==-1);

if(pid1==0)

{

lockf(fd[1],1,0);

sprintf(outpipe,"child 1 process is sending message!");

/*把串放入数组outpipe中*/

write(fd[1],outpipe,50); /*向管道写长为50字节的串*/

sleep(5); /*自我阻塞5秒*/

lockf(fd[1],0,0);

exit(0);

}

else

{

while((pid2=fork( ))==-1);

if(pid2==0)

{

lockf(fd[1],1,0); /*互斥*/

sprintf(outpipe,"child 2 process is sending message!");

write(fd[1],outpipe,50);

sleep(5);

lockf(fd[1],0,0);

exit(0);

}

else

{

wait(0); /*同步*/

read(fd[0],inpipe,50); /*从管道中读长为50字节的串*/

printf("%s\n",inpipe);

wait(0);

read(fd[0],inpipe,50);

printf("%s\n",inpipe);

exit(0);

}

}

}

〈运行结果〉

延迟5秒后显示:

child1 process is sending message!

再延迟5秒:

child2 process is sending message

5 实验结论及分析:

操作系统实验报告 (7)(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/127617.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)