从管道中读取整数会跳过 C 中的值
read integers from pipe skips values in C
我正在尝试了解管道在 C 中的工作方式。
我的父亲进程生成从 1 到 10 的整数并将它们写入管道。我的 child 进程必须读取管道并将值打印到屏幕上。父亲等待 child 终止并退出。容易,对吧?这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#define WRITE 1
#define READ 0
#define N 10
int main(void)
{
pid_t pid;
int B0[2];
int num, status, i;
pipe(B0);
pid = fork();
if (pid > 0){
/*P0*/
close(B0[READ]);
for (i=1; i<=N; i++){
num = i;
write(B0[WRITE],&num,sizeof(num));
printf("P0: written %d\n", num);
}
close(B0[WRITE]);
wait(&status);
exit(EXIT_SUCCESS);
}
else if (pid == 0){
/*P1*/
close(B0[WRITE]);
do{
if (read(B0[READ],&num,sizeof(num)) != -1)
printf("P1: read %d from pipe B0\n", num);
else
printf("read: %s\n", strerror(errno));
} while(read(B0[READ],&num,sizeof(num)) != 0);
exit(EXIT_SUCCESS);
}
}
我不明白为什么我收到以下输出:
P0: written 1
P0: written 2
P0: written 3
P0: written 4
P0: written 5
P0: written 6
P0: written 7
P0: written 8
P0: written 9
P0: written 10
P1: read 1 from pipe B0
P1: read 3 from pipe B0
P1: read 5 from pipe B0
P1: read 7 from pipe B0
P1: read 9 from pipe B0
P1: read 10 from pipe B0
不管我在管道中写入的整数序列是什么,我的 read() 都会跳过每个第二个值。我试图在将值写入管道时睡眠(1),但结果是一样的。我错过了一些东西,但我没有得到什么。
发生什么事了?
你读了 1 并打印,然后在 while
条件下,你读了 2 并丢弃它。同样,您丢弃每个偶数值。在 while 条件下读取 10,并且 returns 非零,因此循环继续,然后在 if 中读取 returns 0,这不是 -1,因此打印 10。将循环写为 while( ( rv = read (... )) != 0 ) { ... }
您的 do-while
循环条件也执行 read
,但您没有使用该值。相反,您只需在循环开始时再次阅读,从而跳过每个第二个值。使用不同的条件,或使用您已阅读的值。
我正在尝试了解管道在 C 中的工作方式。 我的父亲进程生成从 1 到 10 的整数并将它们写入管道。我的 child 进程必须读取管道并将值打印到屏幕上。父亲等待 child 终止并退出。容易,对吧?这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#define WRITE 1
#define READ 0
#define N 10
int main(void)
{
pid_t pid;
int B0[2];
int num, status, i;
pipe(B0);
pid = fork();
if (pid > 0){
/*P0*/
close(B0[READ]);
for (i=1; i<=N; i++){
num = i;
write(B0[WRITE],&num,sizeof(num));
printf("P0: written %d\n", num);
}
close(B0[WRITE]);
wait(&status);
exit(EXIT_SUCCESS);
}
else if (pid == 0){
/*P1*/
close(B0[WRITE]);
do{
if (read(B0[READ],&num,sizeof(num)) != -1)
printf("P1: read %d from pipe B0\n", num);
else
printf("read: %s\n", strerror(errno));
} while(read(B0[READ],&num,sizeof(num)) != 0);
exit(EXIT_SUCCESS);
}
}
我不明白为什么我收到以下输出:
P0: written 1
P0: written 2
P0: written 3
P0: written 4
P0: written 5
P0: written 6
P0: written 7
P0: written 8
P0: written 9
P0: written 10
P1: read 1 from pipe B0
P1: read 3 from pipe B0
P1: read 5 from pipe B0
P1: read 7 from pipe B0
P1: read 9 from pipe B0
P1: read 10 from pipe B0
不管我在管道中写入的整数序列是什么,我的 read() 都会跳过每个第二个值。我试图在将值写入管道时睡眠(1),但结果是一样的。我错过了一些东西,但我没有得到什么。 发生什么事了?
你读了 1 并打印,然后在 while
条件下,你读了 2 并丢弃它。同样,您丢弃每个偶数值。在 while 条件下读取 10,并且 returns 非零,因此循环继续,然后在 if 中读取 returns 0,这不是 -1,因此打印 10。将循环写为 while( ( rv = read (... )) != 0 ) { ... }
您的 do-while
循环条件也执行 read
,但您没有使用该值。相反,您只需在循环开始时再次阅读,从而跳过每个第二个值。使用不同的条件,或使用您已阅读的值。