进程间通信(IPC):管道(Pipe)

来源:互联网 发布:姚明0809赛季数据 编辑:程序博客网 时间:2024/06/10 19:00

管道:一个进程连接数据流到另一个程序

pipe函数的原型:

#include <unistd.h>int pipe(int file_descriptor[2]);
该闪身在数组中填上两个新的文件描述符后返回0,如果失败则返回-1。写到file_descriptor[1]的所有数据都可以从file_descriptor[0]读回来。这里使用的是文件描述符而不是文件流,所以我们必须用底层的read和write调用来访问数据,而不是用文件流库函数fread和fwrite

#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>int main(){    int data_processed;    int file_pipes[2];    const char some_data[] = "123";    char buffer[BUFSIZ + 1];    memset(buffer, '\0', sizeof(buffer));    if (pipe(file_pipes) == 0) {        data_processed = write(file_pipes[1], some_data, strlen(some_data));        printf("Wrote %d bytes\n", data_processed);        data_processed = read(file_pipes[0], buffer, BUFSIZ);        printf("Read %d bytes: %s\n", data_processed, buffer);        exit(EXIT_SUCCESS);    }    exit(EXIT_FAILURE);}~     
输出:

Wrote 3 bytesRead 3 bytes: 123

可以通过管道在两个进程之间传递数据

#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>int main(){    int data_processed;    int file_pipes[2];    const char some_data[] = "123";    char buffer[BUFSIZ + 1];    pid_t fork_result;    memset(buffer, '\0', sizeof(buffer));    if (pipe(file_pipes) == 0) {        fork_result = fork();        if (fork_result == -1) {            fprintf(stderr, "Fork failure");            exit(EXIT_FAILURE);        }// We've made sure the fork worked, so if fork_result equals zero, we're in the child process.        if (fork_result == 0) {            data_processed = read(file_pipes[0], buffer, BUFSIZ);            printf("Read %d bytes: %s\n", data_processed, buffer);            exit(EXIT_SUCCESS);        }// Otherwise, we must be the parent process.        else {            data_processed = write(file_pipes[1], some_data,                                   strlen(some_data));            printf("Wrote %d bytes\n", data_processed);        }    }    exit(EXIT_SUCCESS);}


输出:

Wrote 3 bytesjessica@ubuntu:~$ Read 3 bytes: 123

这个程序实现了再不同的进程之间进行读写操作。但仅仅是运行一个相同的程序。

接下来的程序中子进程中运行一个与其父进程完全不同的另外一个程序。

下面是pipe3.c

#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>int main(){    int data_processed;    int file_pipes[2];    const char some_data[] = "123";    char buffer[BUFSIZ + 1];    pid_t fork_result;    memset(buffer, '\0', sizeof(buffer));    if (pipe(file_pipes) == 0) {        fork_result = fork();        if (fork_result == (pid_t)-1) {            fprintf(stderr, "Fork failure");            exit(EXIT_FAILURE);        }        if (fork_result == 0) {            sprintf(buffer, "%d", file_pipes[0]);            (void)execl("pipe4", "pipe4", buffer, (char *)0);            exit(EXIT_FAILURE);        }      else {            data_processed = write(file_pipes[1], some_data,                                   strlen(some_data));            printf("%d - wrote %d bytes\n", getpid(), data_processed);        }    }    exit(EXIT_SUCCESS);}

下面试pipe4.c

#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>int main(int argc, char *argv[]){    int data_processed;    char buffer[BUFSIZ + 1];    int file_descriptor;    memset(buffer, '\0', sizeof(buffer));    sscanf(argv[1], "%d", &file_descriptor);    data_processed = read(file_descriptor, buffer, BUFSIZ);    printf("%d - read %d bytes: %s\n", getpid(), data_processed, buffer);    exit(EXIT_SUCCESS);}


pipe3在程序中调用pipe4,pipe4程序从参数字符串中提取出文件描述符数字,然后读取该文件描述符来获取数据。





原创粉丝点击