进程通信1

进程间的通信(IPC)方式

1,管道(有名管道和无名管道)
2,消息队列
3,信号量
4,共享内存
5,套接字


进程间通信的目的

1,数据传输:一个进程需要将它的数据发送给另一个进程,发送的数据量在一个字节到几兆字节之间。
2,共享数据:多个进程想要操作共享数据,一个进程对共享数据的修改,别的进程应该第一时间看到
3,通知事件:一个进程需要向另一个或一组进程发送消息,通知它(它们)发生了某种事件(如进程终止时要通知父进程)。
4,资源共享:多个进程之间共享同样的资源。为了作到这一点,需要内核提供锁和同步机制。
5,进程控制:有些进程希望完全控制另一个进程的执行(如Debug进程),此时控制进程希望能够拦截另一个进程的所有陷入和异常,并能够及时知道它的状态改变。


管道通信:无名管道 :一端写,一端读
管道是半双工的,数据只能向一个方向流动,需要双方通信时,建立两个管道。
管道的本质:单独的文件体系,这个体系只存在内核中。

必须在系统调用fork()前调用pipe()。否则子进程将不会继承文件描述符
(1)如果管道的写端不存在,则认为已经读到数据末尾,该函数返回的读出字符数为0
(2)如果管道写端不存在,如果请求的字节数目大于PIPE_BUF,则返回现有的数据字节数;如果不大于,则返回现有数据字节数,或请求字节数

创建管道文件:

mkfifo()
int mkfifoat(int dirfd, const char *pathname, mode_t mode);
mode:O_WRONLY
#include "stdio.h"
#include "sys/stat.h"
#include "sys/types.h"
#include "sys/wait.h"
#include "stdlib.h"
#include "unistd.h"
#include "fcntl.h"
#include "string.h"

int main()
{
    int fd = open("./test.txt", O_RDWR | O_CREAT, 0777);

    pid_t pid = fork();
    int info = mkfifo("./test.txt",0777);
    if (-1 == pid)
    {
        perror("pipe");
        exit(1);
    }

    if (0 == pid)
    {
        while(1)
        {
            char buff[21]={0};
            printf("input:\n");
            scanf("%s", buff);
            write(fd, buff, strlen(buff));
            memset(buff, 0, sizeof(buff));
            sleep(3);
            printf("child\n");
            exit(1);
        }
    }
    else
    {
        char buffer[21] = {0};
        read(fd, buffer,21);
        printf("%s\n", buffer);
        printf("father\n");
    }
    return 0;
}

//运行之后产生一个名为test.txt的管道文件(p开头的即为管道文件)
在这里插入图片描述

创建无名管道

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "sys/stat.h"
#include "sys/wait.h"
#include "sys/types.h"
#include "fcntl.h"
#include "unistd.h"

//无名管道
int main()
{
    int fd;

    int fifo = mkfifo("./test1.txt", 0666);
    if(-1 == fifo)
    {
        perror("fifo");
        exit(1);
    }

    pid_t pid = fork();
    if( -1 == pid )
    {
        perror("pid");
        exit(1);
    }

    if(0 == pid) 
    {
        while(1)
        {
            fd = open("./test1.txt", O_WRONLY);
            char buf[21] = {0};
            printf("input:");
            scanf("%s", buf);
            write(fd, buf, strlen(buf));
            memset(buf, 0, sizeof(buf));
            sleep(1);

            fd = open("./test1.txt", O_RDONLY);
            read(fd, buf, 30);
            printf("child says:%s\n", buf);
            memset(buf, 0, sizeof(buf));
        }
        exit(0);
    }
    else
    {
        while(1)
        {
            fd = open("./test1.txt", O_RDONLY);
            char buff[21] = {0};
            read(fd, buff, 30);
            printf("father says:%s\n", buff);
            memset(buff, 0, sizeof(buff));

            fd = open("./test1.txt", O_WRONLY);
            printf("input:");
            scanf("%s", buff);
            write(fd, buff, strlen(buff));
            memset(buff, 0, sizeof(buff));
            sleep(1);
        }
    }
    return 0;
}

在这里插入图片描述

//聊天进程,轮流输入输出


有名管道和无名管道的区别:
无名管道是无名的,有名管道是有名的;
无名管道只能用于父子进程或兄弟进程之间的通信,而有名管道可用于任意两进程之间通信;
无名管道是无形的,即无名管道的 inode 结构不是在磁盘上存储的,而是临时生成的,而有名管道的 inode 结点在磁盘


创建有名管道

read:

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

int main()
{
    int fd;
    char str[100] ={0};
    fd = open("fifo",O_RDONLY);
    read(fd,str,sizeof(str));
    printf("%s\n",str);
    close(fd);
    return 0;
}

write:

#include "sys/types.h"
#include "sys/stat.h"
#include "stdio.h"
#include "fcntl.h"
#include "unistd.h"
#include "stdlib.h"

int main()
{
    int fd;
    char str[100] = {0};
    int info = mkfifo("fifo",0666); 
    fd = open("fifo",O_WRONLY);   
    scanf("%s",str);
    write(fd,str,sizeof(str));
    close(fd);
    return 0;
}

这样单独运行write进程的时候,会形成阻塞,只有当另一终端运行read才会使阻塞消失

如果不想让FIFO阻塞,打开文件的时候设置为可读可写即可。

fd = open("fifo",O_RDWR);

另:
read:

#include "sys/types.h"
#include "sys/stat.h"
#include "stdio.h"
#include "fcntl.h"
#include "unistd.h"
#include "stdlib.h"
#include "errno.h"
#include "string.h"

#define FIFO "./test.txt"

int main()
{
    pid_t pid;
    int ret;
    char buffer[1024];
    if((ret = mkfifo(FIFO,0644))<0)
    {
	if(ret == -1 && errno == EEXIST)
	{
	}
    else
    {
	perror("make mkfifo error");
	exit(0);
    }
    }
    int fd =open(FIFO,O_RDONLY);
    while(1)
    {
	memset(buffer,0,sizeof(buffer));
	read(fd,buffer,sizeof(buffer));
	printf("recv:%s\n",buffer);
	sleep(2);
    }
    return 0;
}

write:

#include "sys/types.h"
#include "sys/stat.h"
#include "stdio.h"
#include "fcntl.h"
#include "unistd.h"
#include "stdlib.h"
#include "errno.h"
#include "string.h"
#include "sys/wait.h"

#define FIFO "./test.txt"

int main()
{
    pid_t pid;
    char buffer[1024];
    int ret;
    if((ret = mkfifo(FIFO, 0666))<0)
    {
	if(ret == -1 && errno == EEXIST)
	{
	}
	else
    {
	perror("make mkfifo error");
	exit(1);
    }
    }
    int fd = open(FIFO,O_WRONLY);
    while(1)
    {
	memset(buffer,0,sizeof(buffer));
	scanf("%s",buffer);
	write(fd,buffer,strlen(buffer));
    }
    return 0;
}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值