进程线程。

2023-12-13 20:16:00

1> 使用有名管道,完成两个进程的相互通信

#include <myhead.h>
int main(int argc, const char *argv[])
{
	if(argc!=5)
	{
		puts("输入错误请重新输入");
		return -1;
	}
	pid_t pid=-1;
	pid=fork();
	if(pid>0)
	{
		int wfd=-1;
		if((wfd=open(argv[1],O_WRONLY))==-1)
		{
			perror("open 1 error");
			return -1;
		}	
		int rfd=-1;
		if((rfd=open(argv[3],O_RDONLY))==-1)
		{
			perror("open 3 error");
			return -1;
		}
		char a[10];
		while(1)
		{
			bzero(a,sizeof(a));
			int res=read(rfd,a,sizeof(a));
			if(res<=0)
				break;
			write(wfd,a,res);
		}
		close(wfd);
		close(rfd);
		puts("管道1写入完成");
		wait(NULL);
		puts("管道文件均完成任务");
	}
	else if(pid==0)
	{
		int rfd=-1;
		if((rfd=open(argv[2],O_RDONLY))==-1)
		{
			perror("open 2 error");
			return -1;
		}
		int wfd=-1;
		if((wfd=open(argv[4],O_WRONLY|O_TRUNC))==-1)
		{
			perror("open 4 error");
			return -1;
		}	
		char a[10];
		while(1)
		{
			bzero(a,sizeof(a));
			int res=read(rfd,a,sizeof(a));
			if(res<=0)
				break;
			write(wfd,a,res);
		}
		close(wfd);
		close(rfd);
		puts("管道2读取完成完成");
		exit(0);
	}
	else
	{
		perror("fork error");
		return -1;
	}
	return 0;
}

2> 使用无名管道完成父子进程间的通信

#include <myhead.h>
int main(int argc, const char *argv[])
{
	if(argc!=3)
	{
		puts("请重新输入");
		return -1;
	}
	int pipefd[2]={0};
	if(pipe(pipefd)==-1)
	{
		perror("pipe error");
		return -1;
	}
	printf("p1=%d,p2=%d\n",pipefd[0],pipefd[1]);
	pid_t pid=-1;	
	pid=fork();
	if(pid>0)
	{
		//父进程,向无名管道文件内写入数据
		int fd=-1;
		if((fd=open(argv[1],O_RDONLY))==-1)
		{
			perror("open fd");
			return -1;
		}
		char a[50]="";
		while(1)
		{
			bzero(a,sizeof(a));
			int res=read(fd,a,sizeof(a));
			if(res<=0)
			break;
			write(pipefd[1],a,res);
		}
		close(fd);
		close(pipefd[1]);
		puts("文件输入完成");
		wait(NULL);
		puts("拷贝完成");
	}
	else if(pid==0)
	{
		//子进程,写
		int wfd=-1;
		if((wfd=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC|O_EXCL,0664))==-1)
		{
			if(errno=EEXIST)
			{
				puts("文件已存在,请直接使用");
				wfd=open(argv[2],O_WRONLY|O_TRUNC);
			}
			else
			{
				perror("open wfd");
				return -1;
			}
		}
		char a[60]="";
		while(1)
		{
			bzero(a,sizeof(a));
			int res=read(pipefd[0],a,sizeof(a));
			write(wfd,a,res);
			if(res<sizeof(a))
				break;
		}
		close(wfd);
		close(pipefd[0]);
		puts("文件读取完成");
		exit(EXIT_SUCCESS);
	}
	return 0;
}

3> 使用标准IO完成两个文件的拷贝

#include <myhead.h>
int main(int argc, const char *argv[])
{
	FILE *rfd;
	FILE *wfd;
	if((rfd=fopen("./1.c","r"))==NULL)
	{
		perror("fopen rfd");
		return -1;
	}	
	if((wfd=fopen("./1.txt","w"))==NULL)
	{
		perror("fopen wfd");
		return -1;
	}
	char a[20];
	while(1)
	{
		int res=fread(a,1,sizeof(a),rfd);
		if(res<=0)
			break;
		fwrite(a,1,res,wfd);
	}
	fclose(rfd);
	fclose(wfd);
	puts("拷贝完成");
	return 0;
}

