-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob.c
More file actions
179 lines (161 loc) · 4.7 KB
/
Copy pathjob.c
File metadata and controls
179 lines (161 loc) · 4.7 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
171
172
173
174
175
176
177
178
179
#include "job.h"
//#define _POSIX_SOURCE 199309L
//#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <termios.h>
// function to create a new job
struct Job* createJob(int is_background, const char* command, pid_t pid) {
struct termios setting;
if(tcgetattr(STDIN_FILENO, &setting)< 0) {
perror("tcgetattr");
exit(EXIT_FAILURE);
}
struct Job* newJob = (struct Job*)malloc(sizeof(struct Job));
if (newJob == NULL) {
perror("Error creating job");
exit(EXIT_FAILURE);
}
// assign vars
newJob->jobId = ++jobCounter;
newJob->is_background = is_background;
strncpy(newJob->command, command, sizeof(newJob->command) - 1);
newJob->status = RUNNING; // default = RUNNING
newJob->pid = pid;
newJob->setting = setting;
newJob->next = NULL;
newJob->prev = NULL;
if(is_background){printf("[%d]%d\n",newJob->jobId,(int)newJob->pid);}
return newJob;
}
// function to add a job to the doubly linked list
void addJob(int is_background, const char* command, pid_t pid) {
struct Job* newJob = createJob(is_background, command, pid);
if (job_list == NULL) {
// when list is empty
job_list = newJob;
tail = newJob;
} else {
// when list is not empty
tail->next = newJob;
newJob->prev = tail;
tail = newJob;
}
}
// function to remove a job from the doubly linked list
void removeJob(pid_t pid) {
struct Job* current = job_list;
while (current != NULL && current->pid != pid) {
current = current->next;
}
if (current == NULL) {
printf("Job with ID %d not found\n", pid);
return;
}
if (current->prev == NULL) {
// If the job to be removed is the first one
job_list = current->next;
if (job_list != NULL) {
job_list->prev = NULL;
}
} else {
// If the job to be removed is not the first one
current->prev->next = current->next;
if (current->next != NULL) {
current->next->prev = current->prev;
}
}
if (current == tail) {
// Update the tail if the removed job was the last one
tail = current->prev;
}
// Update the jobid
struct Job* update = current->next;
while (update!=NULL){
update->jobId--;
update = update->next;
}
jobCounter--;
free(current);
//printf("Job with PID %d removed\n", pid);
}
// function to print the doubly linked list
void printJobs() {
struct Job* current = job_list;
while (current != NULL) {
printf("[%d]%d %-20s %s\n", current->jobId, (int)current->pid,(current->status == RUNNING)
? "Running" : (current->status == BLOCKED) ? "Blocked" : "Ready",current->command);
current = current->next;
}
}
// function to free the memory allocated for the doubly linked list
void freeJobList() {
struct Job* current = job_list;
while (current != NULL) {
struct Job* temp = current;
current = current->next;
free(temp);
}
}
// function to create a new node with given flag and pid
struct Revise* createNode(pid_t pid, int flag) {
struct Revise* newNode = (struct Revise*)malloc(sizeof(struct Revise));
if (newNode == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
newNode->pid = pid;
newNode->flag = flag;
newNode->next = NULL;
return newNode;
}
// function to insert a node at the end of the linked list
void add(pid_t pid, int flag) {
struct termios setting;
if(tcgetattr(STDIN_FILENO, &setting)< 0) {
perror("tcgetattr");
exit(EXIT_FAILURE);
}
struct Revise* newNode = createNode(pid, flag);
newNode ->setting = setting;
if (revise_list == NULL) {
revise_list = newNode;
} else {
struct Revise* temp = revise_list;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
// function to remove the first node from the linked list
struct Revise* pop() {
if (revise_list == NULL) {
return NULL;
}
struct Revise* temp = revise_list;
revise_list = revise_list->next;
return temp;
}
// function to print the linked list
void printList() {
struct Revise* temp = revise_list;
while (temp != NULL) {
printf("PID: %d, Flag: %d\n", temp->pid, temp->flag);
temp = temp->next;
}
}
// function to free memory allocated for the linked list
void freeList() {
struct Revise* temp;
while (revise_list != NULL) {
temp = revise_list;
revise_list = revise_list->next;
free(temp);
}
}