Home Article Linux进程通信之---管道通信

Linux进程通信之---管道通信

Release time:2020-04-28 08:50:57 Author:一蓑烟雨 Reading volume:56

管道通信

管道是单向的、先进先出的,它把一个进程的输出和另一个进程的输入连接在一起。

一个进程(写进程)在管道的尾部写入数据,另一个进程(读进程)从管道的头部读出数据

数据被一个进程读出后,将被从管道中删除,其它读进程将不能再读到这些数据。

管道提供了简单的流控制机制,进程试图读空管道时,进程将阻塞。同样,管道已经满时,进程再试图向管道写入数据,进程将阻塞

管道包括无名管道和有名管道两种,前者用于父进程和子进程间的通信,后者可用于运行于同一系统中的任意两个进程间的通信

 无名管道

无名管道创建:

int pipe(int filedis[2]);

当一个管道建立时,它会创建两个文件描述符:

filedis[0] 用于读管道,

filedis[1] 用于写管道

#include

#include

#include

#include

int main()

{

int pipe_fd[2];

if(pipe(pipe_fd)<0)

{

printf("pipe create error ");

return -1;

}

else

printf("pipe create success ");

close(pipe_fd[0]);

close(pipe_fd[1]);

}

管道用于不同进程间通信。通常先创建一个管道,再通过fork函数创建一个子进程,该子进程会继承父进程所创建的管道

必须在系统调用fork( )前调用pipe( ),否则子进程将不会继承文件描述符

pipe管道读完之后会删除

无东西读的时候会阻塞

只能父子进程通信

  pipe.c(单独读写)

#include

2 #include

3

4 int main()

5 {

6 int fd[2];

7 int ret;

8 char writebuf[] = "hello linux";

9 char readbuf[128] = {0};

10 ret = pipe(fd);

11 if(ret < 0)

12 {

13 printf("create pipe failure ");

14 exit(1);

15 }

16 printf("create pipe successful ");

17

18 write(fd[1],writebuf,sizeof(writebuf));

19

20 read(fd[0],readbuf,sizeof(readbuf));

21 printf("readbuf = %s ",readbuf);

22

23 close(fd[1]);

24 close(fd[0]);

25 return 0;

26 }

  父子进程间的通信

#include

#include

#include

#include <sys/types.h>

#include

int main()

{

pid_t pid;

int fd[2];

int ret;

char writebuf[] = "hello linux";

char readbuf[128] = {0};

ret = pipe(fd);

if(ret < 0)

{

printf("create pipe failure ");

exit(1);

}

printf("create pipe successful ");

pid = fork();

if (pid == -1)

{

printf("fork fail! ");

exit(1);

}

printf("fork success ");

if (pid == 0)

{ //child process

while(1)

{

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

printf("recived:%s ",readbuf);

if (strcmp(readbuf,"exit") == 0)

{

break;

}

}

sleep(2);

}

else if (pid > 0)

{

//father process

while(1)

{

scanf("%s",writebuf);

write(fd[1],writebuf,sizeof(writebuf));

if (strcmp(writebuf,"exit")==0)

{

break;

}

}

sleep(2);

}

close(fd[1]);

close(fd[0]);

return 0;

}

 有名管道

命名管道和无名管道基本相同,但也有不同点:

无名管道只能由父子进程使用;

但是通过命名管道,不相关的进程也能交换数据。

#include <sys/types.h>

#include <sys/stat.h>

int mkfifo(const char * pathname, mode_t mode)

pathname:FIFO文件名

mode:属性(见文件操作章节)

一旦创建了一个FIFO,就可用open打开它,一般的文件访问函数(close、read、write等)都可用于FIFO

  无关进程间的通信

发送端:

#include

#include

#include

#include <sys/types.h>

#include

int main(){

char buf[100];

 

int ret = mkfifo("fifo",0777);

if (ret == -1)

{

printf("create fifo failure! ");

exit(1);

}

printf("create fifo success! ");

 

int fd = open("./fifo",O_WRONLY,0);

if (fd == -1)

{

perror("error!");

exit(1);

}

while(1)

{

memset(buf,0,sizeof(buf));

scanf("%s",buf);

printf("send :%s ",buf);

write(fd,buf,sizeof(buf));

printf("success! ");

}

close(fd);

return 0;

}

接收端:

#include

#include

#include

int main(){

char buf[100];

int fd = open("./fifo",0644);

while(1){

memset(buf,0,sizeof(buf));

read(fd,buf,sizeof(buf));

printf("recieve:%s ",buf);

}

close(fd);

return 0;

}

  父子进程间的通信

#include

#include

#include <sys/types.h>

#include

#include

#include

int main()

{

pid_t pid;

char buf[120] = {0};

 

int ret = mkfifo("fifo",0644);

if (ret == -1)

{

printf("create fifo error! ");

exit(1);

}

printf("create fifo successful! ");

pid = fork();

if(pid == -1)

{

printf("fork error! ");

exit(1);

}

if(pid == 0)

{//son process

int fd = open("fifo",O_RDONLY);

if(fd == -1)

{

printf("open file error!");

exit(1);

}

while(1)

{

memset(buf,0,sizeof(buf));

read(fd,buf,sizeof(buf));

printf("recived : %s ",buf);

if (strcmp(buf,"exit")==0)

{

sleep(2);

break;

}

}

}

else if (pid > 0)

{//parent process

int fd = open("fifo",O_WRONLY);

if(fd == -1)

{

printf("open file error!");

exit(1);

}

while(1)

{

 

scanf("%s",buf);

write(fd,buf,sizeof(buf));

if (strcmp(buf,"exit")==0)

{

sleep(2);

break;

}

}

}

return 0;

}

支付宝打赏 微信打赏
  
I want to comment

Search

classification

Leave a message
http://blog.rjxj513.com/
User login
You have not written any reviews yet!
You have commented!
Can only praise once!
You have a collection!