6.进程通信 无名管道 有名管道

进程间通信(IPC,InterProcess Communication

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

常用通信方式

无名管道(pipe)

有名管道 (fifo)

信号(signal)

共享内存(mmap)

套接字(socket)

过时的IPC通信方式

System V IPC

共享内存(share memory)

消息队列(message queue)

信号灯集(semaphore set)

无名管道

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

pfd[0] 为读描述符

pfd[1] 为写描述符

无名管道注意事项:

  1. 只能用于亲缘关系的进程间通信(父子进程,兄弟进程)
  1. 管道通信是单工的,一端读,一端写(程序实现设计好)。
  2. 数据自己读不能自己写
  3. 管道可以用于大于2个进程共享

无名管道的读写特性:

  • 读管道:

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

2. 管道中无数据:

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

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

  • 写管道:

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

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

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

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

有名管道(命名管道)

创建管道

#include <sys/types.h>

#include <sys/stat.h>

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

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 单工读写

注意事项:

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

2 第二个参数中的选项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也不会被打开。

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

 pipe1

#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);
            }    
        }
    }


}

pipe2

#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;
    }

pipe3

#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");
            }
        }
    }


}

fifow

#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);
    if(re<0){
        perror("mkfifo");
        //return 0;
    }
    fd = open("/myfifo",O_WRONLY|O_NONBLOCK);
    if(fd<0){
        perror("open");
        return 0;
    }
    printf("after open\n");
    while(1){
        fgets(buf,32,stdin);
        write(fd,buf,strlen(buf));

    }
}

fifor

#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);
    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);
        }

    }
}

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会编程喵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值