rajeevs1992

Parallel file copy

Jul 19th, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.53 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<sys/types.h>
  4. #include<string.h>
  5. main()
  6. {
  7.     char files[20];
  8.     char destn[20];
  9.     int i=0;
  10.     printf("Parent %d\n",getpid());
  11.     for(i=0;i<3;i++)
  12.     {
  13.         gets(files);
  14.         if(fork()==0)
  15.         {
  16.             sprintf(destn,"dir/%s",files);
  17.             FILE *rp=fopen(files,"r");
  18.             FILE *dp=fopen(destn,"w");
  19.             char ch='1';
  20.             printf("Child %d file:%s\n",getpid(),files);
  21.             while(ch>0)
  22.             {
  23.                 ch=fgetc(rp);
  24.                 fputc(ch,dp);
  25.             }
  26.             fputc(EOF,dp);
  27.             fclose(rp);
  28.             fclose(dp);
  29.             _exit(0);
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment