0302_IO进程线程作业

1.将一个文件中的数据打印到终端上,类似cat一个文件。要求如下

A线程读取文件中的数据

B线程将A线程读取到的数据打印到终端上

文件打印完毕后,结束进程。

#include "head.h"
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int flag = 0;
char temp[32] = "";
int rev = 0;
void* callBack_read(void* arg)
{
    int fd = open("01_mutex.c",O_RDONLY);
    if(fd < 0)
    {
        ERR_MSG("open");
        return NULL;
    }
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(0 != flag)
        {
            pthread_cond_wait(&cond,&mutex);
        }
        strcpy(temp,"0");
        rev = read(fd,temp,sizeof(temp));
        if(0 == rev)
        {
            break;
        }
        else if(rev < 0)
        {
            ERR_MSG("read");
            return NULL;
        }
        flag = 1;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
    }
    flag = 1;
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mutex);
    close(fd);
    pthread_exit(NULL);
}
void* callBack_write(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(1 != flag)
        {
            pthread_cond_wait(&cond,&mutex);
        }
        if(write(1,temp,rev) <= 0)
        {
            break;
        }
        flag = 0;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
    pthread_t tid_r,tid_w;
    if(0 != pthread_create(&tid_r,NULL,callBack_read,NULL))
    {
        fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
        return -1;
    }
    if(0 != pthread_create(&tid_w,NULL,callBack_write,NULL))
    {
        fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
        return -1;
    }
    pthread_join(tid_r,NULL);
    pthread_join(tid_w,NULL);
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
    return 0;
}
0302_IO进程线程作业_第1张图片

2.用条件变量实现,有编号为ABC的三个线程,线程内分别打印自己的线程编号,要求打印的顺序为ABC。

提示:多个条件变量

#include "head.h"
pthread_t tid1,tid2,tid3;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER;
int flag = 0;
void* callBack1(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(0 != flag)
        {
            pthread_cond_wait(&cond1,&mutex);
        }
        printf("tid1 = %ld\n",tid1);
        flag = 1;
        pthread_cond_signal(&cond2);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}
void* callBack2(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(1 != flag)
        {
            pthread_cond_wait(&cond2,&mutex);
        }
        printf("tid2 = %ld\n",tid2);
        flag = 2;
        pthread_cond_signal(&cond3);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}
void* callBack3(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(2 != flag)
        {
            pthread_cond_wait(&cond3,&mutex);
        }
        printf("tid3 = %ld\n",tid3);
        flag = 0;
        pthread_cond_signal(&cond1);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
    if(0 != pthread_create(&tid1,NULL,callBack1,NULL))
    {
        fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
        return -1;
    }
    if(0 != pthread_create(&tid2,NULL,callBack2,NULL))
    {
        fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
        return -1;
    }
    if(0 != pthread_create(&tid3,NULL,callBack3,NULL))
    {
        fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
        return -1;
    }
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    pthread_join(tid3,NULL);
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond1);
    pthread_cond_destroy(&cond2);
    pthread_cond_destroy(&cond3);
    return 0;
}
0302_IO进程线程作业_第2张图片

3.要求用信号量的方式实现,打印一次倒置一次。不允许使用flag。

提示:用多个信号量

#include "head.h"
char buf[]="1234567";
sem_t sem1;
sem_t sem2;
void StrRev(char *p)
{
    int i = 0,len = strlen(p)-1;
    char temp;
    while(i
0302_IO进程线程作业_第3张图片

你可能感兴趣的:(linux)