IO-day(7)-(互斥锁、信号量、条件变量)

作业一、用信号量的方法写逆置

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <fcntl.h>

// 创建一个用来存打开文件的结构体
struct file
{
    int fd_r;
};

// 0打印,1读取
int flag = 1;

// 创建互斥锁
pthread_mutex_t lock;
// 创建条件变量
pthread_cond_t cond;

// 定义一个字符变量来接读出的字符
char c = 0;
// 获取文件的大小

ssize_t res = 1;

void *my_printf(void *arg)
{
    while (1)
    {
        // 上锁
        pthread_mutex_lock(&lock);
        if (0 != flag)
        {
            pthread_cond_wait(&cond, &lock);
        }
        // sleep(1);
        if (0 == res)
            break;
        printf("%c", c);

        flag = 1;

        // 通过指定条件唤醒指定线程
        pthread_cond_signal(&cond);

        // 解锁
        pthread_mutex_unlock(&lock);
    }
    printf("111222\n");
    pthread_exit(NULL);
}

void *my_read(void *arg)
{
    int fd_r = ((struct file *)arg)->fd_r;

    while (1)
    {
        pthread_mutex_lock(&lock);

        if (1 != flag)
        {
            pthread_cond_wait(&cond, &lock);
        }
        res = read(fd_r, &c, sizeof(c));

        if (0 == res)
        {
            pthread_cond_signal(&cond);
            pthread_mutex_unlock(&lock);
            break;
        }

        flag = 0;

    }
    printf("111\n");
    pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
    // 初始化互斥锁
    pthread_mutex_init(&lock, NULL);

    // 初始化条件变量
    pthread_cond_init(&cond, NULL);

    // 打开文件
    int fd_r = open("./1.txt", O_RDWR);
    if (fd_r < 0)
    {
        perror("open");
        printf("__%d__\n", __LINE__);
        return -1;
    }

    // 定义一个结构体变量去接打开的文件
    struct file info;
    info.fd_r = fd_r;

    // 创建线程
    pthread_t tid1, tid2;
    if (pthread_create(&tid1, NULL, my_printf, NULL) != 0)
    {
        printf("pthread_creatr failed__%d__\n", __LINE__);
        return -1;
    }
    if (pthread_create(&tid2, NULL, my_read, &info) != 0)
    {
        printf("pthread_creatr failed__%d__\n", __LINE__);
        return -1;
    }

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    pthread_mutex_destroy(&lock);
    pthread_cond_destroy(&cond);

    close(fd_r);

    return 0;
}

 作业二、

1.将一个文件中的数据打印到终端,类似cat一个文件,要求如下:
a.一个线程读取文件中的数据
b.另外一个线程打印文件中的数据

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <fcntl.h>

// 创建一个用来存打开文件的结构体
struct file
{
    int fd_r;
};

// 0打印,1读取
int flag = 1;

// 创建互斥锁
pthread_mutex_t lock;
// 创建条件变量
pthread_cond_t cond;

// 定义一个字符变量来接读出的字符
char c = 0;
// 获取文件的大小

ssize_t res = 1;

void *my_printf(void *arg)
{
    while (1)
    {
        // 上锁
        pthread_mutex_lock(&lock);
        if (0 != flag)
        {
            pthread_cond_wait(&cond, &lock);
        }
        // sleep(1);
        if (0 == res)
            break;
        printf("%c", c);

        flag = 1;

        // 通过指定条件唤醒指定线程
        pthread_cond_signal(&cond);

        // 解锁
        pthread_mutex_unlock(&lock);
    }
    printf("111222\n");
    pthread_exit(NULL);
}

void *my_read(void *arg)
{
    int fd_r = ((struct file *)arg)->fd_r;

    while (1)
    {
        pthread_mutex_lock(&lock);

        if (1 != flag)
        {
            pthread_cond_wait(&cond, &lock);
        }
        res = read(fd_r, &c, sizeof(c));

        if (0 == res)
        {
            pthread_cond_signal(&cond);
            pthread_mutex_unlock(&lock);
            break;
        }

        flag = 0;

    }
    printf("111\n");
    pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
    // 初始化互斥锁
    pthread_mutex_init(&lock, NULL);

    // 初始化条件变量
    pthread_cond_init(&cond, NULL);

    // 打开文件
    int fd_r = open("./1.txt", O_RDWR);
    if (fd_r < 0)
    {
        perror("open");
        printf("__%d__\n", __LINE__);
        return -1;
    }

    // 定义一个结构体变量去接打开的文件
    struct file info;
    info.fd_r = fd_r;

    // 创建线程
    pthread_t tid1, tid2;
    if (pthread_create(&tid1, NULL, my_printf, NULL) != 0)
    {
        printf("pthread_creatr failed__%d__\n", __LINE__);
        return -1;
    }
    if (pthread_create(&tid2, NULL, my_read, &info) != 0)
    {
        printf("pthread_creatr failed__%d__\n", __LINE__);
        return -1;
    }

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    pthread_mutex_destroy(&lock);
    pthread_cond_destroy(&cond);

    close(fd_r);

    return 0;
}

作业三、

现有ID号为a b c的三个线程,每个线程的任务都是循环打印自己id号,要求打印的顺序为abc

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

// 创建互斥锁
pthread_mutex_t lock;

// 创建条件变量
pthread_cond_t cond1, cond2, cond3;

// flag 用来确认三个线程的打印顺序
int flag = 1; // 1:A  2:B  3:C

// 线程A
void *my_tid1(void *arg)
{
    while (1)
    {
        // 上锁
        pthread_mutex_lock(&lock);
        if (1 != flag)
        {
            pthread_cond_wait(&cond1, &lock);
        }

        printf("%ld\n", pthread_self());
       
        
        flag = 2;

        pthread_cond_signal(&cond2);
        pthread_mutex_unlock(&lock);
    }
}

// 线程B
void *my_tid2(void *arg)
{
    while (1)
    {
        pthread_mutex_lock(&lock);
        if (2 != flag)
        {
            pthread_cond_wait(&cond2, &lock);
        }

        printf("%ld\n", pthread_self());

        flag = 3;

        pthread_cond_signal(&cond3);
        pthread_mutex_unlock(&lock);
    }
}

// 线程C
void *my_tid3(void *arg)
{
    while (1)
    {
        pthread_mutex_lock(&lock);
        if (3 != flag)
        {
            pthread_cond_wait(&cond3, &lock);
        }

        printf("%ld\n", pthread_self());
      

        flag = 1;

        pthread_cond_signal(&cond1);
        pthread_mutex_unlock(&lock);
    }
}

int main(int argc, const char *argv[])
{
    // 初始化锁
    pthread_mutex_init(&lock, NULL);

    // 初始化体条件变量
    pthread_cond_init(&cond1, NULL);
    pthread_cond_init(&cond2, NULL);
    pthread_cond_init(&cond3, NULL);
    

    // 创建线程
    pthread_t tid1, tid2, tid3;
    if (pthread_create(&tid1, NULL, my_tid1, NULL) != 0)
    {
        printf("pthread_create failed__%d__\n", __LINE__);
        return -1;
    }
    if (pthread_create(&tid1, NULL, my_tid2, NULL) != 0)
    {
        printf("pthread_create failed__%d__\n", __LINE__);
        return -1;
    }
    if (pthread_create(&tid1, NULL, my_tid3, NULL) != 0)
    {
        printf("pthread_create failed__%d__\n", __LINE__);
        return -1;
    }

    // 销毁条件变量
    // 销毁锁
    getchar();
    pthread_cond_destroy(&cond1);
    pthread_cond_destroy(&cond2);
    pthread_cond_destroy(&cond3);
    pthread_mutex_destroy(&lock);

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值