6.13作业

用线程完成图片拷贝,要求一个线程拷贝一半,另一个线程拷贝另一半。

#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
pthread_t fz1,fz2;
int d;
int x;
off_t dx;
char cc;
ssize_t ds;
pthread_mutex_t hcs = PTHREAD_MUTEX_INITIALIZER;
void* aaa(void* ab){
	pthread_mutex_lock(&hcs);
	lseek(d,dx/2,SEEK_SET);
	lseek(x,dx/2,SEEK_SET);
	for(int i=0;i<dx/2;i++){
		read(d,&cc,1);
		write(x,&cc,1);
	}
	printf("后半段完毕\n");
	pthread_mutex_unlock(&hcs);
	pthread_exit(NULL);
}
void* bbb(void* ab){
	pthread_mutex_lock(&hcs);
	lseek(d,0,SEEK_SET);
	lseek(x,0,SEEK_SET);
	for(int i=0;i<dx/2;i++){
		read(d,&cc,1);
		write(x,&cc,1);
	}
	printf("前半段完毕\n");
	pthread_mutex_unlock(&hcs);
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	struct stat buf;
	stat("./1.png",&buf);
	dx=buf.st_size;
	d=open("./1.png",O_RDONLY);
	x=open("./cp.png",O_WRONLY | O_CREAT | O_TRUNC,0777);

	pthread_create(&fz1,NULL,aaa,NULL);
	pthread_create(&fz2,NULL,bbb,NULL);
	pthread_join(fz1,NULL);
	pthread_join(fz2,NULL);
	pthread_cancel(fz1);
	pthread_cancel(fz2);
	close(d);
	close(x);
	return 0;
}

创建编号为ABC三个线程,三个线程循环打印自己的编号,要求打印出来的结果必须是ABC;

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
char buf[]="1234567";
pthread_cond_t tjbl1,tjbl2,tjbl3;
pthread_mutex_t hcs;
int tj=0;
void* a(void* arg){
	while(1){
		pthread_mutex_lock((pthread_mutex_t*)arg);
		if(tj!=0){
			pthread_cond_wait(&tjbl1,&hcs);
		}
		printf("A");
		tj=1;//修改访问时机
		pthread_cond_signal(&tjbl2);
		pthread_mutex_unlock((pthread_mutex_t*)arg);
	}
	pthread_exit(NULL);
	return NULL;
}
void* b(void* arg){
	while(1){
		pthread_mutex_lock((pthread_mutex_t*)arg);
		if(tj!=1){
			pthread_cond_wait(&tjbl2,&hcs);
		}
		printf("B");		
			tj=2;
		pthread_cond_signal(&tjbl3);
		pthread_mutex_unlock((pthread_mutex_t*)arg);

	}
	pthread_exit(NULL);
}
void* c(void* arg){
	while(1){
		pthread_mutex_lock((pthread_mutex_t*)arg);
		if(tj!=2){
			pthread_cond_wait(&tjbl3,&hcs);
		}
		printf("C\n");		
			tj=0;
		pthread_cond_signal(&tjbl1);
		pthread_mutex_unlock((pthread_mutex_t*)arg);

	}
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	//创建一个线程
	pthread_t tid,tid2;

	//创建互斥锁

	pthread_mutex_init(&hcs,NULL);
	//创建条件变量
	pthread_cond_init(&tjbl1,NULL);
	pthread_cond_init(&tjbl2,NULL);
	pthread_cond_init(&tjbl3,NULL);
	if(pthread_create(&tid,NULL,a,(void*)&hcs)!=0){
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}
	if(pthread_create(&tid,NULL,b,(void*)&hcs)!=0){
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}

	if(pthread_create(&tid2,NULL,c,(void*)&hcs)!=0){
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}
	pthread_join(tid,NULL);
	return 0;
}

无名管道

#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
int main(int argc, const char *argv[])
{
	int pfd[2];
	if(pipe(pfd)<0){
		perror("pipe");
		return -1;
	}
	pid_t pid=fork();
	if(pid>0){//父进程为真
		char buf[128];
		while(1){
			scanf("%s",buf);
			getchar();
			if(write(pfd[1],buf,sizeof(buf))<0){
				perror("write");
				return-1;
			}
			printf("写入\n");
			break;
		}
		wait(NULL);
	}else if(0==pid){
		char buf[128]="";
		ssize_t res;
		while(1){
			res=read(pfd[0],buf,sizeof(buf));
			if(res<0){
				perror("read");
				return-1;
			}
			printf("res=%ld  buf=%s\n",res,buf);
			if(strcmp(buf,"quit")==0){
				break;
			}
		}
	}else{
		perror("fork");
		return-1;
	}

	close(pfd[0]);
	close(pfd[1]);
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值