pipe(建立管道)
表头文件 #include<unistd.h>
定义函数 int pipe(int filedes[2]);
函数说明
pipe()会建立管道,并将文件描述词由参数 filedes 数组返回。
filedes[0]为管道里的读取端,所以pipe用read调用的
filedes[1]则为管道的写入端。
返回值: 若成功则返回零,否则返回-1,错误原因存于 errno 中。
错误代码:
EMFILE 进程已用完文件描述词最大量
ENFILE 系统已无文件描述词可用。
EFAULT 参数 filedes 数组地址不合法。
#include <unistd.h>
#include <stdio.h>
int main( void )
{
int filedes[2];
char buf[80];
pid_t pid;
pipe( filedes );
if ( (pid=fork()) > 0 )
{
printf( "This is in the father process,here write a string to the pipe.\n" );
char s[] = "Hello world , this is write by pipe.\n";
write( filedes[1], s, sizeof(s) );
close( filedes[0] );
close( filedes[1] );
}
else
{
printf( "This is in the child process,here read a string from the pipe.\n" );
read( filedes[0], buf, sizeof(buf) );
printf( "%s\n", buf );
close( filedes[0] );
close( filedes[1] );
}
waitpid( pid, NULL, 0 );
return 0;
}
[root@localhost src]# gcc pipe.c
[root@localhost src]# ./a.out
This is in the child process,here read a string from the pipe.
This is in the father process,here write a string to the pipe.
Hello world , this is write by pipe.
-----------------------------------------------------------------------------
#include<unistd.h>
#include<sys/types.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
int main(){
int pipe_fd[2];
pid_t pid;
char buf_r[100];
char *p_wbuf;
int r_num;
memset(buf_r,0,sizeof(buf_r));
//创建管道
if(pipe(pipe_fd)<0){
printf("pipe create error\n");
return -1;
}
if((pid=fork())==0){//表示在子进程中
printf("\n");
//关闭管道写描述符,进行管道读操作
close(pipe_fd[1]);
sleep(2);
//管道描述符中读取
if((r_num=read(pipe_fd[0],buf_r,100))>0){
printf("%d numbers read from the pipe is %s\n",r_num,buf_r);
}
close(pipe_fd[0]);
exit(0);
}
else if(pid>0){//表示在父进程中,父进程写
//关闭管道读描述符,进行管道写操作
close(pipe_fd[0]);
if(write(pipe_fd[1],"Hello",5)!=-1)
printf("parent write1 success!\n");
if(write(pipe_fd[1],"Pipe",5)!=1)
printf("parent write2 success!\n");
close(pipe_fd[1]);
sleep(3);
waitpid(pid,NULL,0);
exit(0);
}
}
管道读写注意事项:
1.必须在系统调用fork()中调用pipe(),否则子进程将不会继承文件描述符;
2.当使用半双工管道时,任何关联的进程都必须共享一个相关的祖先进程。
----------------------------------------------------------------------------------------
int in[2],out[2],err[2],child_fd[3];
#define close_pipe(pipe) close(pipe[0]); close(pipe[1])
int SendCmd(const char *Cmd)
{
int iRet = 0;
iRet = write(child_fd[0], Cmd, strlen(Cmd));
return iRet;
}
int ReadDate(const char *Buf,int Len)
{
int iRet = 0;
iRet = read(child_fd[1], (void *)Buf, Len);
return iRet;
}
int main(void)
{
//int filedes[2];
char buf[80];
pid_t pid;
pipe(in);
pipe(out);
pipe(err);
pid=fork();
if(pid == 0)
{
printf( "This is in the child process,here read a string from the pipe.\n" );
int err_fd = dup(2);
FILE* errf = fdopen(err_fd,"w");
// Bind the std fd to our pipes
dup2(in[0],0);
dup2(out[1],1);
dup2(err[1],2);
execl("/bin/sh","sh","-c","/usr/bin/mplayer -slave -idle -quiet -v -af",(void*)NULL);
fprintf(errf,"exec failed : %s\n",strerror(errno));
exit(1);
}
else if(pid<0)
{
printf( "This is in the father process\n" );
stderr,"errno : %s\n",strerror(errno);
close_pipe(in);
close_pipe(out);
close_pipe(err);
return 0;
}
child_fd[0] = in[1];
child_fd[1] = out[0];
child_fd[2] = err[0];
printf("Start...\n");
while(1)
{
switch(getchar())
{
case 'r':
SendCmd("loadfile /mnt/Chinese/CH_1.avi\n");
break;
dafault:
break;
}
//SendCmd("loadfile /mnt/Chinese/CH_1.avi\n");
//sleep(5);
}
return 0;
}