diff --git a/.ish_history b/.ish_history index df4aa5f..c4b1046 100644 --- a/.ish_history +++ b/.ish_history @@ -1,21 +1,21 @@ 20 -cd ./uwu/ - -quit -clear -quit -cd .. -cd OS -ls -cd u:x:0:0:root:/root:/bin/bash - -quit -cd uwu -cd .. -OS -cd OS -cd ./uwu/ -cd .. -cat uwu +date +sleep 1 & +jobs quit +vim & +sleep 1 & +vim & +jobs +gf 5 +vim & +jwel +vim & +jobs +fg 3 +jobs +fg 1 +jobs +fg 2 +asbkdj diff --git a/fg-bg.c b/fg-bg.c index d0155b9..866b882 100644 --- a/fg-bg.c +++ b/fg-bg.c @@ -2,84 +2,162 @@ void fg(char* command) { - char* token = strtok(command," \t"); - if(strcmp(token,"sig") != 0) + char* token = strtok(command," \t"); + if(strcmp(token,"bg") != 0) { - if(debug) printf("the cmd is not sig \n"); + if(debug) printf("the cmd is not bg \n"); } - - int jobNo; - int sigNo; token = strtok(NULL," \t"); int args = 0; + + char jid[MY_LEN]; while(token != NULL) { if(args == 0) { - jobNo = atoi(token) - 1; - if(debug) perror("jobNo arguement\n"); + if(debug) perror("jobid arguement\n"); args++; + strcpy(jid,token); } else { - fprintf(stderr,"Too many arguements for sig\n"); + fprintf(stderr,"Too many arguements for bg\n"); return; } token = strtok(NULL," \t"); } - if(args != 1) + if (args == 0) + { + fprintf(stderr, "Error: enter valid jobIndex\n"); + return; + } + + int jobIndex = atoi(jid); + + if (jobIndex > bgpno) { - fprintf(stderr,"Too few arguements for sig\n"); + fprintf(stderr, "No such process\n"); return; } - if(jobNo >= bgpno) + // Getting process information from child pool + pid_t pid = bgpid[jobIndex - 1]; + char pName[MY_LEN]; + strcpy(pName, bgp[jobIndex - 1]); + + // // Removing process from child pool because it is being moved to the foreground + // removeChild(pid); + bexists[jobIndex - 1] = 0; + + // // Ignoring required signals from the shell + signal(SIGTTOU, SIG_IGN); + signal(SIGTTIN, SIG_IGN); + + // // Setting foreground process flag to 1 to indicate that there is a foreground process running + int fgP = 1; + + // // Giving the specified process terminal control and checking for errors + if(tcsetpgrp(STDIN_FILENO, getpgid(pid))) { - fprintf(stderr,"No such job exists \n"); + fprintf(stderr, "Could not give terminal control to job %d\n", jobIndex); + // Control couldn't be give to the child process, we want the shell to continue + // Set signal handlers to default + signal(SIGTTOU, SIG_DFL); + signal(SIGTTIN, SIG_DFL); + // Print error message and return from the function. + return ; } + // // Telling the job to resume and handling errors (same as above) + if(kill(pid, SIGCONT)) + { + fprintf(stderr, "Unable to resume job %d\n", jobIndex); + signal(SIGTTOU, SIG_DFL); + signal(SIGTTIN, SIG_DFL); + return ; + } + + // // Waiting for foreground process to be stopped or to terminate. + // // If it is stopped (Ctrl+Z), add it to the child pool + int status; + if(waitpid(pid, &status, WUNTRACED) > 0) + if(WIFSTOPPED(status)) + { + strcpy(bgp[bgpno], pName); + if (debug) + printf("bgp[%d] = %s\n", bgpno, bgp[bgpno]); + bgpid[bgpno] = pid; + if (debug) + printf("bgpid[%d] = %d\n", bgpno, bgpid[bgpno]); + bexists[bgpno] = 1; + if (debug) + printf("bexists[%d] = %d\n", bgpno, bexists[bgpno]); + bgpno++; + } + + // exitCode = WEXITSTATUS(status) ? -1 : 1; + + // // Since the foreground process has been exitted from, set the flag back to 0 + // fgP = 0; + // // Return terminal control back to the shell and perform error handling + // // If control cannot be returned, quit the program. + if(tcsetpgrp(STDIN_FILENO, getpgid(0))) + { + fprintf(stderr, "Could not return terminal controll to the shell. Exitting the shell\n"); + return; + } + // // Restore default signal handlers + signal(SIGTTOU, SIG_DFL); + signal(SIGTTIN, SIG_DFL); + return; } void bg(char* command) { - char* token = strtok(command," \t"); - if(strcmp(token,"sig") != 0) + char* token = strtok(command," \t"); + if(strcmp(token,"bg") != 0) { - if(debug) printf("the cmd is not sig \n"); + if(debug) printf("the cmd is not bg \n"); } - - int jobNo; - int sigNo; token = strtok(NULL," \t"); int args = 0; + + char jid[MY_LEN]; while(token != NULL) { if(args == 0) { - jobNo = atoi(token) - 1; - if(debug) perror("jobNo arguement\n"); + if(debug) perror("jobid arguement\n"); args++; + strcpy(jid,token); } else { - fprintf(stderr,"Too many arguements for sig\n"); + fprintf(stderr,"Too many arguements for bg\n"); return; } token = strtok(NULL," \t"); } - if(args != 1) + if (args == 0) { - fprintf(stderr,"Too few arguements for sig\n"); + fprintf(stderr, "Error: enter valid jobIndex\n"); return; } - if(jobNo >= bgpno) + int jobIndex = atoi(jid) - 1; + + if (jobIndex > bgpno) { - fprintf(stderr,"No such job exists \n"); + fprintf(stderr, "No such process\n"); + return; } + + kill(bgpid[jobIndex], SIGCONT); + + return; } \ No newline at end of file diff --git a/headers.h b/headers.h index 149c1d0..8a15206 100644 --- a/headers.h +++ b/headers.h @@ -33,8 +33,12 @@ extern int QUIT; extern int errno; extern char *bgp[MY_LEN]; extern int bgpid[MY_LEN]; -extern int exists[MY_LEN]; +extern int bexists[MY_LEN]; extern int bgpno; +extern char *fgp[MY_LEN]; +extern int fgpid[MY_LEN]; +extern int fexists[MY_LEN]; +extern int fgpno; extern int initSTDIN; extern int initSTDOUT; extern int fd1; diff --git a/init.c b/init.c index 1afef64..1d20383 100644 --- a/init.c +++ b/init.c @@ -7,6 +7,7 @@ void init() strcpy(extension,""); QUIT = 0; bgpno = 0; + fgpno = 0; initSTDIN = 0; initSTDOUT = 1; fd1 = -1; diff --git a/ish b/ish index ac19a4a..20e59e0 100755 Binary files a/ish and b/ish differ diff --git a/jobs.c b/jobs.c index a61a48b..d4fb724 100644 --- a/jobs.c +++ b/jobs.c @@ -1,52 +1,84 @@ #include "headers.h" #include "functions.h" - -void jobs(char* command) +void jobs(char *command) { int jobs_r_flag = 0; int jobs_s_flag = 0; - char * token = strtok(command," \t"); - if(strcmp(token,"jobs") != 0) + char *token = strtok(command, " \t"); + if (strcmp(token, "jobs") != 0) { - if(debug) printf("Not a jobs command, Wrong number\n"); + if (debug) + printf("Not a jobs command, Wrong number\n"); } - token = strtok(NULL," \t"); + token = strtok(NULL, " \t"); - while(token != NULL) + while (token != NULL) { - if(!strcmp(token,"-r")) + if (!strcmp(token, "-r")) { jobs_r_flag = 1; } - else if (!strcmp(token,"-s")) + else if (!strcmp(token, "-s")) { - jobs_s_flag = 1; + jobs_s_flag = 1; } else { - fprintf(stderr,"Too many arguements for jobs\n"); + fprintf(stderr, "Too many arguements for jobs\n"); } } - if((jobs_r_flag == 0) && (jobs_s_flag == 0)) + if ((jobs_r_flag == 0) && (jobs_s_flag == 0)) { jobs_r_flag = 1; jobs_s_flag = 1; } - for(int i =0 ; i < bgpno; i++) + char filePath[MY_LEN]; + char statRead[MY_LEN]; + + // for(int i =0 ; i < bgpno; i++) + // { + // if(exists[i]) + // { + // printf("[%d] Running %s [%d]\n",i + 1,bgp[i],bgpid[i]); + // } + // else + // { + // printf("[%d] Stopped %s [%d]\n",i + 1,bgp[i],bgpid[i]); + // } + // } + + for (int i = 0; i < bgpno; i++) { - if(exists[i]) - { - printf("[%d] Running %s [%d]\n",i + 1,bgp[i],bgpid[i]); - } - else + char pidstring[MY_LEN]; + sprintf(pidstring, "%d", bgpid[i]); + + if(bexists[i] == 0) continue; + + char pinfoPath[MY_LEN]; + strcpy(pinfoPath, "/proc/"); + strcat(pinfoPath, pidstring); + strcat(pinfoPath, "/stat"); + + FILE* fp = fopen(pinfoPath, "r"); + if (fp == NULL) { - printf("[%d] Stopped %s [%d]\n",i + 1,bgp[i],bgpid[i]); + fprintf(stderr,"pid proc file doesnt exist\n"); + return; } - } + char temp[MY_LEN]; + fscanf(fp, "%s", temp); + fscanf(fp, "%s", temp); + fscanf(fp, "%s", temp); + + char processStatus[MY_LEN]; + strcpy(processStatus, temp); + + printf("[%d] %s %s [%d]\n", i + 1, !strcmp(processStatus,"S") ? "Running" : "Stopped", bgp[i], bgpid[i]); + } } \ No newline at end of file diff --git a/main.c b/main.c index 495c2c0..9ab0e33 100644 --- a/main.c +++ b/main.c @@ -9,8 +9,12 @@ int QUIT; int errno; char *bgp[MY_LEN]; int bgpid[MY_LEN]; -int exists[MY_LEN]; +int bexists[MY_LEN]; int bgpno; +char *fgp[MY_LEN]; +int fgpid[MY_LEN]; +int fexists[MY_LEN]; +int fgpno; int initSTDIN; int initSTDOUT; int fd1; diff --git a/processControl.c b/processControl.c index f6290e1..35471cd 100644 --- a/processControl.c +++ b/processControl.c @@ -69,16 +69,16 @@ void foregroundProcess(char *path, char *arguements[], int argc) } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus)); - strcpy(bgp[bgpno], arguements[0]); + strcpy(fgp[fgpno], arguements[0]); if (debug) - printf("bgp[%d] = %s\n", bgpno, bgp[bgpno]); - bgpid[bgpno] = pid; + printf("fgp[%d] = %s\n", fgpno, fgp[fgpno]); + fgpid[fgpno] = pid; if (debug) - printf("bgpid[%d] = %d\n", bgpno, bgpid[bgpno]); - exists[bgpno] = 0; + printf("fgpid[%d] = %d\n", fgpno, fgpid[fgpno]); + fexists[fgpno] = 1; if (debug) - printf("exists[%d] = %d\n", bgpno, exists[bgpno]); - bgpno++; + printf("fexists[%d] = %d\n", fgpno, fexists[fgpno]); + fgpno++; } return; @@ -133,9 +133,9 @@ void backgroundProcess(char *path, char *arguements[], int argc) bgpid[bgpno] = pid; if (debug) printf("bgpid[%d] = %d\n", bgpno, bgpid[bgpno]); - exists[bgpno] = 1; + bexists[bgpno] = 1; if (debug) - printf("exists[%d] = %d\n", bgpno, exists[bgpno]); + printf("bexists[%d] = %d\n", bgpno, bexists[bgpno]); bgpno++; printf("[%d] %d\n", bgpno, pid); @@ -197,8 +197,8 @@ int LookforBG() int retval = 0; for (int i = 0; i < bgpno; i++) { - // printf("%s %d %d\n",bgp[i],bgpid[i],exists[i]); - if (exists[i]) + // printf("%s %d %d\n",bgp[i],bgpid[i],bexists[i]); + if (bexists[i]) { retval = 1; char exit_status[MY_LEN] = ""; @@ -249,7 +249,7 @@ int LookforBG() } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus)); fprintf(stderr,"[%d] Done--------%s [with pid = %d] was exceuted in background and %s\n", i+1, bgp[i], bgpid[i], exit_status); - exists[i] = 0; + bexists[i] = 0; } } } @@ -261,8 +261,8 @@ void KillAllBG() { for (int i = 0; i < bgpno; i++) { - // printf("%s %d %d\n",bgp[i],bgpid[i],exists[i]); - if (exists[i]) + // printf("%s %d %d\n",bgp[i],bgpid[i],bexists[i]); + if (bexists[i]) { kill(bgpid[i], SIGKILL); printf("Killed %s [with pid = %d] \n", bgp[i], bgpid[i]); diff --git a/signal.c b/signal.c index abd6278..d18aa4e 100644 --- a/signal.c +++ b/signal.c @@ -70,6 +70,6 @@ void sig(char* command) { fprintf(stderr,"No such job exists \n"); } - kill(bgpid[jobNo],sigNo); + kill(bgpid[jobNo],sigNo); }