Linux:进程间通信

一般的Linux进程间通信的方法:

  • pipe
  • socket
  • shared memory
  • FIFO
  • Message Queue
  • Semaphores
  • Shared Memory

Android另外还有Binder等,也是用的shared memory机制。这有专门的说明,这里就不多说了。

pipe

pipe只能用于有同一个父进程的进程之间,或者在父进程和子进程之间进行通信。

#include <unistd.h>
int pipe(int filedes[2]);

filedes[0] 由于读,fields[1]则被用于写。
下面看一个例子:

#include "apue.h"
int
main(void)
{
    int n;
    int fd[2];
    pid_t pid;
    char line[MAXLINE];
    if (pipe(fd) < 0)//打开一个pipe
        err_sys("pipe error");

    if ((pid = fork()) < 0) {
        err_sys("fork error");
    } else if (pid > 0) { /* parent */
        close(fd[0]);//父进程中,由于没有读的,就先关闭读的Pipe
        write(fd[1], "hello world\n", 12);//fd[1]可以用于写,所以往这个pipe中写内容
    } else { /* child */
        close(fd[1]);//子进程没有写,只读。所以可以把fd[1]先关掉
        n = read(fd[0], line, MAXLINE);//读fd[0]。
        write(STDOUT_FILENO, line, n);
    }
    exit(0);
}

这里写图片描述

FIFO

FIFO可以用于两个不相干的进程间的通信。

#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
//pathname指定用于FIFO的文件的完整路径,

打开之后,就可以打开pathname对应的文件,对其进行写和读了。下面看一个例子:
Server程序:

// fifo.c

#include                 <stdio.h>   
#include                 <sys/types.h>   
#include                 <sys/stat.h>   
#include                 <string.h>   
#include                 <fcntl.h>   
#include                 <errno.h>   
#include                 <sys/time.h>   
#include                 <unistd.h>   

#define   FIFO         "/tmp/fifo.temp1"   
#define   MAXLINE   1024   

int   main(void)   
{   
     int           fifo,   fd;   
     char         buf[MAXLINE];   
     int           len;   
     fd_set     set;   
     struct     timeval   tv;   
     int           i   =   0;   

     unlink(FIFO);   //如果FIFO存在,就先删除   
     if   ((fifo   =   mkfifo(FIFO,   O_RDWR))   <   0)       //产生一个有名管道   
     {   
          printf("mkfifo   error:   %s/n",   strerror(errno));   
          return(0);   
     }   
     if   ((fd   =   open(FIFO,   O_RDWR))   <   0)               //读写打开有名管道   
     {   
          printf("open   error:   %s/n",   strerror(errno));   
          return(0);   
     }   
     FD_ZERO(&set);   
     FD_SET(fd,   &set);   
     tv.tv_sec   =   5;   
     tv.tv_usec   =   0;   //超时设置,超过5秒没有信息,就打印超时   
     while   (1)   
     {   
          FD_SET(fd,   &set);   
          if   ((i   =   select(fd   +   1,   &set,   NULL,   NULL,   &tv))   >   0)//检测管道是否信息   
          {   
              printf("receive   data/n");   
              if   (FD_ISSET(fd,   &set))   
              {   
                              len   =   read(fd,   buf,   MAXLINE);//读取信息   
                              buf[len]   =   '/0';   
                              printf("buf   =   %s/n",   buf);   
                              tv.tv_sec   =   atoi(buf);   
                              tv.tv_usec   =   0;   
              }   
          }   
          else   if   (i   ==   0)   
          {   
               tv.tv_sec   =   5;   
               tv.tv_usec   =   0;   
               printf("chaoshi/n");   
          }   
          else   
               printf("error/n");   
     }   

     unlink(FIFO);     //删除有名管道   
     return(0);   
}  

Client程序:

//fifo_cli.c

#include                 <stdio.h>   
#include                 <sys/types.h>   
#include                 <sys/stat.h>   
#include                 <string.h>   
#include                 <fcntl.h>   
#include                 <errno.h>   

#define   FIFO         "/tmp/fifo.temp1"   
#define   MAXLINE   1024   

int   main(void)   
{   
      int           fifo;   
      char         buf[MAXLINE];   
      int           len;   
      int           i   =   0;   

      strcpy(buf,   "10");   
      if   ((fifo   =   open(FIFO,   O_RDWR))   <   0)                   //读写打开有名管道   
      {   
           printf("mkfifo   error:   %s/n",   strerror(errno));   
           return(0);   
      }   
      while   (i   <   10)   
      {   
           sprintf(buf,   "%d",   i   +   1);   
           len   =   write(fifo,   buf,   strlen(buf));       //写入信息到管道中   
           printf("send   len   =   %d/n",   len);   
           sleep(i);   
           i++;   
      }   

      return(0);   
}   

编译运行:
#gcc -o fifo fifo.c
#gcc -o fifo_cli fifo_cli.c
先运行./fifo,在运行./fifo_cli
程序效果:
fifo_cli将信息通过管道发送给fifo,fifo将信息显示出来。fifo_cli发送10次信息,即0、1、2 ….. 9

fifo接受后可以显示,显示10次以后无数据接受,显示“chaoshi”,需要用kill命令删除进程。

待续…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值