9.12作业

1.创建两个线程,其中一个线程读取文件中的数据,另外一个线程将读取到的内容打印到终端上,类似实现cat一个文件。cat数据完毕后,要结束两个线程。

        提示:先读数据,读到数据后将数据打印到终端上。

#include <head.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

char sign = 0;
char buf[128] = { 0 };
ssize_t res = 0;

void* t_in(void* arg)
{

    while (1) {
        pthread_mutex_lock(&mutex);
        if (sign != 0)
            pthread_cond_wait(&cond, &mutex);

        // 读取
        //***************************************//
        res = read(*(int*)arg, buf, sizeof(buf));
        //***************************************//

        sign = 1;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
        if (res <= 0) {
            pthread_exit(NULL);
        }
    }
}
void* t_out(void* arg)
{
    while (1) {

        pthread_mutex_lock(&mutex);
        if (sign != 1)
            pthread_cond_wait(&cond, &mutex);

        // 打印
        //***************************************//
        write(1, buf, res);
        //***************************************//

        sign = 0;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
        if (res <= 0) {
            pthread_exit(NULL);
        }
    }
}

int main(int argc, const char* argv[])
{
    int fd = open(argv[1], O_RDONLY);
    if (fd < 0)
        PRINT_ERR("open");

    pthread_t t1_id, t2_id;
    if (0 != pthread_create(&t1_id, NULL, t_in, (void*)&fd))
        return -1;
    if (0 != pthread_create(&t2_id, NULL, t_out, NULL))
        return -1;

    pthread_join(t1_id, NULL);
    pthread_join(t2_id, NULL);
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
    return 0;
}

 

 2.有三个线程,ID号分别为ABC,且每个线程中都在循环打印自己的ID。要求打印的结果为ABC。
#include <head.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_a = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_b = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_c = PTHREAD_COND_INITIALIZER;

char sign = 0;

void* A(void* arg)
{

    while (1) {
        pthread_mutex_lock(&mutex);
        if (sign != 0)
            pthread_cond_wait(&cond_a, &mutex);

        //***************************************//
        putchar('A');
        //***************************************//

        sign = 1;
        pthread_cond_signal(&cond_b);
        pthread_mutex_unlock(&mutex);
    }
}
void* B(void* arg)
{
    while (1) {

        pthread_mutex_lock(&mutex);
        if (sign != 1)
            pthread_cond_wait(&cond_b, &mutex);

        //***************************************//
        putchar('B');
        //***************************************//

        sign = 2;
        pthread_cond_signal(&cond_c);
        pthread_mutex_unlock(&mutex);
    }
}
void* C(void* arg)
{
    while (1) {

        pthread_mutex_lock(&mutex);
        if (sign != 2)
            pthread_cond_wait(&cond_c, &mutex);

        //***************************************//
        putchar('C');
        //***************************************//

        sign = 0;
        pthread_cond_signal(&cond_a);
        pthread_mutex_unlock(&mutex);
    }
}

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

    pthread_t tid_a, tid_b, tid_c;
    if (0 != pthread_create(&tid_a, NULL, A, NULL))
        return -1;
    if (0 != pthread_create(&tid_b, NULL, B, NULL))
        return -1;
    if (0 != pthread_create(&tid_c, NULL, C, NULL))
        return -1;

    
    pthread_join(tid_a,NULL);
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond_a);
    pthread_cond_destroy(&cond_b);
    pthread_cond_destroy(&cond_c);
    return 0;
}

 

3.要求定义一个全局变量 char buf[] = "1234567",创建两个线程,不考虑退出条件。(用信号量的方式实现上述代码顺序执行,不允许使用flag;)

        A线程循环打印buf字符串,

        B线程循环倒置buf字符串,即buf中本来存储1234567,倒置后buf中存储7654321. B线程中不打印!!

        倒置不允许使用辅助数组。

        要求A线程打印出来的结果只能为 1234567 或者 7654321 不允许出现7634521 7234567

        不允许使用sleep函数

        分析出现错误的原因。

#include <head.h>

char buf[] = "1234567";
sem_t sem1, sem2;

void invert(void)
{
    int i = 0, j = strlen(buf) - 1;
    char temp;
    while (i < j) {
        temp = buf[i];
        buf[i] = buf[j];
        buf[j] = temp;
        i++;
        j--;
    }
}
void* t1(void* arg)
{
    while (1) {
        sem_wait(&sem1);
        puts(buf);
        sem_post(&sem2);
    }
}
void* t2(void* arg)
{
    while (1) {
        sem_wait(&sem2);
        invert();
        sem_post(&sem1);
    }
}

int main(int argc, const char* argv[])
{
    if (sem_init(&sem1, 0, 1) != 0)
        PRINT_ERR("sem1_init");
    if (sem_init(&sem2, 0, 0) != 0)
        PRINT_ERR("sem2_init");
    pthread_t t1_id, t2_id;
    if (0 != pthread_create(&t1_id, NULL, t1, NULL))
        return -1;
    if (0 != pthread_create(&t2_id, NULL, t2, NULL))
        return -1;

    while (1) {
    }

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值