在Linux中使用較多的處理序間通訊方式主要有以下幾種
1)管道Pipe
2)訊號Signal
3)訊息佇列Message Queue:是訊息的連結資料表
4)共用記憶體Shared Memory:最有效處理序間通訊方式
5)訊號量Semaphore:主要作為進程之間以及同一進程的不同線程之間的同步和互斥
6)通訊端Socket:用於網路中不同機器之間的通訊
1. pipe(建立無名管道)
表標頭檔 #include <unistd.h>
函數定義 int pipe(int filedes[2]);
函數說明 pipe()會建立無名管道,並將檔案描述符由參數filedes數組返回,filedes[0]用於讀管道,filedes[1]用於寫管道
只能用於有親緣關係的進程之間通訊
成功返回0,錯誤返回-1
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>int main(int argc, char **argv){int fd[2];char buf[1024] = "every day is good day";int ret=0;if(pipe(fd) < 0) {//建立無名管道perror("piple");exit(1);}pid_t pid;if((pid = fork()) == 0) { //建立一子進程ret = write(fd[1], buf, strlen(buf));if (ret < 0) {perror("write");exit(1);}printf("write %d bytes [%s]\n", ret, buf);} else if(pid > 0) {sleep(1);ret = read(fd[0], buf, sizeof(buf));printf("%d bytes read from the pipe is [%s]\n", ret, buf);if (ret < 0) {perror("read");exit(1);}}close(fd[0]);close(fd[1]);return 0;}----------------------------------------/pipe# ./test write 21 bytes [every day is good day]21 bytes read from the pipe is [every day is good day]
2. mafifo()建立有名管道
kfifo(建立有名管道)
表標頭檔 #include <sys/types.h>
#include <sys/stat.h>
函數定義 int mkfifo(const char *pathname, mode_t mode);
函數說明 mkfifo()會依參數pathname建立特殊的FIFO檔案,該檔案必須不存在,而參數
mode為該檔案的許可權(mode&~umask)
mode specifies the FIFO's permissions. It is modified by the
process's umask in the usual way: the permissions of the created file are (mode & ~umask)
.umask值也會影響到FIFO檔案的許可權,mkfifo()建立的FIFO檔案其他進程都可以用讀寫一般檔案的方式存取,
當使用open()來開啟FIFO檔案時,O_NONBLOCK會有影響
當使用O_NONBLOCK時,開啟FIFO檔案讀取的操作會立即返回,但是若還沒有其他進程開啟FIFO來讀取,
則寫入的操作會返回ENXIO錯誤碼
當沒有使用O_NONBLOCK檔案,開啟FIFO檔案讀取的操作會等到其他進程開啟FIFO檔案來寫入才正常返回,
同樣,開啟FIFO檔案來寫入的操作會等到其他進程開啟FIFO檔案來讀取後才正常返回
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <string.h>#define FIFO "/tmp/myfifo" //有名管道檔案名稱int main(int argc, char **argv){int fd;int ret = 0;umask(0);char buf[]="hello world";char buf1[1024];if (mkfifo(FIFO, 07777) < 0) {perror("mkfifo");exit(1);}pid_t pid;if ((pid = fork()) == 0){fd = open(FIFO, O_WRONLY);ret = write(fd, buf, strlen(buf));printf("write %d bytes, [%s]\n", ret, buf);close(fd);} else if(pid > 0) {sleep(1);fd = open(FIFO, O_RDONLY);ret = read(fd, buf1, sizeof(buf1));printf("read %d bytes, [%s]\n", ret, buf);close(fd);}}-----------------------------------------------------/myfifo# ./test write 11 bytes, [hello world]read 11 bytes, [hello world]第二次運行myfifo# ./test mkfifo: File exists因為管道也經建立了myfifo# ls /tmp/myfifo /tmp/myfifo