-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJobs.h
134 lines (111 loc) · 3.31 KB
/
Jobs.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
132
133
134
#ifndef UNTITLED6_JOBS_H
#define UNTITLED6_JOBS_H
#include <sys/types.h>
#include <stddef.h>
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "sys/wait.h"
#include "string.h"
#define MAX_CMD_LEN 1025
#define MAX_JOBS 100
typedef struct Job {
int id;
pid_t pid;
char command[MAX_CMD_LEN];
struct Job *next;
} Job;
Job *jobs = NULL;
void add_job(pid_t pid, const char *command) {
Job *new_job = (Job *)malloc(sizeof(Job));
if (!new_job) {
perror("malloc");
_exit(EXIT_FAILURE);
}
//if jobs list is empty, assign the id to 1
if(jobs == NULL) {
new_job->id = 1;
} else {
new_job->id = jobs->id + 1;
}
new_job->pid = pid;
strncpy(new_job->command, command, MAX_CMD_LEN);
//remove ampersand from the end of the command
new_job->command[strlen(new_job->command) - 1] = '\0';
//check if theres spaces at the end of the command and remove them
while(new_job->command[strlen(new_job->command) - 1] == ' ') {
new_job->command[strlen(new_job->command) - 1] = '\0';
}
new_job->command[MAX_CMD_LEN - 1] = '\0'; // Ensure null-termination
new_job->next = jobs;
jobs = new_job;
}
// Function to compare two jobs based on their PIDs for qsort
int compare_jobs(const void *a, const void *b) {
Job *jobA = *(Job **)a;
Job *jobB = *(Job **)b;
return (jobA->pid - jobB->pid);
}
// Function to print the list of background jobs in ascending order of their PIDs
void print_jobs() {
// First, count the number of jobs
int count = 0;
Job *current = jobs;
while (current != NULL) {
count++;
current = current->next;
}
// Create an array of pointers to jobs
Job **jobArray = malloc(count * sizeof(Job *));
current = jobs;
for (int i = 0; i < count; i++) {
jobArray[i] = current;
current = current->next;
}
// Sort the array based on PIDs
qsort(jobArray, count, sizeof(Job *), compare_jobs);
// Print the jobs from the sorted array
for (int i = 0; i < count; i++) {
printf("[%d] %s &\n", jobArray[i]->id, jobArray[i]->command);
}
if(jobArray != NULL) {
free(jobArray);
}
}
// Function to remove completed jobs from the list---------------------------------------------------------------------
void remove_completed_jobs() {
Job *current = jobs;
Job *prev = NULL;
while (current != NULL) {
int status;
pid_t result = waitpid(current->pid, &status, WNOHANG);
if (result == 0) { // Child still running
prev = current;
current = current->next;
} else { // Child finished
if (prev == NULL) { // Head of the list
jobs = current->next;
} else {
prev->next = current->next;
}
Job *temp = current;
current = current->next;
if (temp != NULL) {
free(temp);
}
}
}
}
//iterate through all jobs and kill the jobs that are still running
void kill_all_jobs() {
Job *current = jobs;
while (current != NULL) {
kill(current->pid, SIGKILL);
if (current->next != NULL) {
current = current->next;
} else {
break;
}
}
}
#endif //UNTITLED6_JOBS_H