1.拷贝目录文件 2.打开100长度进程链

1拷贝目录


注:

1argv[2]后要加/ 

2拷贝不了目录里的目录

int cpy_file(char* file,struct dirent*dir,const char*p,const char*q){
	char buf[128]="./";
	strcat(buf,p);
	strcat(buf,file);
	int fd	= open(buf,O_WRONLY|O_CREAT,0664);
	int fd2=open(file,O_RDONLY);
	char c[2] ;
	while(1){
		int res=read(fd2,c,1);
		if(res<0){
			perror("read");
			return 0;
		}else if(res==0)break;
		write(fd,c,1);
		bzero(c,sizeof(c));

	}
	close(fd);

}
int main(int argc, const char *argv[])
{
	DIR *DP=opendir(argv[1]);
	if(NULL==DP){
		perror("opendir");
		return -1;
	}

	mkdir(argv[2],0777);
	while(1){
		struct dirent *dir=readdir(DP);
		if(NULL==dir){
			if(0==errno){
				break;
			}else{
				perror("readdir");
				return -1;
			}
		}
		if(*(dir->d_name)=='.'){
			continue;
		}
		cpy_file(dir->d_name,dir,argv[2],argv[1]);
	}
	if(closedir(DP)!=0){
		perror("closedir");
		return -1;
	}
}

2 创建进程链

int main(int argc, const char *argv[])
{
	int count=1;
	pid_t pid = fork();
	while(count<100){
		if(pid>0)
			while(1)sleep(1);
		else if(pid==0){
			pid=fork();
			count++;
		}
	}
	while(1)sleep(1);
	return 0;
}

结果:

  4838   5326   5326   4838 pts/2      5326 S+    1000   0:00 vim fork_line.c
  2714   5346   5346   5346 pts/3      5363 Ss    1000   0:00 /bin/bash
  5346   5363   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5363   5364   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5364   5365   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5365   5366   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5366   5367   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5367   5368   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5368   5369   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5369   5370   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5370   5371   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5371   5372   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5372   5373   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5373   5374   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5374   5375   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5375   5376   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5376   5377   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5377   5378   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5378   5379   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5379   5380   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5380   5381   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5381   5382   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5382   5383   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5383   5384   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5384   5385   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5385   5386   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5386   5387   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5387   5388   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5388   5389   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5389   5390   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5390   5391   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5391   5392   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5392   5393   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5393   5394   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5394   5395   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5395   5396   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5396   5397   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5397   5398   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5398   5399   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5399   5400   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5400   5401   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5401   5402   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5402   5403   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5403   5404   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5404   5405   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5405   5406   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5406   5407   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5407   5408   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5408   5409   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5409   5410   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5410   5411   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5411   5412   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5412   5413   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5413   5414   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5414   5415   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5415   5416   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5416   5417   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5417   5418   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5418   5419   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5419   5420   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5420   5421   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5421   5422   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5422   5423   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5423   5424   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5424   5425   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5425   5426   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5426   5427   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5427   5428   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5428   5429   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5429   5430   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5430   5431   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5431   5432   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5432   5433   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5433   5434   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5434   5435   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5435   5436   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5436   5437   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5437   5438   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5438   5439   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5439   5440   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5440   5441   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5441   5442   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5442   5443   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5443   5444   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5444   5445   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5445   5446   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5446   5447   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5447   5448   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5448   5449   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5449   5450   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5450   5451   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5451   5452   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5452   5453   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5453   5454   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5454   5455   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5455   5456   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5456   5457   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5457   5458   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5458   5459   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5459   5460   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5460   5461   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5461   5462   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  5462   5463   5363   5346 pts/3      5363 S+    1000   0:00 ./a.out
  2714   5464   5464   5464 pts/4      5489 Ss    1000   0:00 /bin/bash
  5464   5489   5489   5464 pts/4      5489 R+    1000   0:00 ps -ajx

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值