#include "sys/types.h" #include "sys/file.h" #include "unistd.h" char r_buf[4]; //读缓冲 char w_buf[4]; //写缓冲 int pipe_fd[2]; pid_t pid1, pid2, pid3, pid4; int producer(int id); int consumer(int id); int main(int argc,char **argv) { if(pipe(pipe_fd)<0) { printf("pipe create error \n"); exit(-1); } else { printf("pipe is created successfully!\n"); if((pid1=fork())==0) producer(1); if((pid2=fork())==0) producer(2); if((pid3=fork())==0) consumer(1); if((pid4=fork())==0) consumer(2); } close(pipe_fd[0]); //需要加上这两句 close(pipe_fd[1]); //否这会有读者或者写者永远等待 int i,pid,status; for(i=0;i<4;i++) pid=wait(&status); exit(0); } int producer(int id) { printf("producer %d is running!\n",id); close(pipe_fd[0]); int i=0; for(i=1;i<10;i++) { sleep(3); if(id==1) //生产者1 strcpy(w_buf,"aaa\0"); else //生产者2 strcpy(w_buf,"bbb\0"); if(write(pipe_fd[1],w_buf,4)==-1) printf("write to pipe error\n"); } close(pipe_fd[1]); printf("producer %d is over!\n",id); exit(id); } int consumer(int id) { close(pipe_fd[1]); printf("producer %d is running!\n",id); if (id==1) //消费者1 strcpy(w_buf,"ccc\0"); else //消费者2 strcpy(w_buf,"ddd\0"); while(1) { sleep(1); strcpy(r_buf,"eee\0"); if(read(pipe_fd[0],r_buf,4)==0) break; printf("consumer %d get %s, while the w_buf is %s\n",id,r_buf,w_buf); } close(pipe_fd[0]); printf("consumer %d is over!\n", id); exit(id); }在Linux系统下运行,输出结果有误,应该如何修改
时间: 2023-11-30 14:02:32 浏览: 71
这段代码存在以下问题:
1. 在生产者和消费者函数中,应该使用write和read的返回值来判断是否成功,而不是仅仅输出错误信息。
2. 在生产者函数中,每次写入的字符串长度为4,但是字符串"aaa"和"bbb"的长度都是3,因此存在未定义行为。应该将缓冲区大小增加到5,以便容纳字符串末尾的null字符。
3. 在消费者函数中,每次读取的字符串长度为4,但是字符串"ccc"和"ddd"的长度都是3,因此存在未定义行为。应该将缓冲区大小增加到5,以便容纳字符串末尾的null字符。
4. 在主函数中,应该在循环中等待所有子进程结束,而不是仅等待4个子进程中的一个。
修改后的代码如下:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
char r_buf[5]; // 读缓冲
char w_buf[5]; // 写缓冲
int pipe_fd[2];
pid_t pid1, pid2, pid3, pid4;
int producer(int id);
int consumer(int id);
int main(int argc, char **argv) {
if (pipe(pipe_fd) < 0) {
printf("pipe create error\n");
exit(-1);
} else {
printf("pipe is created successfully!\n");
if ((pid1 = fork()) == 0)
producer(1);
if ((pid2 = fork()) == 0)
producer(2);
if ((pid3 = fork()) == 0)
consumer(1);
if ((pid4 = fork()) == 0)
consumer(2);
}
close(pipe_fd[0]);
close(pipe_fd[1]);
int i, pid, status;
for (i = 0; i < 4; i++)
pid = wait(&status);
exit(0);
}
int producer(int id) {
printf("producer %d is running!\n", id);
close(pipe_fd[0]);
int i = 0;
for (i = 1; i < 10; i++) {
sleep(3);
if (id == 1) // 生产者1
strcpy(w_buf, "aaa");
else // 生产者2
strcpy(w_buf, "bbb");
if (write(pipe_fd[1], w_buf, 4) != 4)
printf("write to pipe error\n");
}
close(pipe_fd[1]);
printf("producer %d is over!\n", id);
exit(id);
}
int consumer(int id) {
close(pipe_fd[1]);
printf("consumer %d is running!\n", id);
if (id == 1) // 消费者1
strcpy(w_buf, "ccc");
else // 消费者2
strcpy(w_buf, "ddd");
while (1) {
sleep(1);
memset(r_buf, 0, sizeof(r_buf)); // 清空读缓冲
if (read(pipe_fd[0], r_buf, 4) != 4)
break;
printf("consumer %d get %s, while the w_buf is %s\n", id, r_buf, w_buf);
}
close(pipe_fd[0]);
printf("consumer %d is over!\n", id);
exit(id);
}
```
阅读全文