在生成SIZE项之后,生产者进程应该写入fd_pipe_p2c管道并由消费者进程读取,然后消费者进程输出它正在消耗的内容。之后,消费者应该通过pipe_fd_c2p管道回写给生产者,让它知道缓冲区中有更多的空间。
cnt int似乎没有达到SIZE,我希望它在生成器函数生成SIZE项后执行。 因此,消费者功能永远不会收到所产生的东西。
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
int shared_arr[SIZE];
int cnt = 0, in = 0, out = 0;
int fd_pipe_c2p[2], fd_pipe_p2c[2];
void consumer();
void producer();
int main() {
pipe(fd_pipe_c2p); // consumer to producer
pipe(fd_pipe_p2c); // producer to consumer
if (fork() == 0) {
// child process
consumer();
} else {
// parent process
producer();
sleep(3);
}
exit(0);
}
void consumer() {
// consumer process
close(fd_pipe_c2p[0]);
while (1) {
while (cnt == 0) {
// give up everything
write(fd_pipe_c2p[1], &cnt, 1);
write(fd_pipe_c2p[1], shared_arr, sizeof(shared_arr));
// read in cnt?
read(fd_pipe_p2c[0], &cnt, 1);
if (cnt > 0) { // if we have something, break out of inner loop
break;
}
}
read(fd_pipe_p2c[0], shared_arr, sizeof(shared_arr)); // read in array
int n = shared_arr[out]; // get value to consume
fprintf(stderr, "I am consuming.......%d\n", n, out);
out = (out + 1) % SIZE; // move out forward?
cnt--; // decrease count
}
}
void producer() {
// producer process
close(fd_pipe_p2c[0]);
while (1) {
// read in cnt, if it's not == SIZE then produce something
read(fd_pipe_c2p[0], &cnt, 1);
while (cnt == SIZE) {
// write to consumer, we've produced something for it:
write(fd_pipe_p2c[1], &cnt, 1);
write(fd_pipe_p2c[1], shared_arr, sizeof(shared_arr));
}
// cnt != SIZE, so let's produce something:
shared_arr[in] = rand() % 100;
fprintf(stderr, "I am producing......%d...%d\n", shared_arr[in], in);
in = (in + 1) % SIZE;
cnt++;
}
}
}
也许预期输出的一个例子可以更好地解释:
I am producing......26...0.
I am producing......34...1.
I am producing......6...2.
I am producing......1...3.
I am producing......84...4.
I am producing......35...5.
I am producing......89...6.
I am producing......38...7.
I am producing......65...8.
I am producing......78...9.
Check to see something available.
I am producing......80...0.
I am consuming......26 0
I am producing......67...1.
I am consuming......34 1
I am producing......59...2.
I am consuming......6 2
I am producing......52...3.
I am consuming......1 3
I am producing......85...4.
I am consuming......84 4
I am producing......79...5.
I am consuming......35 5
I am producing......94...6.
I am consuming......89 6
I am producing......58...7.
I am consuming......38 7
I am producing......74...8.
I am consuming......65 8
I am producing......67...9.
I am consuming......78 9
Check to see something available.
相反,这是我当前的输出:
I am producing......83...0
I am producing......86...1
I am producing......77...2
I am producing......15...3
I am producing......93...4
I am producing......35...5
I am producing......86...6
I am producing......92...7
I am producing......49...8
I am producing......21...9
I am producing......62...0
I am producing......27...1
I am producing......90...2
I am producing......59...3
I am producing......63...4
I am producing......26...5
I am producing......40...6
I am producing......26...7
I am producing......72...8
I am producing......36...9
I am producing......11...0
I am producing......68...1
I am producing......67...2
I am producing......29...3
I am producing......82...4
I am producing......30...5
I am producing......62...6
I am producing......23...7
I am producing......67...8
I am producing......35...9
I am producing......29...0
I am producing......2...1
I am producing......22...2
I am producing......58...3
I am producing......69...4
I am producing......67...5
I am producing......93...6
I am producing......56...7
I am producing......11...8
I am producing......42...9
I am producing......29...0