forked from ducminh296/Linux-CFS-Emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
106 lines (103 loc) · 2.84 KB
/
util.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
//*****************************************************************************
// util.c - Utility implementations
//
// AUTHOR: Minh Mai
//
// Utility include:
// + PRIORITY QUEUE Utility
//
//
//
//*****************************************************************************
#include "util.h"
//*****************************************************************************
//
// PROCESS PRORITY QUEUE FACILITY Implementations
//
//
//*****************************************************************************
// Function to check priority and place element
void static check(struct process_t *process, struct queue_t *queue);
//*****************************************************************************
//
//! \External
//!
//! Initialize the process queue before using
//! \param pointer to the queue
//!
//! \return none
//
//*****************************************************************************
void init_queue(struct queue_t *queue)
{
queue->in = 0;
queue->out = 0;
queue->k = QUEUE_SIZE;
}
//*****************************************************************************
//
//! \External
//! Add new process into the queue
//!
//! \param pointer to the process
//! \param pointer to the queue
//!
//! \return none
//
//*****************************************************************************
void append(struct process_t *process, struct queue_t *queue)
{
check(process,queue);
queue->in = (queue->in + 1) % queue->k;
}
//*****************************************************************************
//
//! \External
//!
//! Take the highest priority process out of the queue
//!
//! \param pointer to the queue
//!
//! \return pointer to the process
//
//*****************************************************************************
struct process_t *take(struct queue_t *queue)
{
struct process_t *w;
/* if queue is empty then return NULL */
if (queue->out == queue->in) return NULL;
/* else return pointer to a process */
w = &(queue->pool[queue->out]);
queue->out = (queue->out + 1) % queue->k;
return w;
}
//*****************************************************************************
//
//! \Internal
//!
//! Function to check priority of process and place process into the queue
//!
//! Process with highest priority will be placed in front of the queue
//!
//! \param process
//!
//! \return none
//
//*****************************************************************************
void static check(struct process_t *process, struct queue_t *queue)
{
int i,j;
for (i = queue->out; i <= queue->in; i++)
{
if (process->prio <= queue->pool[i].prio || queue->pool[i].pid == 0)
{
for (j = queue->in + 1; j > i; j--)
{
queue->pool[j] = queue->pool[j - 1];
}
queue->pool[i] = *process;
return;
}
}
queue->pool[i] = *process;
}