1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 【进程 线程和进程间通信】(三)进程间通信

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

时间:2020-04-11 21:05:19

相关推荐

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

进程间通信

一、进程间通信1.进程间通信方式2.无名管道(1)管道创建:pipe(2)特点(3)读写特性(4)示例代码pipe.cpipe2.c 3.有名管道(1)管道创建:mkfifo(2)特点(3)注意事项(4)示例代码fiiow.cfifor.c

一、进程间通信

1.进程间通信方式

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

2.无名管道

(1)管道创建:pipe

#include <unistd.h>

int pipe(int pipefd[2]);

成功时返回0,失败时返回EOF

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

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

(2)特点

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

(3)读写特性

读管道

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

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

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

写管道

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

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

(2) 管道未满,write将数据写入,并返回实际写入的字节数。

(4)示例代码

pipe.c

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

#include <stdio.h>#include <unistd.h>#include <string.h>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;}

pipe2.c

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

#include <stdio.h>#include <unistd.h>#include <string.h>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;}

3.有名管道

(1)管道创建:mkfifo

#include <unistd.h>

#include <fcntl.h>

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)特点

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

(3)注意事项

就是程序不能以O_RDWR(读写)模式打开FIFO文件进行读写操作,而其行为也未明确定义,因为如一个管道以读/写方式打开,进程可以读回自己的输出,同时我们通常使用FIFO只是为了单向的数据传递。第二个参数中的选项O_NONBLOCK,选项O_NONBLOCK表示非阻塞,加上这个选项后,表示open调用是非阻塞的,如果没有这个选项,则表示open调用是阻塞的。对于以只读方式(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也不会被打开。数据完整性,如果有多个进程写同一个管道,使用O_WRONLY方式打开管道,如果写入的数据长度小于等于PIPE_BUF(4K),那么或者写入全部字节,或者一个字节都不写入,系统就可以确保数据决不会交错在一起。

(4)示例代码

fiiow.c

#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <string.h>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;}

fifor.c

#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <string.h>#include <stdlib.h>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;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。