1/*******************************************************************************
2 * Author :Honker.y
3 * Email :Honker.ying@gmail.com
4 * Last modified :2011-05-05 18:59
5 * Filename :read_tset.c
6 * version :
7 * Description :子进程读取父进程传递的信息
8 * Revision :
9 * Compiler :gcc (Debian 4.4 .5 - 8)
10 * Install :
11 * Use :
12 *******************************************************************************/
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <errno.h>
19 #include <string.h>
20 int
21 main (int argc, char *argv[])
22 {
23 int pipe_fd[2];
24 pid_t pid;
25 char r_buf[100];
26 char w_buf[4];
27 char *p_wbuf;
28 int r_num;
29
30 memset (r_buf, 0, sizeof (r_buf));
31 memset (w_buf, 0, sizeof (r_buf));
32 p_wbuf = w_buf;
33 if (pipe (pipe_fd) < 0)
34 {
35 printf ("pipe create error/n");
36 return -1;
37 }
38
39 if ((pid = fork ()) == 0)
40 {
41 printf ("/n");
42 close (pipe_fd[1]);
43 sleep (3); /* 确保父进程关闭写端 */
44 r_num = read (pipe_fd[0], r_buf, 100);
45 printf ("read num is %d the data read from the pipe is %d/n", r_num,
46 atoi (r_buf));
47
48 close (pipe_fd[0]);
49 exit (0);
50 }
51 else if (pid > 0)
52 {
53 close (pipe_fd[0]); /* read */
54 strcpy (w_buf, "111");
55 if (write (pipe_fd[1], w_buf, 4) != -1)
56 printf ("parent write over/n");
57 close (pipe_fd[1]); /* write */
58 printf ("parent close fd[1] over/n");
59 sleep (10);
60 }
61 exit (0);
62 }
just do a test for HTML
最新推荐文章于 2024-08-29 20:46:53 发布