C文件的拷贝,创建100个进程

文件的拷贝

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>

int main(int argc ,const char * argv[])
{
    
//    DIR * dir = opendir("/home/killer/桌面/TXT");
    DIR * dir = opendir(argv[1]);
    if(dir == NULL)
    {
        perror("C");
        return 0;
    }
    
    struct dirent *file;

      char path[256]={0};
      strcat(path,argv[2]);

      char filename[256]="mkdir ";
      strcat(filename,path);
      system(filename);

    while((file = readdir(dir)))
    {
        int rfd = open(file->d_name,O_RDONLY);
        //printf("%s\n",file->d_name);
        //char path[256]="/home/killer/桌面/dest/";
        char path1[256] = {0};
        strcat(path1,path);
        strcat(path1,file->d_name);
       // printf("%s\n",path);
        int wfd = open(path,O_WRONLY | O_TRUNC | O_CREAT,0666);

        char buf[1024]={0};
        while(read(rfd,buf,1023))
            write(wfd,buf,1023);
    }

    
}

创建100个进程
 

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    pid_t pid;  
    int i=0;  
 
    for(i=0;i<99;i++)
    {
        pid = fork();

        if(pid==-1)
        {

            perror("");
            
        }
        else if (pid==0)
        {
            break;
        }
    }

    if(pid > 0)
    {
        printf("进程%d\n",i);
    }
    else if (pid == 0)
    {
        printf("hello%d\n",i);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值