forked from Septchi/CS_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeShareL.c
More file actions
113 lines (98 loc) · 2.35 KB
/
Copy pathtimeShareL.c
File metadata and controls
113 lines (98 loc) · 2.35 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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
typedef struct {
char processName[50];
int duration;
} process;
typedef struct node *nd;
typedef struct node {
process proc;
nd next;
} NODE;
void createQueue(nd*, nd*);
bool isEmpty(nd );
void enqueue(nd *, nd *, process);
process dequeue(nd *, nd *);
process inputProcess();
void timeShare();
int main(void) {
timeShare();
}
void createQueue (nd *front, nd *rear) {
*front = NULL;
*rear = NULL;
}
bool isEmpty(nd rear) {
return (rear == NULL) ? true : false;
}
void enqueue(nd *front, nd *rear, process input) {
nd newNode = malloc(sizeof(NODE));
newNode->proc.duration = input.duration;
strcpy(newNode->proc.processName, input.processName);
newNode->next = NULL;
if (*rear != NULL) {
(*rear)->next = newNode;
*rear = newNode;
}
else {
*rear = newNode;
*front = *rear;
}
}
process dequeue(nd *front, nd *rear) {
nd temp = *front;
//returnVal.duration = temp->proc.duration;
//strcpy(returnVal.processName, temp->proc.processName);
process returnVal = temp->proc;
*front = temp->next;
if (*front == NULL) {
*rear = NULL;
}
free(temp);
return returnVal;
}
process inputProcess() {
process userInput;
printf("Enter process name: ");
gets(userInput.processName);
fflush(stdin);
printf("Enter process duration: ");
scanf("%d", &userInput.duration);
fflush(stdin);
printf("\n");
return userInput;
}
void timeShare() {
process temp;
nd front, rear;
bool empty = false;
createQueue(&front, &rear);
for (int i = 0; i < 2; i++) {
temp = inputProcess();
enqueue(&front, &rear, temp);
}
system("cls");
empty = isEmpty(rear);
while (!empty) {
temp = dequeue(&front, &rear);
printf("Processing %s...\n", temp.processName);
Sleep((temp.duration <= 10) ? temp.duration * 1000 : 10000);
temp.duration = (temp.duration <= 10) ? 0 : temp.duration - 10;
if (temp.duration == 0) {
printf("Process %s is finished!\n", temp.processName);
Sleep(1000);
}
else {
printf("Process %s is being requeued. Time remaining: %d\n", temp.processName, temp.duration);
Sleep(1000);
enqueue(&front, &rear, temp);
}
empty = isEmpty(rear);
}
printf("All processes finished running.\n");
Sleep(3000);
return;
}