IOday7

1.

逆置打印 

#include <head.h>
char str[] = "1234567";
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int flag = 0;
void* inversion(void* arg){
    while(1){
        //printf("%s __%d__\n",str,__LINE__);测试
        pthread_mutex_lock(&mutex);
        if(0 != flag){
            pthread_cond_wait(&cond,&mutex);
        }
        int i = 0, j = strlen(str)-1;
        while(i < j){//逆置
            *(str+i) = *(str+i) + *(str+j);
            *(str+j) = *(str+i) - *(str+j);
            *(str+i) = *(str+i) - *(str+j);
            i++;
            j--;
        }
        flag = 1;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}
void* show(void* arg){
    while(1){
        pthread_mutex_lock(&mutex);
        if(1 != flag){
            pthread_cond_wait(&cond,&mutex);
        }
        printf("%s\n",str);
        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 tid,tid1;
    if(pthread_create(&tid,NULL,inversion,NULL) != 0){
        printf("pthread_create error\n");
        return -1;
    }
    if(pthread_create(&tid1,NULL,show,NULL) != 0){
        printf("pthread_create error\n");
        return -1;
    }

    //接收线程结束
    pthread_join(tid,NULL);
    pthread_join(tid1,NULL);

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
    
    return 0;
}

 

2.cat的实现

#include <head.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
//全局一个flag和一个数组
int flag = 0;

char buf[128] ="";
int res = 0;

void* Read(void* arg){
    int fd = *((int*)arg);
    while(1){
        
        pthread_mutex_lock(&mutex);
        if(0 != flag){//阻塞的条件
            pthread_cond_wait(&cond,&mutex);
        }
        memset(buf,0,sizeof(buf));
        res = read(fd,buf,sizeof(buf)-1);
        if(0 == res ){
            pthread_cond_signal(&cond);
            pthread_mutex_unlock(&mutex);
            break;
        }
        flag = 1;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}
void* Write(void* arg){
    while(1){
       pthread_mutex_lock(&mutex);
        if(1 != flag){//阻塞的条件
            pthread_cond_wait(&cond,&mutex);
        }            
        if(0 == res ){
            break;
        }
        //输出到终端
        printf("%s",buf);

        fflush(stdout);
        flag = 0;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex); 
    }
    pthread_exit(NULL);
}
int main(int argc,const char * argv[])
{   
    int fd;
    //打开文件
    if((fd = open("/home/ubuntu/xwf/01_vacation/togethercopy3.c",O_RDONLY)) < 0){
        perror("open");
        printf("__%d__\n",__LINE__);
        return -1;
    }

    //锁和条件的初始化
    pthread_mutex_init(&mutex,NULL);
    pthread_cond_init(&cond,NULL);
    //创建两个线程
    pthread_t tid,tid1;

    if(pthread_create(&tid,NULL,Read,&fd) != 0){
        printf("pthread_create error\n");
        return -1;
    }
    if(pthread_create(&tid1,NULL,Write,NULL) != 0){
        printf("pthread_create error\n");
        return -1;
    }

    //阻塞等待
    pthread_join(tid,NULL);
    pthread_join(tid1,NULL);

    //销毁锁和
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);

    return 0;
}

 3.循环打印abc的tid号

#include <head.h>
sem_t sem,sem1,sem2;

void* A(void* arg){//打印A
    while(1){
        sem_wait(&sem);
        printf("%ld\n",pthread_self());
        //sleep(1);
        sem_post(&sem1);
    }
    pthread_exit(NULL);
}
void* B(void* arg){//打印B
    while(1){
        sem_wait(&sem1);
        printf("%ld\n",pthread_self());
        //sleep(1);
        sem_post(&sem2);
    }
    pthread_exit(NULL);
}
void* C(void* arg){//打印C
    while(1){
        sem_wait(&sem2);
        printf("%ld\n",pthread_self());
        //sleep(1);
        sem_post(&sem);
    }
    pthread_exit(NULL);
}

int main(int argc,const char * argv[])
{
    //创建三个信号灯
    if(sem_init(&sem,0,1) > 0){
        perror("sem_init");
        printf("__%d__",__LINE__);
        return -1;
    }
    if(sem_init(&sem1,0,0) > 0){
        perror("sem_init");
        printf("__%d__",__LINE__);
        return -1;
    }
    if(sem_init(&sem2,0,0) > 0){
        perror("sem_init");
        printf("__%d__",__LINE__);
        return -1;
    }

    //创建三个线程
    pthread_t tid,tid1,tid2;

    if(pthread_create(&tid,NULL,A,NULL) != 0){
        printf("pthread_create error\n");
        return -1;
    }
    if(pthread_create(&tid,NULL,B,NULL) != 0){
        printf("pthread_create error\n");
        return -1;
    }
    if(pthread_create(&tid,NULL,C,NULL) != 0){
        printf("pthread_create error\n");
        return -1;
    }


    //阻塞等待执行
    pthread_join(tid,NULL);
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);

    //关闭三个灯
    sem_destroy(&sem);
    sem_destroy(&sem1);
    sem_destroy(&sem2);

    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值