Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/resource.h>
  5. #include <sys/time.h>
  6.  
  7. int main(int argc , char *argv[])
  8. {
  9.     pid_t pid, ppid;
  10.     int status;
  11.     struct rusage rusage;
  12.     unsigned long czas1, czas2;
  13.     long suma, i, procesy;
  14.     printf("Liczba procesów to %s \n", argv[1]);
  15.     procesy = atoi(argv[1]);
  16.  
  17. /*
  18.     if ((pid = fork()) == -1)
  19.     {
  20.         perror("Blad fork");
  21.         exit(1);
  22.     }
  23. */
  24. printf ( " proces macierzysty to %d \n", getpid());
  25. ppid = getpid();
  26. for ( int a = 0 ; a < procesy ; a++)
  27. {
  28.  
  29.     if (fork() == 0)
  30.     {
  31.         pid = getpid();
  32.         printf("Proces potomny: PID = %d\n", pid);
  33.         printf("Proces macierzysty: PID = %d\n", getppid());
  34.         for (i=0; i<400000; i++)
  35.         {
  36.             suma+=i*3+5;
  37.             pid = getpid();
  38.         }
  39.  
  40.         printf("kod wyjscia to %d\n",a + 8 );
  41.  
  42.         exit((a + 8));
  43.     }
  44.     else
  45.     {
  46.         //pid = fork();
  47.         //pid = getpid();
  48.         printf("Proces macierzysty: PID = %d\n", pid);
  49.         if (wait3(&status, 0, &rusage) == -1)
  50.         {
  51.             perror("Blad wait3");
  52.             exit(1);
  53.         }
  54.         czas1 = (rusage.ru_utime).tv_sec*1000 + (rusage.ru_utime).tv_usec/1000;
  55.         czas2 = (rusage.ru_stime).tv_sec*1000 + (rusage.ru_stime).tv_usec/1000;
  56.         printf("Czas wykonania potomka\n");
  57.         printf("\t- w trybie u�ytkownika: %ld ms\n", czas1);
  58.         printf("\t- w trybie j�dra: %ld ms\n", czas2);
  59.         printf("Kod wyjścia to %d \n", ( status >> 8));
  60.         //exit(0);
  61.     }
  62. }
  63.     return(0);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement