8.27作业

第一题

#include <myhead.h>
int get_len(const char *p1,const char *p2)
{
	int fd1 = open(p1,O_RDONLY);
	if(fd1==-1)
	{
		perror("open1");
		return -1;
	}
	int fd2 = open(p2,O_WRONLY|O_CREAT|O_TRUNC,0664);
	if(fd2==-1)
	{
		perror("open2");
		return -1;
	}
	int len = lseek(fd1,0,SEEK_END);
	close(fd1);
	close(fd2);
	return len;
}

void *fun1(void *ggg)
{
	int len = get_len("./1.txt","./2.txt");
	int fd1,fd2;
	fd1 = open("./1.txt",O_RDONLY);
	if(fd1==-1)
	{
		perror("open1");
		exit(EXIT_SUCCESS);
	}
	fd2 = open("./2.txt",O_WRONLY);
	if(fd2==-1)
	{
		perror("open2");
		exit(EXIT_SUCCESS);
	}
	lseek(fd1,0,SEEK_SET);
	lseek(fd2,0,SEEK_SET);
	char buff[1024];
	int sum = 0;
	while(1)
	{
		int res = read(fd1,buff,sizeof(buff));
		sum+=res;
		if(sum>=len/2 || res==0)
		{
			write(fd2,buff,res-(sum-len/2));
			break;
		}
		write(fd2,buff,res);
	}
	return 0;
	
}
void *fun2(void *ggg)
{
	int len = get_len("./1.txt","./2.txt");
	int fd1,fd2;
	fd1 = open("./1.txt",O_RDONLY);
	if(fd1==-1)
	{
		perror("open1");
		exit(EXIT_SUCCESS);
	}
	fd2 = open("./2.txt",O_WRONLY);
	if(fd2==-1)
	{
		perror("open2");
		exit(EXIT_SUCCESS);
	}
	lseek(fd1,len/2,SEEK_SET);
	lseek(fd2,len/2,SEEK_SET);
	char buff[1024];
	int sum = 0;
	while(1)
	{
		int res = read(fd1,buff,sizeof(buff));
		sum+=res;
		if(sum>=len/2 || res==0)
		{
			write(fd2,buff,res-(sum-(len-len/2)));
			break;
		}
		write(fd2,buff,res);
	}
	return 0;
}
int main(int argc, const char *argv[])
{
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,fun1,NULL)!=0)
	{
		perror("ptcreat1");
		return -1;
	}
	if(pthread_create(&tid2,NULL,fun2,NULL)!=0)
	{
		perror("ptcreat2");
		return -1;
	}
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	return 0;
}

第二题

#include <myhead.h>
sem_t sem1,sem2,sem3,sem4;
void *fun1(void *gg)
{
	while(1)
	{
		sem_wait(&sem1);
		printf("春\t");
		sem_post(&sem2);
	}
}
void *fun2(void *gg)
{
	while(1)
	{
		sem_wait(&sem2);
		printf("夏\t");
		sem_post(&sem3);
	}
}
void *fun3(void *gg)
{
	while(1)
	{
		sem_wait(&sem3);
		printf("秋\t");
		sem_post(&sem4);
	}
}
void *fun4(void *gg)
{
	while(1)
	{
		sem_wait(&sem4);
		printf("冬\t");
		sem_post(&sem1);
	}
}
int main(int argc, const char *argv[])
{
	pthread_t tid1,tid2,tid3,tid4;
	sem_init(&sem1,0,1);
	sem_init(&sem2,0,0);
	sem_init(&sem3,0,0);
	sem_init(&sem4,0,0);
	if(pthread_create(&tid1,NULL,fun1,NULL)!=0)
	{
		perror("ptcreat1");
		return -1;
	}
	if(pthread_create(&tid2,NULL,fun2,NULL)!=0)
	{
		perror("ptcreat2");
		return -1;
	}
	if(pthread_create(&tid3,NULL,fun3,NULL)!=0)
	{
		perror("ptcreat3");
		return -1;
	}
	if(pthread_create(&tid4,NULL,fun4,NULL)!=0)
	{
		perror("ptcreat4");
		return -1;
	}
	sem_destroy(&sem1);
	sem_destroy(&sem2);
	sem_destroy(&sem3);
	sem_destroy(&sem4);
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	pthread_join(tid3,NULL);
	pthread_join(tid4,NULL);
	return 0;
}

联系

一、

#include <myhead.h>
#define MAX 5
#define SIZE 100000
pthread_mutex_t fastmutex;//定义互斥锁
int num = 0;
void *fun1(void *arg)
{
	pthread_mutex_lock(&fastmutex);
	int i;
	for(i=0;i<SIZE;i++)
	{
		num++;
	}
	pthread_mutex_unlock(&fastmutex);
}
int main(int argc, const char *argv[])
{
	pthread_mutex_init(&fastmutex,NULL);//互斥锁初始化
	pthread_t tid[MAX];
	int i;
	for(i=0;i<MAX;i++)
	{		
		if(pthread_create(&tid[i],NULL,fun1,NULL)!=0)
		{
			perror("ptcreate1");
			return -1;
		}
	}

	for(i=0;i<MAX;i++)
	{
		pthread_join(tid[i],NULL);
	}
	pthread_mutex_destroy(&fastmutex);
	printf("预期结果为:500000\n");
	printf("实际结果为:%d\n",num);
	return 0;
}
ubuntu@ubuntu:~/8.27erduo$ 

二、

#include <myhead.h>
#define MAX 10
sem_t fastsem;//无名信号量
void *fun1(void *ggg)
{
	int i;
	for(i=0;i<MAX;i++)
	{
		printf("我制造了一台机器\n");
	}
		sem_post(&fastsem);
}
void *fun2(void *ggg)
{
	sem_wait(&fastsem);
	printf("我买了一台机器\n");
	sem_post(&fastsem);
}
int main(int argc, const char *argv[])
{
	sem_init(&fastsem,0,0);//初始化无名信号量
	pthread_t tid1,tid2[MAX];
	if(pthread_create(&tid1,NULL,fun1,NULL)!=0)
	{
		perror("pthcreate1");
		return -1;
	}
	int i=0;
	for(i=0;i<MAX;i++)
	{
		if(pthread_create(&tid2[i],NULL,fun2,NULL)!=0)
		{
			perror("pthcreate2");
			return -1;
		}
	}

	pthread_join(tid1,NULL);
	for(i=0;i<MAX;i++)
	{
		pthread_join(tid2[i],NULL);
	}
	return 0;
}

三、

#include <myhead.h>
#define MAX 10
pthread_cond_t cond;//定义条件变量
pthread_mutex_t fastmutex;//定义互斥锁
void *fun1(void *ggg)
{
	int i;
	for(i=0;i<MAX;i++)
	{
		usleep(1);
		printf("我制造了一台机器\n");
		pthread_cond_signal(&cond);
	}
	pthread_exit(NULL);
}
void *fun2(void *ggg)
{
	pthread_mutex_lock(&fastmutex);
	pthread_cond_wait(&cond,&fastmutex);
	printf("我买了一台机器\n");
	pthread_mutex_unlock(&fastmutex);
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	pthread_cond_init(&cond,NULL);//初始化条件变量
	pthread_mutex_init(&fastmutex,NULL);
	pthread_t tid1,tid2[MAX];
	if(pthread_create(&tid1,NULL,fun1,NULL)!=0)
	{
		perror("pthcreate1");
		return -1;
	}
	int i=0;
	for(i=0;i<MAX;i++)
	{
		if(pthread_create(&tid2[i],NULL,fun2,NULL)!=0)
		{
			perror("pthcreate2");
			return -1;
		}
	}

	pthread_join(tid1,NULL);
	for(i=0;i<MAX;i++)
	{
		pthread_join(tid2[i],NULL);
	}
	pthread_mutex_destroy(&fastmutex);
	pthread_cond_destroy(&cond);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值