线程操作(二)

题目一:有四个线程1、2、3、4,分别为4个文件A、B、C、D写入字符,
其中,
线程1每隔200毫秒才能写入一个字符,
线程2每隔400毫秒才能写入一个字符,
线程3每隔600毫秒才能写入一个字符,
线程4每隔800毫秒才能写入一个字符。
4个线程同时工作,模拟云存储写入功能,将从终端输入字符串信息分别存到ABCD四个文件,要求每个字符只能被存储到一个文件,全部信息存储完毕后要给出相应提示。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
FILE *fp1 = NULL,*fp2 = NULL,*fp3 = NULL,*fp4 = NULL;
//char c = ‘a’;
char c;
int i =0;
void *thread_func0()
{
printf(“生产者\n”);
while(1)
{
usleep(100000);//不知道为什么非要加延迟,不然不准确,
printf(“进锁前0\n”);
pthread_mutex_lock(&lock);
printf(“进锁0\n”);
//c = c + 1;
c = getchar();
if(c == ‘\n’)
{
printf(“finish\n”);
exit(1);
}
printf(“生产者:%c\n”,c);
i++;
pthread_mutex_unlock(&lock);
printf(“出锁0\n”);
pthread_cond_signal(&cond);
printf("-----------------------生产者---------------------------\n");
}

printf("finnish!\n");	   	

}
void *thread_func1()
{
printf(“消费者1\n”);

    fp1 = fopen("A", "w");

if(fp1 == NULL)
{	
	printf("erro\n");
	exit(1);
}       
  	while(1)
{       
	usleep(200000); 
	printf("进锁前1\n");
    		pthread_mutex_lock(&lock);
	printf("进锁1\n");
	while(i == 0)
	{
		printf("消费者1进入阻塞\n");
		pthread_cond_wait(&cond,&lock);
		printf("消费者1出阻塞\n");
	}
	printf("消费者1继续运行\n");
	i--;
	printf("A:%c\n",c);
	fputc(c,fp1);
    		fflush(fp1);
	pthread_mutex_unlock(&lock);
	printf("出锁1\n");
	printf("-----------------------消费者1---------------------------\n");
}

}

void *thread_func2()
{
printf(“消费者2\n”);
fp2 = fopen(“B”, “w”);
//sleep(400);
while(1)
{
usleep(400000);
printf(“进锁前2\n”);
pthread_mutex_lock(&lock);
printf(“进锁2\n”);
while(i == 0)
{
printf(“消费者2进入阻塞\n”);
pthread_cond_wait(&cond,&lock);
printf(“消费者2出阻塞\n”);
}
printf(“消费者2继续运行\n”);
i --;
printf(“B:%c\n”,c);
fputc(c,fp2);
fflush(fp2);
pthread_mutex_unlock(&lock);
printf(“出锁2\n”);
printf("-----------------------消费者2---------------------------\n");
}
}
void *thread_func3()
{
printf(“消费者3\n”);
fp3 = fopen(“C”, “w”);
//sleep(600);
while(1)
{
usleep(600000);
printf(“进锁前3\n”);
pthread_mutex_lock(&lock);
printf(“进锁3\n”);
while(i == 0)
{
printf(“消费者3进入阻塞\n”);
pthread_cond_wait(&cond,&lock);
printf(“消费者3出阻塞\n”);
}
printf(“消费者3继续运行\n”);
i --;
printf(“C:%c\n”,c);
fputc(c,fp3);
fflush(fp3);
pthread_mutex_unlock(&lock);
printf(“出锁3\n”);
printf("-----------------------消费者3---------------------------\n");
}
}

void *thread_func4()
{
printf(“消费者4\n”);

fp4 = fopen("D", "w");
//sleep(800);
while(1)
{	
	usleep(800000);
	printf("进锁前4\n");
	pthread_mutex_lock(&lock);
	printf("进锁4\n");
	while(i == 0)    
    		{	
		printf("消费者4进入阻塞\n");
            		pthread_cond_wait(&cond,&lock);
		printf("消费者4出阻塞\n");
    		}
	printf("消费者4继续运行\n");
	i --;
	printf("D:%c\n",c);
	fputc(c,fp4);
	fflush(fp4);
	pthread_mutex_unlock(&lock);
	printf("出锁4\n");
	printf("-----------------------消费者4---------------------------\n"); 
}

}
int main()
{
pthread_t tid[4];

int j;
    	pthread_create(&tid[0],NULL,thread_func0,NULL);
pthread_create(&tid[1],NULL,thread_func1,NULL);
pthread_create(&tid[2],NULL,thread_func2,NULL);
pthread_create(&tid[3],NULL,thread_func3,NULL);
pthread_create(&tid[4],NULL,thread_func4,NULL);


for(j = 0; j < 5;j++)
{
	pthread_join(tid[j],NULL);
	exit(-1);
}
   	 fclose(fp1);
   	 fclose(fp2);
 fclose(fp3);
     fclose(fp4);
   	 return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值