• 【进程、线程和进程间通信】(三)进程间通信


    一、进程间通信

    1.进程间通信方式

    • 无名管道(pipe)
    • 有名管道(fifo)
    • 信号(signal)
    • 共享内存(mmap)
    • 套接字(soket)

    2.无名管道

    在这里插入图片描述
    在这里插入图片描述

    (1)管道创建:pipe

    #include
    int pipe(int pipefd[2]);

    成功时返回0,失败时返回EOF
    pfd 包含两个元素的整形数组,用来保存文件描述符
    pfd[0]用于读管道;pfd[1]用于写管道

    (2)特点

    • 只能用于具有亲缘关系的进程之间的通信(父子进程、兄弟进程)
    • 单工的通信模式,具有固定的读端和写端(不能自己又读又写)
    • 无名管道创建时会返回两个文件描述符,分别用于读写管道
    • 管道可以用于大于2个进程共享

    (3)读写特性

    读管道

    1. 管道中有数据,read返回实际读到的字节数。
    2. 管道中无数据:
      (1) 管道写端被全部关闭,read返回0 (好像读到文件结尾)
      (2) 写端没有全部被关闭,read阻塞等待(不久的将来可能有数据递达,此时会让出cpu)

    写管道

    1. 管道读端全部被关闭, 进程异常终止(也可使用捕捉SIGPIPE信号,使进程不终止)
    2. 管道读端没有全部关闭:
      (1) 管道已满,write阻塞。(管道大小64K)
      (2) 管道未满,write将数据写入,并返回实际写入的字节数。

    (4)示例代码

    pipe.c

    子进程往无名管道写字符串,父进程读无名管道字符串并打印。

    #include 
    #include 
    #include 
    
    int main(int argc, const char *argv[])
    {
    	int pfd[2];
    	int ret;
    	pid_t pid;
    	char buf[100] = {0};
    
    	ret = pipe(pfd);
    	if (ret < 0) {
    		perror("pipe");
    		return -1;
    	}
    	printf("pfd[0] = %d  pfd[1] = %d\n", pfd[0], pfd[1]);
    
    	pid = fork();
    	if (pid < 0) {
    		perror("fork");
    		return -1;
    	} else if (0 == pid) { //子进程
    		close(pfd[0]);
    		while (1) {
    			strcpy(buf, "hello world");
    			write(pfd[1], buf, strlen(buf));
    			sleep(1);
    		}
    	} else { //父进程
    		close(pfd[1]);
    		while (1) {
    			memset(buf, 0, 100);
    			ret = read(pfd[0], buf, 100);
    			if (ret > 0) {
    				printf("read = %s\n", buf);
    			}
    		}
    	}
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    pipe2.c

    2个子进程往无名管道写字符串,父进程读无名管道字符串并打印。

    #include 
    #include 
    #include 
    
    int main(int argc, const char *argv[])
    {
    	int pfd[2];
    	int ret;
    	pid_t pid;
    	char buf[100] = {0};
    	int i = 0;
    
    	ret = pipe(pfd);
    	if (ret < 0) {
    		perror("pipe");
    		return -1;
    	}
    
    	for (i = 0; i < 2; i++) {
    		ret = fork();
    		if (ret < 0) {
    			perror("fork");
    			return -1;
    		} else if (0 == ret) { //子进程
    			break;
    		} else { //父进程
    
    		}
    	}
    
    	if (0 == i) {
    		close(pfd[0]);
    		while (1) {
    			strcpy(buf, "this is child process 1");
    			write(pfd[1], buf, strlen(buf));
    			sleep(1);
    		}
    	}
    	if (1 == i) {
    		close(pfd[0]);
    		while (1) {
    			strcpy(buf, "this is child process 2, memset test");
    			write(pfd[1], buf, strlen(buf));
    			sleep(2);
    		}
    	}
    	if (2 == i) {
    		close(pfd[1]);
    		while (1) {
    			memset(buf, 0, 100);
    			ret = read(pfd[0], buf, 100);
    			if (ret > 0) {
    				printf("read = %s\n", buf);
    			}
    		}
    	}
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    3.有名管道

    (1)管道创建:mkfifo

    #include
    #include
    int mkfifo(const char *path, 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

    (2)特点

    1. 有名管道可以使非亲缘的两个进程互相通信
    2. 通过路径名来操作,在文件系统中可见,但内容存放在内存中
    3. 文件IO来操作有名管道
    4. 遵循先进先出规则
    5. 不支持leek操作
    6. 单工读写

    (3)注意事项

    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),那么或者写入全部字节,或者一个字节都不写入,系统就可以确保数据决不会交错在一起。

    (4)示例代码

    fiiow.c
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, const char *argv[])
    {
    	int ret;
    	int fd;
    	char buf[100] = {0};
    
    	ret = mkfifo("/home/linux/linux_study/02fifo/myfifo", 0666); //0666(4:r 2:w 1:x)
    	if (ret < 0) {
    		perror("mkfifo");
    	}
    
    	fd = open("/home/linux/linux_study/02fifo/myfifo", O_WRONLY);
    	if (fd < 0) {
    		perror("open");
    		return -1;
    	}
    
    	while (1) {
    		fgets(buf, 100, stdin);
    		write(fd, buf, strlen(buf));
    	}
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    fifor.c
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, const char *argv[])
    {
    	int ret;
    	int fd;
    	char buf[100] = {0};
    /*
    	ret = mkfifo("/home/linux/linux_study/02fifo/myfifo", 0666); //0666(4:r 2:w 1:x)
    	if (ret < 0) {
    		perror("mkfifo");
    	}
    */
    	fd = open("/home/linux/linux_study/02fifo/myfifo", O_RDONLY);
    	if (fd < 0) {
    		perror("open");
    		return -1;
    	}
    
    	while (1) {
    		memset(buf, 0, 100);
    		ret = read(fd, buf, 100);
    		if (ret > 0) {
    			printf("read fifo = %s\n", buf);
    		} else if (0 == ret) {
    			close(fd);
    			exit(0);
    		}
    	}
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    分组后统计查询
    Redis之持久化操作
    ssm基于Java和MySql的产业信息管理系统的设计与实现毕业设计源码260839
    java.lang.Exception: No runnable methods
    vue动态修改浏览器title和icon图标
    聚观早报 | 苹果秋季发布会定档9月7日;​饿了么与抖音达成合作
    MyBatis一对多查询,MyBatis中resultMap的使用,MyBatis中collection注意事项,MyBatis的级联搜索
    torch.from_numpy()函数(pytorch版)
    【异常错误】detected dubious ownership in repository ****** is owned by: ‘
    python java开发的气象数据采集的系统
  • 原文地址:https://blog.csdn.net/weixin_50964793/article/details/127375911