-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjobs.c
More file actions
170 lines (147 loc) · 4.19 KB
/
jobs.c
File metadata and controls
170 lines (147 loc) · 4.19 KB
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
167
168
169
170
#include "headers/jobs.h"
#include "headers/utils.h"
// Add a new job
int add_job(pid_t pid, char *command) {
if (job_count >= MAX_JOBS) {
print_error("Maximum number of jobs reached");
return -1;
}
// Find empty slot
for (int i = 0; i < MAX_JOBS; i++) {
if (jobs[i].pid == 0) {
jobs[i].pid = pid;
jobs[i].job_id = ++current_job_id;
jobs[i].command = strdup(command);
jobs[i].status = 0; // Running
jobs[i].start_time = time(NULL);
job_count++;
return jobs[i].job_id;
}
}
return -1;
}
// Remove a job
int remove_job(pid_t pid) {
for (int i = 0; i < MAX_JOBS; i++) {
if (jobs[i].pid == pid) {
if (jobs[i].command) {
free(jobs[i].command);
}
jobs[i].pid = 0;
jobs[i].job_id = 0;
jobs[i].command = NULL;
jobs[i].status = 0;
jobs[i].start_time = 0;
job_count--;
return 0;
}
}
return -1;
}
// Find job by job ID
job_t* find_job(int job_id) {
for (int i = 0; i < MAX_JOBS; i++) {
if (jobs[i].job_id == job_id && jobs[i].pid != 0) {
return &jobs[i];
}
}
return NULL;
}
// Find job by process ID
job_t* find_job_by_pid(pid_t pid) {
for (int i = 0; i < MAX_JOBS; i++) {
if (jobs[i].pid == pid) {
return &jobs[i];
}
}
return NULL;
}
// Update job status
void update_job_status(pid_t pid, int status) {
job_t *job = find_job_by_pid(pid);
if (job) {
job->status = status;
}
}
// Print all jobs
void print_jobs(void) {
printf("Job ID\tPID\tStatus\tCommand\n");
printf("------\t---\t------\t-------\n");
for (int i = 0; i < MAX_JOBS; i++) {
if (jobs[i].pid != 0) {
const char *status_str = "Unknown";
switch (jobs[i].status) {
case 0: status_str = "Running"; break;
case 1: status_str = "Stopped"; break;
case 2: status_str = "Completed"; break;
}
printf("[%d]\t%d\t%s\t%s\n",
jobs[i].job_id, jobs[i].pid, status_str, jobs[i].command);
}
}
}
// Wait for a specific job
int wait_for_job(int job_id) {
job_t *job = find_job(job_id);
if (!job) {
print_error("No such job");
return -1;
}
int status;
pid_t result = waitpid(job->pid, &status, 0);
if (result == -1) {
print_error_with_errno("waitpid failed");
return -1;
}
// Remove completed job
remove_job(job->pid);
return WEXITSTATUS(status);
}
// Resume a job (foreground or background)
int resume_job(int job_id, int foreground) {
job_t *job = find_job(job_id);
if (!job) {
print_error("No such job");
return -1;
}
if (job->status == 1) { // Stopped
if (kill(job->pid, SIGCONT) == -1) {
print_error_with_errno("Cannot resume job");
return -1;
}
job->status = 0; // Running
}
if (foreground) {
// Bring to foreground
if (tcsetpgrp(STDIN_FILENO, getpgid(job->pid)) == -1) {
print_error_with_errno("Cannot bring job to foreground");
return -1;
}
// Wait for job to complete
int status;
waitpid(job->pid, &status, WUNTRACED);
if (WIFSTOPPED(status)) {
job->status = 1; // Stopped
printf("\n[%d] Stopped\t%s\n", job->job_id, job->command);
} else {
remove_job(job->pid);
}
// Restore terminal control
tcsetpgrp(STDIN_FILENO, getpgrp());
}
return 0;
}
// Clean up completed jobs
void cleanup_completed_jobs(void) {
for (int i = 0; i < MAX_JOBS; i++) {
if (jobs[i].pid != 0) {
int status;
pid_t result = waitpid(jobs[i].pid, &status, WNOHANG);
if (result > 0) {
// Process has terminated
printf("\n[%d] Done\t%s\n", jobs[i].job_id, jobs[i].command);
remove_job(jobs[i].pid);
}
}
}
}