8月4日 IO DAY 7

作业

1.在第一题的基础上加上一个需求:要求打印,倒置线程,顺序执行。出现的现象为先打印1234567,后打印7654321,不使用flag

2.创建两个线程,其中一个线程读取文件中的数据,另外一个线程将读取到的内容打印到终端上,类似实现cat一个文件。cat数据完毕后,要结束两个线程。

1.代码

#include <stdio.h>
#include <head.h>

char buf[] = "1234567";
int flag = 0;
pthread_mutex_t mutex;
sem_t sem1, sem2;

void *callBack1(void *arg)
{
	while(1)
	{
		
		if (sem_wait(&sem1) < 0)
		{
			ERR_MSG("sem_wait");
			return NULL;
		}
		

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

		if (sem_post(&sem2) < 0)
		{
			ERR_MSG("sem_post");
			return NULL;
		}

	}

	pthread_exit(NULL);
	return NULL;
}

void *callBack2(void *arg)
{
	 
	int i = 0;
	int j = strlen(buf) - 1;

	while (1)
	{
		
		if (sem_wait(&sem2) < 0)
		{
			ERR_MSG("sem_wait");
			return NULL;
		}
		
		while (i < j)
		{
			char t = buf[i];
			buf[i] = buf[j];
			buf[j] = t;
			i++;
			j--;
		}
		
		
		if (sem_post(&sem1) < 0)
		{
			ERR_MSG("sem_post");
			return NULL;
		}
		
		i = 0;
		j = strlen(buf) - 1;
		
	}
	
	pthread_exit(NULL);
	return NULL;
}

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

	pthread_t tid1, tid2;
	
	if(sem_init(&sem1, 0 , 1) < 0)
	{
		ERR_MSG("sem_init");
		return -1;
	}

	if(sem_init(&sem2, 0 , 0) < 0)
	{
		ERR_MSG("sem_init");
		return -1;
	}


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

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

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

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

	sem_destroy(&sem1);
	sem_destroy(&sem2);
	return 0;
}

结果

2.代码

 

#include <stdio.h>
#include <head.h>

struct Msg
{
	int fd_r;
	off_t size;
};

char c = 0;
pthread_mutex_t mutex;

sem_t sem1, sem2;


void *callBack1(void *arg)
{
	
	int fd_r = ((struct Msg*)arg) -> fd_r;
	off_t size = ((struct Msg*)arg) -> size;

	lseek(fd_r, 0, SEEK_SET);

	for (int i = 0; i < size; i++)
	{
		if (sem_wait(&sem1) < 0)
		{
			ERR_MSG("sem_wait");
			return NULL;
		}
		read(fd_r, &c, 1);
		if (sem_post(&sem2) < 0)
		{
			ERR_MSG("sem_post");
			return NULL;
		}
	}
	
	printf("\n线程1准备退出\n");
	pthread_exit(NULL);
	return NULL;
}

void *callBack2(void *arg)
{
	off_t size = ((struct Msg*)arg) -> size;

	for (int i = 0; i < size; i++)
	{

		if (sem_wait(&sem2) < 0)
		{
			ERR_MSG("sem_wait");
			return NULL;
		}
		printf("%c", c);

		if (sem_post(&sem1) < 0)
		{
			ERR_MSG("sem_post");
			return NULL;
		}
	}

	printf("\n线程2准备退出\n");
	
	pthread_exit(NULL);
	return NULL;
}

int main(int argc, const char *argv[])
{
	pthread_mutex_init(&mutex, NULL);

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

	if(sem_init(&sem2, 0 , 0) < 0)
	{
		ERR_MSG("sem_init");
		return -1;
	}

	printf("<---若出现线程退出提示,可以按回车结束该程序--->\n");
	int fd_r = open("./1.c", O_RDONLY);
	if (fd_r < 0)
	{
		ERR_MSG("open");
		return -1;
	}
	
	off_t size = lseek(fd_r, 0, SEEK_END);
	struct Msg filemsg = {fd_r, size};

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

	char s;
	s = getchar();
	sem_destroy(&sem1);
	sem_destroy(&sem2);
	pthread_mutex_destroy(&mutex);
	close(fd_r);
	printf("文件打印完毕\n");

	return 0;
}

结果

 思维导图

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值