pipe()函数精解
生活随笔
收集整理的這篇文章主要介紹了
pipe()函数精解
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
pipe(建立管道)
表頭文件 #include<unistd.h> 定義函數(shù) int pipe(int filedes[2]); 函數(shù)說明 pipe()會(huì)建立管道,并將文件描述詞由參數(shù) filedes 數(shù)組返回。 filedes[0]為管道里的讀取端,所以pipe用read調(diào)用的 filedes[1]則為管道的寫入端。 返回值: 若成功則返回零,否則返回-1,錯(cuò)誤原因存于 errno 中。 錯(cuò)誤代碼:? EMFILE 進(jìn)程已用完文件描述詞最大量 ENFILE 系統(tǒng)已無文件描述詞可用。 EFAULT 參數(shù) filedes 數(shù)組地址不合法。
#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));
//創(chuàng)建管道 if(pipe(pipe_fd)<0){ printf("pipe create error\n"); return -1; }
if((pid=fork())==0){//表示在子進(jìn)程中 printf("\n"); //關(guān)閉管道寫描述符,進(jì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){//表示在父進(jìn)程中,父進(jìn)程寫 //關(guān)閉管道讀描述符,進(jìn)行管道寫操作 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); } } 管道讀寫注意事項(xiàng): 1.必須在系統(tǒng)調(diào)用fork()中調(diào)用pipe(),否則子進(jìn)程將不會(huì)繼承文件描述符; 2.當(dāng)使用半雙工管道時(shí),任何關(guān)聯(lián)的進(jìn)程都必須共享一個(gè)相關(guān)的祖先進(jìn)程。 ---------------------------------------------------------------------------------------- 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; }
表頭文件 #include<unistd.h> 定義函數(shù) int pipe(int filedes[2]); 函數(shù)說明 pipe()會(huì)建立管道,并將文件描述詞由參數(shù) filedes 數(shù)組返回。 filedes[0]為管道里的讀取端,所以pipe用read調(diào)用的 filedes[1]則為管道的寫入端。 返回值: 若成功則返回零,否則返回-1,錯(cuò)誤原因存于 errno 中。 錯(cuò)誤代碼:? EMFILE 進(jìn)程已用完文件描述詞最大量 ENFILE 系統(tǒng)已無文件描述詞可用。 EFAULT 參數(shù) filedes 數(shù)組地址不合法。
#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));
//創(chuàng)建管道 if(pipe(pipe_fd)<0){ printf("pipe create error\n"); return -1; }
if((pid=fork())==0){//表示在子進(jìn)程中 printf("\n"); //關(guān)閉管道寫描述符,進(jì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){//表示在父進(jìn)程中,父進(jìn)程寫 //關(guān)閉管道讀描述符,進(jìn)行管道寫操作 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); } } 管道讀寫注意事項(xiàng): 1.必須在系統(tǒng)調(diào)用fork()中調(diào)用pipe(),否則子進(jìn)程將不會(huì)繼承文件描述符; 2.當(dāng)使用半雙工管道時(shí),任何關(guān)聯(lián)的進(jìn)程都必須共享一個(gè)相關(guān)的祖先進(jìn)程。 ---------------------------------------------------------------------------------------- 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; }
總結(jié)
以上是生活随笔為你收集整理的pipe()函数精解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: fork()
- 下一篇: write()和read()