Professional Documents
Culture Documents
OPERATING SYSTEM
Assignment
Contents
1. Scheduler ..................................................................................................................... 2
1.1. Question – Priority Feedback Queue .................................................................. 2
1.2. Result – Gantt Diagrams ................................................................................... 3
1.3. Implementation .................................................................................................. 4
1.3.1. Priority Queue ............................................................................................. 4
1.3.2. Scheduler ..................................................................................................... 5
2. Memory Management .................................................................................................. 6
2.1. Question – Segmentation with Paging ................................................................ 6
2.2. Result – Status of RAM ..................................................................................... 6
2.3. Implementation .................................................................................................11
2.3.1. Find page table from segment .....................................................................11
2.3.2. Map virtual addresses to physical addresses ...............................................12
2.3.3. Memory allocation ......................................................................................12
2.3.4. Deallocating Memory ..................................................................................16
3. Put it all together .......................................................................................................18
1
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
1. Scheduler
1.1. Question – Priority Feedback Queue
QUESTION: What is the advantage of using priority feedback queue in comparison with
other scheduling algorithms you have learned?
The Priority Feedback Queue (PFQ) algorithms uses the ideas of a number of other
algorithms, including Priority Scheduling algorithm– each process has a priority for
execution, Multilevel Queue algorithm – using mutliple queue levels of processes, Round
Robin algorithm – uses quantum time for processes to execute. Here are other algorithms
learned
Specifically, the PFQ algorithm uses two (2) queues: ready_queue and run_queue with
the following meanings:
• ready_queue: This queue contains processes with higher execution priority than
the run_queue queue. When the CPU moves to the next slot, it searches for the
process in this queue.
• run_queue: This queue contains processes that are waiting to continue executing
after running out of its slot without completing its process. Processes in this queue
can only continue to the next slot when ready_queue is idle and are moved to the
ready_queue queue to consider the next slot.
• Both queues are priority queues, the priority is based on the priority of the process
in the queue.
Using 2 queues, with the idea of MLQS and MLFQ algorithms, in which 2 queues are
switched between processes until the process is completed, increasing the response time
2
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
for processes. (lower-priority processes that come later can still be executed before higher-
priority processes after they have finished their slots).
Fairness between processes is guaranteed, only depending on the available priority of the
processes. Specifically, in a certain time interval t0, if processes are executing, it
completely depends on their priority. If another process p0 enters the priority queue and
depends on its priority, even though the other processes with higher priority have finished
executing before. They also cannot dispute with process p0 because they are waiting in
run_queue until ready_queue is idle, i.e. p0 has been executing its slot.
REQUIREMENT: Draw Gantt diagram describing how processes are executed by the
CPU
Below is the Gantt diagram for the case in the source code.
Test 0:
In this test, CPU processes on 2 processes S0 and S1 in 22 time slots as above Gantt
diagram
Test 1:
3
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
In this test, CPU processes on 4 processes S0, S1, S2 and S3 in 43 time slots as above
Gantt diagram
1.3. Implementation
1.3.1. Priority Queue
The priorit queue in this case handles no more than 10 processes, so we simply need to
use the loop to handle the functionality a priority queue needs. Specifically with the
enqueue() function, we only need to put it at the end of the queue if it is available
(empty). With the dequeue() function, we browse for the process with the highest
priority, and update the queue’s state when deleting an element.
4
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
}
return proc;
}
1.3.2. Scheduler
The task of the scheduler is to manage the updating of the processes that will be executed
to the CPU. Specifically, the scheduler will manage 2 ready_queue and run_queue as
described above. In this assignment, we just need to implement the function to find a
process for the CPU to execute. Specifically, with the get_proc() function which returns
a process in the ready_queue, if the ready_queue is idle, we update the queue with
processes that are waiting for the next slots in the run_queue. On the contrary, we find
the processes with high priority from this queue.
pthread_mutex_lock(&queue_lock);
if (!empty(&ready_queue))
{
proc = dequeue(&ready_queue);
}
else
{
int i;
for (i = 0; i < run_queue.size; i++)
{
enqueue(&ready_queue, run_queue.proc[i]);
}
ready_queue.size = run_queue.size;
for (i = 0; i < run_queue.size; i++)
{
run_queue.proc[i] = NULL;
}
run_queue.size = 0;
proc = dequeue(&ready_queue);
}
pthread_mutex_unlock(&queue_lock);
return proc;
}
5
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
2. Memory Management
2.1. Question – Segmentation with Paging
QUESTION: What is the advantage and disadvantage of segmentation with paging?
Advantage
Disadvantage
Below are the results of the logging process after each allocation and deallocation
function call in the program, specifically recording the state of RAM in the program at
each step.
6
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
7
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
8
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
9
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
10
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
=======Deallocation=======
014: 03800-03bff - PID: 01 (idx 000, nxt: 015)
015: 03c00-03fff - PID: 01 (idx 001, nxt: -01)
*NOTE: In order to show the allocation and deallocation function call, we add another
dump() function at the end of alloc_mem() and free_mem() function, right before
pthread_mutex_unlock(&mem_lock);.
If you want to see the result above in the terminal, please uncomment those code lines
in the mem.c file.
2.3. Implementation
2.3.1. Find page table from segment
In this assignment, each address is represented by 20 bits, where the first 5 bits represent
the segment index, the next 5 bits represent the page index, and the last 10 bits represent
the offset.
This function takes the 5 bit segment index and the seg_table, needs to find the res
of the paging table of the corresponding segment in the above segment table.
int i;
for (i = 0; i < seg_table->size; i++) {
if (index == seg_table->table[i].v_index){
return seg_table->table[i].pages;
}
}
return NULL;
11
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
int i;
for (i = 0; i < page_table->size; i++) {
if (page_table->table[i].v_index == second_lv) {
*physical_addr = offset +
(page_table->table[i].p_index << OFFSET_LEN);
return 1;
}
}
return 0;
}
On the physical area, we check the number of empty pages, not yet used by any process,
if enough pages need to be allocated, the physical area is ready. In addition, to optimize
the search time when falling into the case of insufficient memory, we can organize
12
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
mem_stat in the form of a list, including size management, free memory,… to access
needed information quickly.
On the logical memory area, we check based on the break point of the process, not
exceeding the allowed memory area.
int mem_avail = 0;
int count_free_page = 0;
int i;
for (i = 0; i < NUM_PAGES; i++)
{
if (_mem_stat[i].proc == 0) {
count_free_page++;
}
}
• Browse the physical memory, find idle pages, assign this page to be used by the
process.
• Create variable prev_page to update next value easier.
• On the logical memory area, based on the allocated address, counting from the
start address and the page order position, we find its segments and pages. From
there we update corresponding paging and segment tables.
13
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
if (mem_avail==1) {
/* We could allocate new memory region to the process */
ret_mem = proc->bp;
proc->bp += num_pages * PAGE_SIZE;
/* Update status of physical pages which will be allocated
* to [proc] in _mem_stat. Tasks to do:
* - Update [proc], [index], and [next] field
* - Add entries to segment table page tables of [proc]
* to ensure accesses to allocated memory slot is
* valid. */
// Initializing ...
addr_t vir_addr = ret_mem;
int up_idx_count = 0;
int seg_idx = -1;
int page_idx = -1;
int prev_page = -1;
14
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
_mem_stat[i].index = up_idx_count++;
// If prev_page is not the initial value (!= -1) we will update the
previous _mem_stat's next field
if (prev_page != -1) {
_mem_stat[prev_page].next = i;
}
prev_page = i;
// Calculate the segment index and updating the table for index
seg_idx = get_first_lv(vir_addr);
// Finding the position in the segment table that has the v_index ==
seg_idx
// Check whether found or not, if not found that means this is a new
segment to the table
// So we need to increase the size of segment table by 1
int position;
int found = 0;
for (position = 0; position < proc->seg_table->size; position++) {
if (seg_idx == proc->seg_table->table[position].v_index) {
found = 1;
break;
}
}
if (found == 0) {
proc->seg_table->size++;
}
// If the pages are not there yet, we must allocate the pages
if (proc->seg_table->table[position].pages == NULL) {
proc->seg_table->table[position].pages
= (struct page_table_t *)malloc(sizeof(struct page_table_t));
proc->seg_table->table[position].pages->size = 0;
}
// To make the virtual memory to be contiguous, we must allocate the new
page right after the last page
// For example, the size of the page table is 5, that means we have the
pages 0, 1, 2, 3, 4
// The new page to be allocate will be 5, exactly the size of pages table
// The codes below show the assignment of pages table and increment of
the pages table size
// v_index of the table will be the second level of virtual address take
by get_second_lv(vir_addr)
// p_index exactly the i in the loop
// The virtual address we have is ( 5 | 5 | 10 ) for first level, second,
offset respectively
// The physical address we have will is (10 | 10) for the position in
_mem_stat (i) and offset from virtual address
page_idx = proc->seg_table->table[position].pages->size;
proc->seg_table->table[position].v_index = seg_idx;
proc->seg_table->table[position].pages->table[page_idx].v_index
= get_second_lv(vir_addr);
proc->seg_table->table[position].pages->table[page_idx].p_index = i;
proc->seg_table->table[position].pages->size++;
// The virtual address increase to be used in the next loop
vir_addr += PAGE_SIZE;
// Now that the number of page we have allocate is equal to num_pages,
the next field of our will be -1 and step out the loops
if (up_idx_count == num_pages) {
_mem_stat[i].next = -1;
15
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
break;
}
}
}
}
//Uncomment code below to show the allocation process
/*
printf("========Allocation==========");
dump();
*/
pthread_mutex_unlock(&mem_lock);
return ret_mem;
}
• Convert logical address from the process to the physical, the based on the value
next of mem, we update the corresponding address string
• Based on the number of deleted pages on the block of the physical address, we find
the pages of the logical address in turn, based on the address, we find the
corresponding segment and page. Then update the paging table again, after the
update process, if the table is empty, delete this table in the segment
• Only when the last block on the logical address is deleted, then from there cycle
back through the pages, until the page is being used, then stop
16
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
17
Ho Chi Minh University of Technology - Faculty of Computer Science & Engineering
Test 0:
Test 1:
18