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

这些代码示例展示了在C语言中使用pthread库进行多线程编程时,如何通过互斥锁、条件变量和信号量实现线程间的同步。cat.c使用了互斥锁和条件变量来同步读文件和显示文件内容;catsem.c则使用信号量实现同样的功能;printfabc.c通过信号量实现a,b,c的循环打印,确保打印顺序;abccond.c则用条件变量控制a,b,c的交替打印。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cat.c

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
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<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<semaphore.h>
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<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<semaphore.h>
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<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<semaphore.h>
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值