进程间通信

上一篇 / 下一篇  2014-06-15 19:46:12 / 个人分类:

进程间通信概述

发展

Linux进程间通信(IPC)由以下几部分发展而来:

1、UNIX进程间通信

2、基于System V进程间通信,局限于单个计算机内

3、POSIX进程间通信,POSIX-可移植操作系统接口

现在Linux使用的进程间通信方式包括:

1、管道(pipe)和有名管道(FIFO),用于亲缘关系进程间的通信

2、信号(signal)

3、消息队列,是消息链表

4、共享内存

5、信号量

6、套接字(Socket)

要点:1、管道是单向的、先进先出,是单工通信模式。2、无名管道用于父进程和子进程间的通信。3、有名管道用于运行于同一系统中的任意两个子进程间的通信。4、用户进程用fd[1]向管道写数据,数据从管道的末端进入,用户进程用fd[0]从管道读数据,数据从管道的始端离开。

Ex1:pipe.c

//purpose:create pipe between father and child.

#include <unistd.h>

#include <sys/types.h>

#include <errno.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

#define MAX_DATA_LEN  256

#define DELAY_TIME 1

 

int main()

{

   pid_t pid;

   int pipe_fd[2];

   char buf[MAX_DATA_LEN];

   const char data[] = "Pipe Test Program";

   int real_read, real_write;

 

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

   /*创建管道*/

   if (pipe(pipe_fd) < 0)

   {

       printf("pipe create error\n");

       exit(1);

   }

 

   /*创建一子进程*/

   if ((pid = fork()) == 0)

   {

       /*子进程关闭写描述符并通过使子进程暂停1s等待父进程已关闭相应的读描述符*/

       close(pipe_fd[1]);

       sleep(DELAY_TIME * 3);

 

       /*子进程读取管道内容*/

       if ((real_read = read(pipe_fd[0], buf, MAX_DATA_LEN)) > 0)

       {

           printf("%d bytes read from the pipe is '%s'\n", real_read, buf);

       }

 

       /*关闭子进程读描述符*/

       close(pipe_fd[0]);

       exit(0);

   }

   else if (pid > 0)

   {

       /*父进程关闭读描述符并通过使父进程暂停1s等待子进程已关闭相应的写描述符*/

       close(pipe_fd[0]);

       sleep(DELAY_TIME);

 

       if((real_write = write(pipe_fd[1], data, strlen(data))) != -1)

       {

           printf("Parent wrote %d bytes : '%s'\n", real_write, data);

       }

 

       /*关闭父进程写描述符*/

       close(pipe_fd[1]);

 

       /*收集子进程退出信息*/

       waitpid(pid, NULL, 0);

       exit(0);

   }

}

Ex2:bspipe.c

//purpose: create pipe between brothersprocess

#include <unistd.h>

#include <sys/types.h>

#include <errno.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

#define MAX_DATA_LEN  256

#define DELAY_TIME 1

 

int main()

{

   pid_t pid, pid2, pc;

   int pipe_fd[2];

   char buf[MAX_DATA_LEN];

   const char data[] = "Pipe Test Program";

   int real_read, real_write;

 

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

   /*创建管道*/

   if (pipe(pipe_fd) < 0)

   {

       printf("pipe create error\n");

       exit(1);

   }

 

   /*创建一子进程*/

   if ((pid = fork()) == 0)

   {

       /*子进程关闭写描述符并通过使子进程暂停1s等待父进程已关闭相应的读描述符*/

       close(pipe_fd[1]);

       sleep(DELAY_TIME * 3);

 

       /*子进程读取管道内容*/

       if ((real_read = read(pipe_fd[0], buf, MAX_DATA_LEN)) > 0)

       {

           printf("%d bytes read from the pipe is '%s'\n", real_read, buf);

       }

 

       /*关闭子进程读描述符*/

       close(pipe_fd[0]);

       exit(0);

   }

 

   /*创建子进程2*/

   if ((pid2 = fork()) == 0) {

     /*子进程2-5 -3 -1 - +1 +3 +5

评分:0

我来说两句

我的栏目

日历

« 2024-03-28  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 5491
  • 日志数: 8
  • 建立时间: 2013-10-15
  • 更新时间: 2014-06-15

RSS订阅

Open Toolbar