Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- UNIX Shell Project
- Sistemas Operativos
- Grados I. Informatica, Computadores & Software
- Dept. Arquitectura de Computadores - UMA
- Some code adapted from "Fundamentos de Sistemas Operativos", Silberschatz et al.
- To compile and run the program:
- $ gcc Shell_project.c job_control.c -o Shell
- $ ./Shell
- (then type ^D to exit program)
- **/
- #include "job_control.h" // remember to compile with module job_control.c
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX_LINE 256 /* 256 chars per line, per command, should be enough. */
- // -----------------------------------------------------------------------
- // MANEJADOR
- //------------------------------------------------------------------------
- //a tener en cuenta:
- // el waitpid puede NO devolver el &status del proceso pid correcto, hacer
- // un if con el pid que le hemos dado a ver si es el mismo
- // como tercer parametro podemos usar WCONTINUED que sirve para saber QUE VA A HACER
- // despues hacemos un analyze_status y tenemos que informar de su situacion y si ha terminado tenemos que borrarlo de la lista
- /*void manejador(int pid_fork,int info,const char bg,int status,job wea){
- int ver=waitpid(pid_fork,&status,WCONTINUED);
- if(pid_fork==ver){
- job* qwea=get_item_bypid(bg,ver);
- int pepito=analyze_status(status,&info);
- if(pepito==1||pepito==2){
- print_item(qwea);
- delete_job(bg,qwea);
- printf("Proceso terminado\n");
- }
- }
- }*/
- // -----------------------------------------------------------------------
- // MAIN
- // -----------------------------------------------------------------------
- int main(void)
- {
- char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
- int background; /* equals 1 if a command is followed by '&' */
- char *args[MAX_LINE/2]; /* command line (of 256) has max of 128 arguments */
- // probably useful variables:
- int pid_fork, pid_wait; /* pid for created and waited process */
- int status; /* status returned by wait */
- enum job_state state;
- enum status status_res; /* status processed by analyze_status() */
- int info; /* info processed by analyze_status() */
- int nestado;
- job * vaina = new_list("Tareas Shell");
- ignore_terminal_signals();
- //signal(SIGCHLD,manejador);
- while (1) /* Program terminates normally inside get_command() after ^D is typed*/
- {
- char ubi[MAX_LINE];
- 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)));
- fflush(stdout);
- get_command(inputBuffer, MAX_LINE, args, &background); /* get next command */
- if(args[0]==NULL) continue; // if empty command
- int wea=strcmp(args[0],"cd");
- if(wea==0)
- {
- if(args[1]==NULL){
- chdir(getenv("HOME"));
- }else{
- chdir(args[1]);
- }
- }if(strcmp(args[0],"jobs")==0)
- {
- print_job_list(vaina);
- }
- /* the steps are:
- (1) fork a child process using fork()
- (2) the child process will invoke execvp()
- (3) if background == 0, the parent will wait, otherwise continue
- (4) Shell shows a status message for processed command
- (5) loop returns to get_commnad() function
- */
- pid_fork=fork();
- if(pid_fork==0)
- {
- int pid=getpid();
- if(background==0){
- new_process_group(pid);
- set_terminal(pid);
- restore_terminal_signals();
- execvp(inputBuffer,args);
- printf("Comman not found: %s\n",inputBuffer);
- exit(-1);
- }if(background!=0){
- add_job(vaina,new_job(pid,args[0],background));
- execvp(inputBuffer,args);
- printf("Comman not found: %s\n",inputBuffer);
- exit(-1);}
- }
- if(pid_fork!=0){
- if(background==0)
- {
- // ******************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 ("|")
- waitpid(pid_fork,&status,WUNTRACED);
- nestado=analyze_status(status,&info);
- set_terminal(getpid());
- printf("Foreground pid:%d, command: %s, %s, info: %d\n",pid_fork,inputBuffer,status_strings[nestado],info);
- }if(background==1){
- printf("Background job running... pid:%d, command: %s\n",pid_fork,inputBuffer);
- }
- }
- } // end while
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement