关于pipe的使用
進程間通信(Interprocess Communication, IPC),經典的IPC:管道、FIFO、消息隊列、信號量以及共享存儲和套接字。
一、管道
管道是UNIX系統IPC的最古老的形式,所有的UNIX系統都提供此種通信機制。
1·、兩個局限性:
(1)半雙工,數據只能在一個方向流動,現在有些系統可以支持全雙工管道,但是為了最佳的可移植性,應認為系統不支持全雙工管道;
(2)管道只能在具有公共祖先之間的兩個進程之間使用;
2、管道的創建:
它可以看成是一種特殊的文件,對于它的讀寫也可以使用普通的read、write 等函數。但是它不是普通的文件,并不屬于其他任何文件系統,并且只存在于內存中。管道是通過調用pipe函數創建的。
通過使用man 2 pipe 查看pipe的用法,使用man 7 pipe了解細節
經由參數fd返回的兩個文件描述符:pipefd[0]為讀而打開,pipefd[1]為寫而打開,pipefd[1]的輸出是pipefd[0]的輸入。通常,進程會先調用pipe,接著調用fork,從而創建了父進程與子進程的IPC通道。fork之后做什么取決于我們想要的數據流的方向,對于從父進程到子進程,父進程關閉管道的讀端pipefd[0],子進程關閉寫端pipefd[1]。
3、關閉管道的一端
(1)當讀一個寫端被關閉的管道時,在所有數據都被讀取后,read返回0,表示文件結束;
(2)當寫一個讀端被關閉的管道時,則產生信號SIGPIPE,如果忽略該信號或者捕捉該信號并從其處理程序返回,則wirte返回-1.
代碼部分:如下所示
主要是通過注釋掉read end 或者 write end 來實現關閉管道,以及對于管道基本的使用方法
#include <stdio.h> #include <unistd.h> #include <signal.h> #include <stdlib.h>#define DE// signal process function void sig_pipe(int signal) {printf("catch the SIGPIPE signal\n"); }// main function int main(int argc, const char *argv[]) {int n;int pipefd[2];int count = 0;char buf[100] = {0};char buff[100] = {0};// Register a signal for sig_pipe functionsignal(SIGPIPE,sig_pipe);// creat pipe for interprocess communicationif(pipe(pipefd) < 0){perror("fail to create pipe");exit(EXIT_FAILURE);}// close read end pipefd// close(pipefd[0]);printf("please input a string,what you want to say!\n");// gets have some dangerous fgets is betterfgets(buff,100,stdin);if((n=write(pipefd[1],buff,sizeof(buff))) < 0){perror("write error");exit(EXIT_FAILURE);}printf("Write %d bytes : %s\n",n,buff);// close write end pipefd #if 1close(pipefd[1]);// close(pipefd[0]); #endif#ifdef DEif((n=read(pipefd[0],buf,sizeof(buf))) < 0){perror("fail to read pipe");exit(EXIT_FAILURE);}printf("Rread %d bytes : %s\n",n,buf); #endifreturn 0; }在寫管道時,常量PIPE_BUF規定了內核管道的緩沖區大小,如果對管道調用write,而且要求寫的字節數小于等于PIPE_BUF,則此操作不會與其他進程對同一管道的write操作交叉進行,如果多個進程對同一管道寫的字節數超過PIPE_BUF,所寫的數據可能會與其他進程所寫的數據相互交叉。用pathconf或fpathconf函數可以獲得PIPE_BUF的值。
FPATHCONF(3) Linux Programmer's Manual FPATHCONF(3) NAME topfpathconf, pathconf - get configuration values for files SYNOPSIS top#include <unistd.h>long fpathconf(int fd, int name);long pathconf(const char *path, int name);DESCRIPTION topfpathconf() gets a value for the configuration option name for theopen file descriptor fd.pathconf() gets a value for configuration option name for thefilename path.The corresponding macros defined in <unistd.h> are minimum values; ifan application wants to take advantage of values which may change, acall to fpathconf() or pathconf() can be made, which may yield moreliberal results.Setting name equal to one of the following constants returns thefollowing configuration options:_PC_LINK_MAXThe maximum number of links to the file. If fd or path referto a directory, then the value applies to the whole directory.The corresponding macro is _POSIX_LINK_MAX._PC_MAX_CANONThe maximum length of a formatted input line, where fd or pathmust refer to a terminal. The corresponding macro is_POSIX_MAX_CANON._PC_MAX_INPUTThe maximum length of an input line, where fd or path mustrefer to a terminal. The corresponding macro is_POSIX_MAX_INPUT._PC_NAME_MAXThe maximum length of a filename in the directory path or fdthat the process is allowed to create. The correspondingmacro is _POSIX_NAME_MAX._PC_PATH_MAXThe maximum length of a relative pathname when path or fd isthe current working directory. The corresponding macro is_POSIX_PATH_MAX._PC_PIPE_BUFThe maximum number of bytes that can be written atomically toa pipe of FIFO. For fpathconf(), fd should refer to a pipe orFIFO. For fpathconf(), path should refer to a FIFO or adirectory; in the latter case, the returned value correspondsto FIFOs created in that directory. The corresponding macrois _POSIX_PIPE_BUF._PC_CHOWN_RESTRICTEDThis returns a positive value if the use of chown(2) andfchown(2) for changing a file's user ID is restricted to aprocess with appropriate privileges, and changing a file'sgroup ID to a value other than the process's effective groupID or one of its supplementary group IDs is restricted to aprocess with appropriate privileges. According to POSIX.1,this variable shall always be defined with a value other than-1. The corresponding macro is _POSIX_CHOWN_RESTRICTED.If fd or path refers to a directory, then the return valueapplies to all files in that directory._PC_NO_TRUNCThis returns nonzero if accessing filenames longer than_POSIX_NAME_MAX generates an error. The corresponding macrois _POSIX_NO_TRUNC._PC_VDISABLEThis returns nonzero if special character processing can bedisabled, where fd or path must refer to a terminal.?個人書寫方式如下:
// creat pipe for interprocess communicationif(pipe(pipefd) < 0){perror("fail to create pipe");exit(EXIT_FAILURE);}printf("#####pipebuf#### %ld\n",fpathconf(pipefd[1],_PC_PIPE_BUF));// printf("#####pipebuf#### %ld\n",pathconf((char*)pipefd,_PC_PIPE_BUF));#if 0 ########################################################## 只需要在創建pipe后,添加函數,按照man手冊上面的要求輸出打印就可以。 ########################################################## #endif精品連接https://www.cnblogs.com/funblogs/p/7675515.html
總結
- 上一篇: 一分钟教你批量制作视频的胶卷效果
- 下一篇: ES笔记