IO day6

1->x.mind

2->

#include <myhead.h>
char c;
ssize_t res=1;
//互斥锁
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
//创建条件变量
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;

int flag=0;//0:打印  1:倒置
void* callBack1(void* arg)
{
    int fd=open("./1.txt",O_RDONLY);
    int size=lseek(fd,0,SEEK_END);
    //将光标偏移到文件头
    lseek(fd,0,SEEK_SET);
    for(int i=0;i<=size;i++)
    {
        //上锁
        pthread_mutex_lock(&mutex);
        if(flag!=0)  // 不是当前线程要访问的时机
        {
            //解开互斥锁,并设置一个唤醒条件
            //等待被唤醒
            pthread_cond_wait(&cond,&mutex);
            //当被唤醒后,会立即尝试上锁
            //上锁成功,则完全被唤醒,从当前位置继续往后执行
            //上锁失败,重新回到cond上继续休眠,等待下一次唤醒
        }
        ///临界区//
        res=read(fd,&c,1);
        /临界区
        flag=1; //修改访问时机
        //通过指定的条件变量唤醒对方方程
        pthread_cond_signal(&cond);
        //只要唤醒了,对方线程肯定能上锁成功
        //解锁
        pthread_mutex_unlock(&mutex);
    }
    
    close(fd);
    pthread_exit(NULL);
}
void* callBack2(void* arg)
{
    while(1)
    {
        //上锁
        pthread_mutex_lock(&mutex);
        if(flag!=1)  //不是当前线程要访问的时机
        {
            //解开互斥锁,并设置一个唤醒条件
            //等待被唤醒
            pthread_cond_wait(&cond,&mutex);
            //当被唤醒后,会立即尝试上锁
            //上锁成功,则完全被唤醒,从当前位置继续往后执行
            //上锁失败,重新回到cond上继续休眠,等待下一次唤醒
        }
        //临界区/
        if(res==0)
        {
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
        pthread_exit(NULL);

            break;
        }
        printf("%c",c);
        /临界区///
        flag=0;  //修改访问时机
        //通过指定条件变量唤醒对方线程
        pthread_cond_signal(&cond);
        //解锁
        pthread_mutex_unlock(&mutex);

    }
    pthread_exit(NULL);
}


int main(int argc, const char *argv[])
{
    //创建并初始化一个互斥锁
    //pthread_mutex_init(&mutex,NULL);
    //创建条件变量
    //pthread_cond_init(&cond,NULL);
    pthread_t tid1,tid2;
    if(pthread_create(&tid1,NULL,callBack1,NULL)!=0)
    {
        fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
        return -1;
    }
    if(pthread_create(&tid2,NULL,callBack2,NULL)!=0)
    {
        fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
        return -1;
    }
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    //销毁互斥锁
    pthread_mutex_destroy(&mutex);
    //销毁条件变量
    pthread_cond_destroy(&cond);

    return 0;
}
3->

#include<myhead.h>

pthread_t tid1,tid2,tid3;
//临界资源
char buf=0;
//互斥锁
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=1;
void* A(void* arg)
{
    while(1)
    {
        //上锁
        pthread_mutex_lock(&mutex);
        if(flag !=1)   //不是当前线程要访问的时机
        {
            //解开互斥锁,并设置一个唤醒条件
            //等待被唤醒
            pthread_cond_wait(&cond1,&mutex);
            //当被唤醒后,会立即尝试上锁
            //上锁成功,则完全被唤醒,从当前位置继续往后执行官
            //上锁失败,重新回到cond上继续休眠,等待下一次唤醒
        }
        //临界区//
        printf("A");
        //临界区/
        flag=2;//修改访问时机
        //通过指定的条件变量唤醒对方线程 
        pthread_cond_signal(&cond2);
        //只要唤醒,对方线程肯定能上锁成功
        pthread_cond_signal(&cond2);
        //只要唤醒,对方线程肯定能上锁成功
        pthread_cond_signal(&cond2);
        //只要唤醒,对方线程肯定能上锁成功

        //解锁
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}
void* B(void* arg)
{
    while(1)
    {
        //上锁
        pthread_mutex_lock(&mutex);
        if(flag!=2)//不是当前线程要访问的时机
        {
            //解开互斥锁,并设置一个唤醒条件
            //等待被唤醒
            pthread_cond_wait(&cond2,&mutex);
            //上锁成功,则完全被唤醒,从当前位置继续往后执行官
            //上锁失败,重新回到cond上继续休眠,等待下一次唤醒
        }
        //临界区//
        printf("B");
        //临界区///
        flag=3;//修改访问时机
        //通过指定的条件变量唤醒对方线程
        pthread_cond_signal(&cond3);

        //解锁
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}
void* C(void* arg)
{
    while(1)
    {
        //上锁
        pthread_mutex_lock(&mutex);
        if(flag!=3)//不是当前线程要访问的时机
        {
            //解开互斥锁,并设置一个唤醒条件
            //等待被唤醒
            pthread_cond_wait(&cond3,&mutex);
            //上锁成功,则完全被唤醒,从当前位置继续往后执行官
            //上锁失败,重新回到cond上继续休眠,等待下一次唤醒
        }
        //临界区//
        printf("C");
        //临界区///
        flag=1;//修改访问时机
        //通过指定的条件变量唤醒对方线程
        pthread_cond_signal(&cond1);

        //解锁
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
    if(pthread_create(&tid1,NULL,A,NULL)!=0)
    {
        fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
        return -1;
    }
    if(pthread_create(&tid2,NULL,B,NULL)!=0)
    {
        fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
        return -1;
    }
    if(pthread_create(&tid3,NULL,C,NULL)!=0)
    {
        fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
        return -1;
    }
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    //销毁互斥锁
    pthread_mutex_destroy(&mutex);
    //销毁条件变量
    pthread_cond_destroy(&cond1);
    pthread_cond_destroy(&cond2);
    pthread_cond_destroy(&cond3);
    return 0;
}
 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值