Advertisement
SMASIF

Mandelbrot 02

Nov 4th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <string.h>
  7. #include <pthread.h>
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <errno.h>
  13. #include <signal.h>
  14. #include <time.h>
  15. #include <unistd.h>
  16.  
  17.  
  18. int counter = 0;
  19. float scale = 2.00000;
  20. int main(int argc, char* argv[]) {
  21.  
  22.     int forkCount = atoi(argv[1]);
  23.  
  24.     printf("Fork Count: %d\n", forkCount);
  25.  
  26.  
  27.     pid_t* processes = malloc(forkCount * sizeof(pid_t));
  28.     pid_t pid; // Parent process id
  29.     int status;
  30.  
  31.     float step = 0.0399999;
  32.  
  33.     counter = 0;
  34.  
  35.  
  36.     char* command[] = {"./mandel", "-x 0.135", "-y 0.60", "-W 900", "-H 900", "-m 1000", "-s 0.000005", "", ""};
  37.  
  38.  
  39.     char* commands[100];
  40.  
  41.  
  42.     commands[0] = "./mandel";
  43.     commands[1] = "-x 0.135";
  44.     commands[2] = "-y 0.60";
  45.     commands[3] = "-W 900";
  46.     commands[4] = "-H 900";
  47.     commands[5] = "-m 1000";
  48.     commands[6] = "-s 0.000005";
  49.     commands[7] = malloc(100 * sizeof(char));
  50.  
  51.     printf("Segging here?\n");
  52.     sprintf(commands[7], "-o mandel%d.bmp", counter);
  53.  
  54.  
  55.     printf("No? Maybe at the forking shit then?\n");
  56.     pid = fork();
  57.  
  58.     if(pid >= 0) {
  59.         if(pid == 0) {
  60.             printf("FORK SUCCESSFUL! YEAH!\n");
  61.             execvp(commands[0], commands);
  62.             counter++;
  63.         }
  64.  
  65.         else {
  66.             pid_t result = wait(&status);
  67.  
  68.             if(result == -1) {
  69.                 printf("mandelmovie: wait error %s\n", strerror(errno));
  70.                 exit(1);
  71.             }
  72.  
  73.             else {
  74.                 if(status == 0) {
  75.                     printf("mandelmovie: process exited normally with status %d\n", WEXITSTATUS(status));
  76.                 }
  77.  
  78.                 else {
  79.                     printf("mandelmovie: process exited abnormally with status %d\n", status);
  80.                 }
  81.             }
  82.         }
  83.     }
  84.  
  85.     else {
  86.         perror("Forking failed");
  87.         exit(1);
  88.     }
  89.  
  90.     int i = 0;
  91.     int forkCounter = 0;
  92.     int imgCounter = 0;
  93.  
  94.     printf("Before new Forking?\n");
  95.  
  96.     while(counter < 50) {
  97.         if(forkCounter <= forkCount) {
  98.             pid = fork();
  99.  
  100.             if(pid >= 0) {
  101.                 if(pid == 0) {
  102.                     forkCounter++;
  103.                     counter++;
  104.                     scale -= step;
  105.                     printf("Counter %d and the scale %f\n", counter, scale);
  106.                     printf("mandelmovie2: process %d started\n", getpid());
  107.                     sprintf(commands[6], "-s %f", scale);
  108.                     sprintf(commands[7], "-o mandel%d.bmp", counter);
  109.                     execvp(commands[0], commands);
  110.  
  111.                 }
  112.             }
  113.  
  114.             else {
  115.                 perror("Forking error!");
  116.                 exit(1);
  117.             }
  118.         }
  119.  
  120.         else {
  121.             pid_t result = wait(&status);
  122.  
  123.             if(result == -1) {
  124.                 printf("mandelmovie: wait error %s\n", strerror(errno));
  125.                 exit(1);           
  126.             }
  127.  
  128.             else {
  129.                 if(status == 0) {
  130.                     printf("mandelmovie: process %d exited normally with status %d\n", result, WEXITSTATUS(status));
  131.                 }
  132.  
  133.                 else {
  134.                     printf("mandelmovie: process %d exited abnormally with status %d\n", result, status);
  135.                 }
  136.                 forkCounter--;
  137.             }
  138.         }
  139.  
  140.  
  141.     }
  142.    
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement