1. 条件变量顺序打印ABC的那道题2. 父子进程对话那题

本文详细介绍了C语言中父子进程的通信方法(使用管道)以及如何通过互斥锁和条件变量实现三线程按顺序打印ABC。
摘要由CSDN通过智能技术生成

1.父子进程对话

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
#include <pthread.h>
int main(void)
{
    int pfd1[2];
    int pfd2[2];
    pipe(pfd1);
    pipe(pfd2);
    pid_t pid=fork();
    if(pid<0)
    {
        perror("fork");
        return -1;
    }
    if(pid==0)
    {
        char buf[255]={0};
        while (1)
        {
            memset(buf,0,sizeof(buf));
            read(pfd1[0],buf,sizeof(buf));
            if(strcmp(buf,"quit")==0)
                break;
            printf("father:%s\n",buf);
            memset(buf,0,sizeof(buf));
            printf("子进程讲话:");
            fscanf(stdin,"%s",buf);
            write(pfd2[1],buf,strlen(buf));
            if(strcmp(buf,"quit")==0)
                break;
        }
        
    }
    else
    {
        char buf[255]={0};
        while (1)
        {
            memset(buf,0,sizeof(buf));
            printf("你是父亲,你先说话:");
            fscanf(stdin,"%s",buf);
            write(pfd1[1],buf,strlen(buf));
            if(strcmp(buf,"quit")==0)
                break;
            memset(buf,0,sizeof(buf));
            read(pfd2[0],buf,sizeof(buf));
            printf("son:%s\n",buf);
            if(strcmp(buf,"quit")==0)
                break;
            
        }
        wait(NULL);
        
    }
    close(pfd1[0]);
    close(pfd2[0]);
    close(pfd1[1]);
    close(pfd2[1]);
    return 0;
}

运行事例:

 

2.三线程分别打印ABC,打印顺序必须是ABC

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
#include <pthread.h>
pthread_mutex_t mutex;
pthread_cond_t cond1;
pthread_cond_t cond2;
pthread_cond_t cond3;
int flag = 1;
void *A(void *arg)
{
    while (1)
    {
        pthread_mutex_lock(&mutex);
        if (flag != 1)
        {
            pthread_cond_wait(&cond3, &mutex);
        }
        printf("A");
        flag = 2;
        pthread_cond_signal(&cond1);
        pthread_mutex_unlock(&mutex);
    }
}
void *B(void *arg)
{
    while (1)
    {
        pthread_mutex_lock(&mutex);
        if (flag != 2)
        {
            pthread_cond_wait(&cond1, &mutex);
        }
        printf("B");
        flag = 3;
        pthread_cond_signal(&cond2);
        pthread_mutex_unlock(&mutex);
    }
}
void *C(void *arg)
{
    while (1)
    {
        pthread_mutex_lock(&mutex);
        if (flag != 3)
        {
            pthread_cond_wait(&cond2, &mutex);
        }
        printf("C\n");
        flag = 1;
        pthread_cond_signal(&cond3);
        pthread_mutex_unlock(&mutex);
    }
}
int main()
{
    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond1, NULL);
    pthread_cond_init(&cond2, NULL);
    pthread_cond_init(&cond3, NULL);
    pthread_t tid1, tid2, tid3;
    pthread_create(&tid1, NULL, A, NULL);
    pthread_create(&tid2, NULL, B, NULL);
    pthread_create(&tid3, NULL, C, NULL);
    pthread_join(tid1, NULL);
    return 0;
}

运行测试:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值