-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJobManager.h
executable file
·131 lines (94 loc) · 4.07 KB
/
JobManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* file: JobManager.h
Job Manager Class Header File
==================================================
Wimpy Shell Project - Com Sci 342
Shea Daniels
This class manages and keeps track of all the background
jobs that are being run. It is in charge of starting,
managing the status of, and waiting for all of the
background jobs spawned by the shell. Each background
job is represented by an instance of the BackJob class.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Information on constructors: !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
JobManager()
--------------------------------------------------
This is the basic constructor for the class.
POST: The object has been initialized. There are
no background jobs running.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Information on public methods: !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void createBackgroundJob(Command new_command)
--------------------------------------------------
Creates and tries to execute as a background job
the command the user passes.
PRE: new_command must be a parsed Command object.
POST: If the job starts successfully, it is added to
the jobs data structure as a running job.
Otherwise, it is recorded as failed.
bool waitForJob(int job_num)
--------------------------------------------------
Tries to suspend the process until the background
job specified by the passed integer has finished
executing.
PRE: job_num is an integer that refers to a job
number of a background job.
POST: Returns true after the background process is
finished. Returns false if the job number
specified by the user doesn't correspond
to a running process or if an error was
encountered.
void updateJobStatus()
--------------------------------------------------
Checks for all background jobs that have finished
executing and updates their status.
POST: All jobs that have finished executing have
their status set to finished.
void printJobs()
--------------------------------------------------
Prints to standard out all of the running and
recently finished jobs.
POST: All running and finished jobs are printed.
void clearOldJobs()
--------------------------------------------------
Clears out all recently finished background jobs.
If no jobs are running and there is nothing left
to display to the user, the job history is cleaned
out. (next new background job will have a job # of 1)
POST: All finished jobs are set to terminated. If no
jobs are left running or set to be displayed,
the data structure that keeps track of jobs
is cleared out.
*/
#ifndef MNGR_HEADER
#define MNGR_HEADER
#include "Command.h"
#include "BackJob.h"
#include <vector>
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
class JobManager {
public:
//constructor
JobManager();
// job control methods
void createBackgroundJob(Command new_command);
bool waitForJob(int job_num);
// methods related to job status updates
void updateJobStatus();
void printJobs();
void clearOldJobs();
private:
// vector index conversion methods
int noToVec(int job_num);
int vecToNo(int vec_index);
//------------------------------------------------------------
// Data
//------------------------------------------------------------
vector<BackJob> jobs;
int num_running;
int num_finished;
};
#endif