chengouxuan@chengouxuan-laptop:~/Desktop$ cat > isum.c
#include <stdio.h>
int main()
{
int a, b;
while(scanf("%d%d", &a, &b) != EOF)
printf("%d
", a + b);
return 0;
}
chengouxuan@chengouxuan-laptop:~/Desktop$ gcc isum.c -o isum
chengouxuan@chengouxuan-laptop:~/Desktop$ cat > rand.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int i;
srand(time(NULL));
for(i=0; i<10; ++i)
printf("%d %d
", rand() % 100, rand() % 100);
return 0;
}
chengouxuan@chengouxuan-laptop:~/Desktop$ gcc rand.c -o rand
chengouxuan@chengouxuan-laptop:~/Desktop$ cat > redirection.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char *argv1[] = {argv[1], NULL};
char *argv2[] = {argv[2], NULL};
int ppid[2];
int pid1, pid2;
if (argc < 3)
{
fprintf(stdout, "usage : %s receiver sender
", argv[0]);
exit(EXIT_FAILURE);
}
pipe(ppid);
if ((pid1 = fork()) == 0)
{//child1
close(ppid[1]);
close(STDIN_FILENO);
dup(ppid[0]);
close(ppid[0]);
execvp(argv[1], argv1);
exit(EXIT_FAILURE);
}
else if ((pid2 = fork()) == 0)
{//child2
close(ppid[0]);
close(STDOUT_FILENO);
dup(ppid[1]);
close(ppid[1]);
execvp(argv[2], argv2);
exit(EXIT_FAILURE);
}
else
{//parent
close(ppid[0]);
close(ppid[1]);
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
exit(EXIT_SUCCESS);
}
return 0;
}
chengouxuan@chengouxuan-laptop:~/Desktop$ gcc redirection.c -o redir
chengouxuan@chengouxuan-laptop:~/Desktop$ ./redir ./isum ./rand
102
88
137
120
82
97
97
66
49
132建立一根pipe,将rand的stdout定向到isum的stdin。Ubuntu Android ADB 使用Ubuntu中VirtualBox使用招行usb_key相关资讯 Linux知识
- 时光总是太匆匆!Linux已经诞生23 (08/29/2014 14:12:03)
- Linux虚拟文件系统之文件打开(sys (02/14/2012 11:41:54)
- 2012 年 Linux 峰会时间表 (02/14/2012 06:47:27)
| - 报告称当前 Linux 人才抢手 高薪也 (02/15/2012 06:35:56)
- 解析企业为何选择Linux及其特别之 (02/14/2012 08:17:59)
- Linux禁用字符闪烁的方法 (11/02/2011 10:28:25)
|
本文评论 查看全部评论 (0)