4> 使用文件IO实现两个文件的拷贝

#include <myhead.h>
int main(int argc, const char *argv[])
{
	int rfd=-1;
	int wfd=-1;
	if((rfd=open("./1.c",O_RDONLY))==-1)
	{
		perror("open rfd");
		return -1;
	}
	if((wfd=open("./1.txt",O_WRONLY))==-1)
	{
		perror("open wfd");
		return -1;
	}
	char a[20]="";
	while(1)
	{
		int res=read(rfd,a,sizeof(a));
		if(res<=0)
			break;
		write(wfd,a,res);
	}
	close(rfd);
	close(wfd);
	puts("拷贝完成");
	return 0;
}

5> 使用多进程完成两个文件的拷贝

#include <myhead.h>
int lengh()
{
	int rfd=-1;
	int wfd=-1;
	if((rfd=open("./1.c",O_RDONLY))==-1)
	{
		perror("open rfd");
		return -1;
	}
	if((wfd=open("./1.txt",O_WRONLY|O_CREAT|O_TRUNC,0664))==-1)
	{
		perror("open wfd");
		return -1;
	}
	int len=lseek(rfd,0,SEEK_END);
	return len;
}
int kb(int s,int e)
{
	int rfd=-1;
	int wfd=-1;
	if((rfd=open("./1.c",O_RDONLY))==-1)
	{
		perror("open rfd");
		return -1;
	}
	if((wfd=open("./1.txt",O_WRONLY))==-1)
	{
		perror("open wfd");
		return -1;
	}
	lseek(rfd,s,SEEK_SET);
	lseek(wfd,s,SEEK_SET);
	char a[20];
	int sum=0;
	while(1)
	{
		int res=read(rfd,a,sizeof(a));
		sum+=res;
		if(res<=0||sum>e)
		{
			write(wfd,a,res-(sum-e));
			break;
		}
		write(wfd,a,res);
	}
	close(rfd);
	close(wfd);
}
int main(int argc, const char *argv[])
{
	int len=lengh();
	pid_t pid=-1;
	pid=fork();
	if(pid>0)
	{
		kb(0,len/2);
		puts("父进程拷贝完成");
		wait(NULL);
		puts("拷贝完成");
	}
	else if(pid==0)
	{
		kb(len/2,len-len/2);
		puts("子进程拷贝完成");
		exit(0);
	}
	else
	{
		perror("fork");
		return -1;
	}
	return 0;
}

6> 使用多线程完成两个文件的拷贝

#include <myhead.h>
typedef struct a
{
	int s;
	int e;
}ok;
int lengh()
{
	int rfd=-1;
	int wfd=-1;
	if((rfd=open("./1.c",O_RDONLY))==-1)
	{
		perror("open rfd");
		return -1;
	}
	if((wfd=open("./1.txt",O_WRONLY|O_CREAT|O_TRUNC,0664))==-1)
	{
		perror("open wfd");
		return -1;
	}
	int len=lseek(rfd,0,SEEK_END);
	return len;
}
int kb(int s,int e)
{
	int rfd=-1;
	int wfd=-1;
	if((rfd=open("./1.c",O_RDONLY))==-1)
	{
		perror("open rfd");
		return -1;
	}
	if((wfd=open("./1.txt",O_WRONLY))==-1)
	{
		perror("open wfd");
		return -1;
	}
	lseek(rfd,s,SEEK_SET);
	lseek(wfd,s,SEEK_SET);
	char a[20];
	int sum=0;
	while(1)
	{
		int res=read(rfd,a,sizeof(a));
		sum+=res;
		if(res<=0||sum>e)
		{
			write(wfd,a,res-(sum-e));
			break;
		}
		write(wfd,a,res);
	}
	close(rfd);
	close(wfd);
}
void *task(void *arg)
{
	 ok a=*(ok*)arg;	
	kb(a.s,a.e);
	puts("子线程拷贝完成");
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	int len=lengh();
	ok f1={0,len/2};
	ok f2={len/2,len-len/2};
	pthread_t tid1=-1;
	pthread_create(&tid1,NULL,task,&f1);	
	pthread_t tid2=-1;
	pthread_create(&tid2,NULL,task,&f2);
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	puts("拷贝完成");
	return 0;
}

7> 将互斥锁的案例重新写一遍


#include <myhead.h>
 
pthread_cond_t cond;
pthread_mutex_t mutex;
 
void *task1(void *arg)
{
	int n=5;
	while(n--)
	{
		sleep(1);
		printf("sum+1\n");
		pthread_cond_signal(&cond);
	}
	printf("结束\n");
	pthread_exit(NULL);
}
void *task2(void *arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
		pthread_cond_wait(&cond,&mutex);
 
		printf("sum-1\n");
		
		pthread_mutex_unlock(&mutex);
		pthread_exit(NULL);
	}
}
 
int main(int argc, const char *argv[])
{
			
	pthread_t tid1,tid2,tid3,tid4,tid5,tid6;
 
	pthread_cond_init(&cond,NULL);
	pthread_mutex_init(&mutex,NULL);
 
	if(pthread_create(&tid1,NULL,task1,NULL)!=0)
	{
		printf("error\n");
		return -1;
	}
	if(pthread_create(&tid2,NULL,task2,NULL)!=0)
	{
		printf("error\n");
		return -1;
	}
	if(pthread_create(&tid3,NULL,task2,NULL)!=0)
	{
		printf("error\n");
		return -1;
	}
	if(pthread_create(&tid4,NULL,task2,NULL)!=0)
	{
		printf("error\n");
		return -1;
	}
	if(pthread_create(&tid5,NULL,task2,NULL)!=0)
	{
		printf("error\n");
		return -1;
	}
	if(pthread_create(&tid6,NULL,task2,NULL)!=0)
	{
		printf("error\n");
		return -1;
	}
	
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	pthread_join(tid3,NULL);
	pthread_join(tid4,NULL);
	pthread_join(tid5,NULL);
	pthread_join(tid6,NULL);
 
	pthread_cond_destroy(&cond);
	pthread_mutex_destroy(&mutex);
	
 
	return 0
}

8> 将无名信号量实现生产者消费者程序重新实现一遍

#include <myhead.h>
 
sem_t sem;
 
void *task1(void *arg)
{
	while(1)
	{
		sleep(1);
		printf("sum+1\n");
		sem_post(&sem);
	}
 
 
}
void *task2(void *arg)
{
	while(1)
	{
		sem_wait(&sem);
		printf("sum-1\n");
	}
 
}
int main(int argc, const char *argv[])
{
 
	pthread_t tid1,tid2;
	sem_init(&sem,0,0);
	if(pthread_create(&tid1,NULL,task1,NULL)!=0)
	{
		printf("error\n");
		return -1;
	}
	if(pthread_create(&tid2,NULL,task2,NULL)!=0)
	{
		printf("error\n");
		return -1;
	}
 
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	
	sem_destroy(&sem);
	return 0;
}

9> 将条件变量实现生产者消费者程序重新实现一遍

#include <myhead.h>
 
int main(int argc, const char *argv[])
{
	int pipefd[2];
	if(pipe(pipefd)==-1)
	{
		perror("pipe");
		return -1;
	}
 
 
	pid_t pid=fork();
	if(pid==0)//子进程
	{
		close(pipefd[1]);
		
		char rbuf[128]="";
		while(1)
		{
			bzero(rbuf,sizeof(rbuf));
			read(pipefd[0],rbuf,sizeof(rbuf));
			printf("子进程%s\n",rbuf);
		
			if(strcmp(rbuf,"quit")==0)
				break;
		}
		close(pipefd[0]);
		exit(EXIT_SUCCESS);
 
	}
	else if(pid>0)//父进程
	{
	
		close(pipefd[0]);
		
		char buf[128]="";
		while(1)
		{
			fgets(buf,sizeof(buf),stdin);
			buf[strlen(buf)-1]='\0';
			write(pipefd[1],buf,sizeof(buf));
			
			if(strcmp(buf,"quit")==0)
				break;
		}
		close(pipefd[1]);
	}
	else
	{
		printf("error\n");
		return -1;
	}
	wait(NULL);
	return 0;
}

文章来源:https://blog.csdn.net/m0_64059184/article/details/134894511
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。