当前位置: 技术问答>linux和unix
Linux编程管道操作不正常
来源: 互联网 发布时间:2016-03-17
本文导语: 本来想验证一下管道的写端对读端有倚赖.但是写出来的代码却不是想要的效果. 不知道是程序写的有毛病还是本人对管道理解的有偏颇,还请高手指点. 代码如下: 1 #include 2 #include ...
本来想验证一下管道的写端对读端有倚赖.但是写出来的代码却不是想要的效果.
不知道是程序写的有毛病还是本人对管道理解的有偏颇,还请高手指点.
代码如下:
1 #include
2 #include
3
4 #define BUFFSIZE 2*1024
5 int main(void)
6 {
7 int pipe_fd[2];
8 int ret;
9 char buf[BUFFSIZE];
10 int pid;
11
12 if ((ret = pipe(pipe_fd)) == -1)
13 {
14 fprintf(stderr,"pipe creat error!n");
15 exit(0);
16 }
17
18 if ((pid = fork()) 0)
31 {
32 close(pipe_fd[0]);//close read fd
33
34 if ((ret = write(pipe_fd[1],"This is a test!n",30)) == -1)
35 {
36 fprintf(stderr,"write error!n");
37 exit(0);
38 }
39 else
40 printf("write %d byte to pipen",ret);
41 close(pipe_fd[1]);
42 return 0;
43 }
44}
环境: REDHAT AS4(kernel 2.6.18) + gcc 3.4.3
结果如下(运行了20次):
-bash-3.00# ls
pipe_write_only pipe_write_only.c source
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
write 30 byte to pipe
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
write 30 byte to pipe
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
write 30 byte to pipe
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
write 30 byte to pipe
-bash-3.00# ./pipe_write_only
-bash-3.00#
不知道是程序写的有毛病还是本人对管道理解的有偏颇,还请高手指点.
代码如下:
1 #include
2 #include
3
4 #define BUFFSIZE 2*1024
5 int main(void)
6 {
7 int pipe_fd[2];
8 int ret;
9 char buf[BUFFSIZE];
10 int pid;
11
12 if ((ret = pipe(pipe_fd)) == -1)
13 {
14 fprintf(stderr,"pipe creat error!n");
15 exit(0);
16 }
17
18 if ((pid = fork()) 0)
31 {
32 close(pipe_fd[0]);//close read fd
33
34 if ((ret = write(pipe_fd[1],"This is a test!n",30)) == -1)
35 {
36 fprintf(stderr,"write error!n");
37 exit(0);
38 }
39 else
40 printf("write %d byte to pipen",ret);
41 close(pipe_fd[1]);
42 return 0;
43 }
44}
环境: REDHAT AS4(kernel 2.6.18) + gcc 3.4.3
结果如下(运行了20次):
-bash-3.00# ls
pipe_write_only pipe_write_only.c source
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
write 30 byte to pipe
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
write 30 byte to pipe
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
write 30 byte to pipe
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
-bash-3.00# ./pipe_write_only
write 30 byte to pipe
-bash-3.00# ./pipe_write_only
-bash-3.00#
|
L25,L26和L32,L34抢时间的问题吧。
要验证就要在L32前先sleep(1),
你再试试看。
现有的代码不能保证L32,L34在L25,L26之后执行。
供参考。
请多注意多进程多线程的同步问题。
要验证就要在L32前先sleep(1),
你再试试看。
现有的代码不能保证L32,L34在L25,L26之后执行。
供参考。
请多注意多进程多线程的同步问题。