IO进程线程day6

作业1

//临界资源
int fd=open("./work.txt",O_RDONLY);
if(fd<0)
{
	perror("open");
	return -1;
}
char buf[20]="";
ssize_t res=0;
//创建互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//创建条件变量
 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

 int flag=0 		//0:打印  1:倒置


void* callback1(void* arg)
{
	while(1)
	{
		pthread_mutex_lock; 		//上锁
		if(flag!=0) 		//不是当前线程访问时机
		{
			//解开互斥锁,设置一个唤醒条件,等待被唤醒
			pthread_cond_wait(&mutex,&wait);
			//当被唤醒后,会立即尝试上锁
			//上锁成功,则完全被唤醒,从当前位置往后执行
			//上锁失败,重新回到cond上继续休眠,等待下一次唤醒

		}


		//临界区
		while(1)
		{
			bzero(buf,sizeof(buf));
			res=read(fd,buf,sizeof(buf));
			if(res=0)
				break;
		}
		//临界区
		flag=1;  					//修改访问时机

		pthread_cond_signal(&cond); 		//通过指定的条件变量唤醒对方线程;只要唤醒了,对方线程肯定能上锁成功

		pthread_mutex_unlock(&mutex); 		//解锁
	}
	pthread_exit(NULL);
}


void* callback2(void* arg)
{
	char temp = 0;
    while(1)
    {
        pthread_mutex_lock;         //上锁
        if(flag!=1)         //不是当前线程访问时机                                                                                                                                      
        {
            //解开互斥锁,设置一个唤醒条件,等待被唤醒
            pthread_cond_wait(&mutex,&wait);
            //当被唤醒后,会立即尝试上锁
            //上锁成功,则完全被唤醒,从当前位置往后执行
            //上锁失败,重新回到cond上继续休眠,等待下一次唤醒

        }
		/************临界区****************/
		while(1)
		{
			write(1,buf,sizeof(buf));
		}
		/************临界区****************/
		close(fd);

        flag=0;                     //修改访问时机

        pthread_cond_signal(&cond);         //通过指定的条件变量唤醒对方线程;只要唤醒了,对方线程肯定能上锁成功

        pthread_mutex_unlock(&mutex);       //解锁
    }
    pthread_exit(NULL);
}


int main(int argc, const char *argv[])
{
	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;
}

作业2

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int flag=0;

void* callback1(void* arg)
{
	while(1)
	{
		if(flag=0)
		{
			sleep(1);
			printf("this is first func %A",*arg);
		}
		flag=1;
	}
	pthread_exit(NULL);
}

void* callback2(void* arg)
{
    while(1)
    {   
        if(flag=1)
        {   
			sleep(1);
            printf("this is first func %B",*arg);
        }   
        flag=2;
    }   
    pthread_exit(NULL);
}

void* callback3(void* arg)
{
    while(1)
    {   
        if(flag=2)
        {   
			sleep(1);
            printf("this is first func %C",*arg);
        }   
        flag=0;
    }   
    pthread_exit(NULL);
}


int main(int argc, const char *argv[])
{
	pthread_t A,B,C;
	if(pthread_create(&A,NULL,callback1,NULL)!=0)
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}

	if(pthread_create(&B,NULL,callback2,NULL)!=0)
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}

	if(pthread_create(&C,NULL,callback3,NULL)!=0)
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}

	pthread_join(A,NULL);
	pthread_join(B,NULL);
	pthread_join(C,NULL);

	pthread_mutex_destroy(&mutex);
	
	return 0;
}

作业3

char buf[]="1234567";

//V操作
void* callback(void* arg)
{
	if(sem_post((sem_t*)arg)<0)
	{
		perror("sem_post");
		return NULL;
	}

	char temp = 0;
	for(int i=0;i<strlen(buf)/2;i++)
	{
		temp = buf[i];
		buf[i] = buf[strlen(buf)-1-i];
		buf[strlen(buf)-1-i]=temp;
	}


	printf("V operation success __%d__\n",__LINE__);
	pthread_exit(NULL);
}


int main(int argc, const char *argv[])
{
	sem_t sem;
	//创建并初始化一个信号灯
	if(sem_init(&sem,0,2)<0)
	{
		perror("sem_init");
		return -1;
	}
	printf("sem_init success\n");

	pthread_t tid;
	pthread_create(&tid,NULL,callback,&sem);

	//P操作
	if(sem_wait(&sem)<0)
	{
		perror("sem_wait");
		return -1;
	}

	printf("%s\n",buf);

	printf("P operation success __%d__\n",__LINE__);



	//销毁信号灯
	sem_destroy(&sem);

	return 0;
}

思维导图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值