进程、线程和进程间通信-进程间通讯

进程间通信(IPC,InterProcess Communication

概念:就是进程和进程之间交换信息。

常用通信方式

无名管道(pipe)

有名管道 (fifo)

信号(signal)

共享内存(mmap)

套接字(socket)

过时的IPC通信方式

System V IPC

共享内存(share memory)

消息队列(message queue)

信号灯集(semaphore set)

无名管道

无名管道特点

无名管道具有如下特点:

只能用于具有亲缘关系的进程之间的通信

单工的通信模式,具有固定的读端和写端

无名管道创建时会返回两个文件描述符,分别用于读写管道

无名管道创建 – pipe
#include  <unistd.h>
int  pipe(int pfd[2]);

 int pipe(int pfd[2]); 成功:0;失败:-1,设置errno

 pfd  包含两个元素的整形数组,用来保存文件描述符  

pfd[0]用于读管道;pfd[1]用于写管道

#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main()
{
    //管道文件描述符
    int pfd[2];
    //管道创建返回值
    int re;
    char buf[20] = { 0 };
    pid_t pid;
    re = pipe(pfd);
    if (re < 0)
    {
        perror("pipe");
        return 0;
    }
    printf("%d,%d\n", pfd[0], pfd[1]);
    //创建子进程
    pid = fork();
    if (pid < 0)
    {
        perror("fork");
        return 0;
    }
    else if (pid > 0)//父进程
    {
        //close(pfd[0]);
        while (1)
        {

            strcpy(buf, "hhahahahah");
            //父进程写数据
            write(pfd[1], buf, strlen(buf));

            sleep(1);
        }

    }
    else
    {

        close(pfd[1]);
        while (1)
        {
            //子进程读数据
            re = read(pfd[0], buf, 20);
            if (re > 0)
            {
                printf("read pipe=%s\n", buf);
            }
        }
    }


}

 

无名管道注意事项:

  1. 只能用于亲缘关系的进程间通信(父子进程,兄弟进程)
  1. 管道通信是单工的,一端读,一端写(程序实现设计好)。
  2. 数据自己读不能自己写
  3. 管道可以用于大于2个进程共享
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main()
{
    int pfd[2];
    int i;
    int re;
    char buf[40] = { 0 };
    pid_t pid;
    re = pipe(pfd);
    if (re < 0)
    {
        perror("pipe");
        return 0;
    }
    printf("%d,%d\n", pfd[0], pfd[1]);
    //创建两个子进程
    for (i = 0;i < 2;i++)
    {
        pid = fork();
        if (pid < 0)
        {
            perror("fork");
            return 0;
        }
        else if (pid > 0)
        {

        }
        else//子进程不会在创建子进程
        {
            break;

        }
    }
    if (i == 2)//父进程
    {
        close(pfd[1]);
        while (1)
        {
            memset(buf, 0, 40);
            re = read(pfd[0], buf, 40);//读
            if (re > 0)
            {
                printf("%s\n", buf);
            }
        }
        return 0;
    }

    if (i == 1)//子进程
    {
        close(pfd[0]);
        while (1)
        {
            strcpy(buf, "this is 2 process");
            write(pfd[1], buf, strlen(buf));//写
            usleep(930000);
        }
        return 0;
    }
    if (i == 0)//子进程
    {
        close(pfd[0]);
        while (1)
        {
            strcpy(buf, "this is 1 process");
            write(pfd[1], buf, strlen(buf));//写
            sleep(1);
        }

        return 0;
    }


}

无名管道的读写特性:

  • 读管道:

1. 管道中有数据,read返回实际读到的字节数。

2. 管道中无数据:

(1) 管道写端被全部关闭,read返回0 (好像读到文件结尾)

  (2) 写端没有全部被关闭,read阻塞等待(不久的将来可能有数据递达,此时会让出cpu)

  • 写管道:

1. 管道读端全部被关闭, 进程异常终止(也可使用捕捉SIGPIPE信号,使进程不终止)

2. 管道读端没有全部关闭:

(1) 管道已满,write阻塞。(管道大小64K)

  1. 管道未满,write将数据写入,并返回实际写入的字节数。
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    int pfd[2];
    int re;
    char buf[20] = { 0 };
    pid_t pid;
    re = pipe(pfd);
    if (re < 0)
    {
        perror("pipe");
        return 0;
    }
    printf("%d,%d\n", pfd[0], pfd[1]);
    pid = fork();
    if (pid < 0)
    {
        perror("fork");
        return 0;
    }
    else if (pid > 0)
    {
        close(pfd[0]);
        //        close(pfd[1]);
        int j = 0;
        while (1)
        {
            j++;
            strcpy(buf, "hhahahahah");
            for (int i = 0;i < 1000;i++)
            {
                write(pfd[1], buf, strlen(buf));
            }
            printf("write %d times\n", j);
            sleep(1);
        }

    }
    else
    {
        close(pfd[1]);
        // close(pfd[0]);
        sleep(30000);
        exit(0);
        while (1)
        {
            re = read(pfd[0], buf, 20);
            if (re > 0)
            {
                printf("read pipe=%s\n", buf);
            }
            else if (re == 0)
            {
                printf("re=0\n");
            }
        }
    }


}

 

有名管道(命名管道)

有名管道特点

有名管道可以使非亲缘的两个进程互相通信

通过路径名来操作,在文件系统中可见,但内容存放在内存中  

文件IO来操作有名管道 遵循先进先出规则  

不支持leek操作 单工读写

创建管道
#include <sys/types.h>

#include <sys/stat.h>

int mkfifo(const char *filename, mode_t mode);

 成功时返回0,失败时返回EOF  path  创建的管道文件路径  mode 管道文件的权限,如0666

open(const char *path, O_RDONLY);//1

open(const char *path, O_RDONLY | O_NONBLOCK);//2

open(const char *path, O_WRONLY);//3

open(const char *path, O_WRONLY | O_NONBLOCK);//4

特点:

1有名管道可以使非亲缘的两个进程互相通信

2通过路径名来操作,在文件系统中可见,但内容存放在内存中

3 文件IO来操作有名管道

4 遵循先进先出规则

5 不支持leek操作

6 单工读写

fifor.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>


int main()
{
    int re;
    int fd;
    char buf[32];
    /*
        re = mkfifo("/myfifo",0666);//不能在win和linux共享文件下。
        if(re<0){
            perror("mkfifo");
            return 0;
        }
        */
    fd = open("/myfifo", O_RDONLY);
    if (fd < 0)
    {
        perror("open");
        return 0;
    }
    printf("after open\n");
    while (1)
    {

        re = read(fd, buf, 32);
        if (re > 0)
        {
            printf("read fifo=%s\n", buf);
        }
        else if (re == 0)
        {
            exit(0);
        }

    }
}
fifow.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>


int main()
{
    int re;
    int fd;
    char buf[32];
    re = mkfifo("/myfifo", 0666);//不能在win和linux共享文件下。
    if (re < 0)
    {
        perror("mkfifo");
        //return 0;
    }
    fd = open("/myfifo", O_WRONLY);
    if (fd < 0)
    {
        perror("open");
        return 0;
    }
    printf("after open\n");
    while (1)
    {
        fgets(buf, 32, stdin);//通过键盘输入写到缓冲区
        write(fd, buf, strlen(buf));//写到文件中

    }
}

 

注意事项:

1 就是程序不能以O_RDWR(读写)模式打开FIFO文件进行读写操作,而其行为也未明确定义,因为如一个管道以读/写方式打开,进程可以读回自己的输出,同时我们通常使用FIFO只是为了单向的数据传递

2 第二个参数中的选项O_NONBLOCK,选项O_NONBLOCK表示非阻塞,加上这个选项后,表示open调用是非阻塞的,如果没有这个选项,则表示open调用是阻塞的

3  对于以只读方式(O_RDONLY)打开的FIFO文件,如果open调用是阻塞的(即第二个参数为O_RDONLY),除非有一个进程以写方式打开同一个FIFO,否则它不会返回;如果open调用是非阻塞的的(即第二个参数为O_RDONLY | O_NONBLOCK),则即使没有其他进程以写方式打开同一个FIFO文件,open调用将成功并立即返回。

对于以只写方式(O_WRONLY)打开的FIFO文件,如果open调用是阻塞的(即第二个参数为O_WRONLY),open调用将被阻塞,直到有一个进程以只读方式打开同一个FIFO文件为止;如果open调用是非阻塞的(即第二个参数为O_WRONLY | O_NONBLOCK),open总会立即返回,但如果没有其他进程以只读方式打开同一个FIFO文件,open调用将返回-1,并且FIFO也不会被打开。

4.数据完整性,如果有多个进程写同一个管道,使用O_WRONLY方式打开管道,如果写入的数据长度小于等于PIPE_BUF(4K),那么或者写入全部字节,或者一个字节都不写入,系统就可以确保数据决不会交错在一起。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值