EXTREMEXPLOIT

Processes

Mar 19th, 2021 (edited)
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #define NSTRUCTS 100;
  2. #define NPROCESSES 2;
  3.  
  4. type_def struct num_struct { int n; bool isDone; } NumStruct;
  5. int Process(int n){ sleep(1); return n + 1; }
  6.  
  7. int FD = open("Nums.dat", O_RDWR, 0600);
  8. NumStruct auxStruct1; NumStruct auxStruct2;
  9.  
  10. for (int i=0; i<NPROCESSES; i++){
  11.   int ProcessID = fork();
  12.   if (ProcessID == 0 && i == 0){ // Child 1 Code
  13.     for (int i=0; i<NSTRUCTS; i++){
  14.       lseek(FD, i*sizeof(NumStruct), SEEK_SET);
  15.       read(FD, &auxStruct1, sizeof(NumStruct));
  16.       if (&auxStruct1->isDone) break;
  17.       auxStruct1->n = Process(auxStruct1->n);
  18.       auxStruct1->isDone = true;
  19.       write(FD, &auxStruct1, sizeof(NumStruct));
  20.     }
  21.     exit(0);
  22.   }
  23.   else if (ProcessID == 0 && i == 1){ // Child 2 Code
  24.     for (int i=NSTRUCTS-1; i>=0; i--){
  25.       lseek(FD, i*sizeof(NumStruct), SEEK_SET);
  26.       read(FD, &auxStruct2, sizeof(NumStruct));
  27.       if (&auxStruct2->isDone) break;
  28.       auxStruct2->n = Process(auxStruct2->n);
  29.       auxStruct2->isDone = true;
  30.       write(FD, &auxStruct2, sizeof(NumStruct));
  31.     }
  32.     exit(0);
  33.   }
  34. }
  35.  
  36. // Father Code
  37. while (wait(NULL) > 0);
  38.  
Add Comment
Please, Sign In to add comment