day7 IO进程 c语言

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

#include <stdio.h>
#include <pthread.h>

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER;

static int flag = 0;

void* print_a(void* arg){
	while(1){
		pthread_mutex_lock(&mutex1);
		if( flag == 1 || flag == 2){
			pthread_cond_wait(&cond1,&mutex1);
		}
		printf("a = %ld",*(pthread_t *)arg);
		fflush(stdout);
		flag = 1;
		pthread_cond_signal(&cond2);
		pthread_mutex_unlock(&mutex1);
	}
}
void* print_b(void* arg){
	while(1){
		pthread_mutex_lock(&mutex1);
		if( flag == 0 || flag == 2){
			pthread_cond_wait(&cond2,&mutex1);
		}
		printf("b = %ld",*(pthread_t *)arg);
		fflush(stdout);
		flag = 2;
		pthread_cond_signal(&cond3);
		pthread_mutex_unlock(&mutex1);
	}
}
void* print_c(void* arg){
	while(1){
		pthread_mutex_lock(&mutex1);
		if( flag == 1 || flag == 0){
			pthread_cond_wait(&cond3,&mutex1);
		}
		printf("c = %ld\n",*(pthread_t *)arg);
		flag = 0;
		pthread_cond_signal(&cond1);
		pthread_mutex_unlock(&mutex1);
	}
}
int main(int argc, const char *argv[])
{
	pthread_t tid1,tid2,tid3;
	if(pthread_create(&tid1,NULL,print_a,&tid1) != 0){
		printf("error at %d\n",__LINE__);
	}	
	if(pthread_create(&tid2,NULL,print_b,&tid2) != 0){
		printf("error at %d\n",__LINE__);
	}	
	if(pthread_create(&tid3,NULL,print_c,&tid3) != 0){
		printf("error at %d\n",__LINE__);
	}
	while(1){
	}
	pthread_cond_destroy(&cond1);	
	pthread_cond_destroy(&cond2);	
	pthread_cond_destroy(&cond3);
	pthread_mutex_destroy(&mutex1);
	return 0;
}

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

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

struct info{
	sem_t sem1;
	sem_t sem2;
	int fdr;
	int total_byte;
	char c;
};

void *read_file(void *arg){
	int total_byte = ((struct info*)arg)->total_byte;
	int total = 0;
	while(total < total_byte){
		sem_wait(&((struct info*)arg)->sem1);
		read(((struct info*)arg)->fdr,&((struct info*)arg)->c,1);
			total++;
		sem_post(&((struct info*)arg)->sem2);
	}
	pthread_exit(NULL);
}

void *printf_file(void *arg){
	int total_byte = ((struct info*)arg)->total_byte;
	int total = 0;
	while(total < total_byte){
		sem_wait(&((struct info*)arg)->sem2);
		write(1,&(((struct info*)arg)->c),1);
		total++;
		sem_post(&((struct info*)arg)->sem1);
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	pthread_t tid1,tid2;
	struct info myInfo;
	if(sem_init(&(myInfo.sem1),0,1) < 0){
		printf("Error at %d\n",__LINE__);
	}	
	if(sem_init(&(myInfo.sem2),0,0) < 0){
		printf("Error at %d\n",__LINE__);
	}
	myInfo.fdr = open("./1.txt",O_RDONLY);
	if(myInfo.fdr < 0){
		perror("open");
		return -1;
	}
	myInfo.total_byte = lseek(myInfo.fdr,0,SEEK_END);
	myInfo.c = 0;
	lseek(myInfo.fdr,0,SEEK_SET);
	if(pthread_create(&tid1,NULL,read_file,&myInfo) != 0){
		printf("Error at %d\n",__LINE__);
		return -1;
	}	
	if(pthread_create(&tid2,NULL,printf_file,&myInfo) != 0){
		printf("Error at %d\n",__LINE__);
		return -1;
	}
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	close(myInfo.fdr);
	sem_destroy(&myInfo.sem1);
	sem_destroy(&myInfo.sem2);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值