Linux内核学习8:进程间通信(1)——基本概念与管道通信

关于进程间通信的问题,是处理并发进程的一个重点问题

为什么要进行进程间的通信?

  • 数据传输:一个进程需要将它的数据发送给另一个进程
  • 资源共享:多个进程之间共有一个资源,特别是临界资源
  • 通知事件:一个进程需要向另一组进程发送消息,通知它们发生了某种事件
  • 进程控制:有些进程希望完全控制另一个进程的执行,此时控制进程希望能够拦截另一个进程的所有操作,并能够及时知道它状态的改变
1:Linux进程通信(IPC)的发展历程

  • UNIX进程间通信
  • 基于system V进程间通信
  • POSIX进程通信
说明:POSIX:portable operating system interface

表示可移植操作系统接口:由IEEE开发的接口标准,提高UNIX环境下应用程序的可移植性。这个可移植操作系统接口,当某几个操作系统支持POSIX,则不需要修改直接可以使用。

system V:Linux系统独有的,自带的通信方式接口。

2:进程间通信方式

  • 管道(pipe)和有名管道(FIFO)
  • 信号(signal)
  • 消息队列
  • 信号量
  • 共享内存
  • 套接字(socket)
   2.1管道通信

   2.1.1什么是管道?
特点:

A:它把一个进程的输出和另一个进程的输入连接在一起

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

C:数据被一个进程读出后,将从管道中删除,其他的读进程不会读到

具体的流程如下图所示:


   2.1.2 管道创建

概述:无名管道与有名管道之间的区别

无名管道:用于父进程与子进程之间的通信

有名管道:用于同一系统中任意两个进程之间的通信

(1)无名管道的创建——pipe函数创建

int pipe(int filedis[2]);

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

filedis[0]用于读管道(指向管道头部),filedis[1]用于写管道(指向尾部)


(2)关闭:只需要将两个文件描述符关闭即可,用close函数逐个关闭

(3)步骤

上面提到了无名管道主要用于父子进程之间的通信,那么具体的步骤如下:

  • 先创建一个管道
  • 在通过fork函数创建一个进程,该进程会继承父进程所创建的管道
图示:

说明:在创建子进程之前必须先调用pipe(),否则进程将不会继承文件描述符

    而子进程操作管道前需要知道管道文件描述符

    目的:是因为父子进程使用一个管道。

案列:使用pipe实现两个进程之间的通信。在父进程中写端写入数据,通过子进程读出数据

#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    int pipe_fd[2];
    pid_t pid;
    char buf_r[100];
    char* p_wbuf;
    int r_num;
    memset (buf_r,0,sizeof(buf_r));
/*create gd*/
if(pipe(pipe_fd)<0)
{
    printf("pipe create error\n");
    return -1;
}
/*create child process*/
if((pid=fork())==0)//child process do
{
    printf("\n");
    close(pipe_fd[1]);
    sleep(2);//let parent process running
    if((r_num=read(pipe_fd[0],buf_r,100))>0)
    {
        printf("%d numbers read from the pipe is %s\n",r_num,buf_r);
    }
    close(pipe_fd[0]);
    exit(0);
}
else if(pid>0)//father process do
{
    close(pipe_fd[0]);
    if(write(pipe_fd[1],"hello",5)!=-1)
    printf("parent write1 hello!\n");
    if(write(pipe_fd[1],"pipe",5)!=-1)
    printf("parent write2 pipe!\n");
    close(pipe_fd[1]);
    waitpid(pid,NULL,0);
    exit(0);
}
    return 0;
}



(2)有名名管道的创建——mkfifo函数创建

1:有名管道基本概念

上一节中提到了,无名管道是用于父子进程之间的通信,而有名管道是用于任意两个进程之间的通信。

且有名管道在一些特点上更类似于文件,其具有文件名、文件属性以及存放路径等信息,也就是说利用有名管道进行操作后,我们可以在相应的路径下查找到它,更有利于编程的需要和操作,且有名管道遵循先进先出的原则。

2:有名管道的具体用法

其基本步骤如下:

先利用mkfio创建一个有名管道

然后像操作一个文件一样通过open()、close()打开和关闭,通过write和read进行读写操作

创建有名管道——mkfio

函数原型:

#include <sys/types.h>
#include <sys/stat.h>
int mkfio(const char *pathname,mode_t mode);
参数说明:

pathname:创建有名管道对应的实名文件路径

mode:文件权限

3:实例

要求:创建两个进程,在A进程中创建一个有名管道,并向内写入数据,通过B进程从有名管道中读出数据

/****read.c*****/
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO "/tmp/myfifo"
int main(int argc,char** argv)
{
    char buf_r[100];
    int fd;
    int nread;
    printf("preparing for reading bytes...\n");
    memset (buf_r,0,sizeof(buf_r));
    /******open FIFO*****/
    fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
    if(fd==-1)
    {
        perror("open");
        exit(1);
    }
    while(1)
    {
        memset (buf_r,0,sizeof(buf_r));
        if((nread=read(fd,buf_r,100))==-1)
        {
            if(errno==EAGAIN)
                printf("no data yet\n");
        }
        printf("read %s from FIFO\n",buf_r);
        sleep(1);
    }
    close(fd);
    pause();
    unlink(FIFO);
}



/****write.c*****/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#define FIFO_SERVER  "/tmp/myfifo"
int main(int argc,char** argv)
{
    int fd;
    char w_buf[100];
    int nwrite;
        /*******create fifo********/
    if((mkfifo(FIFO_SERVER,O_CREAT|O_EXCL|O_RDWR)<0)&&
      (errno!=EEXIST))
    {
        printf("cannot create fifoserver\n");
    }
        /********open fifo********/
    fd=open("/tmp/myfifo",O_WRONLY|O_NONBLOCK,0);
    if(fd==-1)
    {
        perror("open");
        exit(1);
    }
        /******parameter test*******/
    if(argc==1)
    {
        printf("please send something\n");
        exit(-1);
    }
    strcpy(w_buf,argv[1]);
        /*******write into ifio****/
    if((nwrite=write(fd,w_buf,100))==-1)
    {
        if(errno==EAGAIN)
        printf("The FIFO has not been read yet.Please             try later\n");
    }
    else
    {
        printf("write %s to the FIFO\n",w_buf);
    }
    close(fd);
    return 0;
}    




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值