嵌入式学习day30-进程间通信

day29练习:

编写3个线程任务,线程1循环打印"A",线程2循环打印"B",线程3循环打印"C",要求打印出的字符顺序总为ABC

#include "../head.h"

sem_t sem_a;
sem_t sem_b;
sem_t sem_c;

void *thread1(void *arg)
{
    while (1)
    {
        sem_wait(&sem_a);
        printf("A");
        sem_post(&sem_b);
    }

    return NULL;
}

void *thread2(void *arg)
{
    while (1)
    {
        sem_wait(&sem_b);
        printf("B");
        sem_post(&sem_c);
    }

    return NULL;
}

void *thread3(void *arg)
{
    while (1)
    {
        sem_wait(&sem_c);
        printf("C");
        sem_post(&sem_a);
    }

    return NULL;
}

int main(void)
{
    pthread_t tid1;
    pthread_t tid2;
    pthread_t tid3;

    sem_init(&sem_a, 0, 1);
    sem_init(&sem_b, 0, 0);
    sem_init(&sem_c, 0, 0);
    pthread_create(&tid1, NULL, thread1, NULL);
    pthread_create(&tid2, NULL, thread2, NULL);
    pthread_create(&tid3, NULL, thread3, NULL);

    pthread_join(tid1, NULL);
    pthread_join
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值