多线程互斥以及同步操作

两个线程,A线程读取文件数据, B线程将A线程读取到的数据打印终端

提示:可顺序执行A, B 线程即可, 可用全局变量,信号量,以及条件变量实现

使用信号量实现:

#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <semaphore.h>
#include <errno.h>

sem_t sem1;
sem_t sem2;
FILE *fd;
char read_buf[128];

#define PRINT_ERR(msg)  \
        do {                            \
                printf("%s:%s:%d\n", __FILE__, __func__, __LINE__); \
                perror(msg);    \
                exit(-1);               \
        } while (0)

void read_file(FILE *fd) {
    fgets(read_buf, sizeof(read_buf), fd);
}

void *func2(void *argv) {
    while (1) {
        sem_wait(&sem1);
        read_file(fd);
        if (strlen(read_buf) == 0) {
            sem_post(&sem2);
            break;
        }
        sem_post(&sem2);
    }
}

void *func1(void *argv) {
    while (1) {
        sem_wait(&sem2);
        if (strlen(read_buf) == 0) {
            sem_post(&sem1);
            break;
        }
        printf("%s", read_buf);
        memset(read_buf, 0, sizeof(read_buf));
        sem_post(&sem1);
    }
}


int main(int argc, const char *argv[]) {
    if (argc < 2) {
        printf("please input file name\n");
        return -1;
    }
    fd = fopen(argv[1], "r");
    sem_init(&sem1, 0, 1);
    sem_init(&sem2, 0, 0);
    pthread_t tid1;
    pthread_t tid2;
    if (errno = pthread_create(&tid1, NULL, func1, NULL)) {
        PRINT_ERR("error");
    }
    if (errno = pthread_create(&tid2, NULL, func2, NULL)) {
        PRINT_ERR("error");
    }
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    sem_destroy(&sem1);
    sem_destroy(&sem2);
    return 0;
}

执行结果

unbuntu@unbuntu:~$ gcc cat.c -lpthread
unbuntu@unbuntu:~$ ./a.out a.txt 
怒发冲冠⑵,凭阑处⑶、潇潇雨歇⑷。
抬望眼,仰天长啸⑸,壮怀激烈⑹。三十功名尘与土⑺,八千里路云和月⑻。
莫等闲⑼、白了少年头,空悲切⑽。
靖康耻⑾,犹未雪。臣子恨,何时灭。驾长车,踏破贺兰山缺⑿。壮志饥餐胡虏肉⒀,笑谈渴饮匈奴血⒁。待从头、收拾旧山河,朝天阙
unbuntu@unbuntu:~$ 

三个线程顺序打印出 ABC, 使用条件变量实现

#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <semaphore.h>
#include <errno.h>

#define PRINT_ERR(msg)  \
        do {                            \
                printf("%s:%s:%d\n", __FILE__, __func__, __LINE__); \
                perror(msg);    \
                exit(-1);               \
        } while (0)

pthread_cond_t cond;
pthread_mutex_t mutex;

int flag = 1;

void *func1(void *argv) {
    while (1) {
        pthread_mutex_lock(&mutex);
        while (flag != 1) {
            pthread_cond_wait(&cond, &mutex);
        }
        printf("A");
        flag = 2;
        pthread_cond_broadcast(&cond);
        pthread_mutex_unlock(&mutex);
    }
}

void *func2(void *argv) {
    while (1) {
        pthread_mutex_lock(&mutex);
        while (flag != 2) {
            pthread_cond_wait(&cond, &mutex);
        }
        printf("B");
        flag = 3;
        pthread_cond_broadcast(&cond);
        pthread_mutex_unlock(&mutex);
    }
}

void *func3(void *argv) {
    while (1) {
        pthread_mutex_lock(&mutex);
        while (flag != 3) {
            pthread_cond_wait(&cond, &mutex);
        }
        printf("C\n");
        flag = 1;
        pthread_cond_broadcast(&cond);
        pthread_mutex_unlock(&mutex);
    }
}

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

    pthread_cond_init(&cond, NULL);
    pthread_mutex_init(&mutex, NULL);

    pthread_t tid1;
    pthread_t tid2;
    pthread_t tid3;
    if (errno = pthread_create(&tid1, NULL, func1, NULL)) {
        PRINT_ERR("error");
    }
    if (errno = pthread_create(&tid2, NULL, func2, NULL)) {
        PRINT_ERR("error");
    }
    if (errno = pthread_create(&tid3, NULL, func3, NULL)) {
        PRINT_ERR("error");
    }
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);
    return 0;
}

执行结果

unbuntu@unbuntu:~$ gcc cond.c -lpthread
unbuntu@unbuntu:~$ ./a.out 
ABC
ABC
ABC
ABC
ABC
ABC
^C

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值