- 论坛徽章:
- 0
|
- jackin@jackin-desktop:~/xxx$ cat xxx.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #define INPUT_STR "23 45\n"
- int main()
- {
- int p2c[2];
- int c2p[2];
- pid_t pid;
- char buff[128] = {0};
- if(pipe(p2c) || pipe(c2p))
- {
- printf(" pipe error\n");
- return -1;
- }
-
- pid = vfork();
- if( pid < 0 )
- {
- printf(" fork error\n");
- return -1;
- }
- else if( pid ) ///parent
- {
- close(p2c[0]);
- close(c2p[1]);
-
- write(p2c[1], INPUT_STR, strlen(INPUT_STR));
- read(c2p[0], buff, sizeof(buff));
-
- printf("Answer:%s\n",buff);
-
- return 0;
- }
- else //// child
- {
- close(p2c[1]);
- close(c2p[0]);
- close(0);
- close(1);
-
- dup2(p2c[0], 0);
- dup2(c2p[1], 1);
-
- execl("./add", NULL);
- }
- }
-
- jackin@jackin-desktop:~/xxx$ cat test.c
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- int a, b;
-
- scanf("%d %d", &a, &b);
- printf("%d", a+b);
- }
- jackin@jackin-desktop:~/xxx$ ./a.out
- Answer:68
- jackin@jackin-desktop:~/xxx$
复制代码 |
|