IO 2023-3-2 作业

1.将一个文件打印到终端,类似cat

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

int fd;
ssize_t res;
char buf[20];
int flag = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void* callBack_read(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(flag != 0)
		{
			pthread_cond_wait(&cond,&mutex);
		}
		res=read(fd,buf,sizeof(buf));

		if(res < 0)
		{
			perror("read");
			return NULL;
		}
		
		flag = 1;
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&mutex);

		if(0 == res)
		{
			break;
		}

	}
	pthread_exit(NULL);
}

void* callBack_write(void* arg)
{
	ssize_t res_w;
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(flag != 1)
		{
			pthread_cond_wait(&cond,&mutex);
		}

		res_w = write(1,buf,res);
		if(res_w<0)
		{
			perror("write");
			return NULL;
		}

		flag = 0;
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&mutex);

		if(res_w == 0)
		{
			break;
		}		
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	fd = open("sem.c",O_RDONLY);
	if(fd<0)
	{
		perror("open");
		return -1;
	}

	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,callBack_read,NULL)<0)
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}
	pthread_detach(tid1);

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

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);

	close(fd);
	

	return 0;
}

2.用条件变量实现,ABC三个线程分别打印编号,顺序为ABC

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
sem_t sem;
int flag = 0;


void* callBackA(void* arg)
{
	char a='A';
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(flag!=0)
		{
			pthread_cond_wait(&cond,&mutex);
		}

		printf("%c",a);

		flag = 1;

		pthread_cond_signal(&cond1);

		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}

void* callBackB(void* arg)
{
	char a='B';
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(flag!=1)
		{
			pthread_cond_wait(&cond1,&mutex);
		}

		printf("%c",a);
		flag = 2;

		pthread_cond_signal(&cond2);

		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);


}

void* callBackC(void* arg)
{
	char a='C';
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(flag!=2)
		{
			pthread_cond_wait(&cond2,&mutex);
		}

		printf("%c\n",a);

		flag = 0;

		pthread_cond_signal(&cond);

		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{

	pthread_t tida,tidb,tidc;
	if(pthread_create(&tida,NULL,callBackA,NULL)<0)
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}

	pthread_detach(tida);

	if(pthread_create(&tidb,NULL,callBackB,NULL)<0)
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}

	pthread_detach(tidb);

	if(pthread_create(&tidc,NULL,callBackC,NULL)<0)
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}
	
	pthread_join(tidc,NULL);

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

3.信号变量实现,打印一次倒置一次

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

char buf[]="1234567";
sem_t sem;
sem_t sem1;

void* callBash(void* arg)
{
	while(1)
	{
		sem_wait(&sem);
		printf("%s\n",buf);
		sem_post(&sem1);
	}

	pthread_exit(NULL);
}

void* callBash1(void* arg)
{
	char temp;
	int len = strlen(buf);

	while(1)
	{
		sem_wait(&sem1);
		for(int i=0;i<len/2;i++)
		{
			temp=buf[i];
			buf[i]=buf[len-i-1];
			buf[len-i-1]=temp;	
		}
		sem_post(&sem);
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	if(sem_init(&sem,0,1)<0)
	{
		perror("sem_init");
		return -1;
	}

	if(sem_init(&sem1,0,0)<0)
	{
		perror("sem1_init");
		return -1;
	}

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

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

	pthread_join(tid1,NULL);

	sem_destroy(&sem);
	sem_destroy(&sem1);

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值