#include <unistd.h> int pipe(int file_descriptor[2]);参数:是一个由两个整数类型的文件描述符组成的数组.
<pre name="code" class="cpp">/*************************************************************************
> File Name: pipe1.c
> Description: pipe1.c程序用pipe函数创建一个管道
> Author: Liubingbing
> Created Time: 2015年07月10日 星期五 10时54分58秒
> Other: pipe1.c程序用数组files_pipes[]的两个文件描述符创建一个管道.
然后用文件描述符file_pipes[1]向管道中写数据,用文件描述符file_pipes[0]读回数据
管道有一些内置的缓存区,它在write和read调用之间保存数据
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main()
{
/* data_processed存储返回值write和read调用的返回值 */
int data_processed;
/* 文件描述符组成的数组,file_pipes[0]用于读,file_pipes[1]用于写 */
int file_pipes[2];
const char some_data[] = "123";
char buffer[BUFSIZ + 1];
memset(buffer, '\0', sizeof(buffer));
/* pipe函数在file_pipes[0]和file_pipes[1]创建一个管道 */
if (pipe(file_pipes) == 0) {
/* write函数从指针some_data所指的内存中写入strlen(some_data)个字节到file_pipes[1]所指的文件中
* 如果成功,则返回实际写入的字节数;如果失败,则返回-1,并将错误代码保存在errno中;此外返回0表示未写入任何数据 */
data_processed = write(file_pipes[1], some_data, strlen(some_data));
printf("Wrote %d bytes\n", data_processed);
/* read函数从文件描述符file_pipes[0]指向的文件中读取BUFSIZ个字节到buffer指向的内存中
* 如果成功,则返回实际读取的字节数;如果失败,则返回-1,并将错误代码保存在errno中;此外返回0表示未读入任何数据,已经到达文件尾 */
data_processed = read(file_pipes[0], buffer, BUFSIZ);
printf("Read %d bytes: %s\n", data_processed, buffer);
exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
}
程序运行结果如下所示:/*************************************************************************
> File Name: pipe2.c
> Description: pipe2.c程序在父进程中创建一个管道,然后调用fork创建子进程,通过管道在父进程和子进程之间传递数据
> Author: Liubingbing
> Created Time: 2015年07月10日 星期五 11时41分09秒
> Other: pipe2.c程序 父进程----file_pipes[1](向管道写数据)----file_pipes[0](从管道读回数据)----子进程
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.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));
/* pipe函数用file_pipes文件描述符数组创建管道
* 用文件描述符file_pipes[1]向管道中写数据
* 用文件描述符file_pipes[0]从管道中读回数据 */
if (pipe(file_pipes) == 0) {
/* fork创建一个子进程
* 如果创建失败,返回-1
* 如果成功,返回0表示子进程pid
* 其他为父进程 */
fork_result = fork();
if (fork_result == -1) {
fprintf(stderr, "Fork failure");
exit(EXIT_FAILURE);
}
if (fork_result == 0) {
/* 子进程中使用read系统调用从file_pipes[0]指向的文件中读取BUFSIZ个字节的数据到buffer指向的内存
* 如果成功返回实际读取数据的字节数 */
//sleep(2);
data_processed = read(file_pipes[0], buffer, BUFSIZ);
printf("Read %d bytes: %s\n", data_processed, buffer);
exit(EXIT_SUCCESS);
} else {
/* 父进程中使用write系统调用从some_data指向的内存中读入strlen(some_data)个字节的数据到file_pipes[1]指向的文件
* 如果成功返回实际读入数据的字节数 */
data_processed = write(file_pipes[1], some_data, strlen(some_data));
printf("Wrote %d bytes\n", data_processed);
}
}
exit(EXIT_SUCCESS);
}这个程序首先用pipe调用创建一个管道,接着用fork调用创建一个新进程.如果fork调用成功,父进程就写数据到管道中,而子进程从管道中读取数据.父子进程都在只调用了一次write或read之后就退出.如果父进程在子进程之前退出,就会在两部分输出内容之间看到shell提示符.如下所示(./a.out在子进程中添加sleep(2)):版权声明:本文为博主原创文章,未经博主允许不得转载。
linux程序设计——pipe调用在两进程之间通信(第十三章)
原文:http://blog.csdn.net/yiranant/article/details/46834123