IO进程线程day4

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
void file_copy(const char* rfile,const char* wfile){
	int res=-2;
	int rfd=open(rfile,O_RDONLY);
		if(rfd==-1){
			printf("读取的文件不存在\n");
			return;
		}
	int wfd=open(wfile,O_WRONLY|O_CREAT|O_TRUNC,0664);
	if(wfd==-1){return;}
	while(1){
		char data[2]={0};
		res=read(rfd,data,2);
		if(res==0){break;}
		write(wfd,data,2);
	}
	close(rfd);
	close(wfd);
int main(int argc, const char *argv[])
{
	struct stat buf={0};
	stat(argv[1],&buf);
	mode_t mode=buf.st_mode;
	if(S_ISDIR(mode)){
		mkdir(argv[2],0755);
		DIR* dp=opendir(argv[1]);
		while(1);{
			struct dirent* res=readdir(dp);
			if(res==NULL){break;}
			char rfile[64]={0};
			strcpy(rfile,argv[1]);
 			char wfile[64]={0};
			strcpy(wfile,argv[2]);
	strcpy(rfile,"/");
	strcpy(rfile,res->d_name);
	strcpy(wfile,"/");
	strcpy(wfile,res->d_name);
if(	strcpy(res->d_name,".")&&strcpy(res->d_name,"..")!=0);
		}
	}
	close(dp);
}else{
	file_copy(argv[1],argv[2]);
}

	return 0;
}   
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
	for(int i=1;i<100;i++)
	{
		int res=fork();
		if(res<0)
		{
			return 1;
		}
		else if(res>0)
		{
			printf("父进程\n");
		}
		else if(res==0)
		{
			printf("子进程\n");
			break;
		}
	}


	return 0;
}   

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值