IOday7

本文展示了两个C语言程序,分别使用线程和信号量实现并发打印文件内容,以及用条件变量控制线程按特定顺序打印ID。第一个程序中,两个线程协同工作,一个读取文件,另一个打印内容。第二个程序通过条件变量保证三个线程按a->b->c的顺序循环打印ID。
摘要由CSDN通过智能技术生成

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

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

sem_t sem1,sem2;
pthread_t pth1,pth2;
char c='a';

void* cat(void* arg)
{
	lseek(*(int*)arg,0,SEEK_SET);
	while(1)
	{
		sem_wait(&sem1);
		size_t res=read(*(int*)arg,&c,1);	
		if(0==res)
			break;
		sem_post(&sem2);
	}
}
void* put(void* arg)
{
	while(1)
	{
		sem_wait(&sem2);
		printf("%c",c);
		sem_post(&sem1);
	}
}

int main(int argc, const char *argv[])
{
	int fp=open("./ex1.c",O_RDONLY);
	sem_init(&sem1,0,1);
	sem_init(&sem2,0,0);
	pthread_create(&pth1,NULL,cat,&fp);	
	pthread_create(&pth2,NULL,put,NULL);
	pthread_join(pth1,NULL);
	sem_destroy(&sem1);
	sem_destroy(&sem2);
	printf("\n打印完毕!\n");
	return 0;
}
//展示
/*
linux@linux:~/Desktop/demo3/day6$ gcc w2.c -pthread
linux@linux:~/Desktop/demo3/day6$ ./a.out 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

pthread_mutex_t mutex;


//------------------>前半部分
void* front(void* arg)
{
	int len=*((int*)arg+1);
	int fp=*((int*)arg);
	int fp1=*((int *)arg+2);
	pthread_mutex_lock(&mutex);
	lseek(fp,0,SEEK_SET);
	lseek(fp1,0,SEEK_SET);
	for(int i=0;i<len/2;i++)
	{
		char c;
		read(fp,&c,1);
		write(fp1,&c,1);
	}
	pthread_mutex_unlock(&mutex);
	printf("前半部分完成!\n");
	pthread_exit(NULL);
}

void* rear(void* arg)
{
	int len=*((int*)arg+1);
	int fp=*((int*)arg);
	int fp1=*((int*)arg+2);
	pthread_mutex_lock(&mutex);
	lseek(fp,len/2,SEEK_SET);
	lseek(fp1,len/2,SEEK_SET);
	for(int i=len/2;i<len;i++)
	{
		char c;
		read(fp,&c,1);
		write(fp1,&c,1);
	}
	pthread_mutex_unlock(&mutex);
	printf("后半部分完成!\n");
	pthread_exit(NULL);

}

int main(int argc, const char *argv[])
{
	int fp=open("./pic.png",O_RDONLY);
	off_t len =lseek(fp,0,SEEK_END);
	int fp1=open("./copy.png",O_WRONLY|O_CREAT|O_TRUNC,0777);
	int a[3];
	a[0]=fp;
	a[1]=len;
	a[2]=fp1;


	pthread_mutex_init(&mutex,NULL);
	pthread_t tid1,tid2;
	pthread_create(&tid1,NULL,front,(void*)a);
	pthread_create(&tid2,NULL,rear,(void*)a);
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	pthread_mutex_destroy(&mutex);
	close(fp);
	close(fp1);
	return 0;
}

打印完毕!

*/

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

sem_t sem1,sem2,sem3;
pthread_t pth1,pth2,pth3;
void* out1(void* argv)
{
	while(1)
	{
		sem_wait(&sem1);
		printf("1---------->%ld\n",pth1);
		sem_post(&sem2);
	}
}
void* out2(void* argv)
{
	while(1)
	{
		sem_wait(&sem2);
		printf("2---------->%ld\n",pth2);
		sem_post(&sem3);
	}
}
void* out3(void* argv)
{
	while(1)
	{
		sem_wait(&sem3);
		printf("3---------->%ld\n",pth3);
		sem_post(&sem1);
		putchar(10);
	}
}
int main(int argc, const char *argv[])
{
	sem_init(&sem1,0,1);
	sem_init(&sem2,0,0);
	sem_init(&sem3,0,0);
	pthread_create(&pth1,NULL,out1,NULL);
	pthread_create(&pth2,NULL,out2,NULL);
	pthread_create(&pth3,NULL,out3,NULL);
	pthread_join(pth1,NULL);
	sem_destroy(&sem1);
	sem_destroy(&sem2);
	sem_destroy(&sem3);
	return 0;
}
//展示
/*
linux@linux:~/Desktop/demo3/day6$ gcc w3.c -pthread
linux@linux:~/Desktop/demo3/day6$ ./a.out 
1---------->140418605229824
2---------->140418596837120
3---------->140418519922432

1---------->140418605229824
2---------->140418596837120
3---------->140418519922432

1---------->140418605229824
2---------->140418596837120
3---------->140418519922432


*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值