IO第七天

 

1.用信号量的方式实现倒置线程打印线程交替执行

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>    
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <semaphore.h>
 
char buf[]="1234567";
 
sem_t sem1;
sem_t sem2;
 
void*callbacka(void *arg)     
{
 
    while(1)
    {
        if(sem_wait(&sem1)<0)
        {
            perror("sem_wait");
            break;
        }
        printf("%s\n",buf);
 
        if(sem_post(&sem2)<0)
        {
            perror("sem_post");
            break;
        }
    }
    pthread_exit(NULL);
}
 
void* callbackb(void* arf)
{
    int temp=0;
    while(1)
    {
        
        sem_wait(&sem2);
        for(int i=0;i<strlen(buf)/2;i++)
        {
            temp=buf[i];
            buf[i]=buf[strlen(buf)-i-1];
            buf[strlen(buf)-i-1]=temp;
        }
        
        if(sem_post(&sem1) <0)
        {
            perror("sem_post");
            break;
        }
    }
    pthread_exit(NULL);
}
 
int main(int argc, const char *argv[])
{  
    //创建信号量
    if(sem_init(&sem1,0,0)<0)
    {
        perror("sem_init");
        return -1;
    }
 
    if(sem_init(&sem2,0,1)<0)
    {
        perror("sem_init");
        return -1;
    }
    
    
    //主线程
    pthread_t a;  
    pthread_t b;  
 
    //创建A的分支线程
    
    if(pthread_create(&a, NULL, callbacka,NULL)!=0)
    {
        fprintf(stderr,"pthread_create failed");
        return -1;
    }
 
    //创建B的分支线程
    
    if(pthread_create(&b, NULL, callbackb,NULL)!=0)
    {
        fprintf(stderr,"pthread_create failed");
        return -1;
    }   
    //阻塞线程a b
    pthread_join(a,NULL);
    pthread_join(b,NULL);
 
    //销毁信号量
    sem_destroy(&sem1);
    sem_destroy(&sem2);
 
    return 0;
}

 1.#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
//互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//条件变量
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
 
int flag=0;
int fp;
char buf;
 
void* callBack_r(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex); //上锁
 
        if(0 != flag)
        {
            pthread_cond_wait(&cond,&mutex);
        }
 
        if(read(fp,&buf,1) == 0)
        {    
            pthread_exit(NULL);
        }    
        flag = 1;
 
        pthread_cond_signal(&cond);//唤醒
 
        pthread_mutex_unlock(&mutex);//解锁
 
    }
        pthread_exit(NULL);
}
 
void* callBack_w(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex); //上锁
 
        if(1 != flag)
        {
            pthread_cond_wait(&cond,&mutex);
        }
 
        printf("%c",buf);
        flag = 0;
 
 
        pthread_cond_signal(&cond);//唤醒
        
        pthread_mutex_unlock(&mutex);//解锁        
    }
    pthread_exit(NULL);
 
}
int main(int argc, const char *argv[])
{
    pthread_t A;
    pthread_t B;
    fp = open("./03_seem.c",O_RDONLY);
    if(fp < 0)
    {
        perror("open");
        return -1;
    }
 
 
    if(pthread_create(&A,NULL,callBack_r,NULL) != 0)
    {
        fprintf(stderr,"prthread_create failed __%d__\n",__LINE__);
        return -1;
    }
 
    if(pthread_create(&B,NULL,callBack_w,NULL) != 0)
    {
        fprintf(stderr,"prthread_create failed __%d__\n",__LINE__);
        return -1;
    }
    pthread_join(A,NULL);
    pthread_mutex_destroy(&mutex); //销毁互斥锁    
    //pthread_cond_destroy(&cond); //销毁条件变量
 
    close(fp);
 
    return 0;
}

2.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>    
#include <pthread.h>
 
//临界资源
char arr[3]="ABC";
 
//创建互斥锁
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* CallBack_A(void* arg)
{
    while(1)
    {
        /*********临界区*************/
        pthread_mutex_lock(&mutex);
        if(0 !=flag)
        {
            pthread_cond_wait(&cond1,&mutex);
        }
        printf("%c\n",arr[0]);
        flag = 1;
        pthread_cond_signal(&cond2);
        pthread_mutex_unlock(&mutex);
        /*********临界区**********/
 
    }
    pthread_exit(NULL);
}
 
 
void* CallBack_B(void* arg)
{
    while(1)
    {
        /**********临界区*********/
        pthread_mutex_lock(&mutex);
        if(1 != flag)
        {
            pthread_cond_wait(&cond2,&mutex);
        }
        printf("%c\n",arr[1]);
        flag = 2;
        pthread_cond_signal(&cond3);
        pthread_mutex_unlock(&mutex);
        /*********临界区***********/
 
    }
    pthread_exit(NULL);
}
 
void* CallBack_C(void* arg)
{
    while(1)
    {
        /**********临界区*********/
        pthread_mutex_lock(&mutex);
        if(2 != flag)
        {
            pthread_cond_wait(&cond3,&mutex);
        }
        printf("%c\n",arr[2]);
        flag = 0;
        pthread_cond_signal(&cond1);
        pthread_mutex_unlock(&mutex);
        /*********临界区***********/
 
    }
    pthread_exit(NULL);
}
 
 
int main(int argc, const char *argv[])
{
    pthread_t tid1,tid2,tid3;    
    if(pthread_create(&tid1,NULL,CallBack_A,NULL) != 0)
    {
        return -1;
    }
 
    if(pthread_create(&tid2,NULL,CallBack_B,NULL) != 0)
    {
        return -1;
    }
 
    if(pthread_create(&tid3,NULL,CallBack_C,NULL) != 0)
    {
        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;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值