1、双分支线程,互斥锁,条件变量实现自我cat 2、信号量实现自我cat 3用信号量实现循环打印自己的线程号

cat.c

#include
#include
#include
#include
#include
#include
#include
#include
int flag=0;
int fp,size;
char c;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void* call_back(void*val)
{
	int len = 0;
	while(len<size){
		pthread_mutex_lock(&mutex);
		if('\0'!=c)
			pthread_cond_wait(&cond,&mutex);
		read(fp,&c,1);
		len = lseek(fp,0,SEEK_CUR);
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}
void* call_back_a(void*value)
{
	int len=0;
	while(len<size){
		pthread_mutex_lock(&mutex);
		if('\0'==c)
			pthread_cond_wait(&cond,&mutex);
		write(1,&c,1);
		c='\0';
		len = lseek(fp,0,SEEK_CUR);
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	fp=open("cat.c",O_RDONLY);
	size = lseek(fp,0,SEEK_END);
	lseek(fp,0,SEEK_SET);
	pthread_t tid,pid;
	if((pthread_create(&tid,NULL,call_back,NULL))!=0)
	{
		printf("tid error\n");
		return -1;
	}
	if((pthread_create(&pid,NULL,call_back_a,NULL))!=0)
	{
		printf("pid error\n");
		return -1;
	}
	pthread_join(tid,NULL);
	pthread_join(pid,NULL);
	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond);
	if(close(fp))
		perror("close");
	return 0;
}

catsem.c

#include
#include
#include
#include
#include
#include
#include
#include
#include
int fp,size;
char c;
sem_t sem_a,sem_b;
void* call_back(void*val)
{
	int len = 0;
	while(len<size){
		sem_wait(&sem_a);
		read(fp,&c,1);
		len = lseek(fp,0,SEEK_CUR);
		sem_post(&sem_b);
	}
	pthread_exit(NULL);
}
void* call_back_a(void*value)
{
	int len=0;
	while(len<size){
		sem_wait(&sem_b);
		write(1,&c,1);	
		len = lseek(fp,0,SEEK_CUR);
		sem_post(&sem_a);	
	}
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	fp=open("cat.c",O_RDONLY);
	size = lseek(fp,0,SEEK_END);
	lseek(fp,0,SEEK_SET);
	pthread_t tid,pid;
	sem_init(&sem_a,0,1);
	sem_init(&sem_b,0,0);
	if((pthread_create(&tid,NULL,call_back,NULL))!=0)
	{
		printf("tid error\n");
		return -1;
	}
	if((pthread_create(&pid,NULL,call_back_a,NULL))!=0)
	{
		printf("pid error\n");
		return -1;
	}
	pthread_join(tid,NULL);
	pthread_join(pid,NULL);
	sem_destroy(&sem_a);
	sem_destroy(&sem_b);
	if(close(fp))
		perror("close");
	return 0;
}

printfabc.c

#include
#include
#include
#include
#include
#include
#include
#include
#include
sem_t sem_a,sem_b,sem_c;
void* call_back(void*val)
{
	while(1){
		sem_wait(&sem_a);
		printf("a\t");
		sem_post(&sem_b);
	}
	pthread_exit(NULL);
}
void* call_back_a(void*value)
{
	while(1){
		sem_wait(&sem_b);
		printf("b\t");
		sem_post(&sem_c);
	}
	pthread_exit(NULL);
}
void* call_back_b(void*value)
{
	while(1){
		sem_wait(&sem_c);
		printf("c\n");
		sem_post(&sem_a);
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	pthread_t tid,pid,qid;
	sem_init(&sem_a,0,1);
	sem_init(&sem_b,0,0);
	sem_init(&sem_c,0,0);
	if((pthread_create(&tid,NULL,call_back,NULL))!=0)
	{
		printf("tid error\n");
		return -1;
	}
	if((pthread_create(&pid,NULL,call_back_a,NULL))!=0)
	{
		printf("pid error\n");
		return -1;
	}
	if((pthread_create(&qid,NULL,call_back_b,NULL))!=0)
	{
		printf("qid error\n");
		return -1;
	}
	pthread_join(qid,NULL);
	pthread_join(tid,NULL);
	pthread_join(pid,NULL);
	sem_destroy(&sem_a);
	sem_destroy(&sem_b);
	sem_destroy(&sem_c);
	return 0;
}

abccond.c

#include
#include
#include
#include
#include
#include
#include
#include
#include
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int flag=0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void* call_back(void*val)
{
	while(1){
		pthread_mutex_lock(&mutex);
		if(0!=flag){
			pthread_cond_wait(&cond,&mutex);
		}else{
			printf("a\t");
			flag=1;	
		}
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}
void* call_back_a(void*value)
{
	while(1){
		pthread_mutex_lock(&mutex);
		if(1!=flag){
			pthread_cond_wait(&cond,&mutex);
		}else{
			printf("b\t");
			flag=2;
		}
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&mutex);

	}
	pthread_exit(NULL);
}
void* call_back_b(void*value)
{
	while(1){
		pthread_mutex_lock(&mutex);
		if(2!=flag){
			pthread_cond_wait(&cond,&mutex);
		}else{
			printf("c\n");
			flag=0;
		}
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	pthread_t tid,pid,qid;
	if((pthread_create(&tid,NULL,call_back,NULL))!=0)
	{
		printf("tid error\n");
		return -1;
	}
	if((pthread_create(&pid,NULL,call_back_a,NULL))!=0)
	{
		printf("pid error\n");
		return -1;
	}
	if((pthread_create(&qid,NULL,call_back_b,NULL))!=0)
	{
		printf("qid error\n");
		return -1;
	}
	pthread_join(qid,NULL);
	pthread_join(tid,NULL);
	pthread_join(pid,NULL);
	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond);
	return 0;
}

你可能感兴趣的:(c++,c语言,算法)