Advertisement
heavenriver

processesB.c

Nov 26th, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.61 KB | None | 0 0
  1. /* basic operations with processes, part B, 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 CMDLEN 256
  27.  
  28.  int main(int argc, char * argv[])
  29.     {
  30.      char cmd[CMDLEN];
  31.      int pid, status, cond;
  32.      cond = 1;
  33.      
  34.      while(cond > 0) // NOTE: THE LOOP DOES NOT BREAK (presumably because of the char, not char*, declaration?)
  35.     {
  36.      printf("Command ('quit' to leave): ");
  37.      scanf("%s", cmd); // memorises the command input by user
  38.      if(strcmp(cmd, "quit") == 0)
  39.         {
  40.          cond = 0;
  41.          break;
  42.         }
  43.      
  44.      pid = fork(); // forks the current process
  45.      // if(pid == -1) exit(1);
  46.      if(pid == 0) execlp(cmd, cmd, 0); // if current process is child process, replaces it with the cmd
  47.      else wait(&status); // else, waits until first child death
  48.     }
  49.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement