From c13c9c96943b312bc9c404d8cbe95394b77f96d7 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Mon, 31 Aug 2026 06:43:48 +0000 Subject: jobs --- src/job.cc | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cc | 189 +++------------------------------------------------------ src/setting.cc | 8 +++ 3 files changed, 182 insertions(+), 182 deletions(-) create mode 100644 src/job.cc create mode 100644 src/setting.cc (limited to 'src') diff --git a/src/job.cc b/src/job.cc new file mode 100644 index 0000000..a22f327 --- /dev/null +++ b/src/job.cc @@ -0,0 +1,167 @@ +#include "job.h" + +namespace ov4 +{ + +struct job_t jobs[MAXJOBS]; +int nextjid = 1; + +void clearjob(struct job_t *job) { + job->pid = 0; + job->jid = 0; + job->state = UNDEF; + job->cmdline[0] = '\0'; +} + +/* initjobs - Initialize the job list */ +void initjobs(struct job_t *jobs) { + int i; + + for (i = 0; i < MAXJOBS; i++) + clearjob(&jobs[i]); +} + +/* maxjid - Returns largest allocated job ID */ +int maxjid(struct job_t *jobs) +{ + int i, max=0; + + for (i = 0; i < MAXJOBS; i++) + if (jobs[i].jid > max) + max = jobs[i].jid; + return max; +} + +/* addjob - Add a job to the job list */ +int addjob(struct job_t *jobs, pid_t pid, int state, char *cmdline) +{ + int i; + + if (pid < 1) + return 0; + + for (i = 0; i < MAXJOBS; i++) { + if (jobs[i].pid == 0) { + jobs[i].pid = pid; + jobs[i].state = state; + jobs[i].jid = nextjid++; + if (nextjid > MAXJOBS) + nextjid = 1; + strcpy(jobs[i].cmdline, cmdline); + if(verbose){ + printf("Added job [%d] %d %s\n", jobs[i].jid, jobs[i].pid, jobs[i].cmdline); + } + return 1; + } + } + printf("Tried to create too many jobs\n"); + return 0; +} + +/* deletejob - Delete a job whose PID=pid from the job list */ +int deletejob(struct job_t *jobs, pid_t pid) +{ + int i; + + if (pid < 1) + return 0; + + for (i = 0; i < MAXJOBS; i++) { + if (jobs[i].pid == pid) { + clearjob(&jobs[i]); + nextjid = maxjid(jobs)+1; + return 1; + } + } + return 0; +} + +/* fgpid - Return PID of current foreground job, 0 if no such job */ +pid_t fgpid(struct job_t *jobs) { + int i; + + for (i = 0; i < MAXJOBS; i++) + if (jobs[i].state == FG) + return jobs[i].pid; + return 0; +} + +/* getjobpid - Find a job (by PID) on the job list */ +struct job_t *getjobpid(struct job_t *jobs, pid_t pid) { + int i; + + if (pid < 1) + return NULL; + for (i = 0; i < MAXJOBS; i++) + if (jobs[i].pid == pid) + return &jobs[i]; + return NULL; +} + +/* getjobjid - Find a job (by JID) on the job list */ +struct job_t *getjobjid(struct job_t *jobs, int jid) +{ + int i; + + if (jid < 1) + return NULL; + for (i = 0; i < MAXJOBS; i++) + if (jobs[i].jid == jid) + return &jobs[i]; + return NULL; +} + +/* pid2jid - Map process ID to job ID */ +int pid2jid(pid_t pid) +{ + int i; + + if (pid < 1) + return 0; + for (i = 0; i < MAXJOBS; i++) + if (jobs[i].pid == pid) { + return jobs[i].jid; + } + return 0; +} + +/* listjobs - Print the job list */ +void listjobs(struct job_t *jobs) +{ + int i; + + for (i = 0; i < MAXJOBS; i++) { + if (jobs[i].pid != 0) { + printf("[%d] (%d) ", jobs[i].jid, jobs[i].pid); + switch (jobs[i].state) { + case BG: + printf("Running "); + break; + case FG: + printf("Foreground "); + break; + case ST: + printf("Stopped "); + break; + default: + printf("listjobs: Internal error: job[%d].state=%d ", + i, jobs[i].state); + } + printf("%s", jobs[i].cmdline); + } + } +} + +bool job_suspend(struct job_t *jobs, pid_t pid) { + int i; + + for (i = 0; i < MAXJOBS; i++) + if (jobs[i].pid == pid) + { + jobs[i].state = ST; + return true; + } + return false; +} + +} diff --git a/src/main.cc b/src/main.cc index c004564..95ac9b0 100644 --- a/src/main.cc +++ b/src/main.cc @@ -15,18 +15,15 @@ #include #include #include + #include "util.h" +#include "job.h" using namespace std; -using ov4::atomic_print; -using ov4::atomic_debug; -using ov4::isnum; +using namespace ov4; /* Misc manifest constants */ -#define MAXLINE 1024 /* max line size */ -#define MAXARGS 128 /* max args on a command line */ -#define MAXJOBS 16 /* max jobs at any point in time */ -#define MAXJID 1<<16 /* max job ID */ + /* Job states */ #define UNDEF 0 /* undefined */ @@ -47,17 +44,10 @@ using ov4::isnum; /* Global variables */ extern char **environ; /* defined in libc */ char prompt[] = "tsh> "; /* command line prompt (DO NOT CHANGE) */ -int verbose = 0; /* if true, print additional output */ -int nextjid = 1; /* next job ID to allocate */ char sbuf[MAXLINE]; /* for composing sprintf messages */ -struct job_t { /* The job struct */ - pid_t pid; /* job PID */ - int jid; /* job ID [1, 2, ...] */ - int state; /* UNDEF, BG, FG, or ST */ - char cmdline[MAXLINE]; /* command line */ -}; -struct job_t jobs[MAXJOBS]; /* The job list */ + + vector builtin_cmd_list = {"quit", "fg", "bg", "jobs"}; sigset_t BLOCK_HANDLER; @@ -80,17 +70,6 @@ void sigint_handler(int sig); int parseline(const char *cmdline, char **argv); void sigquit_handler(int sig); -void clearjob(struct job_t *job); -void initjobs(struct job_t *jobs); -int maxjid(struct job_t *jobs); -int addjob(struct job_t *jobs, pid_t pid, int state, char *cmdline); -int deletejob(struct job_t *jobs, pid_t pid); -pid_t fgpid(struct job_t *jobs); -struct job_t *getjobpid(struct job_t *jobs, pid_t pid); -struct job_t *getjobjid(struct job_t *jobs, int jid); -int pid2jid(pid_t pid); -void listjobs(struct job_t *jobs); -bool job_suspend(struct job_t *jobs, pid_t pid); void usage(void); void unix_error(char *msg); @@ -541,151 +520,7 @@ void sigtstp_handler(int sig) **********************************************/ /* clearjob - Clear the entries in a job struct */ -void clearjob(struct job_t *job) { - job->pid = 0; - job->jid = 0; - job->state = UNDEF; - job->cmdline[0] = '\0'; -} - -/* initjobs - Initialize the job list */ -void initjobs(struct job_t *jobs) { - int i; - for (i = 0; i < MAXJOBS; i++) - clearjob(&jobs[i]); -} - -/* maxjid - Returns largest allocated job ID */ -int maxjid(struct job_t *jobs) -{ - int i, max=0; - - for (i = 0; i < MAXJOBS; i++) - if (jobs[i].jid > max) - max = jobs[i].jid; - return max; -} - -/* addjob - Add a job to the job list */ -int addjob(struct job_t *jobs, pid_t pid, int state, char *cmdline) -{ - int i; - - if (pid < 1) - return 0; - - for (i = 0; i < MAXJOBS; i++) { - if (jobs[i].pid == 0) { - jobs[i].pid = pid; - jobs[i].state = state; - jobs[i].jid = nextjid++; - if (nextjid > MAXJOBS) - nextjid = 1; - strcpy(jobs[i].cmdline, cmdline); - if(verbose){ - printf("Added job [%d] %d %s\n", jobs[i].jid, jobs[i].pid, jobs[i].cmdline); - } - return 1; - } - } - printf("Tried to create too many jobs\n"); - return 0; -} - -/* deletejob - Delete a job whose PID=pid from the job list */ -int deletejob(struct job_t *jobs, pid_t pid) -{ - int i; - - if (pid < 1) - return 0; - - for (i = 0; i < MAXJOBS; i++) { - if (jobs[i].pid == pid) { - clearjob(&jobs[i]); - nextjid = maxjid(jobs)+1; - return 1; - } - } - return 0; -} - -/* fgpid - Return PID of current foreground job, 0 if no such job */ -pid_t fgpid(struct job_t *jobs) { - int i; - - for (i = 0; i < MAXJOBS; i++) - if (jobs[i].state == FG) - return jobs[i].pid; - return 0; -} - -/* getjobpid - Find a job (by PID) on the job list */ -struct job_t *getjobpid(struct job_t *jobs, pid_t pid) { - int i; - - if (pid < 1) - return NULL; - for (i = 0; i < MAXJOBS; i++) - if (jobs[i].pid == pid) - return &jobs[i]; - return NULL; -} - -/* getjobjid - Find a job (by JID) on the job list */ -struct job_t *getjobjid(struct job_t *jobs, int jid) -{ - int i; - - if (jid < 1) - return NULL; - for (i = 0; i < MAXJOBS; i++) - if (jobs[i].jid == jid) - return &jobs[i]; - return NULL; -} - -/* pid2jid - Map process ID to job ID */ -int pid2jid(pid_t pid) -{ - int i; - - if (pid < 1) - return 0; - for (i = 0; i < MAXJOBS; i++) - if (jobs[i].pid == pid) { - return jobs[i].jid; - } - return 0; -} - -/* listjobs - Print the job list */ -void listjobs(struct job_t *jobs) -{ - int i; - - for (i = 0; i < MAXJOBS; i++) { - if (jobs[i].pid != 0) { - printf("[%d] (%d) ", jobs[i].jid, jobs[i].pid); - switch (jobs[i].state) { - case BG: - printf("Running "); - break; - case FG: - printf("Foreground "); - break; - case ST: - printf("Stopped "); - break; - default: - printf("listjobs: Internal error: job[%d].state=%d ", - i, jobs[i].state); - } - printf("%s", jobs[i].cmdline); - } - } -} /****************************** * end job list helper routines ******************************/ @@ -758,17 +593,7 @@ void builtin_command_quit() } // return true if suspended, otherwise false -bool job_suspend(struct job_t *jobs, pid_t pid) { - int i; - - for (i = 0; i < MAXJOBS; i++) - if (jobs[i].pid == pid) - { - jobs[i].state = ST; - return true; - } - return false; -} + inline int block_all(sigset_t *prev) { diff --git a/src/setting.cc b/src/setting.cc new file mode 100644 index 0000000..0461c16 --- /dev/null +++ b/src/setting.cc @@ -0,0 +1,8 @@ +#include "setting.h" + +namespace ov4 +{ + +int verbose = 0; + +} -- cgit v1.3.1-4-g156e