找bug#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); }
时间: 2023-11-30 08:02:32 浏览: 70
这段代码存在以下问题:
1. 在生产者函数中,每次写入数据时,写入的是一个字符串,但缓冲区只有4个字节大小,会导致溢出。
2. 在消费者函数中,每次读取数据时,读取的是4个字节大小的缓冲区,但实际上写入的数据可能小于4个字节,导致读取到的数据不正确。
3. 在主函数中,等待子进程退出的循环条件不正确,应该循环4次而不是循环3次。
4. 在主函数中,需要关闭管道的读端和写端,否则会有某个进程永远等待。
修改后的代码如下:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
char r_buf[1024]; // 读缓冲
char w_buf[1024]; // 写缓冲
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, strlen(w_buf) + 1) == -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("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));
int n = read(pipe_fd[0], r_buf, sizeof(r_buf));
if (n <= 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);
}
```
阅读全文