1 #include <unistd.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <errno.h>
6 int main (void)
7 {
8 int forkId;
9 int fd[2];
10 char readBuf[100];
11 int i = 0;
12 if (pipe (fd) < 0)
13 {
14 perror ("\npipe error\n");
15 exit;
16 }
17 forkId = fork ();
18 if (forkId < 0)
19 {
20 perror ("\nfork error\n");
21 exit;
22 }
23 if (forkId == 0)
24 {
25 printf ("\nchild process\nleave pipe fd[1] for write\n");
26 close (fd[0]);
27 while (1)
28 {
29 write (fd[1], "hello,pipe", 10);
30 sleep (1);
31 }
32 }
33 else
34 {
35 printf ("\nfather process\nleave pipe fd[0] for read\n");
36 close (fd[1]);
37 printf ("\nwait for read\n");
38 while (1)
39 {
40 sleep (1);
41 memset (readBuf, '\0', 100);
42 read (fd[0], readBuf, 100);
43 #if 0
44 for (i = 0; i < 20; i++)
45 {
46 printf ("\3X", readBuf[i]);
47 }
48 #endif
49 puts (readBuf);
50 }
百度空间不能插入代码,没有办法,只有先用gvim转成html,然后用浏览器打开,在复制到这里面了,比直接插入代码多了一步,不过勉强凑合吧。
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <errno.h>
6 int main (void)
7 {
8 int forkId;
9 int fd[2];
10 char readBuf[100];
11 int i = 0;
12 if (pipe (fd) < 0)
13 {
14 perror ("\npipe error\n");
15 exit;
16 }
17 forkId = fork ();
18 if (forkId < 0)
19 {
20 perror ("\nfork error\n");
21 exit;
22 }
23 if (forkId == 0)
24 {
25 printf ("\nchild process\nleave pipe fd[1] for write\n");
26 close (fd[0]);
27 while (1)
28 {
29 write (fd[1], "hello,pipe", 10);
30 sleep (1);
31 }
32 }
33 else
34 {
35 printf ("\nfather process\nleave pipe fd[0] for read\n");
36 close (fd[1]);
37 printf ("\nwait for read\n");
38 while (1)
39 {
40 sleep (1);
41 memset (readBuf, '\0', 100);
42 read (fd[0], readBuf, 100);
43 #if 0
44 for (i = 0; i < 20; i++)
45 {
46 printf ("\3X", readBuf[i]);
47 }
48 #endif
49 puts (readBuf);
50 }
百度空间不能插入代码,没有办法,只有先用gvim转成html,然后用浏览器打开,在复制到这里面了,比直接插入代码多了一步,不过勉强凑合吧。