操作系统实验报告_Linux进程创建与通信
2011-2012学年第一学期
计算机操作系统实验报告
专 业:
班 级:
学 号:
姓 名:提交日期:2011年11月
1
实验二 Linux进程创建与进程通信
【实验目的】
1. 熟悉有关Linux系统调用;
2. 学习有关Linux的进程创建,理解进程创建后两个并发进程的执行; 3. 通过系统调用wait()和exit(),实现父子进程同步;
4. 掌握管道、消息缓冲等进程通信方法并了解其特点和使用限制。
【实验内容】
1. 父进程创建子进程
实现父进程创建一个子进程,返回后父子进程分别循环输出字符串“The parent process.”及“The child process.”5次,每次输出后使用sleep(1)延时一秒,然后再进入下一次循环。给出源程序代码和运行结果。
程序代码: main() {
int p1,i;
while ((p1=fork())==-1); if (p1>0) for (i=0;i<5;i++) { printf("I am parent.\n"); sleep(1); }
else for (i=0;i<5;i++) { printf("I am child.\n"); sleep(1); } }
运行结果:
The parent process. The child process. The parent process. The child process. The parent process. The child process.
The parent process. The child process. The parent process. The child process. 2. 父子进程同步
修改上题程序,使用exit()和wait()实现父子进程同步,其同步方式为父进程等待子进程的同步,即:子进程循环输出5次,然后父进程再循环输出5次。给出源程序代码和运行结果。
程序代码: main() {
int p1,i;
while ((p1=fork())==-1); if (p1>0) { wait(0); for (i=0;i<5;i++) { printf("I am parent.\n"); sleep(1); } } else { for (i=0;i<5;i++) { printf("I am child.\n"); sleep(1); } exit(0); } }
运行结果: I am parent. I am parent. I am parent. I am parent. I am parent. I am child. I am child. I am child. I am child. I am child.
3. Linux管道通信
编写一个程序,实现以下功能。给出源程序代码和运行结果。 (1)父进程使用系统调用pipe()创建一个无名管道; (2)创建两个子进程,分别向管道发送一条信息后结束;
子进程1发送:Child 1 is sending a message to parent! 子进程2发送:Child 2 is sending a message to parent!
(3)父进程从管道中分别接收两个子进程发来的消息并显示在屏幕上,父进程结束。两个子进程发送信息没有先后顺序要求。
源程序代码: #include<stdio.h> #include<unistd.h> #include<signal.h>main() {
int p1,p2,fd[2]; char outpipe[50];
char inpipe1[50]="Child1 is sending a message to parent!"; char inpipe2[50]="Child2 is sending a messege to parent!"; pipe(fd);
while((p1=fork())==-1); if (p1==0) { lockf(fd[1],1,0); write(fd[1],inpipe1,50); exit(0); } else {
while((p2=fork())==-1); if (p2==0) {
lockf(fd[1],1,0);
write(fd[1],inpipe2,50); exit(0); } else {
wait(0);
read(fd[0],outpipe,50);
lockf(fd[1],0,0);
printf("Parent has received first message:\n"); printf("%s\n",outpipe); wait(0);
read(fd[0],outpipe,50); lockf(fd[1],0,0);
printf("Parent has received second message:\n"); printf("%s\n",outpipe); exit(0); }
} }
运行结果:
Child 1 is sending a message to parent! Child 2 is sending a message to parent! 4. Linux消息缓冲通信
编写一个程序,实现以下功能。给出源程序代码和运行结果。
(1)父进程创建一个消息队列和一个子进程,由子进程发送消息,父进程等待子进程结束后接收子进程发来的消息,并显示消息内容。以“end”作为结束消息。
父进程:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h>
#include<sys/types.h> #include<sys/ipc.h> #include<sys/msg.h> #define MAX_TEXT 512 struct my_msg { long int my_msg_type; char some_text[MAX_TEXT]; }msg;
main() { int p1,msgid,running=1; char buffer[BUFSIZ]; long int msg_to_receive=0; char *path="./msgchld.exe"; char *argv[3]={"msgchld.exe",NULL};
}
while((p1=fork())==-1); if(p1==0) { execv(path,argv); exit(0); } else { wait(0); while(running) { msgrcv(msgid,&msg,BUFSIZ,msg_to_receive,0); printf("You wrote:%s",msg.some_text); if(strncmp(msg.some_text,"end",3)==0) running=0; } msgctl(msgid,IPC_RMID,0); exit(0); }
子进程:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h>
#include<sys/types.h> #include<sys/ipc.h> #include<sys/msg.h> #define MAX_TEXT 512
struct my_msg { long int my_msg_type; char some_text[MAX_TEXT]; }msg;
main() { int msgid,running=1; char buffer[BUFSIZ]; msgid=msgget(1234,0666|IPC_CREAT);
}
{ puts("Enter some text:"); fgets(buffer,BUFSIZ,stdin); msg.my_msg_type=1; strcpy(msg.some_text,buffer); msgsnd(msgid,&msg,MAX_TEXT,0); if(strncmp(msg.some_text,"end",3)==0) running=0; }
exit(0);
运行结果:
子进程运行,显示:“Enter some text:”;输入内容后,运行父进程,显示“You wrote:”和刚输入的内容。
(2)分别编写发送进程和接收进程,由发送进程发送消息,接收进程接收消息。采用先执行发送进程后执行接收进程的方式同步。以“end”作为结束消息。 发送方:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/msg.h> #define MAX_TEXT 512 struct my_msg { long int my_msg_type; char some_text[MAX_TEXT]; }msg;
main() { int msgid,running=1; char buffer[BUFSIZ]; msgid=msgget(1234,0666|IPC_CREAT); while(running) {
}
fgets(buffer,BUFSIZ,stdin); msg.my_msg_type=1; strcpy(msg.some_text,buffer); msgsnd(msgid,&msg,MAX_TEXT,0); if(strncmp(msg.some_text,"end",3)==0) running=0; }
exit(0);
接收方:
#include<stdio.h> #include<stdli …… 此处隐藏:3675字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [专业资料]《蜜蜂之家》教学反思
- [专业资料]过去分词作定语和表语1
- [专业资料]苏州工业园区住房公积金贷款申请表
- [专业资料]保安管理制度及处罚条例细则
- [专业资料]2018年中国工程咨询市场发展现状调研及
- [专业资料]2015年电大本科《学前教育科研方法》期
- [专业资料]数字信号处理实验 matlab版 离散傅里叶
- [专业资料]“十三五”重点项目-虎杖白藜芦醇及功
- [专业资料]2015-2020年中国竹木工艺市场需求及投
- [专业资料]国际贸易理论与实务作业五:理论案例分
- [专业资料]财政部修订发布事业单位会计制度
- [专业资料]BCA蛋白浓度测定试剂盒(增强型)
- [专业资料]工程进度总计划横道图模板(通用版)
- [专业资料]七年级地理同步练习(天气与气候)
- [专业资料]X光安检机介绍火灾自动报警系统的组成
- [专业资料]衢州市人民政府办公室关于印发衢州市区
- [专业资料]经济全球化及其影响[1]
- [专业资料]质粒DNA限制性酶切图谱分析
- [专业资料]国家安全人民防线工作“六项”制度
- [专业资料]劳动力投入计划及保证措施
- 电子账册联网监管培训手册
- 人教版语文七年级上第1课《在山的那边
- 对我区担保行业发展现状的思考与建议
- 平面四边形网格自动生成方法研究
- 2016年党课学习心得体会范文
- 如何设置电脑定时关机
- 全球最美人妖排行榜新鲜出炉
- 社会实践调查报告及问卷
- Visual Basic习题集
- 《鱼我所欲也》课件2
- 浙江省会计从业资格考试试卷
- 全遥控数字音量控制的D 类功率放大器资
- 鞍钢宪法与后福特主义
- 电表的改装与校准实验报告(1)
- 2014年高考理科数学真题解析分类汇编:
- Windows 7 AIK 的使用
- 风电场全场停电事故应急处置方案
- 化工原理选填题题库(下)
- 关于产学研合作教育模式的学习与思考
- 西安先锋公馆项目前期定位报告




