【IO】两个线程实现cat;条件变量循环打印abc

1、创建两个线程,实现将一个文件的内容打印到终端上,类似cat一个文件
一个线程读取文件中的内容
另一个线程将读取到的内容打印到终端上

//临死前也要带着这些头文件走,哐哐报错!!
#include<stdio.h>
#include <semaphore.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>

sem_t sem1,sem2;
char buf[32] = "";
//int flag = 0;//0读1打
int fd;

void* callBack1(void* arg)
{
	while(1)
	{
		sem_wait(&sem1);
		printf("%s",buf);
		fflush(stdout);
		sem_post(&sem2);
	};
	printf("__%d__",__LINE__);
	pthread_exit(NULL);
	printf("__%d__",__LINE__);

}

void* callBack2(void* arg)
{
	
	fd = open("mutex_jpg.c",O_RDONLY);
	off_t len = lseek(fd,0,SEEK_END);
	off_t offset = 0;
	for(int i = 0; i < len; i++)
	{
		sem_wait(&sem2);
		lseek(fd,offset,SEEK_SET);
		bzero(buf,sizeof(buf));
		int res = read(fd,buf,sizeof(buf));
		if(0 == res)
			break;
		offset = lseek(fd,0,SEEK_CUR);
		sem_post(&sem1);
	}
	printf("__%d__",__LINE__);
	pthread_exit(NULL);
	printf("__%d__",__LINE__);
}

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

	sem_init(&sem1,0,1);
	sem_init(&sem2,0,0);

	pthread_t tid1,tid2;
	pthread_create(&tid1,NULL,callBack1,NULL);
	pthread_create(&tid2,NULL,callBack2,NULL);

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	printf("exit........\n");

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

//用条件变量

#include<stdio.h>
#include <semaphore.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
char buf[32] = "";
int flag = 0;//0读1打
int fd;

void* callBack1(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(flag != 1)
		{
			pthread_cond_wait(&cond,&mutex);
		}
		printf("%s",buf);
		fflush(stdout);
		flag = 0;
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&mutex);
	};
	pthread_exit(NULL);

}

void* callBack2(void* arg)
{
	
	fd = open("mutex_jpg.c",O_RDONLY);
	off_t len = lseek(fd,0,SEEK_END);
	off_t offset = 0;
	for(int i = 0; i < len; i++)
	{
		pthread_mutex_lock(&mutex);
		if(flag != 0)
			pthread_cond_wait(&cond,&mutex);

		lseek(fd,offset,SEEK_SET);
		bzero(buf,sizeof(buf));
		int res = read(fd,buf,sizeof(buf));
		if(0 == res)
			break;
		offset = lseek(fd,0,SEEK_CUR);
		flag = 1;
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&mutex);

	}
	pthread_exit(NULL);
}

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

	//sem_init(&sem1,0,1);
	//sem_init(&sem2,0,0);

	pthread_t tid1,tid2;
	pthread_create(&tid1,NULL,callBack1,NULL);
	pthread_create(&tid2,NULL,callBack2,NULL);

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	printf("exit........\n");

	//sem_destroy(&sem1);
	//sem_destroy(&sem2);
	
	close(fd);
	return 0;
}

2、 用条件变量的方式实现:现有ID号为a b c的三个线程,每个线程的任务都是循环打印自己id号,要求打印的顺序为abc

#include<stdio.h>
#include <pthread.h>
#include <unistd.h>
int flag = 0;//0a1b2c
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t conda = PTHREAD_COND_INITIALIZER;
pthread_cond_t condb = PTHREAD_COND_INITIALIZER;
pthread_cond_t condc = PTHREAD_COND_INITIALIZER;


void* printf_a(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(flag != 0)
		{
			pthread_cond_wait(&conda,&mutex);
		}
		printf("A\n");
		flag = 1;
		pthread_cond_signal(&condb);
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}

void* printf_b(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(flag != 1)
		{
			pthread_cond_wait(&condb,&mutex);
		}
		printf("B\n");
		flag = 2;
		pthread_cond_signal(&condc);
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}

void* printf_c(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(flag != 2)
		{
			pthread_cond_wait(&condc,&mutex);
		}
		printf("C\n");
		flag = 0;
		pthread_cond_signal(&conda);
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}


int main(int argc, const char *argv[])
{
	pthread_t tid1,tid2,tid3;
	pthread_create(&tid1,NULL,printf_a,NULL);
	pthread_create(&tid2,NULL,printf_b,NULL);
	pthread_create(&tid3,NULL,printf_c,NULL);
	
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	pthread_join(tid3,NULL);
	
	pthread_mutex_destroy(&mutex);
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值