Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 30th, 2012  |  syntax: None  |  size: 1.94 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // apple xcode
  2. // paulogp
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <string.h>
  10.  
  11. void func_simula_processamento();
  12.  
  13. int main (int argc, const char * argv[])
  14. {
  15.         int the_fork, the_status;
  16.  
  17.         // function shall create a new process
  18.         // the new process (child process) shall be an exact copy of the
  19.         // calling process (parent process)
  20.         the_fork = fork();
  21.  
  22.         // getppid: shall return the parent process ID of the calling process
  23.         printf("pid = %d, ppid = %d\n", getpid(), getppid());
  24.  
  25.         // waits for seconds or until a signal is delivered, whichever happens first
  26.         sleep(1);
  27.  
  28.         // seed random number generator
  29.         // 'srandom' call only needs to be done once per program
  30.         srandom(getpid());
  31.  
  32.         char *ptr = malloc(8);
  33.         strcpy(ptr, "pai\n");
  34.  
  35.         // the_fork == 0: == "filho": termina no exit(1)
  36.         // apenas o filho realiza esta accao
  37.         // as variaveis dentro deste filho nao afectam o pai ou outros filhos
  38.         if (the_fork == 0)
  39.         {
  40.                 // pretende demonstrar que a var alterada pelo filho nao afecta o pai
  41.                 strcpy(ptr, "filho\n");
  42.                 fwrite(ptr, 8, 1, stdout);
  43.  
  44.                 // flush a stream: forca saida dos valores
  45.                 fflush(stdout);
  46.  
  47.                 // a accao do filho termina aqui
  48.                 // o filho morre neste ponto
  49.                 exit(1);
  50.         }
  51.  
  52.         // The waitpid() system call suspends execution of the calling process until a
  53.         // child specified by pid argument has changed state.  By default, waitpid()
  54.         // waits only for terminated children, but this behavior is modifiable via the
  55.         // options argument
  56.         the_fork = waitpid(the_fork, &the_status, 0);
  57.  
  58.         fwrite(ptr, 8, 1, stdout);
  59.  
  60.         printf("\n");
  61.  
  62.         // WIFEXITED: this macro queries the child termination status provided
  63.         // by the wait and waitpid functions, and determines whether the child
  64.         // process ended normally
  65.         if (WIFEXITED(the_status))
  66.                 printf("valor de retorno de (%d): %d\n", the_fork, WEXITSTATUS(the_status));
  67.         else
  68.                 printf("filho (%d) terminou de forma anormal\n", the_fork);
  69.  
  70.         return 0;
  71. }