Advertisement
heavenriver

processes.c

Nov 25th, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1.  /* basic operations with processes, UNIX-native */
  2.  
  3.  /* pointer arithmetics:
  4.   *
  5.   * push(pointer, v) INDICATES *(pointer++) = v
  6.   * pop(pointer) INDICATES *(--pointer)
  7.   * (*++v)[0] INDICATES (**++v)
  8.   *           INDICATES first char in v
  9.   *           INDICATES name of string/vector v
  10.   * likewise, *v[0] INDICATES **v
  11.   *       and *v[n] INDICATES **(v + n)
  12.   * returntype (*funct)(args) INDICATES a function funct with arguments args which returns...
  13.   * char **argv INDICATES pointer to char pointer
  14.   * int(*v)[len] INDICATES pointer "v" to a vector of "len" int elements
  15.   * int *v[len] INDICATES vector "v" of "len" pointers to int elements
  16.   * void *funct() INDICATES function "funct" that returns a pointer-to-void
  17.   * void (*funct)() INDICATES pointer to a function "funct" that returns void
  18.   *
  19.   */
  20.  
  21.  /* useful characters: [] # */
  22.  
  23.  # include <stdio.h>
  24.  # include <unistd.h> // execlp is defined here
  25.  
  26.  # define BUFFERSIZE 1024
  27.  
  28.  int main(int argc, char * argv[])
  29.     {
  30.      /* PID generation and fork() call */
  31.      int pid = fork(); // process ID is now the ID of the current process
  32.      if(pid == 0) printf("{Call 1} Child process, PID = %d\n", pid);
  33.      else printf("{Call 1} Parent process, PID = %d\n", pid);
  34.      
  35.      
  36.      /* process status */
  37.      pid = fork();
  38.      int status; // process status
  39.      if(pid == 0) printf("{Call 2} Child process, PID = %d\n", pid);
  40.      else
  41.     {
  42.      wait(&status); // waits until at least one child terminates
  43.      printf("{Call 2} Parent process, PID = %d\n", pid);
  44.     }
  45.    
  46.    
  47.     /* exec* calls */
  48.     execlp("ls", "ls", 0);
  49.     printf("execlp(...) not executed");
  50.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement