Advertisement
Guest User

Untitled

a guest
May 20th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  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. #include <stdio.h>
  20. #include <stdlib.h>
  21. #define MAX_LINE 256 /* 256 chars per line, per command, should be enough. */
  22.  
  23.  
  24.  
  25. // -----------------------------------------------------------------------
  26. // MANEJADOR
  27. //------------------------------------------------------------------------
  28. //a tener en cuenta:
  29. // el waitpid puede NO devolver el &status del proceso pid correcto, hacer
  30. // un if con el pid que le hemos dado a ver si es el mismo
  31. // como tercer parametro podemos usar WCONTINUED que sirve para saber QUE VA A HACER
  32. // despues hacemos un analyze_status y tenemos que informar de su situacion y si ha terminado tenemos que borrarlo de la lista
  33.  
  34. /*void manejador(int pid_fork,int info,const char bg,int status,job wea){
  35. int ver=waitpid(pid_fork,&status,WCONTINUED);
  36. if(pid_fork==ver){
  37. job* qwea=get_item_bypid(bg,ver);
  38. int pepito=analyze_status(status,&info);
  39. if(pepito==1||pepito==2){
  40. print_item(qwea);
  41. delete_job(bg,qwea);
  42. printf("Proceso terminado\n");
  43. }
  44.  
  45. }
  46. }*/
  47.  
  48.  
  49.  
  50.  
  51.  
  52. // -----------------------------------------------------------------------
  53. // MAIN
  54. // -----------------------------------------------------------------------
  55.  
  56.  
  57. int main(void)
  58. {
  59. char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
  60. int background; /* equals 1 if a command is followed by '&' */
  61. char *args[MAX_LINE/2]; /* command line (of 256) has max of 128 arguments */
  62. // probably useful variables:
  63. int pid_fork, pid_wait; /* pid for created and waited process */
  64. int status; /* status returned by wait */
  65. enum job_state state;
  66. enum status status_res; /* status processed by analyze_status() */
  67. int info; /* info processed by analyze_status() */
  68. int nestado;
  69. job * vaina = new_list("Tareas Shell");
  70.  
  71.  
  72. ignore_terminal_signals();
  73. //signal(SIGCHLD,manejador);
  74.  
  75. while (1) /* Program terminates normally inside get_command() after ^D is typed*/
  76. {
  77. char ubi[MAX_LINE];
  78. printf("\033[0;34m P\033[0;31m s\033[1;32m Y\033[0;35m c\033[1;33m H\033[0;36m y\033[0;34m S\033[1;32m::%s::-----> \033[0m",getcwd(ubi,sizeof(ubi)));
  79. fflush(stdout);
  80. get_command(inputBuffer, MAX_LINE, args, &background); /* get next command */
  81.  
  82. if(args[0]==NULL) continue; // if empty command
  83. int wea=strcmp(args[0],"cd");
  84.  
  85. if(wea==0)
  86. {
  87. if(args[1]==NULL){
  88. chdir(getenv("HOME"));
  89. }else{
  90. chdir(args[1]);
  91. }
  92. }if(strcmp(args[0],"jobs")==0)
  93. {
  94. print_job_list(vaina);
  95. }
  96. /* the steps are:
  97. (1) fork a child process using fork()
  98. (2) the child process will invoke execvp()
  99. (3) if background == 0, the parent will wait, otherwise continue
  100. (4) Shell shows a status message for processed command
  101. (5) loop returns to get_commnad() function
  102. */
  103.  
  104. pid_fork=fork();
  105.  
  106.  
  107.  
  108. if(pid_fork==0)
  109. {
  110. int pid=getpid();
  111. if(background==0){
  112. new_process_group(pid);
  113. set_terminal(pid);
  114. restore_terminal_signals();
  115. execvp(inputBuffer,args);
  116. printf("Comman not found: %s\n",inputBuffer);
  117. exit(-1);
  118. }if(background!=0){
  119. add_job(vaina,new_job(pid,args[0],background));
  120. execvp(inputBuffer,args);
  121. printf("Comman not found: %s\n",inputBuffer);
  122. exit(-1);}
  123. }
  124. if(pid_fork!=0){
  125. if(background==0)
  126. {
  127. // ******************en el waitpid para evitar que se bloquee hay que poner como tercer parametro WNOHANG y si queremos que se mantengan WUNTRACED y WNOHANG hay que hacer un or ("|")
  128. waitpid(pid_fork,&status,WUNTRACED);
  129. nestado=analyze_status(status,&info);
  130. set_terminal(getpid());
  131.  
  132. printf("Foreground pid:%d, command: %s, %s, info: %d\n",pid_fork,inputBuffer,status_strings[nestado],info);
  133. }if(background==1){
  134. printf("Background job running... pid:%d, command: %s\n",pid_fork,inputBuffer);
  135. }
  136. }
  137.  
  138. } // end while
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement