Advertisement
Guest User

Untitled

a guest
Feb 9th, 2012
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/wait.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <time.h>
  8. #include <malloc.h>
  9.  
  10. int minValue = 0;
  11. int maxValue = 0;
  12.  
  13. void findMinVal(int *tmpArray, int size)
  14. {
  15.     int tmp = tmpArray[0];
  16.     int i;
  17.     for(i=1;i<size;i++)
  18.     {
  19.         if(tmp > tmpArray[i])
  20.         {
  21.             tmp = tmpArray[i];
  22.         }
  23.     }
  24.     minValue = tmp;
  25. }
  26.  
  27. void findMaxVal(int *tmpArray, int size)
  28. {
  29.     int tmp = tmpArray[0];
  30.     int i;
  31.     for(i=1;i<size;i++)
  32.     {
  33.         if(tmp < tmpArray[i])
  34.         {
  35.             tmp = tmpArray[i];
  36.         }
  37.     }
  38.     maxValue = tmp;
  39. }
  40.  
  41. int main(void)
  42. {
  43.     int *array;
  44.     int cnt = 10 ; //wielkosc tablicy
  45.     int i;
  46.     srand(time(0));
  47.  
  48.     //#+
  49.     array = (int*)malloc(cnt * sizeof(int));
  50.    
  51.     for(i=0; i<cnt; i++)
  52.     {
  53.         array[i] = (rand() % 10) + 1;  
  54.     }  
  55.     //#-
  56.  
  57.     int status;
  58.     pid_t child1 = fork();
  59.     pid_t child2 = fork();
  60.  
  61.     if (child1 > 0 && child2 > 0)
  62.     {   printf("PARENT: my pid is %d\n", getpid());
  63.  
  64.         waitpid(child1,&status,0);
  65.         waitpid(child2,&status,0);
  66.    
  67.     }
  68.  
  69.     if (child1 == 0)
  70.     {
  71.         printf("Child1: my pid is %d\n", getpid());
  72.         findMinVal(array,cnt);
  73.         exit(0);
  74.     }
  75.    
  76.     if (child2 == 0)
  77.     {
  78.         printf("Child2: my pid is %d\n", getpid());
  79.         findMaxVal(array,cnt);
  80.         exit(0);
  81.     }
  82.  
  83.    
  84.     printf("%d\n", minValue);
  85.     printf("%d\n", maxValue);
  86.    
  87.     free(array);
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement