[操作系统作业]os实验三:进程的管道通信

View Code
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <sys/types.h>
 4 #include <unistd.h>
 5 #include <string.h>
 6 #include  <sys/stat.h>
 7 #include  <fcntl.h>
 8 #include  <error.h>
 9 #include  <wait.h>
10 #define PSIZE 2
11 
12 int main(){
13     int fd[2];
14     int flag = 1;
15     //
16     if((flag = pipe(fd)) == -1){
17       printf("error in create pipe.");
18       exit(1);
19     }else if(flag == 0){
20     //  printf("success in create pipe.");
21     }
22 
23     pid_t pid_1,pid_2;
24     char outpipe[50];
25 // ******************问题所在********************//
26     if(((pid_1 = fork()) == -1)||((pid_2 = fork()) == -1)){
27 // ******************问题所在********************//
28         printf("error in fork()!");
29     }
30     else if(pid_1 == 0){
31         printf("\n ...................%d   %d...................\n",pid_1,pid_2);
32         printf("i am a child 1, my pid is %d ,my fatherpid is %d.\n",getpid(),getppid());
33         lockf(fd[1],1,0);// 对管道的写入端口jia锁。
34         strcpy(outpipe,"Child process 1 is sending message!\n");
35                 printf("i am a child, i am writing %s \n",outpipe);
36         write(fd[1],outpipe,sizeof(outpipe));
37         sleep(5);
38         lockf(fd[1],0,0);// 对管道的写入端口解锁。
39         //getchar();//must write
40         exit(0);
41     }else if(pid_2 == 0){
42                 printf("\n ...................%d   %d...................\n",pid_1,pid_2);
43 
44         printf("i am a child 2, my pid is %d ,my fatherpid is %d..\n",getpid(),getppid());
45         lockf(fd[1],1,0);// 对管道的写入端口jia锁。
46         strcpy(outpipe,"Child process 2 is sending message!");
47                 printf("i am a child, i am writing %s \n",outpipe);
48         write(fd[1],outpipe,sizeof(outpipe));
49         sleep(5);
50         lockf(fd[1],0,0);// 对管道的写入端口解锁。
51           //getchar();//must write
52         exit(0);
53     }else if(pid_1 > 0&&pid_2>0){
54         printf("i am father, my pid is %d, my sonpid is %d  and  %d.\n",getpid(),pid_1,pid_2);
55         int i = 0;
56         pid_t pid;
57         while(i < PSIZE){
58           if((pid = wait(0)) < 0){
59             printf("error in waitepid.\n");
60             exit(1);
61           }else{
62             read(fd[0],outpipe,sizeof(outpipe));
63             printf("father process read from pipe : %s    son pid is %d.\n",outpipe,pid);
64           }
65           i++;
66         }
67         //getchar();//must write, if not ,the son process will be an orphen process.       相当于停了一下
68         exit(0);
69     }
70 }
错误在于父进程同时fork()生成两个子进程。
子进程有可能在连续fork过程中生成自己的子进程,是否生成与子进程执行的顺序相关。
简要说明:
执行顺序(应该是运行结果更恰当)
父进程:(pid :2000)
fork()(子进程pid :2001)||fork()(子进程pid :2002)
子进程2:(pid :2002)
fork()(子进程pid : 2001)||fork()(子进程pid : 0
子进程1:(pid :2001)
fork()(子进程pid :    0   )||fork()(子进程pid : 2003
子进程至少保证了与所属父进程在调用fork()的位置处,fork()返回值为 0.
但是子进程继续fork(),返回值就与各进程的执行顺序相关了。
具体,比如 子进程2:pid 2001,我认为是由于父进程刚第一次fork,子进程2就fork了,那返回值为什么不是0呢?
这个问题我也不知到,欢迎大家指正。应该跟编译器有关。
最后结论:
父进程可以fork多个子进程,但是得一个一个fork,必须使得每fork一次后都需要有控制语句将其父子进程执行的程序段完全隔离开。避免使用 fork()||fork()连续fork子进程。
 
修改后:
View Code
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <sys/types.h>
 4 #include <unistd.h>
 5 #include <string.h>
 6 #include  <sys/stat.h>
 7 #include  <fcntl.h>
 8 #include  <error.h>
 9 #include  <wait.h>
10 #define PSIZE 2
11 
12 int main(){
13     int fd[2];
14     int flag = 1;
15     if((flag = pipe(fd)) == -1){    //创建管道
16       printf("error in create pipe.");
17       exit(1);
18     }else if(flag == 0){
19     //  printf("success in create pipe.");
20     }
21     pid_t pid_1,pid_2;
22     char outpipe[50];
23     if((pid_1 = fork()) == -1){    //创建子进程
24         printf("error in fork()!");
25     }else if(pid_1 == 0){   //子进程执行代码段
26         printf("i am child 1, my pid is %d ,my fatherpid is %d.\n",getpid(),getppid());
27         lockf(fd[1],1,0);// 对管道的写入端口jia锁。
28         strcpy(outpipe,"Child process 1 is sending message!\n");
29                         printf("i am writing : %s \n",outpipe);
30         write(fd[1],outpipe,sizeof(outpipe));
31         sleep(5);// 让自己睡眠,好让父进程读出pipe内的数据。
32         lockf(fd[1],0,0);// 对管道的写入端口解锁。
33         //getchar();//must write
34         exit(0);
35     }else if(pid_1 > 0){    //父进程执行代码段
36          if((pid_2 = fork()) == -1){
37                 printf("error in fork()!");
38             }else if(pid_2 == 0){
39                 printf("i am child 2, my pid is %d ,my fatherpid is %d..\n",getpid(),getppid());
40                 lockf(fd[1],1,0);// 对管道的写入端口jia锁。
41                 strcpy(outpipe,"Child process 2 is sending message!");
42                         printf("i am writing : %s \n",outpipe);
43                 write(fd[1],outpipe,sizeof(outpipe));
44                 sleep(5);
45                 lockf(fd[1],0,0);// 对管道的写入端口解锁。
46                   //getchar();//must write
47                 exit(0);
48             }
49         printf("i am father, my pid is %d, my son pid is %d  and  %d.\n",getpid(),pid_1,pid_2);
50         int i = 0;
51         pid_t pid;
52         while(i < PSIZE){
53           if((pid = wait(0)) < 0){//等待子进程结束后返回,返回值为子进程pid
54             printf("error in waitepid.\n");
55             exit(1);
56           }else{
57             read(fd[0],outpipe,sizeof(outpipe));
58             printf("father process read from pipe : %s  from son pid: %d.\n",outpipe,pid);
59           }
60           i++;
61         }
62         //getchar();//must write, if not ,the son process will be an orphen process.       相当于停了一下
63         exit(0);
64     }
65 }

关键在于子进程2,是如何执行父子共享代码段的。

 

修正:

wait函数,父进程需等待子进程结束后返回,才可继续执行

因此,code中 子进程1,2的程序段中的sleep函数的作用:保证子进程将数据全部写入管道。

而非等待以保证父进程从管道中读出数据。

 

 

 

转载于:https://www.cnblogs.com/wanping/archive/2012/06/01/2531407.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 实验目的 1) 加深对进程概念的理解,明确进程和程序的区别。 2) 进一步认识并发执行的实质。 3) 分析进程争用资源的现象,学习解决进程互斥的方法。 4) 学习解决进程同步的方法。 5) 了解Linux系统中进程通信的基本原理。   进程操作系统中最重要的概念,贯穿始终,也是学习现代操作系统的关键。通过本次实验,要求理解进程的实质和进程管理的机制。在Linux系统下实现进程从创建到终止的全过程,从中体会进程的创建过程、父进程和子进程之间的关系、进程状态的变化、进程之间的互斥、同步机制、进程调度的原理和以管道为代表的进程间的通信方式的实现。 2. 内容及要求:   这是一个设计型实验,要求自行编制程序。   使用系统调用pipe()建立一条管道,两个子进程分别向管道写一句话:   Child process1 is sending a message!   Child process2 is sending a message!   父进程从管道读出来自两个子进程的信息,显示在屏幕上。   要求: 1) 父进程先接收子进程1发来的消息,然后再接收子进程2发来的消息。 2) 实现管道的互斥使用,当一个子进程正在对管道进行写操作时,另一子进程必须等待。使用系统调用lockf(fd[1],1,0)实现对管道的加锁操作,用lockf(fd[1],0,0)解除对管道的锁定。 3) 实现父子进程的同步,当子进程把数据写入管道后,便去睡眠等待;当父进程试图从一空管道中读取数据时,也应等待,直到子进程将数据写入管道后,才将其唤醒。 3.相关的系统调用 1) fork() 用于创一个子进程。 格式:int fork(); 返回值:在子进程中返回0;在父进程中返回所创建的子进程的ID值;当返回-1时,创建失败。 2) wait() 常用来控制父进程与子进程的同步。 在父进程中调用wait(),则父进程被阻塞,进入等待队列,等待子进程结束。当子进程结束时,父进程从wait()返回继续执行原来的程序。 返回值:大于0时,为子进程的ID值;等于-1时,调用失败。 3) exit() 是进程结束时最常调用的。 格式:void exit( int status); 其中,status为进程结束状态。 4) pipe() 用于创建一个管道 格式:pipe(int fd); 其中fd是一个由两个数组元素fd[0]和fd[1]组成的整型数组,fd[0]是管道的读端口,用于从管道读出数据,fd[1] 是管道的写端口,用于向管道写入数据。 返回值:0 调用成功;-1 调用失败。 5) sleep() 调用进程睡眠若干时间,之后唤醒。 格式:sleep(int t); 其中t为睡眠时间。 6) lockf() 用于对互斥资源加锁和解锁。在本实验中,该调用的格式为: lockf(fd[1],1,0);/* 表示对管道的写入端口加锁。 lockf(fd[1],0,0);/* 表示对管道的写入端口解锁。 7) write(fd[1],String,Length) 将字符串String的内容写入管道的写入口。 8) read(fd[0],String,Length) 从管道的读入口读出信息放入字符串String中。 4.程序流程 父进程: 1) 创建管道; 2) 创建子进程1; 3) 创建子进程2; 4) 等待从管道中读出子进程1写入的数据,并显示在屏幕上; 5) 等待从管道中读出子进程2写入的数据,并显示在屏幕上; 6) 退出。 子进程: 1) 将管道的写入口加锁; 2) 将信息“Child process n is sending message!”输入到变量OutPipe中,n=1,2; 3) 将OutPipe中信息写入管道; 4) 睡眠等待; 5) 将管道的写入口解锁; 6) 退出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值