国庆假期day4

作业: 完成父子进程的通信, 父进程发送一句话后,子进程接收打印 然后子进程发送一句话,父进程接收后打印

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <pthread.h>

#define BUFFER_SIZE 100

typedef struct {
    int pipefd[2];
    char buffer[BUFFER_SIZE];
    int turn;
    pthread_cond_t cond;
    pthread_mutex_t mutex;
} SharedData;

void* childProcess(void* arg) {
    SharedData* sharedData = (SharedData*)arg;
    close(sharedData->pipefd[1]);  // 关闭写入端

    while (1) {
        pthread_mutex_lock(&sharedData->mutex);

        if (sharedData->turn == 0) {
            // 从父进程读取数据
            read(sharedData->pipefd[0], sharedData->buffer, BUFFER_SIZE);
            printf("子进程接收到的消息:%s\n", sharedData->buffer);

            sharedData->turn = 1;

            // 向父进程发送数据
            printf("请输入子进程发送的消息:");
            fgets(sharedData->buffer, BUFFER_SIZE, stdin);
            sharedData->buffer[strlen(sharedData->buffer) - 1] = '\0';  // 去除末尾的换行符

            write(sharedData->pipefd[1], sharedData->buffer, strlen(sharedData->buffer) + 1);

            pthread_cond_signal(&sharedData->cond);
        }

        pthread_mutex_unlock(&sharedData->mutex);
    }

    close(sharedData->pipefd[0]);  // 关闭读取端
    return NULL;
}

int main() {
    SharedData sharedData;
    pid_t pid;
    pthread_t tid;

    // 创建管道
    if (pipe(sharedData.pipefd) == -1) {
        perror("pipe");
        exit(EXIT_FAILURE);
    }

    // 初始化共享数据
    sharedData.turn = 0;
    pthread_cond_init(&sharedData.cond, NULL);
    pthread_mutex_init(&sharedData.mutex, NULL);

    // 创建子进程和线程
    pid = fork();

    if (pid == -1) {  // 错误处理
        perror("fork");
        exit(EXIT_FAILURE);
    } 
    else if (pid == 0) {  // 子进程
        childProcess(&sharedData);
        exit(EXIT_SUCCESS);
    } 
    else {  // 父进程
        close(sharedData.pipefd[0]);  // 关闭读取端

        while (1) {
            pthread_mutex_lock(&sharedData.mutex);

            if (sharedData.turn == 1) {
                // 从终端输入数据,并向子进程发送
                printf("请输入父进程发送的消息:");
                fgets(sharedData.buffer, BUFFER_SIZE, stdin);
                sharedData.buffer[strlen(sharedData.buffer) - 1] = '\0';  // 去除末尾的换行符

                write(sharedData.pipefd[1], sharedData.buffer, strlen(sharedData.buffer) + 1);

                // 从子进程读取数据
                pthread_cond_wait(&sharedData.cond, &sharedData.mutex);

                read(sharedData.pipefd[0], sharedData.buffer, BUFFER_SIZE);
                printf("父进程接收到的消息:%s\n", sharedData.buffer);

                sharedData.turn = 0;
            }

            pthread_mutex_unlock(&sharedData.mutex);
        }

        close(sharedData.pipefd[1]);  // 关闭写入端
        wait(NULL);
        exit(EXIT_SUCCESS);
    }

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值