Skip to content

Commit

Permalink
fg-bg attempt 1
Browse files Browse the repository at this point in the history
  • Loading branch information
ishwarbb committed Sep 25, 2022
1 parent 141e02d commit 3505a0b
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 81 deletions.
36 changes: 18 additions & 18 deletions .ish_history
Original file line number Diff line number Diff line change
@@ -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
130 changes: 104 additions & 26 deletions fg-bg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 5 additions & 1 deletion headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions init.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ void init()
strcpy(extension,"");
QUIT = 0;
bgpno = 0;
fgpno = 0;
initSTDIN = 0;
initSTDOUT = 1;
fd1 = -1;
Expand Down
Binary file modified ish
Binary file not shown.
72 changes: 52 additions & 20 deletions jobs.c
Original file line number Diff line number Diff line change
@@ -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]);
}
}
6 changes: 5 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 3505a0b

Please sign in to comment.