【系统编程】 进程间通信方式

2021/11/17 7:14:14

本文主要是介绍【系统编程】 进程间通信方式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.管道pipe:

2.有名管道FIFO:
创建方法:1.直接mkfifo  FIFONAME 创建有名管道。

      

     2.在.c里写代码创建,头文件包括sys/stat.h  

                     

 

 

 

 实现无血缘间进程的通信:

  其实FIFO和文件打开关闭相似性大,与管道pipe其实关联性并不大。

  写端write:

#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>

int main(int argc,char *argv[]){
    int fd,i;
    char buf[4096];

    fd = open(argv[1],O_WRONLY);
    i=0;
    while(1){
        sprintf(buf,"hello itcast: %d\n",i++);

        write(fd,buf,strlen(buf));
        sleep(1);
    }
    close(fd);
    return 0;
}
View Code

  读端read:

#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>

int main(int argc,char *argv[]){
    int fd,len;
    char buf[4096];

    fd = open(argv[1],O_RDONLY);
    int i=0;
    while(1){
        len = read(fd,buf,sizeof(buf));
        write(STDOUT_FILENO,buf,len);
        sleep(1);
    }
    close(fd);
    return 0;
}
View Code

 



这篇关于【系统编程】 进程间通信方式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程