您的位置:首页 > 其它

小白笔记----pipe管道(2)(dup的用法)

2015-10-24 17:06 417 查看
大概理解了dup的意思,mark一下
/*-------2015/10/24----------*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<string.h>
void pipe_read(int p)
{
char m[100] ;
read(p,m,100);
printf("that: is what we get:%s\n",m);
}
void pipe_write(int p)
{
char m[100] = "aaron";
write(p,m,strlen(m)+1);

}
main(int agrc,char **argv[],char * environ)
{
int p;
pid_t pid;
int pipefd[2];
p = pipe(pipefd);
pid = fork();
if(pid < 0){

printf("error\n");
}
if(pid == 0 )
{
printf("i am a child process\n");
close(0);
dup(pipefd[0]);
close(pipefd[0]);
close(pipefd[1]);
pipe_read(0);
execv("list2",NULL);//  The  exec() family of functions replaces the current process image witha new process image.
exit(0);

}
if(pid > 0)
{
printf("i am a parent process\n");
close(0);
dup(pipefd[1]);
close(pipefd[1]);
close(pipefd[0]);
pipe_write(0);
wait(NULL);
exit(0);

}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: