LinuxC 进程间通信 --- 管道

无名管道

1、内核会开辟一个“管道”,通信的进程通过共享这个管道,从而实现通信。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int fd[2];

void read_msg(int sig)
{
    char buffer[1024] = {0};
    read(fd[0], buffer, sizeof(buffer));
    printf("buffer = %s\n", buffer);
}

int main(int argc, char const *argv[])
{
    // 子进程从键盘上获取数据 往 父进程上发送
    pid_t pid;

    if (pipe(fd) < 0) // 放在创建进程之前
    {
        perror("PIPE ERROR");
        exit(1);
    }

    pid = fork();

    if (pid < 0)
    {
        perror("fork error!");
        exit(1);
    }

    if (pid == 0) // 子进程
    {
        char buffer[1024];
        close(fd[0]); // 把读关掉,
        while (1)
        {
            memset(buffer, 0, sizeof(buffer));
            scanf("%s", buffer);
            if (write(fd[1], buffer, strlen(buffer)) < 0)
            {
                perror("write data error:");
            }
        }
    }

    else if (pid > 0)
    {
        char buffer[1024];
        close(fd[1]);
        while (1)
        {
            memset(buffer, 0, sizeof(buffer));
            read(fd[0], buffer, sizeof(buffer));
            printf("父进程 buffer = %s\n", buffer);
        }
    }

    return 0;
}

运行结果如下:

superlan@GodFather:~/C_Language/interprocess_communication$ ./a.out 
SuperXiaobai        
父进程 buffer = SuperXiaobai
^C
superlan@GodFather:~/C_Language/interprocess_communication$ 

无名管道的特点:

1、管道只允许具有血缘关系的进程间通信,如父子进程间的通信

2、管道只允许单向通信

3、读管道时,如果没有数据的话,读操作会休眠(阻塞),写数据时,缓冲区写满会休眠(阻塞)

4、数据被读出,数据就会被管道删除;

使用时要注意SIGPIPE信号

1、写管道时,如果管道的读端被close了话,向管道“写”数据的进程会被内核发送一个SIGPIPE信号, 发这个信号的目的就是想通知你,管道所有的“读”都被关闭了。

2、由于这个信号的默认动作是终止,所以收到这个信号的进程会被终止,如果你不想被终止的 话,你可以忽略、捕获、或者屏蔽这个信号

3、只有当管道所有的读端都被关闭时,才会产生这个信号,只有还有一个读端开着,就不会产生

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>

void my_exit(int sig)
{
    printf("SIGCHLD!\n");
}

int main(int argc, char const *argv[])
{
    int fd[2];
    pid_t pid;

    if (pipe(fd) < 0)
    {
        perror("PIPE ERROR");
        exit(1);
    }

    signal(SIGCHLD, my_exit); // 子进程异常终止发出SIGCHLD信号!!!
    signal(SIGPIPE, SIG_IGN); // 忽略 SIGPIPE 可以让子进程不退出。

    pid = fork();

    if (pid < 0)
    {
        perror("fork error!");
        exit(1);
    }

    if (pid == 0)
    {
        char buffer[1024];
        close(fd[0]); // 把读关掉
        while (1)
        {
            memset(buffer, 0, sizeof(buffer));
            scanf("%s", buffer);
            if (write(fd[1], buffer, strlen(buffer)) < 0)
            {
                perror("write data error:");
            }
        }
    }

    else if (pid > 0)
    {
        char buffer[1024];
        close(fd[1]); // 把写关掉
        // close(fd[0]); 如果把管道的读管道关掉 会给子进程发送信号SIGPIPE信号,如果不处理子进程就退出

        while (1)
        {
            memset(buffer, 0, sizeof(buffer));
            read(fd[0], buffer, sizeof(buffer));
            printf("buffer = %s\n", buffer);
            sleep(1);
        }
    }

    return 0;
}

---------------------------------------------------------------------------------------------------------------------------------

有名管道 : FIFO

管道应用的一个重大限制是它没有名字,只适合具有亲缘性质的进程之间通信。

命名管道克服了这种限制,FIFO不同于管道之处在于它提供一个路径名与之关联,以FIFO的文件形式存在于文件系统中。这样,即使与FIFO的创建进程不存在亲缘关系的进程,只要可以访问该路径,就能够彼此通过FIFO相互通信(能够访问该路径的进程以及FIFO的创建进程之间),因此,通过FIFO不相关的进程也能交换数据。
 

有亲缘关系的进程:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>

#define FIFO_NAME "./file" // 管道文件

void my_exit(int sig)
{
    remove(FIFO_NAME); // 删除 FIFO_NAME 文件
}

int main(int argc, char const *argv[])
{
    pid_t pid;

    if (mkfifo(FIFO_NAME, 0655) < 0) // 创建有名管道
    {
        perror("mkfile error!");
        exit(1);
    }

    signal(SIGINT, my_exit); // 收到 Ctrl + C 信号去删除 fifo文件

    pid = fork();

    if (pid < 0)
    {
        perror("fork error!");
        exit(1);
    }

    if (pid == 0)
    {
        int fd = open(FIFO_NAME, O_WRONLY); // 以只写的方式打开

        char buffer[1024];

        while (1)
        {
            memset(buffer, 0, sizeof(buffer));
            scanf("%s", buffer);
            write(fd, buffer, strlen(buffer)); // 写给父进程
        }
    }

    else if (pid > 0)
    {
        int fd = open(FIFO_NAME, O_RDONLY);

        char buffer[1024] = {0};

        while (1)
        {
            memset(buffer, 0, sizeof(buffer)); // 以只读的方式打开

            read(fd, buffer, sizeof(buffer));

            printf("父进程 read data = %s\n", buffer);
        }
    }
    return 0;
}

运行结果如下:

superlan@GodFather:~/C_Language/interprocess_communication$ ./a.out 
super
父进程 read data = super
xiaobai
父进程 read data = xiaobai

没有亲缘关系的进程,代码如下:

写程序代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>

#define FIFO_NAME "./file"

int main(int argc, char const *argv[])
{

    int fd = open(FIFO_NAME, O_WRONLY);

    char buffer[1024] = {0};

    while (1)
    {
        memset(buffer, 0, sizeof(buffer));

        scanf("%s", buffer);

        write(fd, buffer, strlen(buffer));
    }

    return 0;
}

读的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>

#define FIFO_NAME "./file"

int main(int argc, char const *argv[])
{
    if (mkfifo(FIFO_NAME, 0655) < 0)
    {
        perror("mkfile error!");
        exit(1);
    }

    int fd = open(FIFO_NAME, O_RDONLY);

    char buffer[1024] = {0};

    while (1)
    {
        memset(buffer, 0, sizeof(buffer));

        read(fd, buffer, sizeof(buffer));

        printf("read data = %s\n", buffer);
    }

    return 0;
}

运行结果如下:

1、为了保证管道一定被创建,最好是两个进程都包含创建管道的代码,谁先运行就谁先创建,后运行的发现管道已经创建好了,那就直接open打开使用。

2、不能以O_RDWR模式打开命名管道FIFO文件,否则其行为是未定义的,管道是单向的,不能同时读写;

3、“有名管道”这种特殊文件,只能使用mkfifo函数来创建

4、使用一个“有名管道”是无法实现双向通信的,因为也涉及到抢数据的问题

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值