一、有名管道
1. 為何提出有名管道的說法,目的是為了克服無名管道的不足之處:
- 無名管道只能是用于具有親緣關系的進程之間,這就限制了無名管道的使用范圍。
- 有名管道可以使互不相關的兩個進程互相通信,有名管道可以通過路徑名來指出,并在文件系統課件
為了這種有名管道,Linux中專門設立了一個專門的特殊文件系統-管道文件,以FIFO的形式存在于文件系統中,這樣,即使與FIFO的創建者不存在親緣關系的進程,只要訪問該路徑,就能彼此通過FIFO相互通信,因此,通過FIFO不相關的進程也能交換數據,但在磁盤只是一個節點,而文件的數據只存在內存緩沖頁面上,與普通管道一樣。
?
2. 有名管道的創建
- 有名管道可以從命令行上創建,命令行方法是使用下面這個命令:
$ mkfifo myfifo
- 有名管道也可以從程序里創建,相關API有:
#include <sys/stat/h>
int mkfifo(cosnt char *path, mode_t mode);
參數:
- 第一個參數是一個普通的路徑名,也就是創建后FIFO名字。
- 第二個參數與打開普通文件的open函數中的mode參數相同(文件的讀寫權限),如果mkfifo的一個參數是一個已經存在路徑名時,會返回EEXIST錯誤,所以一般典型的調用代碼會檢查是否返回該錯誤,如果確實返回該錯誤,那么要調用打開FIFO的函數open就可以了。
?
3. FIFO的open函數打開規則:
O_RDONLY、O_WRONLY和O_NONBLOCK標志共有四種合法的組成方式:
- flags = O_RDONLY:open將會調用阻塞,除非有另外一個進程以寫的方式打開用一個FIFO,否則一直等待。
- flags = O_WRONLY:open將會調用阻塞,除非有另外一個進程以讀的方式打開同一個FIFO,否則一直等待。
- flags =?O_RDONLY | O_NONBLOCK:如果此時沒有其他進程以寫的方式打開FIFO,此時open也會成功返回,此時FIOF被讀打開,而不會返回錯誤。
- flag是= O_WRONLY | O_NONBLOCK:立即返回,如果此時沒有其他進程以讀的方式打開,open會失敗打開,此時FIOF沒有被打開,返回-1。
?
二、程序清單
1. 測試代碼:
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>int main(int argc, const char *argv[])
{int ret = mkfifo("myfifo", 0666);if (ret == -1) {perror("mkfifo");exit(-1);}return 0;
}
輸出結果:
2. 測試代碼:
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>int main(int argc, const char *argv[])
{if (access(argv[1], F_OK) != 0) {int ret = mkfifo(argv[1], 0666);if (ret == -1) {perror("mkfifo");exit(-1);}}return 0;
}
輸出結果:
3. 測試代碼:
程序1:
// writefifo.c
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>int main(int argc, const char *argv[])
{int fd, nwrite;char buf[1024] = "\0";if (access(argv[1], F_OK) != 0) {int ret = mkfifo(argv[1], 0666);if (ret == -1) {perror("mkfifo");exit(-1);}}fd = open(argv[1], O_WRONLY);if (fd == -1) {perror("open");return -1;}while (1) {fgets(buf, sizeof(buf), stdin);buf[strlen(buf) - 1] = '\0';nwrite = write(fd, buf, strlen(buf));if (nwrite == -1) {perror("write error");return -1;}if (!strncmp(buf, "quit", 4))break;}return 0;
}
2. 程序2:
//readfifo.c
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, const char *argv[])
{int ret, fd, nread;char buf[1024] = "\0";if (access(argv[1], F_OK) != 0) {ret = mkfifo(argv[1], 0666);if (ret == -1) {perror("mkfifo");exit(-1);}}fd = open(argv[1], O_RDONLY);if (fd == -1) {perror("open");exit(-1);}while (1) {nread = read(fd, buf, sizeof(buf));if (nread == -1) {perror("read errro");exit(-1);}printf("read from fifo is %s\n", buf);if (!strncmp(buf, "quit", 4))break;memset(buf, '\0', sizeof(buf));}return 0;
}
?輸出結果:
【題目】驗證:flags =?O_RDONLY | O_NONBLOCK:如果此時沒有其他進程以寫的方式打開FIFO,此時open也會成功返回,此時FIOF被讀打開,而不會返回錯誤。
3. 測試代碼:
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, const char *argv[])
{int ret, fd, nread;char buf[1024] = "\0";if (access(argv[1], F_OK) != 0) {ret = mkfifo(argv[1], 0666);if (ret == -1) {perror("mkfifo");exit(-1);}}fd = open(argv[1], O_RDONLY | O_NONBLOCK);if (fd == -1) {perror("open");exit(-1);}printf("open read fifo successfully\n");while (1) {nread = read(fd, buf, sizeof(buf));if (nread == -1) {perror("read errro");exit(-1);}printf("read from fifo is %s\n", buf);if (!strncmp(buf, "quit", 4))break;memset(buf, '\0', sizeof(buf));}return 0;
}
?輸出結果:
?
【題目】驗證:flag是= O_WRONLY | O_NONBLOCK:立即返回,如果此時沒有其他進程以讀的方式打開,open會失敗打開,此時FIOF沒有被打開,返回-1。
4. 測試代碼:
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>int main(int argc, const char *argv[])
{int fd, nwrite;char buf[1024] = "\0";if (access(argv[1], F_OK) != 0) {int ret = mkfifo(argv[1], 0666);if (ret == -1) {perror("mkfifo");exit(-1);}}fd = open(argv[1], O_WRONLY | O_NONBLOCK);if (fd == -1) {perror("open");return -1;}printf("open write fifo successfully\n");while (1) {fgets(buf, sizeof(buf), stdin);buf[strlen(buf) - 1] = '\0';nwrite = write(fd, buf, strlen(buf));if (nwrite == -1) {perror("write error");return -1;}if (!strncmp(buf, "quit", 4))break;}return 0;
}
?輸出結果: