Advertisement
cforgeron

Saturate.c

Nov 4th, 2012
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.03 KB | None | 0 0
  1. # Saturate.c
  2.  
  3. #define _FILE_OFFSET_BITS 64  /* enable large file support  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9.  
  10. float nCM=0; /* count of 100's of megs we've written */
  11.  
  12.  
  13. void finish(const char *where) {
  14.         printf("%s: wrote %.1f gig file\n",where,0.1*nCM);
  15.         exit(0);
  16. }
  17.  
  18. void hitError(const char *where) { perror(where); finish(where); }
  19.  
  20. void writer(int id)
  21. {
  22.         char c=0; /* The byte we're writing */
  23.         int cm=100*1024*1024;
  24.         FILE *f[50];
  25.  
  26.         int     r;
  27.         int     block[262114];
  28.         int     i;
  29.         char    fName[40];
  30.         int     numFiles=4;
  31.  
  32.         printf("This is the %i writer, going to create %i files.\nOpened : ", id, numFiles);
  33.  
  34.         for(i=0; i<numFiles; i++)
  35.         {
  36.  
  37.         sprintf(fName, "test.%i\0", i);
  38.  
  39.         f[i]=fopen(fName,"wb");
  40.  
  41.         if (f[i]==NULL) hitError("Error in fopen of ");
  42.         printf("%s ", fName);
  43.         }
  44.         printf("\n");
  45.  
  46.  
  47.         while (nCM<10000) {
  48.  
  49.                 for(i=0; i < numFiles; i++)
  50.                         r=fwrite(&block, 1048576, 1, f[i]);
  51.                 nCM++;
  52.         }
  53. }
  54.  
  55.  
  56. int main(int argc, char *argv[])
  57. {
  58.         int i=0;
  59.         int maxProc=12;
  60.         pid_t pid[50];
  61.  
  62.         for(i = 0; i < maxProc; ++i) {
  63.             pid[i] = fork();
  64.             if (pid[i] == -1) {
  65.                   exit(-1); /* error */
  66.                 } else if (pid[i] == 0) {
  67.        
  68.                 /* child */
  69.  
  70.                 writer(i);
  71.                 waitpid(pid[i], 0, 0);
  72.                 break;
  73.                  } else {
  74.   //                    printf("doing parent things\n");
  75.                 }
  76.         }
  77.  
  78. // Wait for PIDs to finish
  79.  
  80. printf("All done spawning, lets wait for them to finish.\n");
  81.       for(i=0; i<maxProc; i++)
  82.       {
  83.       printf("#%i ,", pid[i]);
  84.       waitpid(pid[i],0,0);
  85.       }
  86.  
  87.         waitpid(pid[i],0,0);
  88.     printf("Last PID done.");
  89.  
  90.      writer();
  91.      finish("Done");
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement