Linux c 开发 - 进程间通信

本文详细介绍了进程间通信的多种方法,重点阐述了管道和命名管道的使用方式,包括创建、读写操作及其实现原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、管道

二、命名管道


进程间通信主要有以下几种方法:

  • 共享内存
  • 信号量
  • 管道
  • 命名管道
  • 消息队列
  • 套接口
  • 全双工管道

其中共享内存、信号量、管道、命名管道和消息队列可以实现本地进程间通信,而套接口和全双工管道属于网络编程部分。这里只介绍管道、命名管道的使用。

一、管道


管道pipe通信。管道通信有一个显著的特点就是当一个管道建立后,将获得两个文件描述符,分别用于对管道的读取和写入。

管道pipe只能用于两个进程间的通信,而且两个进程需要有同源性,必须由同一个进程所派生出来的。

创建pipe:

#include<unistd.h>
int pipe(int pipe[2]);         //其中pipe[0]是读取数据的描述字,pipe[1]是写数据的描述字

对管道的读写可以通过read和write方法进行读写操作,类似于文件操作。

看一个详细的例子:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
void check_exit(int status);
int main() {
        printf("Test\n");
        pid_t pid; //定义一个进程ID
        char buf[1024]; //容器
        int fd[2]; //其中fd[0]是读取数据的描述字,fd[1]是写数据的描述字
        if (pipe(fd) < 0) {
                printf("pipe erroe!");
                exit(1);
        }
        pid = fork(); //创建一个进程
        if (pid < 0) {
                printf("fork error!\n");
                exit(1);
        }
        if (pid == 0) {
                printf("child process! PID:%d\n", getpid()); //如果pid等于0,子进程,getpid()方法获取当前的进程ID
                close(fd[0]);
                char* x = "pipe hello!\n";
                write(fd[1], x, 12);//写入一个字符串    
        } else {
                printf("parent process! PID:%d\n", getpid()); //父进程
                close(fd[1]);
                int c;
                int rc;
                while( (rc = read(fd[0], &c, 1)) > 0 ){ //等待读取pipe管道中的数据  
                        putchar(c);
                }
        }
        return 0;
}

输出:

Test
parent process! PID:3303
child process! PID:3304
pipe hello!

二、命名管道


命名管道又称先入先出队列,是特殊的管道,存在于文件系统中

命名管道和管道的最大区别就是命名管道可以用于任何两个进程间的通信,并不限制于这两个进程的同源。

创建命名管道的函数:

    #include <sys/types.h>  
    #include <sys/stat.h>  
    int mkfifo(const char *filename, mode_t mode);  
    int mknod(const char *filename, mode_t mode | S_IFIFO, (dev_t)0);  

mkfifo函数更加简单和规范

  • filename:是存放管道的名称
  • mode:用于指定创建文件的权限。
  • 如果调用成功,返回0;否则返回-1

打开FIFO文件通常有四种方式:

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 

看一个例子:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
        printf("Test\n");
        pid_t pid; //定义一个进程ID
        pid = fork(); //创建一个进程
        if (pid < 0) {
                printf("fork error!\n");
                exit(1);
        }
        if (pid == 0) {
                printf("child process! PID:%d\n", getpid()); //如果pid等于0,子进程,getpid()方法获取当前的进程ID
                int res = mkfifo("hello", 0777); //创建一个FIFO管道
                if (res != 0) {
                        printf("mkfifo error!");
                        exit(1);
                }
                int fd = -1;
                fd = open("hello", O_WRONLY); //打开文件,可写模式
                if (fd < 0) {
                        printf("openfile error!");
                        exit(1);
                }
                write(fd, "fifo\n", 5); //写入文件 
        } else {
                sleep(5); //等待5秒
                printf("parent process! PID:%d\n", getpid()); //父进程
                //读取fifo管道数据
                char x[1024];
                int len;
                int fd = open("hello", O_RDONLY); //读取fifo管道
                if (fd < 0) {
                        printf("open faile error!");
                        exit(1);
                }
                while(len = read(fd, x, 5) > 0) {
                        printf("%s", x);
                }
        }
        return 0;
}

输出:

Test
child process! PID:2739
parent process! PID:2738
fifo

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值