无名管道,有名管道,信号

文章描述了如何通过创建两根管道实现两个进程(A和B)之间的对话,其中进程A和B可以交替发送和接收消息,直到接收到quit为止。同时,文章还包含了对特定信号(SIGINT和SIGQUIT)的捕获和处理,当接收到这些信号时,程序会给出相应的响应。
摘要由CSDN通过智能技术生成

1.要求实现AB进程对话
a.A进程先发送一句话给B进程,B进程接收后打印

b.B进程再回复一句话给A进程,A进程接收后打印

c.重复1.2步骤,当收到quit后,要结束AB进程

d.提示:两根管道

//****************创建管道****************************//
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <sys/wait.h>
int main(int argc, const char *argv[])
{
    // 1创建有名管道
    umask(0);
    if (0 != mkfifo("./fifo", 0664))
    {
        if(17!=errno)
        {
        perror("creat");
        return -1;
        }
    }
    umask(0);
    if (0 != mkfifo("./fifo1", 0664))
    {
        if(17!=errno)
        {
        perror("creat");
        return -1;
        }
    }
    return 0;
}
//********************A进程********************************//
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <sys/wait.h>
#include <fcntl.h>
int main(int argc, const char* argv[])
{
    int fd= open("./fifo", O_RDONLY);
    char buf[128] = {0};
    if (fd== -1)
    {
        if(17!=errno)
        {
            perror("open");
        }
    }
    int fd1 = open("./fifo1", O_WRONLY);
        char buf1[128] = {0};
        if (fd1== -1)
        {
            if (17 != errno)
            {
                perror("open");
            }
        }
    while (1)
    {
         //在fifo管道读
        //memset(buf, 0, sizeof(buf));
        read(fd, buf, sizeof(buf));
        if (strcmp(buf, "quit") == 0)
            break;
        printf("B传过来数据为:%s\n", buf);


        //在fifo1管道中写:
        printf("输给B的数据为:");
        scanf("%s",buf1);
        write(fd1, buf1, sizeof(buf1));
        if (strcmp(buf1, "quit") == 0)
            break;
        //memset(buf1, 0, sizeof(buf1));
       
    }

   close(fd);
   close(fd1);
    return 0;
}
//****************************B进程*********************//
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <sys/wait.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
    int fd = open("./fifo", O_RDWR);
    char buf[128] = {0};
    if (fd == -1)
    {
        if (17 != errno)
        {
            perror("open");
        }
    }
    int fd1 = open("./fifo1", O_RDWR);
    char buf1[128] = {0};
    if (fd1 == -1)
    {
        if (17 != errno)
        {
            perror("open");
        }
    }
    while (1)
    {
        //在fifo管道中写:
        printf("输给A的数据为:");
        scanf("%s",buf);
        write(fd, buf, sizeof(buf));
        if (strcmp(buf, "quit") == 0)
            break;
        //memset(buf, 0, sizeof(buf));

        //在fifo1管道读
        //memset(buf1, 0, sizeof(buf1));
        read(fd1, buf1, sizeof(buf1));
        if (strcmp(buf1, "quit") == 0)
            break;
        printf("A传过来数据为:%s\n", buf1);
    }

    close(fd);
    close(fd1);
    return 0;
}

输出结果为:

 捕获2号信号:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <signal.h>
#include<errno.h>
void signal_handle(int signo)
{
    if(signo==SIGINT)
    {
        printf("收到了ctrl+c的信号\n");
    }
}

int main(int argc,const char *argv[])
{
    //捕捉SIGIN信号
    if(SIG_ERR==signal(SIGINT,signal_handle))
    {
        perror("signal");
        return -1;
    }
    while(1);
    return 0;
}

输出结果:

捕获 3 30号信号:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <signal.h>
#include<errno.h>
void signal_handle(int signo)
{
    if(signo==SIGQUIT)
    {
        printf("收到了sigquit信号\n");
    }
}

int main(int argc,const char *argv[])
{
    //捕捉SIGQUIT信号
    if(SIG_ERR==signal(SIGQUIT,signal_handle))
    {
        perror("signal");
        return -1;
    }
    while(1);
    return 0;
}

输出结果为:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值