一、處理序間通訊
每個進程各自有不同的使用者地址空間,任何一個進程的全域變數在另一個進程中都看不到,所以進程之間要交換資料必須通過核心,在核心中開闢一塊緩衝區,進程1把資料從使用者空間拷到核心緩衝區,進程2再從核心緩衝區把資料讀走,核心提供的這種機制稱為處理序間通訊(IPC,InterProcess Communication)。如所示。
二、管道是一種最基本的IPC機制,由pipe函數建立:
#include <unistd.h>
int pipe(int filedes[2]);
調用pipe函數時在核心中開闢一塊緩衝區(稱為管道)用於通訊,它有一個讀端一個寫端,然後通過filedes參數傳出給使用者程式兩個檔案描述符,filedes[0]指向管道的讀端,filedes[1]指向管道的寫端(很好記,就像0是標準輸入1是標準輸出一樣)。所以管道在使用者程式看起來就像一個開啟的檔案,通過read(filedes[0]);或者write(filedes[1]);向這個檔案讀寫資料其實是在讀寫核心緩衝區。pipe函數調用成功返回0,調用失敗返回-1。
開闢了管道之後如何?兩個進程間的通訊呢?比如可以按下面的步驟通訊。
樣本程式如下:
C++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
|
/*************************************************************************
> File Name: process_.c
> Author: Simba
> Mail: dameng34@163.com
> Created Time: Sat 23 Feb 2013 02:34:02 PM CST
************************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<signal.h>
#define ERR_EXIT(m) \
do { \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
int main(int argc, char *argv[])
{
int pipefd[2];
if (pipe(pipefd) == -1)
ERR_EXIT("pipe error");
pid_t pid;
pid = fork();
if (pid == -1)
ERR_EXIT("fork error");
if (pid == 0)
{
close(pipefd[0]);
write(pipefd[1], "hello", 5);
close(pipefd[1]);
exit(EXIT_SUCCESS);
}
close(pipefd[1]);
char buf[10] = {0};
read(pipefd[0], buf, 10);
printf("buf=%s\n", buf);
return 0;
} |
1. 父進程調用pipe開闢管道,得到兩個檔案描述符指向管道的兩端。
2. 父進程調用fork建立子進程,那麼子進程也有兩個檔案描述符指向同一管道。
3. 父進程關閉管道寫端,子進程關閉管道讀端。子進程可以往管道裡寫,父進程可以從管道裡讀,管道是用環形隊列實現的,資料從寫端流入從讀端流出,這樣就實現了處理序間通訊。
三、利用pipe和dup2函數類比命令列 ls | wc -w 功能
C++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
|
/*************************************************************************
> File Name: process_.c
> Author: Simba
> Mail: dameng34@163.com
> Created Time: Sat 23 Feb 2013 02:34:02 PM CST
************************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<signal.h>
#define ERR_EXIT(m) \
do { \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
int main(int argc, char *argv[])
{
int pipefd[2];
if (pipe(pipefd) == -1)
ERR_EXIT("pipe error");
pid_t pid;
pid = fork();
if (pid == -1)
ERR_EXIT("fork error");
if (pid == 0)
{
dup2(pipefd[1], STDOUT_FILENO); //輸出重新導向
close(pipefd[1]);
close(pipefd[0]);
execlp("ls", "ls", NULL);
fprintf(stderr, "error execute ls\n");
exit(EXIT_FAILURE);
}
dup2(pipefd[0], STDIN_FILENO);
close(pipefd[0]);
close(pipefd[1]);
execlp("wc", "wc", "-w", NULL);
fprintf(stderr, "error execute wc\n");
exit(EXIT_FAILURE);
return 0;
} |
我們知道命令列 ls | wc -w 中 ls 會輸出到管道,而wc 從管道裡讀取,現在使用dup2複製檔案描述符,使ls 的標準輸出為管道,wc 的標準輸入也為管道,即使父進程先被調度,因為預設是阻塞I/O操作,故wc 會read 阻塞直到管道被子進程寫入了資料。
使用管道有一些限制:
兩個進程通過一個管道只能實現單向通訊,比如最上面的例子,父進程讀子進程寫,如果有時候也需要子進程讀父進程寫,就必須另開一個管道。
管道的讀寫端通過開啟的檔案描述符來傳遞,因此要通訊的兩個進程必須從它們的公用祖先那裡繼承管道檔案描述符。上面的例子是父進程把檔案描述符傳給子進程之後父子進程之間通訊,也可以父進程fork兩次,把檔案描述符傳給兩個子進程,然後兩個子進程之間通訊,總之需要通過fork傳遞檔案描述符使兩個進程都能訪問同一管道,它們才能通訊。
參考:《APUE》、《linux c 編程一站式學習》