Linux匿名管道及实例

Linux匿名管道及实例

匿名管道,也称管道,是Linux下最常见的进程间通信方式之一。匿名管道在系统中没有实名,它只是进程的一种资源,会随着进程的结束而被系统清除。

管道的创建与关闭Linux中使用pipe()函数创建一个匿名管道,其函数原型为:

代码语言:javascript复制#include

int pipe(int fd[2]);创建成功返回0,出错返回1。参数fd[2]是一个长度为2的文件描述符数组,fd[1]是写入端的文件描述符,fd[0]是读出端的文件描述符。

可以使用文件I/O函数read()和write()读管道进行读写,使用close()函数关闭管道两端。

示例,create_pipe.c:

代码语言:javascript复制#include

#include

#include

int main(void)

{

int fd[2];

char str[256];

if((pipe(fd))<0)

{

printf("create the pipe failed!\n");

exit(1);

}

write(fd[1], "create the pipe sucessfully!\n", 31);

read(fd[0], str, sizeof(str));

printf("%s", str);

printf("pipe file descriptors are%d,%d\n", fd[0], fd[1]);

close(fd[0]);

close(fd[1]);

return 0;

}编译后执行:

代码语言:javascript复制$ ./create_pipe

create the pipe sucessfully!

pipe file descriptors are3,4程序中使用pipe函数建立了一个匿名管道fd,之后向管道一端写入数据并从另一端读出数据,将数据输出到标准输出,在程序的最后使用close函数关闭管道的两端。

进程间管道的读写单独对一个进程进行管道的读写是没有实际意义的,管道的应用体现在父子进程或兄弟进程之间的通信。

子进程的创建可以通过fork()函数完成,fork()函数原型为:

代码语言:javascript复制#include

#include

pid_t fork(void);若创建成功,父进程中返回子进程ID,子进程中返回0;若创建出错返回-1。

父子进程间管道的读写父进程利用管道向子进程发送消息,使用pipe函数建立管道,使用fork函数创建子进程,在父进程中维护管道的数据方向,并在父进程中向子进程发送消息,parent_pipe_child.c:

代码语言:javascript复制#include

#include

#include

#include

#include

#include

#define BUFSIZE PIPE_BUF

void err_quit(char *msg)

{

printf(msg);

exit(1);

}

int main(void)

{

int fd[2];

char buf[BUFSIZE] = "hello my child!\n";

pid_t pid;

int len;

// create a pipe

if((pipe(fd)) < 0)

err_quit("pipe failed\n");

// create a son process

pid = fork();

if(pid<0)

err_quit("fork failed\n");

// parent process do this

else if(pid>0)

{

close(fd[0]);// father process close read port (fd[0])

write(fd[1], buf, strlen(buf));// write to pipe

printf("I am parent process(ID:%d),write pipe ok\n", getpid());

exit(0);

}

// son process do this

else //(pid == 0)

{

close(fd[1]);// son process close itself write port (fd[1])

len = read(fd[0], buf, BUFSIZE);// read from pipe

if(len<0)

err_quit("process failed when read a pipe\n");

else

{

printf("I am son process(ID:%d),read pipe ok\n", getpid());

write(STDOUT_FILENO, buf, len);

}

exit(0);

}

}编译后执行:

代码语言:javascript复制$ ./parent_pipe_child

I am parent process(ID:4953),write pipe ok

I am son process(ID:4954),read pipe ok

hello my child!上述程序使用pipe加fork组合,实现父进程到子进程的通信,程序在父进程段中关闭了管道的读出端,并相应地在子进程中关闭了管道的输入端,从而实现数据从父进程流向子进程。

兄弟进程间管道的读写管道在兄弟进程间传递数据,brother_pipe.c:

代码语言:javascript复制#include

#include

#include

#include

#include

#include

#define BUFSIZE PIPE_BUF

void err_quit(char *msg)

{

printf(msg);

exit(1);

}

int main(void)

{

int fd[2];

char buf[BUFSIZE] = "hello my brother!\n";

pid_t pid;

int len;

// create pipe

if((pipe(fd))<0)

err_quit("pipe failed\n");

// create the son process1

pid = fork();

if(pid < 0)

err_quit("fork failed\n");

else if(pid == 0)// son process1

{

close(fd[0]);// close read port

write(fd[1], buf, strlen(buf));// write

printf("I am son process1(ID:%d), write pipe ok\n", getpid());

exit(0);

}

// create the son process2

pid = fork();

if(pid < 0)

err_quit("fork failed\n");

else if(pid > 0)

{

close(fd[0]);// close read port

close(fd[1]);// close write port

printf("I am parent process(ID:%d), close my pipe\n", getpid());

exit(0);

}

else

{

close(fd[1]);// close write port

len = read(fd[0], buf, BUFSIZE);// read

printf("I am son process2(ID:%d), read pipe ok\n", getpid());

write(STDOUT_FILENO, buf, len);

exit(0);

}

return 0;

}编译后执行:

代码语言:javascript复制$ ./brother_pipe

I am parent process(ID:4962), close my pipe

I am son process1(ID:4963), write pipe ok

I am son process2(ID:4964), read pipe ok

hello my brother!上述程序中父进程分别建立了两个子进程,在子进程1中关闭了管道的读出端,在子进程2中关闭了管道的输入端,并在父进程中关闭了管道的两端,从而构成了从子进程1到子进程2的管道。另外,程序中父进程创建第1个子进程时并没有关闭管道两端,而是在创建第2个子进程时才关闭管道,这是为了在创建第2个进程时,子进程可以继承存活的管道。

参考:《精通Linux C编程》- 程国钢

附:fork()函数的使用示例,参考:https://blog.csdn.net/jason314/article/details/5640969

代码语言:javascript复制#include

#include

int main ()

{

pid_t fpid; //fpid表示fork函数返回的值

int count=0;

fpid=fork();

if (fpid < 0)

printf("error in fork!");

// son process see fpid as 0

else if (fpid == 0)

{

printf("I am the child process, my process id:%d\n",getpid());

count++;

}

// parent process see fpid as a non 0 num

else// (fpid > 0)

{

printf("I am the parent process, my process id: %d\n",getpid());

count++;

}

printf("统计结果是: %d\n",count);

return 0;

}运行结果:

代码语言:javascript复制./forktest

I am the parent process, my process id: 5892

统计结果是: 1

I am the child process, my process id:5893

统计结果是: 1

养生小贴士

SM有限公司 (041510)
💡 小知识

SM有限公司 (041510)

📅 10-17 👍 207
问道顿悟价格一览及攻略分享
💡 小知识

问道顿悟价格一览及攻略分享

📅 09-20 👍 194
如何开通花呗?手把手教你步骤+避坑指南