IO线程第五天作业

  用两个线程复印图片,一个线程拷贝一半  
     1	#include <stdio.h>
     2	#include <unistd.h>
     3	#include <pthread.h>
     4	#include <sys/types.h>
     5	#include <sys/stat.h>
     6	#include <fcntl.h>
     7	pthread_mutex_t mutex;
     8	void *callback(void *arg)
     9	{
    10		pthread_mutex_lock(&mutex);
    11		int fpr = open("./01_燕双鹰.jpg",O_RDONLY);
    12		if(fpr < 0)
    13		{
    14			perror("open");
    15			return NULL;
    16		}
    17		
    18		int fqw = open("./05_复制燕双鹰.jpg",O_WRONLY|O_CREAT|O_APPEND,0777);
    19		if(fqw < 0)
    20		{
    21			perror("open");
    22			return NULL;
    23		}
    24		char arr;	
    25		off_t offset = lseek(fpr,0,SEEK_END);
    26		lseek(fpr,offset/2,SEEK_SET);
    27		lseek(fqw,offset/2,SEEK_SET);
    28		int res1 = offset/2;
    29		while(res1 > 0)
    30		{
    31			read(fpr,&arr,1);
    32			write(fqw,&arr,1);
    33			res1--;
    34		}
    35		close(fpr);
    36		close(fqw);
    37	
    38		pthread_mutex_unlock(&mutex);
    39	
    40	}
    41	void *callback2(void *arg)
    42	{
    43		
    44		pthread_mutex_lock(&mutex);
    45		int fp = open("./01_燕双鹰.jpg",O_RDONLY);
    46		if(fp < 0)
    47		{
    48			perror("open");
    49			return NULL;
    50		}
    51		int fq = open("./05_复制燕双鹰.jpg",O_WRONLY|O_CREAT|O_TRUNC,0777);
    52		if(fq < 0)
    53		{
    54			perror("open");
    55			return NULL;
    56		}
    57		char arr;
    58		off_t offset = lseek(fp,0,SEEK_END);
    59		lseek(fp,0,SEEK_SET);
    60		lseek(fq,0,SEEK_SET);
    61		int res = offset/2;
    62		while(res > 0)
    63		{
    64			read(fp,&arr,1);
    65			write(fq,&arr,1);
    66			res--;
    67		}
    68		close(fp);
    69		close(fq);
    70		pthread_mutex_unlock(&mutex);
    71	}
    72	int main(int argc, const char *argv[])
    73	{
    74		pthread_mutex_init(&mutex,NULL);
    75		pthread_t tid1,tid2;
    76		if(pthread_create(&tid1,NULL,callback,NULL) != 0)
    77		{
    78			fprintf(stderr,"pthread_creat failed __%d__\n",__LINE__);
    79			return -1;
    80		}
    81		if(pthread_create(&tid2,NULL,callback2,NULL) != 0)
    82		{
    83	
    84			fprintf(stderr,"pthread_creat failed __%d__\n",__LINE__);
    85			return -1;
    86		}
    87		pthread_join(tid1,NULL);
    88		pthread_join(tid2,NULL);
    89		pthread_mutex_destroy(&mutex);
    90		return 0;
    91	}
用信号量来拷贝文件中的数据到终端
     1	#include <stdio.h>
     2	#include <semaphore.h>
     3	#include <pthread.h>
     4	#include <sys/types.h>
     5	#include <sys/stat.h>
     6	#include <fcntl.h>
     7	#include <errno.h>
     8	sem_t sem1;
     9	sem_t sem2; 
    10	char buf[128];
    11	int res = 0;
    12	void *callback1(void *agr)
    13	{
    14		FILE* fpr = fopen("./02_传递字符串给主线程.c","r");
    15		if(fpr == NULL)
    16		{
    17			perror("open");
    18			return NULL;
    19		}
    20		while(1)
    21		{
    22	
    23			if(sem_wait(&sem2) < 0)
    24			{
    25				perror("sem_wait");
    26				pthread_exit(NULL);
    27			}
    28			res = fread(buf,1,sizeof(buf),fpr);
    29			if(sem_post(&sem1) < 0)
    30			{
    31				perror("sem_post");
    32				pthread_exit(NULL);
    33			}
    34			if(res == 0)
    35			{
    36				break;
    37			}
    38		}
    39		pthread_exit(NULL);
    40	}
    41	void *callback2(void *agr)
    42	{
    43		while(1)
    44		{
    45			if(sem_wait(&sem1) < 0)
    46			{
    47				perror("sem_wait");
    48				pthread_exit(NULL);
    49			}
    50			fwrite(buf,res,1,stdout);
    51			if(sem_post(&sem2) < 0)
    52			{
    53				perror("sem_post");
    54				pthread_exit(NULL);
    55			}
    56			if(res == 0)
    57			{
    58				break;
    59			}
    60		}
    61		pthread_exit(NULL);
    62	}
    63	
    64	int main(int argc, const char *argv[])
    65	{
    66		if(sem_init(&sem1,0,0) < 0)
    67		{
    68			perror("sem_init");
    69			return -1;
    70		}
    71		if(sem_init(&sem2,0,1) < 0)
    72		{
    73			perror("sem_init");
    74			return -1;
    75		}
    76		pthread_t tid1,tid2;
    77		if(pthread_create(&tid1,NULL,callback1,NULL) != 0)
    78		{
    79			fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
    80			return -1;
    81		}
    82		if(pthread_create(&tid2,NULL,callback2,NULL) != 0)
    83		{
    84			fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
    85			return -1;
    86		}
    87		pthread_join(tid1,NULL);
    88		pthread_join(tid2,NULL);
    89		sem_destroy(&sem1);
    90		sem_destroy(&sem2);
    91		return 0;
    92	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值