-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.c
166 lines (136 loc) · 3.8 KB
/
jobs.c
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* mshell - a job manager */
#include <stdio.h>
#include <string.h>
#include "jobs.h"
#define MAXJOBS 16 /* max jobs at any point in time */
static struct job_t jobs[MAXJOBS]; /* the job list */
int nextjid = 1; /* next job ID to allocate */
/*** Helper routines that manipulate the job list ***/
/* clearjob - Clear the entries in a job struct */
void jobs_clearjob(struct job_t *job) {
job->jb_pid = 0;
job->jb_jid = 0;
job->jb_state = UNDEF;
job->jb_cmdline[0] = '\0';
}
/* initjobs - Initialize the job list */
void jobs_initjobs() {
int i;
for (i = 0; i < MAXJOBS; i++)
jobs_clearjob(&jobs[i]);
}
/* maxjid - Returns largest allocated job ID */
int jobs_maxjid() {
int i, max = 0;
for (i = 0; i < MAXJOBS; i++)
if (jobs[i].jb_jid > max)
max = jobs[i].jb_jid;
return max;
}
/* addjob - Add a job to the job list */
int jobs_addjob(pid_t pid, int state, char *cmdline) {
int i;
if (pid < 1)
return 0;
for (i = 0; i < MAXJOBS; i++) {
if (jobs[i].jb_pid == 0) {
jobs[i].jb_pid = pid;
jobs[i].jb_state = state;
jobs[i].jb_jid = nextjid++;
if (nextjid > MAXJOBS)
nextjid = 1;
strcpy(jobs[i].jb_cmdline, cmdline);
if (verbose) {
printf("Added job [%d] %d %s\n", jobs[i].jb_jid, (int) jobs[i].jb_pid,
jobs[i].jb_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 jobs_deletejob(pid_t pid) {
int i;
if (pid < 1)
return 0;
for (i = 0; i < MAXJOBS; i++) {
if (jobs[i].jb_pid == pid) {
jobs_clearjob(&jobs[i]);
nextjid = jobs_maxjid() + 1;
return 1;
}
}
return 0;
}
/* fgpid - Return PID of current foreground job, 0 if no such job */
pid_t jobs_fgpid() {
int i;
for (i = 0; i < MAXJOBS; i++)
if (jobs[i].jb_state == FG)
return jobs[i].jb_pid;
return 0;
}
/* getjobpid - Find a job (by PID) on the job list */
struct job_t *jobs_getjobpid(pid_t pid) {
int i;
if (pid < 1)
return NULL;
for (i = 0; i < MAXJOBS; i++)
if (jobs[i].jb_pid == pid)
return &jobs[i];
return NULL;
}
/* getjobjid - Find a job (by JID) on the job list */
struct job_t *jobs_getjobjid(int jid) {
int i;
if (jid < 1)
return NULL;
for (i = 0; i < MAXJOBS; i++)
if (jobs[i].jb_jid == jid)
return jobs + i;
return NULL;
}
/* pid2jid - Map process ID to job ID */
int jobs_pid2jid(pid_t pid) {
int i;
if (pid < 1)
return 0;
for (i = 0; i < MAXJOBS; i++)
if (jobs[i].jb_pid == pid) {
return jobs[i].jb_jid;
}
return 0;
}
/*getstoppedjob - Return a stopped job if any */
struct job_t *jobs_getstoppedjob() {
int i;
for (i = 0; i < MAXJOBS; i++)
if (jobs[i].jb_state == ST)
return &jobs[i];
return NULL;
}
/* listjobs - Print the job list */
void jobs_listjobs() {
int i;
for (i = 0; i < MAXJOBS; i++) {
if (jobs[i].jb_pid != 0) {
printf("[%d] (%d) ", jobs[i].jb_jid, (int) jobs[i].jb_pid);
switch (jobs[i].jb_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].jb_state);
}
printf("%s\n", jobs[i].jb_cmdline);
}
}
}