4.3 IO进程作业

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

一个线程读取文件中的数据

另外一个线程打印文件中的数据

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>

int flag=0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
char buf[200]="";
int len;

void *callBack1(void *arg)
{
	FILE *fp_r = fopen("./1.c","r");

	pthread_mutex_lock(&mutex);
	while(fgets(buf,sizeof(buf),fp_r)!=NULL)
	{
		if(0 != flag)
		{
			pthread_cond_wait(&cond,&mutex);
		}

		flag = 1;

		bzero(buf,sizeof(buf));
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&mutex);
	}
	len=1;
	pthread_cond_signal(&cond);
	pthread_mutex_unlock(&mutex);
	fclose(fp_r);
	pthread_exit(NULL);
}

void *callBack2(void *arg)
{

	pthread_mutex_lock(&mutex);
	while(len!=1)
	{
		if(1 != flag)
		{
			pthread_cond_wait(&cond,&mutex);
		}

		flag = 0;
		
		fputs(buf,stdout);
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}

int main(int argc,const char *argv[])
{	
	FILE *fp =fopen("./cat.txt","w+");
	fclose(fp);

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

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

	pthread_join(tid2,NULL);
	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond);

	return 0;
}

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

提示:用多个信号量

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>

sem_t sem1;
sem_t sem2;

char buf[20]="1234567";
char s;

void *callBack1(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;
		}

	}
}

void *callBack2(void *arg)
{
	while(1)
	{
		if(sem_wait(&sem2)<0)
		{
			perror("sem_wait");
			break;
		}

		int i=0;
		int j=strlen(buf)-1;
		while(i<j)
		{
			s=buf[i];
			buf[i]=buf[j];
			buf[j]=s;
			i++;
			j--;
		}

		if(sem_post(&sem1)<0)
		{
			perror("sem_post");
			break;
		}

	}
}
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 tid1;
	if(pthread_create(&tid1,NULL,callBack1,NULL)!=0)
	{
		fprintf(stderr,"pthread_create failed\n");
		return -1;
	}

	pthread_t tid2;
	if(pthread_create(&tid2,NULL,callBack2,NULL)!=0)
	{
		fprintf(stderr,"pthread_create failed\n");
		return -1;
	}
		pthread_join(tid1,NULL);
		pthread_join(tid2,NULL);
		sem_destroy(&sem1);
		sem_destroy(&sem2);

		return 0;
}

3.现有ID号为a b c的三个线程,每个线程的任务都是循环打印自己id号,要求打印的顺序为abc

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>

int flag=0;
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;

void *callBack1(void *arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(0 != flag)
		{
			pthread_cond_wait(&cond1,&mutex);
		}
		flag=1;
		printf("%ld ",tid1);
		pthread_cond_signal(&cond2);
		pthread_mutex_unlock(&mutex);
	}
}

void *callBack2(void *arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(1 != flag)
		{
			pthread_cond_wait(&cond2,&mutex);
		}
		flag=2;
		printf("%ld ",tid2);
		pthread_cond_signal(&cond3);
		pthread_mutex_unlock(&mutex);
	}
}

void *callBack3(void *arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(2 != flag)
		{
			pthread_cond_wait(&cond3,&mutex);
		}
		flag=0;
		printf("%ld\n",tid3);

		pthread_cond_signal(&cond1);
		pthread_mutex_unlock(&mutex);
	}
}

int main(int argc,const char *argv[])
{	
	if(pthread_create(&tid1,NULL,callBack1,NULL))
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}
	if(pthread_create(&tid2,NULL,callBack2,NULL))
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}
	if(pthread_create(&tid3,NULL,callBack3,NULL))
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}

	pthread_join(tid3,NULL);
	pthread_mutex_destroy(&mutex);  
	pthread_cond_destroy(&cond1);    
	pthread_cond_destroy(&cond2);    
	pthread_cond_destroy(&cond3);    

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值