fork(1) download
  1. /**
  2. UNIX Shell Project
  3.  
  4. Sistemas Operativos
  5. Grados I. Informatica, Computadores & Software
  6. Dept. Arquitectura de Computadores - UMA
  7.  
  8. Some code adapted from "Fundamentos de Sistemas Operativos", Silberschatz et al.
  9.  
  10. To compile and run the program:
  11.   $ gcc Shell_project.c job_control.c -o Shell
  12.   $ ./Shell
  13. (then type ^D to exit program)
  14.  
  15. **/
  16.  
  17. #include "job_control.h" // remember to compile with module job_control.c
  18.  
  19. #define MAX_LINE 256 /* 256 chars per line, per command, should be enough. */
  20.  
  21. // -----------------------------------------------------------------------
  22. // MAIN
  23. // -----------------------------------------------------------------------
  24.  
  25. int main(void)
  26. {
  27. char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
  28. int background; /* equals 1 if a command is followed by '&' */
  29. char *args[MAX_LINE/2]; /* command line (of 256) has max of 128 arguments */
  30. // probably useful variables:
  31. int pid_fork, pid_wait; /* pid for created and waited process */
  32. int status; /* status returned by wait */
  33. enum status status_res; /* status processed by analyze_status() */
  34. int info; /* info processed by analyze_status() */
  35.  
  36. while (1) /* Program terminates normally inside get_command() after ^D is typed*/
  37. {
  38. printf("COMMAND->");
  39. fflush(stdout);
  40. get_command(inputBuffer, MAX_LINE, args, &background); /* get next command */
  41.  
  42. if(args[0]==NULL) continue; // if empty command
  43.  
  44.  
  45. pid_fork = fork(); // creamos un proceso hijo
  46.  
  47. if (pid_fork == 0) {//Proceso hijo
  48. execvp(args[0], args)
  49. perror("Fallo de exec");
  50. exit(EXIT_FAILURE);
  51. }
  52. else{ //proceso padre
  53. if(background == 0){
  54. pid_wait=waitpid(pid_fork);
  55. }else{
  56. continue;
  57. }
  58. printf("Motivo de terminación: %s\n",status_strings[status]);
  59.  
  60. }
  61.  
  62.  
  63. /* the steps are:
  64. (1) fork a child process using fork()
  65.  
  66. (2) the child process will invoke execvp()
  67. (3) if background == 0, the parent will wait, otherwise continue
  68. (4) Shell shows a status message for processed command
  69. (5) loop returns to get_commnad() function
  70. */
  71.  
  72. } // end while
  73. }
  74.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:17:77: fatal error: job_control.h: No such file or directory
compilation terminated.
stdout
Standard output is empty