IO进程和线程作业2023 2.07

代码示例展示了如何通过创建FIFO管道实现两个进程(A和B)之间的对话。每个进程都包含一个读线程和一个写线程,允许它们同时进行读写操作。当接收到quit命令时,对话结束。多线程的使用确保了进程可以随时收发消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、要求实现AB进程对话

        a、A进程先发送一句话给B进程,B进程接收后打印

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

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

文件A代码。

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

#include <errno.h>

#include <string.h>

#include <strings.h>

int main(int argc, char const *argv[])

{

    int ff = mkfifo("./test.txt", 0666);

    if (ff < 0)

    {

        if (errno != 17)

        {

            perror("mkfifo");

        }

    }

    printf("fifo create success\n");

    int ff1 = mkfifo("./test1.txt", 0666);

    if (ff1 < 0)

    {

        if (errno != 17)

        {

            perror("mkfifo");

        }

    }

    printf("fifo1 create success\n");

    int op = open("./test.txt", O_RDWR);

    if (op < 0)

    {

        perror("open");

        return -1;

    }

    int op1= open("./test1.txt", O_RDONLY);

    if (op1<0)

    {

        perror("open");

        return -1;

    }

    char buf[36];

    while (1)

    {

        printf("A线程写入数据是:");

        fgets(buf, sizeof(buf), stdin);

        if (write(op, buf, sizeof(buf)) < 0)

        {

            perror("write");

            return -1;

        }

        buf[strlen(buf) - 1] = '\0';

        if (strcmp(buf, "quit") == 0)

        {

            break;

        }

        ssize_t res = read(op1, buf, sizeof(buf));

        if (res < 0)

        {

            perror("read");

            return -1;

        }

        if (res == 0)

        {

            printf("file read end or file is empty\n");

            return -1;

        }

        buf[strlen(buf) - 1] = '\0';

        if (strcmp(buf, "quit") == 0)

        {

            break;

        }

        printf("A线程读出数据是:");

        printf("%s\n", buf);

    }

    close(op);

    close(op1);

    return 0;

}

文件B代码。

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

#include <errno.h>

#include <string.h>

#include <strings.h>

int main(int argc, char const *argv[])

{

    int ff = mkfifo("./test.txt", 0666);

    if (ff < 0)

    {

        if (errno != 17)

        {

            perror("mkfifo");

        }

    }

    printf("fifo create success\n");

    int ff1 = mkfifo("./test1.txt", 0666);

    if (ff1 < 0)

    {

        if (errno != 17)

        {

            perror("mkfifo");

        }

    }

    printf("fifo1 create success\n");

    int op = open("./test.txt", O_RDONLY);

    if (op < 0)

    {

        perror("open");

        return -1;

    }

    int op1 = open("./test1.txt", O_RDWR);

    if (op1 < 0)

    {

        perror("open");

        return -1;

    }

    char buf[36];

    while (1)

    {

        ssize_t res = read(op, buf, sizeof(buf));

        if (res < 0)

        {

            perror("write");

            return -1;

        }

        if (res == 0)

        {

            printf("file read end or file is empty\n");

            return -1;

        }

        buf[strlen(buf) - 1] = '\0';

        if (strcmp(buf, "quit") == 0)

        {

            break;

        }

        printf("B线程读出数据是:");

        printf("%s\n", buf);

        bzero(buf, sizeof(buf));

        printf("B线程写入数据是:");

        fgets(buf, sizeof(buf), stdin);

        if (write(op1, buf, sizeof(buf)) < 0)

        {

            perror("write");

            return -1;

        }

        buf[strlen(buf) - 1] = '\0';

        if (strcmp(buf, "quit") == 0)

        {

            break;

        }

    }

    close(op);

    close(op1);

    return 0;

}

 

2、在上述练习基础上实现AB进程对话,要求AB进程能够随时收发。

        a、提示:多进程,多线程。

第一个文件代码。

 

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

#include <errno.h>

#include <string.h>

#include <strings.h>

#include<pthread.h>

void *hang(void *arg)

{

    int op = open("./test.txt", O_RDONLY);

    if (op < 0)

    {

        perror("open");

        return NULL;

    }

    char buf[36];

    while (1)

    {

        ssize_t res = read(op, buf, sizeof(buf));

        if (res < 0)

        {

            perror("read");

            return NULL;

        }

        if (res == 0)

        {

            printf("file read end or file is empty\n");

            return NULL;

        }

        buf[strlen(buf) - 1] = '\0';

        if (strcmp(buf, "quit") == 0)

        {

            break;

        }

        printf("B线程读出数据是:");

        printf("%s\n", buf);

    }

    close(op);

    pthread_cancel(*(pthread_t *)arg);

    pthread_exit(NULL);

}

void *yi(void *arg)

{

    int op = open("./test1.txt", O_WRONLY);

    if (op < 0)

    {

        perror("open");

        return NULL;

    }

    char buf[36];

    while (1)

    {

        bzero(buf, sizeof(buf));

        printf("B线程写入数据是:");

        fgets(buf, sizeof(buf), stdin);

        if (write(op, buf, sizeof(buf)) < 0)

        {

            perror("write");

            return NULL;

        }

        buf[strlen(buf) - 1] = '\0';

        if (strcmp(buf, "quit") == 0)

        {

            break;

        }

    }

    close(op);

    pthread_cancel(*(pthread_t *)arg);

    pthread_exit(NULL);

}

int main(int argc, char const *argv[])

{

    int ff = mkfifo("./test.txt", 0666);

    if (ff < 0)

    {

        if (errno != 17)

        {

            perror("mkfifo");

        }

    }

    printf("fifo create success\n");

    int ff1 = mkfifo("./test1.txt", 0666);

    if (ff1 < 0)

    {

        if (errno != 17)

        {

            perror("mkfifo");

        }

    }

    printf("fifo1 create success\n");

    pthread_t tid, tid1;

    if (pthread_create(&tid, NULL, hang, &tid1) != 0)

    {

        printf("tread create fail\n");

        return -1;

    }

    if (pthread_create(&tid1, NULL, yi, &tid) != 0)

    {

        printf("tread create fail\n");

        return -1;

    }

    pthread_join(tid, NULL);

    pthread_join(tid1, NULL);

    return 0;

}

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

#include <errno.h>

#include <string.h>

#include <strings.h>

#include<pthread.h>

void *hang(void *arg)

{

    int op = open("./test.txt", O_WRONLY);

    if (op < 0)

    {

        perror("open");

        return NULL;

    }

    char buf[36];

    while (1)

    {

        printf("A线程写入数据是:");

        fgets(buf, sizeof(buf), stdin);

        if (write(op, buf, sizeof(buf)) < 0)

        {

            perror("write");

            return NULL;

        }

        buf[strlen(buf) - 1] = '\0';

        if (strcmp(buf, "quit") == 0)

        {

            break;

        }

    }

    close(op);

    pthread_cancel(*(pthread_t *)arg);

    pthread_exit(NULL);

}

void *yi(void *arg)

{

    int op = open("./test1.txt", O_RDONLY);

    if (op < 0)

    {

        perror("open");

        return NULL;

    }

    char buf[36];

    while (1)

    {

        ssize_t res = read(op, buf, sizeof(buf));

        if (res < 0)

        {

            perror("read");

            return NULL;

        }

        if (res == 0)

        {

            printf("file read end or file is empty\n");

            return NULL;

        }

        buf[strlen(buf) - 1] = '\0';

        if (strcmp(buf, "quit") == 0)

        {

            break;

        }

        printf("A线程读出数据是:");

        printf("%s\n", buf);

    }

    close(op);

    pthread_cancel(*(pthread_t *)arg);

    pthread_exit(NULL);

}

int main(int argc, char const *argv[])

{

    int ff = mkfifo("./test.txt", 0666);

    if (ff < 0)

    {

        if (errno != 17)

        {

            perror("mkfifo");

        }

    }

    printf("fifo create success\n");

    int ff1 = mkfifo("./test1.txt", 0666);

    if (ff1 < 0)

    {

        if (errno != 17)

        {

            perror("mkfifo");

        }

    }

    printf("fifo1 create success\n");

    pthread_t tid, tid1;

    if (pthread_create(&tid, NULL, hang, &tid1) != 0)

    {

        printf("tread create fail\n");

        return -1;

    }

    if (pthread_create(&tid1, NULL, yi, &tid) != 0)

    {

        printf("tread create fail\n");

        return -1;

    }

    pthread_join(tid, NULL);

    pthread_join(tid1, NULL);

    return 0;

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值