2024.8.5

题目

1.用有名管道实现两个进程间的通信

create.c

#include <myhead.h>
int main()
{
    //创建一个有名管道文件
    if (mkfifo("./linux", 0664) == -1)
    {
        perror("mkfifo error");
        return -1;
    }
    getchar();
    system("rm linux");
    return 0;
}

create1.c

#include <myhead.h>
int main()
{
    //创建第二个有名管道文件
    if (mkfifo("./linux1", 0664) == -1)
    {
        perror("mkfifo error");
        return -1;
    }
    getchar();
    system("rm linux1");
    return 0;
}

send.c

#include <myhead.h>
int main()
{
    //创建两个进程
    pid_t pid = fork();
    //子进程
    if (pid == 0)
    {
        //以写的形式打开管道文件
        int wfd = open("./linux", O_WRONLY);
        if (wfd == -1)
        {
            perror("open error");
            return -1;
        }
        printf("管道文件已经打开\n");
        //发送数据
        char wbuf[128] = "";
        while (1)
        {
            printf("请输入>>>");
            fgets(wbuf, sizeof(wbuf), stdin);
            wbuf[strlen(wbuf) - 1] = 0;
            //将数据发送到管道中
            write(wfd, wbuf, strlen(wbuf));
            //判断数据
            if (strcmp(wbuf, "quit") == 0)
            {
                break;
            }
        }
        //关闭文件描述符
        close(wfd);
    }
    //父进程
    else if (pid > 0)
    {
        //以读的形式打开文件
        int afd = open("./linux1", O_RDONLY);
        if (afd == -1)
        {
            perror("open error");
            return -1;
        }
        printf("管道文件读端以打开\n");
        //定义接受容器
        char abuf[128] = "";
        while (1)
        {
            bzero(abuf, sizeof(abuf));
            read(afd, abuf, sizeof(abuf));
            if (strcmp(abuf, "quit") == 0)
            {
                break;
            }
            printf("\n");
            printf("收到的消息为:%s\n", abuf);
        }
        close(afd);
    }

    return 0;
}

recv.c

#include <myhead.h>
int main()
{
    //创建两个进程
    pid_t pid = fork();
    //父进程
    if (pid > 0)
    {
        //以读的形式打开文件
        int rfd = open("./linux", O_RDONLY);
        if (rfd == -1)
        {
            perror("open error");
            return -1;
        }
        printf("管道文件读端以打开\n");
        //定义接受容器
        char rbuf[128] = "";
        while (1)
        {
            bzero(rbuf, sizeof(rbuf));
            read(rfd, rbuf, sizeof(rbuf));
            if (strcmp(rbuf, "quit") == 0)
            {
                break;
            }
            printf("\n");
            printf("收到的消息为:%s\n", rbuf);
        }
        close(rfd);
    }
    //子进程
    else if (pid == 0)
    {
        //以写的形式打开管道文件
        int bfd = open("./linux1", O_WRONLY);
        if (bfd == -1)
        {
            perror("open error");
            return -1;
        }
        printf("管道文件已经打开\n");
        //发送数据
        char bbuf[128] = "";
        while (1)
        {
            printf("请输入>>>");
            fgets(bbuf, sizeof(bbuf), stdin);
            bbuf[strlen(bbuf) - 1] = 0;
            //将数据发送到管道中
            write(bfd, bbuf, strlen(bbuf));
            //判断数据
            if (strcmp(bbuf, "quit") == 0)
            {
                break;
            }
        }
        //关闭文件描述符
        close(bfd);
    }
    return 0;
}

2.使用有名管道实现,一个进程用于给另一个进程发消息,另一个进程收到消息后,展示到终端上,并且将消息保存到文件上一份

create.c

#include <myhead.h>
int main()
{
    //创建一个有名管道文件
    if (mkfifo("./linux", 0664) == -1)
    {
        perror("mkfifo error");
        return -1;
    }
    getchar();
    system("rm linux");
    return 0;
}

send.c

#include <myhead.h>
int main()
{
    //以写的形式打开管道文件
    int wfd = open("./linux", O_WRONLY);
    if (wfd == -1)
    {
        perror("open error");
        return -1;
    }
    printf("管道文件写端已打开\n");
    //发送数据
    char wbuf[128] = "";
    while (1)
    {
        printf("请输入>>>");
        fgets(wbuf, sizeof(wbuf), stdin);
        wbuf[strlen(wbuf)] = 0;
        //将数据发送到管道中
        write(wfd, wbuf, strlen(wbuf));
        //判断数据
        if (strcmp(wbuf, "quit") == 0)
        {
            break;
        }
    }
    //关闭文件描述符
    close(wfd);
    return 0;
}

rev.c

#include <myhead.h>
int main()
{
    //以只读的形式打开管道文件
    int rfd = open("./linux", O_RDONLY);
    if (rfd == -1)
    {
        perror("open error");
        return -1;
    }
    printf("管道文件读端已打开\n");

    //以只写的形式打开要将数据存入的文件
    int yfd = open("./test.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (yfd == -1)
    {
        perror("open 1 error");
        return -1;
    }
    //定义接受容器
    char rbuf[128] = "";
    ssize_t bytesRead;
    int count = 0;
    while (count != 5)
    {
        bytesRead = read(rfd, rbuf, sizeof(rbuf));
        rbuf[bytesRead] = '\0'; // 确保字符串正确终止
        if (strcmp(rbuf, "quit") == 0)
        {
            break;
        }

        write(yfd, rbuf, bytesRead);
        printf("收到的消息为%s\n", rbuf);
        count++;
    }
    printf("数据写入成功\n");
    close(rfd);
    close(yfd);
    return 0;
}

思维导图

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Kali Linux是一个专门为安全评估和渗透测试设计的操作系统,不断更新版本以提供最新的工具和服务。Kali 2024.1的安装过程类似于其他Linux发行版,但包含一些特定的安全软件包。 以下是基本的步骤: 1. **下载ISO镜像**:首先,访问Kali Linux官方网站(https://www.kali.org/downloads/) 下载适合你硬件架构的最新ISO镜像文件。 2. **刻录USB或创建虚拟机**:你可以使用如Rufus(Windows)、Etcher(跨平台)、UNetbootin等工具将ISO映像刻录到USB闪存驱动器,或者在虚拟化平台上(如VirtualBox、VMware等)创建一个新的虚拟机,并选择从ISO启动。 3. **引导安装**:重启电脑并进入BIOS设置,确保优先加载USB或CD/DVD。按照提示引导至安装界面。 4. **开始安装**:选择语言并接受许可协议,然后选择安装类型,通常推荐“高级”选项以便自定义安装。 5. **分区硬盘**:分配硬盘空间给根分区和交换分区,以及根据需要安装的LXDE、GNOME或其他桌面环境。 6. **配置网络**:选择网络连接的方式,如果无线可用,建议配置自动获取IP地址。 7. **用户名和密码设置**:创建管理员账户,设置登录密码。 8. **安装组件**:在安装过程中可以选择安装所需的安全工具包,包括但不限于Metasploit Framework、Nmap、Wireshark等。 9. **启动修复**:完成安装后,首次启动可能会有启动修复程序运行,这是正常现象。 10. **个性化设置**:安装完成后,你可以根据个人喜好调整防火墙、更新源等设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值