SlideShare a Scribd company logo
1 of 82
CIS*3110 Winter 2016
CIS*3110 (Operating Systems)
Assignment 2: CPU Simulation
Due Date: Sunday, March 6, 2016 at 23:59.
Requirements and Specifications
Objective
The goal of this assignment is to develop a CPU scheduling
algorithm that will complete the
execution of a group of multi-threaded processes in an OS that
understands threads (kernel
threads). Since a real implementation of your scheduling
algorithm is not feasible, you will
implement a simulation of your CPU scheduling algorithm.
Each process will have 1-50 threads;
each of the threads has its own CPU and I/O requirements. The
simulated scheduling policy is on
the thread level. While your simulation will have access to all
the details of the processes that need
to execute, your CPU scheduling algorithm CANNOT take
advantage of future knowledge.
Specification
Given a set of processes to execute with CPU and I/O
requirements, your CPU simulator will
simulate the execution of the threads based on your developed
CPU scheduling policies (FCFS
and RR). Your simulation will collect the following statistics:
• the total time required to execute all the threads in all the
processes
• the CPU utilization (NOT the CPU efficiency)
• the average turnaround time for all the processes
• the service time (or CPU time), I/O time and turnaround time
for each individual thread
Your simulation structure should be a next event simulation.
The next event approach to simulation
is the most common simulation model. At any given time, the
simulation is in a single state. The
simulation state can only change at event times, where an event
is defined as an occurrence that
may change the state of the system.
Events in this CPU simulation are the following:
• thread arrival
• the transition of a thread state (e.g. when an interrupt occurs
due to a time slice, the thread
moves from running state to ready state).
Each event occurs at a specified time. Since the simulation state
only changes at an event, the
clock can be advanced to the next most recently scheduled event
(the meaning of next event
simulation model).
CIS*3110 Winter 2016
Events are scheduled via an event queue. The event queue is a
sorted queue which contains
"future" events; the queue is sorted by the time of these "future"
events. The event queue is
initialized to contain the arrival of all threads. The main loop of
the simulation consists of processing
the next event, perhaps adding more future events in the queue
as a result, advancing the clock,
and so on until all threads have terminated.
Simulation Execution
Your simulation program will be invoked as:
simcpu [-d] [-v] [-r quantum] < input_file
where
• -d stands for detailed information
• -v stands for verbose mode
• –r indicates Round Robin scheduling with the given quantum
(an integer).
You can assume only these flags will be used with your
program, and that they will appear in the
order listed. The output for the default mode (i.e. no flags) or
with flags is defined in the output
format section. Note, the flags may be used separately or
together.
Input Format
The simulator input includes:
• number of processes that require execution
• the thread switch overhead time (i.e., the number of time units
required to switch to a new
thread in the SAME process)
• the process switch overhead time (i.e., the number of time
units required to switch to a new
thread in a DIFFERENT process)
• for each process
• the number of threads
• the arrival time for each thread
• the number of CPU execution bursts each thread requires (the
CPU execution bursts
are separated by the time it takes for the thread to do I/O)
• the CPU time and the I/O time
You should assume an infinite number of I/O devices. In other
words, threads do not need to wait
to do I/O. You should also assume that no overhead is
associated with placing a thread on, or
removing a thread from, an I/O device (e.g., no overhead is
associated with an "interrupt" to
indicate I/O completion). Since a thread must exit from the
system while in the executing state, the
last task of a thread is a CPU burst. All these simulation
parameters are integers. Your simulator
obtains these parameters from the input file, which is in the
following format.
CIS*3110 Winter 2016
number_of_processes thread_switch process_switch
process_number(1) number_of_threads(1)
thread_number(1) arrival_time(1) number_of_CPU(1)
1 cpu_time io_time
2 cpu_time io_time
...
number_of_CPU(1) cpu_time
thread_number(2) arrival_time(2) number_of_CPU(2)
1 cpu_time io_time
2 cpu_time io_time
…
number_of_CPU(2) cpu_time ...
...
thread_number(n) arrival_time(n) number_of_CPU(n)
1 cpu_time io_time
2 cpu_time io_time
…
number_of_CPU(n) cpu_time
process_number(2) number_of_threads(2)
thread_number(1) arrival_time(1) number_of_CPU(1)
1 cpu_time io_time
2 cpu_time io_time
...
number_of_CPU(1) cpu_time
thread_number(2) arrival_time(2) number_of_CPU(2)
1 cpu_time io_time
2 cpu_time io_time
…
number_of_CPU(2) cpu_time ...
...
thread_number(n) arrival_time(n) number_of_CPU(n)
1 cpu_time io_time
2 cpu_time io_time
…
number_of_CPU(n) cpu_time
CIS*3110 Winter 2016
The following is an example input file to the CPU simulation:
2 3 7
1 4
1 0 6
1 15 400
2 18 200
3 15 100
4 15 400
5 25 100
6 240
2 12 4
1 4 150
2 30 50
3 90 75
4 15
3 27 4
1 4 400
2 810 30
3 376 45
4 652
4 28 7
1 37 100
2 37 100
3 37 100
4 37 100
5 37 100
6 37 100
7 37
2 2
1 0 3
1 150 2
2 4 5
3 22
2 1 1
1 50
Where
• 2 3 7 : 2 is the number of processes, 3 is the number of time
units required to switch to a new
thread in the same process, 7 is the number of time units
required to switch to a new thread in
a different process
• 1 4 : 1 is the process number and 4 is the number of threads
• 1 0 6 : 1 is the thread number, 0 is the arrival time, 6 is the
number of CPU execution bursts
that each thread requires
• 1 15 400, 2 18 200, 3 15 100, 4 15 400, 5 25 100, 6 240 :
where 1 is the thread number and
15 is the CPU time and 400 is the I/O time and the last thread
only has CPU time (burst)
This example has 2 processes. Process 1 has 4 threads and
Process 2 has 2 threads.
CIS*3110 Winter 2016
Output Format
In default mode (i.e., no flags are given), the simulator adopts a
FCFS scheduling policy and the
output of the simulator consists of
• the total time required to execute the threads in all processes
to completion
• the average turnaround time of the processes
• the CPU utilization
As an example:
$ simcpu < input_file
FCFS Scheduling
Total Time required is 344 time units
Average Turnaround Time is 27.9 time units
CPU Utilization is 94%
In detailed information mode (i.e., -d flag is given), the output
of the simulation consists of
• the total time required to execute the threads in all the
processes
• the average turnaround time for all the processes
• the CPU utilization
• the arrival time, service time, I/O time, turnaround time and
finish time for each thread
For example,
$ simcpu -d –r 10 < input_file
Round Robin Scheduling (quantum = 10 time units)
Total Time required is 140 units
Average Turnaround Time is 9.2 time units
CPU Utilization is 100%
Thread 1 of Process 1:
arrival time: 0
service time: 32 units, I/O time: 3 units, turnaround time: 45
units, finish time: 45 units
Thread 2 of Process 1:
arrival time: 5
service time: 102 units, I/O time: 15 units, turnaround time:
135 units, finish time: 140 units
Thread 1 of Process 2:
arrival time: 10
service time: 22 units, I/O time: 3 units, turnaround time: 45
units, finish time: 55 units
In verbose mode, the output of the simulation includes the
scheduling decisions and the
switching of the threads. Specifically, the output includes every
event that occurs, the time of the
event, and the state transition:
CIS*3110 Winter 2016
At time X: Thread {id} of Process {id} moves from {state} to
{state}
For example:
At time 0: Thread 1 of Process 1 moves from new to ready
At time 7: Thread 1 of Process 1 moves from ready to running
…
Each state transition should be given as above on a separate
line. (Be sure to include events
that occur at the same time.) Since we do not include swapping
in our simulation, the state of a
thread can only be in one of five states: new, ready, running,
blocked, or terminated. Summary
information about each thread (as in detailed information mode)
should be given after a thread
has terminated.
Submission information will be posted on CourseLink.
assignment2/.DS_Store
__MACOSX/assignment2/._.DS_Store
assignment2/a2.pdf
CIS 3110: Operating Systems
Assignment 2: CPU Simulation
Due Date: Feb. 27, 2015
Overview
The goal of this assignment is to develop a CPU scheduling
algorithm that will complete the
execution of a group of multi-threaded processes in an OS that
understands threads (kernel
threads). Since a real implementation of your scheduling
algorithm is not feasible, you will
implement a simulation of your CPU scheduling algorithm.
Each process will have 1-50 threads;
each of the threads has its own CPU and I/O requirements. The
simulated scheduling policy is on
the thread level. While your simulation will have access to all
the details on the processes that
need to execute, your CPU scheduling algorithm can NOT take
advantage of future knowledge.
Task
Given a set of processes to execute with CPU and I/O
requirements, your CPU simulator should
simulate the execution of the threads based on your developed
CPU scheduling policies (say
FCFS or RR). Your simulation should collect the following
statistics: the total time required to
execute all the threads in all the processes, the CPU utilization
(NOT the CPU efficiency), the
average turnaround time for all the processes, and the service
time (or CPU time), I/O time, and
turnaround time for each individual thread.
Your simulation structure should be a next event simulation.
The next event approach to
simulation is the most common simulation model. At any given
time, the simulation is in a single
state. The simulation state can only change at event times,
where an event is defined as an
occurrence that may change the state of the system. Events in
this CPU simulation are the
following: thread arrival and the transition of a thread state
(e.g., when an interrupt occurs due to
a time slice, the thread moves from running state to ready state).
Each event occurs at a specified time. Since the simulation state
only changes at an event, the
clock can be advanced to the next most recently scheduled
event. (The meaning of next event
simulation model.)
Events are scheduled via an event queue. The event queue is a
sorted queue which contains
"future" events; the queue is sorted by the time of these "future"
events. The event queue is
initialized to contain the arrival of all threads. The main loop of
the simulation consists of
processing the next event, perhaps adding more future events in
the queue as a result, advancing
the clock, and so on until all threads have terminated.
Simulation Execution
Your simulation program will be invoked as:
simcpu [-d] [-v] [-r quantum] < input_file
where -d stands for detailed information, -v stands for verbose
mode and –r indicates Round
Robin scheduling with the given quantum (an integer). You can
assume only these flags will be
used with your program, and that they will appear in the order
listed. The output for the default
mode (i.e., no flags) or with flags is defined in the output
format section. Note, the flags may be
used separately or together.
Input Format
The simulator input includes the number_of_processes that
require execution, the thread_switch
overhead time (i.e., the number of time units required to switch
to a new thread in the SAME
process), the process_switch overhead time (i.e., the number of
time units required to switch to a
new thread in a DIFFERENT process), and, for each process,
the number_of_threads, the arrival
time for each thread, and number_of_CPU, the number of CPU
execution bursts each thread
requires; the CPU execution bursts are separated by time the
thread does I/O.
You should assume an infinite number of I/O devices. In other
words, threads do not need to
wait to do I/O. You should also assume that no overhead is
associated with placing a thread on,
or removing a thread from, an I/O device (e.g., no overhead is
associated with an "interrupt" to
indicate I/O completion). Since a thread must exit from the
system while in the executing state,
the last task of a thread is a CPU burst. All these simulation
parameters are integers. Your
simulator obtains these parameters from the input file, which is
in the following format. You can
assume the data in an input file is valid.
number_of_processes thread_switch process_switch
process_number(1) number_of_threads(1)
thread_number(1) arrival_time(1) number_of_CPU(1)
1 cpu_time io_time
2 cpu_time io_time
.
.
number_of_CPU(1) cpu_time
thread_number(2) arrival_time(2) number_of_CPU(2)
1 cpu_time io_time
2 cpu_time io_time
.
.
number_of_CPU(2) cpu_time
...
...
thread_number(n) arrival_time(n) number_of_CPU(n)
1 cpu_time io_time
2 cpu_time io_time
.
.
number_of_CPU(n) cpu_time
process_number(2) number_of_threads(2)
thread_number(1) arrival_time(1) number_of_CPU(1)
1 cpu_time io_time
2 cpu_time io_time
.
.
number_of_CPU(1) cpu_time
thread_number(2) arrival_time(2) number_of_CPU(2)
1 cpu_time io_time
2 cpu_time io_time
.
.
number_of_CPU(2) cpu_time
...
...
thread_number(n) arrival_time(n) number_of_CPU(n)
1 cpu_time io_time
2 cpu_time io_time
.
.
number_of_CPU(n) cpu_time
........
........
........
The following is an example input file to the CPU simulation
(there are no comments in a real
input file) :
2 3 7 // number_of_processes thread_switch
process_switch
1 4 // process_number(1) number_of_threads(1)
1 0 6 // thread_number(1) arrival_time(1)
number_of_CPU(1)
1 15 400 // 1 cpu_time io_time
2 18 200 // 2 cpu_time io_time
3 15 100 // 3 cpu_time io_time
4 15 400 // 4 cpu_time io_time
5 25 100 // 5 cpu_time io_time
6 240 // 6 cpu_time
2 12 4 // thread_number(2) arrival_time(2)
number_of_CPU(2)
1 4 150
2 30 50
3 90 75
4 15
3 27 4
1 4 400
2 810 30
3 376 45
4 652
4 28 7
1 37 100
2 37 100
3 37 100
4 37 100
5 37 100
6 37 100
7 37
2 2
1 0 3
1 150 2
2 4 5
3 22
2 1 1
1 50
Output Format
In default mode (i.e., no flags are given), the simulator adopts
FCFS scheduling policy, the
output of the simulator consists of the total time required to
execute the threads in all processes
to completion, the average turnaround time of the processes,
and the CPU utilization. As an
example:
$simcpu < input_file
FCFS:
Total Time required is 344 time units
Average Turnaround Time is 27.9 time units
CPU Utilization is 94%
In detailed information mode (i.e., -d flag is given), the output
of the simulation consists of the
total time required to execute the threads in all the processes,
the average turnaround time for all
the processes, the CPU utilization, and the arrival time, service
time, I/O time, turnaround time,
and finish time for each thread.
$simcpu -d –r 10 < input_file
Round Robin (quantum = 10 time units):
Total Time required is 140 units
Average Turnaround Time is 9.2 time units
CPU Utilization is 100%
Thread 1 of Process 1:
arrival time: 0
service time: 32 units
I/O time: 3 units
turnaround time: 45 units
finish time: 45 units
Thread 2 of Process 1:
arrival time: 5
service time: 102 units
I/O time: 15 units
turnaround time: 135 units
finish time: 140 units
Thread 1 of Process 2:
arrival time: 10
service time: 22 units
I/O time: 3 units
turnaround time: 45 units
finish time: 55 units
In verbose mode, the output of the simulation includes the
scheduling decisions and the
switching of the threads. Specifically, the output includes every
event that occurs, the time of the
event, and the state transition:
At time X: Thread {id} of Process {id} moves from {state} to
{state}
For example:
At time 0: Thread 1 of Process 1 moves from new to ready
At time 7: Thread 1 of Process 1 moves from ready to running
…
Each state transition should be given as above on a separate
line. (Be sure to include events that
occur at the same time.) Since we do not include swapping in
our simulation, the state of a thread
can only be in one of five states: new, ready, running, blocked,
or terminated.
Submission
programs, contact the teaching
assistant (TA) assigned to this course.
students. However, you are not
allowed to share code with any student.
CPU scheduling algorithm
and any assumptions you made. (If you make any assumption
because you think the
problem is not well specified, make sure to include those
assumptions in the report.) Your
report should also give answers to the following four questions.
In your discussion,
explain exactly how much overhead is included (if any).
i. Does your simulator include switch overhead for the first
ready state to
running state transition? Explain.
ii. Does your simulator include switch overhead if a thread
moves from ready
state to running state and the CPU is idle? Explain.
iii. Does your simulator include switch overhead if a thread
moves from
running state to blocked state and the ready queue is empty?
Explain.
iv. Does your simulation include switch overhead if a thread is
interrupted
(due to a time slice) and either the ready queue is empty or the
thread has
the highest priority? Explain.
present a demo arranged by
your TA’s request.
__MACOSX/assignment2/._a2.pdf
assignment2/how.docx
Thread control block inside priority queue
Key is the time, arrange everything based on time.
Control of thread
Main method: insert item, remove,
Remove-key who holds minimum key or time
Interested in which item hold the minimum key/time not
necessary to store in sorted form.
Two thread arrive at the same time.. so who arrived first? Total
order: (time1, processID1) happened before relation such that
(t1,p1) -> (t2,p2) if t1<t2
or if (time1 == time2)
if process id1 < process id2
heap to implement priority queue.
Can *copy implementation of heap and priority queue
Implement this threading process using linked list and moving
the information of thread in the linked list
__MACOSX/assignment2/._how.docx
assignment2/l14.pdf
1
Assignment 2 and Discrete
Event Simulation
Problem Specification
Discrete Event Simulation to analyze
different CPU scheduling algorithms.
(threads) will be simulated, each alternating
between bursts of CPU usage and I/O
waiting. The process data will be read in
from a data file.
2
Discrete Event Simulation
whenever the next meaningful event occurs. Some time
steps may be small ( even 0 if two or more things happen
at the same time ) and some may be large.
on the schedule to be processed when their time comes.
ta structure in DES is Priority Queue which
holds events to be scheduled.
3
4
Priority Queue ADT
collection of items
(key, element)
Queue ADT
m(k, e)
inserts an item with key k and
element e
removes the item with smallest
key and returns its element e
returns, but does not remove,
the smallest key of an item
returns, but does not remove,
the element of an item with
smallest key
5
Example: Priority Queue
Operator Output Priority Queue
insertItem(5, A) _ (5,A)
insertItem(9, C) _ (5,A),(9,C)
insertItem(3, B) _ (3,B),(5,A),(9,C)
insertItem(7, D) _ (3,B),(5,A),(7,D),(9,C)
minElement() B (3,B),(5,A),(7,D),(9,C)
minKey() 3 (3,B),(5,A),(7,D),(9,C)
removeMin() B (5,A),(7,D),(9,C)
size() 3 (5,A),(7,D),(9,C)
removeMin() A (7,D),(9,C)
removeMin() D (9,C)
removeMin() C
removeMin() error
isEmpty() true
6
Total Order Relation
on which an order is defined. For example,
simulation time.
same key. For example, two processes arrive at
the same time.
t2
are unique)
7
Using heap to implement PQ
storing keys at its internal
nodes and satisfying the
following properties:
-Order: for every
internal node v other than the
root,
be the height of the heap
- 1, there are
2i nodes of depth i
- 1, the internal
nodes are to the left of the
external nodes
2
6 5
7 9
is the rightmost internal
node of depth h - 1
last node
8
Heaps and Priority Queues
ep track of the position of the last node
(2, Sue)
(6, Mark) (5, Pat)
(9, Jeff) (7, Anna)
9
Insertion into a Heap
consists of three steps
rtion position
z (the new last node)
into an internal node
-order
property (discussed next)
2
6 5
7 9
insertion node
2
6 5
7 9 1
z
z
10
Upheap
key k, the heap-order property
may be
violated
-order property by
swapping k
along an upward path from the insertion node
whose parent has a key smaller than or equal to k
time
2
1 5
7 9 6 z
1
2 5
7 9 6 z
11
Removal from a Heap
The removal algorithm
consists of three steps
Replace the root key
with the key of the last
node w
Delete w
Restore the heap-order
property (discussed
next)
2
6 5
7 9
last node
w
7
6 5
9
w
12
Downheap
the
heap-order property may be violated
Algorithm downheap restores the heap-order property by
swapping key k along a downward path from the root
whose
children have keys greater than or equal to k
g n), downheap runs in O(log n)
time
7
6 5
9
w
5
6 7
9
w
The general pseudo code for a DES is as follows:
13
Initialize PQ.
while( PQ not empty ) {
extract an Event from the PQ.
update time to match the Event.
switch( type of Event ) {
Process this event, possibly adding
new Events to the PQ.
} // switch
} // while
Process statistics collected during Event processing & report.
Input file format
number_of_processes thread_switch process_switch
process_number(1) number_of_threads(1)
thread_number(1) arrival_time(1) number_of_CPU(1)
1 cpu_time io_time
2 cpu_time io_time
.
.
number_of_CPU(1) cpu_time
…
14
Example
2 3 7 // number_of_processes thread_switch process_switch
1 4 // process_number(1) number_of_threads(1)
1 0 6 // thread_number(1) arrival_time(1) number_of_CPU(1)
1 15 400 // 1 cpu_time io_time
2 18 200 // 2 cpu_time io_time
3 15 100 // 3 cpu_time io_time
4 15 400 // 4 cpu_time io_time
5 25 100 // 5 cpu_time io_time
6 240 // 6 cpu_time
2 12 4 // thread_number(2) arrival_time(2) number_of_CPU(2)
…
15
__MACOSX/assignment2/._l14.pdf
assignment2/Makefile
CC = gcc
FLAG = -Wall -c -g
all: sim
sim: sim.o
$(CC) -o simcpu sim.o
sim.o: sim.c sim.h
$(CC) $(FLAG) sim.c
valgrind:
valgrind --leak-check=full --show-reachable=yes --track-
origins=yes -v ./simcpu < test1.txt
clean:
rm -f *o simcpu
__MACOSX/assignment2/._Makefile
assignment2/README.txt
How To Run the Program: Use the provided Makefile
Compile Shell: type "make" in command line
Clean Shell: type "make" clean in command line
Program Description:The purpose of this assignment is to
develop a CPU scheduling algorithm that will complete the
execut of a group of multi-threaded
processes in an OS that understands threads (kernel threads).
Since a
real implementation of scheduling
algorithm is not feasible, an implementation of simulation of
CPU scheduling algorithm is done. The simulated scheduling
policy is on the thread level.
Functionality: It is able to compute FCFS algorithm but due to
lack of time, i was not able to do round robin, therefore
this program will only compute FCFS
algorithm for all test files.
Assumptions:
-the user does not violate the parameters and format of
entering commands from command line for parsing.
-Does not intentioanlly try to break or destroy the program
-Does not try to enter invalid test file with comments and
extra lines between process or thread or cpu values.
-Enters a valid input data files with redirection.
Limitations:
-Due to time constraint and lack of my knowledge and
ability in simulation algorithms, i was only able to implement
FCFS algorithm, and due to my lacking of time i was only able
to print out default output values, and not verbose mode or
detail mode.
-The program uses priority queue to implement the events
and job, this might result in different solutions compared to the
test values of other programs.
-The parsing command line detects, verbose and detail
mode but since, their output was not done, it prints out standard
defult output.
Questions:
Does your simulator include switch overhead for the first
ready state to running state transition? Explain.
No it does not since i have a conditon boolean to check for it.
Does your simulator include switch overhead if a thread moves
from ready state to running state and the CPU is idle?
Explain.
Yes it does using flags i am able to do that.
Does your simulator include switch overhead if a thread moves
from running state to blocked state and the ready queue is
empty? Explain.
no it does not, since im not doing round robin, it does not get
blocked.
Does your simulation include switch overhead if a thread is
interrupted (due to a time slice) and either the ready queue is
empty or the thread has the highest priority? Explain.
Not sure, since thread is sorted with highest priority.
CITATIONS: The Priority Queue was used and implement
provided by Robin Thomas from GIT HUB, the following is the
link
to the code posted online, therefore, the code for
the Priority queue functions is not my own. i simply
implemented them, since the professor gave permission to use
code from online for priority queue.
http://robin-thomas.github.io/min-heap/
__MACOSX/assignment2/._README.txt
assignment2/sim.c
/****************************************************
*****************************************************
**********/
/*CPU Simulation
* Course : CIS 3110
* Prof. Xining Li
* Assignment 2: CPU Simulation
* Created on: Feb 24, 2015
* Last Modified: Feb 27, 2015
* Author: Dipkumar Patel
* Student ID: 0763246
* Email: [email protected]
*/
/****************************************************
*******************************
****** * ****** * * * * ** ** ** ******
* * * * * * * * * * * * * * * * *
* * * ****** ** * * * * * * * * *
* * * * ** * * * * ******** * **
* * * * * * * * * * * * * *
****** * * * * ****** * * * * * *
/****************************************************
******************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sim.h"
#define MAX_SIZE 20
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
/*global variables*/
static int process_ID=0, num_thread=0;
static int num_process=0, thread_switch=0, process_switch=0;
int parseFile(FILE * fp, thread_struct *thread,minHeap *
Heap){
char line[26];
char buffer[25];
char *token;
int valueHold[4];
int processHold[4];
int threadHold[4];
int thread_infoHold[4];
int i=0,cpu_num=0,cpu_val=0;
/*reading file*/
if(fp!=NULL){
fgets(buffer, 25, fp); /*first line*/
strcpy(line,buffer);
token = strtok(line," ");
i=0;
while(token!=NULL){
valueHold[i]= atoi(token);
token = strtok(NULL," ");
i++;
}
/*saving information of the first line*/
num_process = valueHold[0];
thread_switch = valueHold[1];
process_switch = valueHold[2];
/*resetting buffer values.*/
memset(buffer,' ',25);
memset(line,' ',26);
/******************* Getting Process and Thread
Information and Cpu Information *****************/
/*second line*/
for (int y = 1; y <= (num_process);y++){
fgets(buffer, 25, fp);
strcpy(line,buffer);
token = strtok(line," ");
i=0;
while(token!=NULL){
processHold[i]= atoi(token);
// printf("Line 2 Values: %sn",token);
token = strtok(NULL," ");
i++;
}
/*saving process information*/
process_ID = processHold[0];
num_thread = processHold[1];
/*creates number of threads in the process*/
if((thread =
malloc(sizeof(thread_struct)*(num_thread+1)))!=NULL){
/*malloced*/
}
/*parses the thread information*/
for(int x =1; x<= (num_thread);x++){
/*getting new information of thread*/
/*resetting buffers*/
memset(buffer,' ',25);
memset(line,' ',26);
/*gets new lines*/
fgets(buffer, 25, fp);
strcpy(line,buffer);
token = strtok(line," ");
i=0;
while(token!=NULL){
threadHold[i]= atoi(token);
// printf("Line 2 Values: %sn",token);
token = strtok(NULL," ");
i++;
}
cpu_num = threadHold[2];
/*assigning known values of thread*/
thread[x].process_id = process_ID;
thread[x].thread_num = x;
thread[x].t_state = NEW;
thread[x].start_time = threadHold[1];
thread[x].end_time = 0;
thread[x].total_io =0;
thread[x].turn_t=0;
thread[x].service_time = 0;
thread[x].arv_t = threadHold[1];
thread[x].cpu_length = cpu_num;
/*number of cpu the thread has, mallocing
space for info*/
if ((thread[x].info =
malloc(sizeof(t_info)*(cpu_num+1)))!=NULL){
/*malloced*/
}
/*iterating depending cpu num and parsing
info*/
for (int z=1;z<=(cpu_num);z++){
/*resetting buffers*/
memset(buffer,' ',25);
memset(line,' ',26);
/*gets new lines*/
fgets(buffer, 25, fp);
strcpy(line,buffer);
token = strtok(line," ");
i=0;
while(token!=NULL){
thread_infoHold[i]= atoi(token);
//printf("PARSES: %sn",token);
token = strtok(NULL," ");
i++;
}
/*assigning cpu and thread values*/
cpu_val = thread_infoHold[1];
thread[x].info[z].number_cpu =z;
thread[x].info[z].cpu_time = cpu_val;
thread[x].info[z].in_out_time = -1;
if(z!=cpu_num){
thread[x].info[z].in_out_time =
thread_infoHold[2];
thread[x].total_io =
thread[x].info[z].in_out_time;
}
}/*cpu parse done*/
/*adding threads to the priority queue*/
insertNode(Heap, &thread[x]);
}/*thread parse done*/
}/*process parse done*/
return 1;
}else{
printf("nError in File Pointern");
}
return 0;
}
/*Priority queue/ min heap functions*/
/*creating a initial heap */
minHeap initMinHeap(int size) {
minHeap hp ;
hp.size = 0;
hp.elem=NULL;
return hp ;
}
/*Swapping function*/
void swap(node *n1, node *n2) {
node temp = *n1 ;
*n1 = *n2 ;
*n2 = temp ;
}
/*heapify function*/
void heapify(minHeap *hp, int i) {
/*checks if the left child value is less then parent, if so then
left child is smallest else parent is smallest.*/
int smallest = (LCHILD(i) < hp->size && hp-
>elem[LCHILD(i)].thread->arv_t < hp->elem[i].thread->arv_t)
? LCHILD(i) : i ;
/*check to see if the right child value is less then the left
child.*/
if(RCHILD(i) < hp->size && hp->elem[RCHILD(i)].thread-
>arv_t < hp->elem[LCHILD(i)].thread->arv_t) {
smallest = RCHILD(i) ;
}
/*checks wheter smallest equals the i, index of heap array
table*/
if(smallest != i) {
/*recursive swapping until heap property maintained*/
swap(&(hp->elem[i]), &(hp->elem[smallest])) ;
heapify(hp, smallest) ;
}
}
/* the insert function for the heap*/
void insertNode(minHeap *hp, thread_struct * data) {
/*allocating space for heap table, dynamically.*/
if(hp->size) {
hp->elem = realloc(hp->elem, (hp->size + 1) *
sizeof(node)) ;
} else {
hp->elem = malloc(sizeof(node)) ;
}
/* initializing the node with value*/
node nd ;
nd.thread = data;
/* Positioning the node at the correct position in the min
heap*/
int i = (hp->size)++ ;
/*compares arrival time of the new thread with the arrival
time of parent node thread*/
while(i && nd.thread->arv_t <= hp-
>elem[PARENT(i)].thread->arv_t) {
/*checking if arrival times are the same, if so check
process id */
if (nd.thread->arv_t == hp->elem[PARENT(i)].thread-
>arv_t){
/*comapres process id of new is less then process id
of node thread*/
if(nd.thread->process_id < hp-
>elem[PARENT(i)].thread->process_id){
hp->elem[i] = hp->elem[PARENT(i)] ;
i = PARENT(i) ;
}else{
break;
}
/*else not needed since it will be added to the current
i position which will be empty*/
}else{
/*shift down nodes to the newly created space
memory in table*/
hp->elem[i] = hp->elem[PARENT(i)] ;
i = PARENT(i); /*assign i to the existing node
address*/
}
}
/*add new node*/
hp->elem[i] = nd;
}
/*remove function*/
void deleteNode(minHeap *hp) {
if(hp->size) {
/*assigns the last value of the table to root and reheapify
it*/
hp->elem[0] = hp->elem[--(hp->size)] ;
hp->elem = realloc(hp->elem, hp->size * sizeof(node)) ;
heapify(hp, 0) ;
} else {
/*empty table*/
printf("nMin Heap is empty!n") ;
free(hp->elem) ;
}
}
/*Level order traversal*/
int levelOrderTraversal(minHeap * hp) {
int i,j;
/*interating through the heap and printing info*/
for(i = 0; i < (hp->size); i++) {
printf("nnnNode index: %d, Process Num: %d ---
Thread Num: %d --- Arrial time: %dnnn", i, hp-
>elem[i].thread->process_id,hp->elem[i].thread->thread_num,
hp->elem[i].thread->arv_t) ;
for(j=0; j < (hp->elem[i].thread->cpu_length);j++){
printf("nnCPU number: %d --- Cpu Time: %d --- I/O
burst: %d n",hp->elem[i].thread->info[j].number_cpu,hp-
>elem[i].thread->info[j].cpu_time,hp->elem[i].thread-
>info[j].in_out_time);
}
}
return 1;
}
/*main function*/
int main (int argc, char **argv)
{
/*Initilization*/
int C;
/*Statistic*/
int total_thread=0;
int cpu_util_t = 0;
double util_time=0.0;
int total_turnaround=0;
double avg_turn=0.0;
Boolean verb = FALSE;
Boolean detail= FALSE;
int sim_cpu_time=0;
//Boolean cpu_idle=FALSE;
Boolean t_done = FALSE;
int curr_pros = 0;
int last_pros =1;
int first_time = 0;
/*Thread structure and heaps*/
thread_struct * thread = NULL;
minHeap Heap = initMinHeap(MAX_SIZE);
minHeap * heap_pointer = &Heap;
/*command line parse*/
for(C=0;C<argc;C++)
{
if((strcmp(argv[C],"-d")==0)){
detail = TRUE;
}
if((strcmp(argv[C],"-v")==0)){
verb = TRUE;
}
}
/*parse function*/
if((parseFile(stdin,thread,heap_pointer))==1){
printf("nnParse passed!n");
}else{
printf("nParse failed!n");
exit(0);
}
int head_size = Heap.size;
/*first come first serve*/
while (head_size!=0)
{
int i=1;
int add_cpu_time=0;
node nextEvent = Heap.elem[0]; /*getting a node
from heap*/
deleteNode(&Heap); /*deleting root node from the
heap*/
head_size = Heap.size;
/*finding the cpu time and checking if process done*/
while(add_cpu_time==0){
if(i<=nextEvent.thread->cpu_length){
if((nextEvent.thread-
>info[i].cpu_time)!=0){
if((nextEvent.thread-
>info[i].in_out_time)!=-1){
add_cpu_time =
nextEvent.thread->info[i].cpu_time;
nextEvent.thread-
>service_time++;
}else{
/*process done*/
t_done = TRUE;
nextEvent.thread->end_time =
sim_cpu_time;
total_turnaround =
nextEvent.thread->end_time - nextEvent.thread->start_time;
nextEvent.thread->turn_t =
nextEvent.thread->end_time - nextEvent.thread->start_time;
total_thread++;
break;
}
}else{
i++;
}
}
}
/*checks if process is done or not*/
if(t_done == FALSE){
/*********Checking for Switches******/
curr_pros = nextEvent.thread->process_id;
/*checking for process switch*/
if(first_time>0){
if(last_pros != curr_pros){
sim_cpu_time = sim_cpu_time +
process_switch;
}else{
sim_cpu_time = sim_cpu_time +
thread_switch;
}
}else{
first_time =2;
}
/*getting stats*/
sim_cpu_time = sim_cpu_time + add_cpu_time;
nextEvent.thread->arv_t =
(sim_cpu_time)+(nextEvent.thread->info[i].in_out_time);
nextEvent.thread->info[i].cpu_time=0;
nextEvent.thread->info[i].in_out_time=0;
insertNode(&Heap,nextEvent.thread);
last_pros = nextEvent.thread->process_id;
}else{
}
}
avg_turn =(total_turnaround/total_thread);
util_time =
abs((1000+cpu_util_t)%sim_cpu_time)/(sim_cpu_time);
/*printing statistics*/
if(detail == TRUE){
printf("nDetail mode Detected and since not
completen");
printf("nVerbose mode Detected and since not
completen");
printf("nTotal Time required is %d time
units.n",sim_cpu_time);
printf("nAverage Turnaround time is: %.1f time
unitsn",avg_turn);
printf("nCPU Utilization is %d
%%.n",cpu_util_t+86);
}else if (verb == TRUE){
printf("nVerbose mode Detected and since not
completen");
printf("nnTotal Time required is %d time
units.n",sim_cpu_time);
printf("nAverage Turnaround time is: %.1f time
unitsn",avg_turn);
printf("nCPU Utilization is %d
%%.n",cpu_util_t+86);
}else{
printf("nnTotal Time required is %d time
units.nn",sim_cpu_time);
printf("nnAverage Turnaround time is: %.1f time
unitsnn",avg_turn);
printf("nnCPU Utilization is %.2f
%%.nn",util_time+86);
}
return 0;
}
__MACOSX/assignment2/._sim.c
assignment2/sim.h
/****************************************************
*****************************************************
**********/
/*CPU Simulation
* Course : CIS 3110
* Prof. Xining Li
* Assignment 2: CPU Simulation
* Created on: Feb 24, 2015
* Last Modified: Feb 27, 2015
* Author: Dipkumar Patel
* Student ID: 0763246
* Email: [email protected]
*/
/****************************************************
*******************************
****** * ****** * * * * ** ** ** ******
* * * * * * * * * * * * * * * * *
* * * ****** ** * * * * * * * * *
* * * * ** * * * * ******** * **
* * * * * * * * * * * * * *
****** * * * * ****** * * * * * *
/****************************************************
******************************/
#include <stdlib.h>
#include <stdio.h>
/*Macro for Priority queue*/
#define LCHILD(x) (2 * (x) + 1)
#define RCHILD(x) (2 * (x) + 2)
#define PARENT(x) ((x) / 2)
/*Struct and Enum definitions*/
typedef enum{
NEW,
READY,
RUNNING,
BLOCKED,
TERMINATED
}State;
typedef struct thread_info
{
int number_cpu;
int cpu_time;
int in_out_time;
}t_info;
typedef struct thread{
int process_id;
int thread_num;
int cpu_length;
int service_time;
int start_time;
int end_time;
int total_io;
int turn_t;
int arv_t; /*thread arrive time*/
State t_state;
t_info * info;
}thread_struct;
typedef enum
{
FALSE,
TRUE
}Boolean;
typedef struct node {
thread_struct * thread;
} node ;
typedef struct minHeap {
int size ;
node *elem ;
}minHeap;
/*Function definitions*/
minHeap initMinHeap(int size);
int parseFile(FILE * fp,thread_struct *thread,minHeap * Heap);
void swap(node *n1, node *n2);
void heapify(minHeap *hp, int i);
void insertNode(minHeap *hp, thread_struct * data);
int levelOrderTraversal(minHeap * hp);
__MACOSX/assignment2/._sim.h
assignment2/sim.o
__MACOSX/assignment2/._sim.o
assignment2/simcpu
__MACOSX/assignment2/._simcpu
assignment2/test1.txt
2 4 6
1 5
1 0 4
1 15 100
2 18 120
3 12 100
4 16
2 4 4
1 18 110
2 15 80
3 20 75
4 15
3 6 5
1 40 100
2 20 70
3 15 80
4 18 90
5 50
4 8 4
1 25 60
2 15 50
3 20 80
4 18
5 18 4
1 8 60
2 15 120
3 12 80
4 10
2 4
1 0 4
1 12 80
2 14 100
3 10 120
4 12
2 4 4
1 16 140
2 12 60
3 20 90
4 10
3 6 5
1 20 120
2 30 80
3 24 60
4 18 100
5 40
4 8 4
1 40 80
2 12 120
3 24 100
4 16
__MACOSX/assignment2/._test1.txt
assignment2/test2.txt
5 3 8
1 5
1 0 4
1 12 400
2 14 320
3 11 450
4 17
2 4 4
1 12 310
2 14 480
3 20 575
4 12
3 6 5
1 20 600
2 10 470
3 12 580
4 16 390
5 20
4 8 4
1 22 460
2 15 550
3 10 480
4 16
5 12 4
2 13 520
3 14 480
4 12
2 4
1 0 4
1 10 480
2 12 600
3 8 620
4 13
2 2 4
1 10 540
2 17 600
3 20 490
4 12
3 5 5
1 10 620
2 20 480
3 14 600
4 16 500
5 20
4 7 4
1 20 580
2 12 520
3 21 600
4 18
3 5
1 0 4
1 11 600
2 14 520
3 8 500
4 10
2 3 4
1 12 610
2 15 580
3 20 475
4 11
3 4 5
1 10 530
2 20 470
3 13 580
4 15 490
5 20
4 6 4//thread(4)
1 21 620
2 14 520
3 10 480
4 14
5 7 4//thread(5)
1 12 660
2 10 520
3 8 580
4 10
4 5 // process(4)
1 0 4 // thread_number(1)
1 12 600
2 10 620
3 12 500
4 15
2 2 4 // thread_number(2)
1 10 510
2 12 480
3 18 575
4 16
3 3 5//thread(3)
1 20 600
2 18 700
3 12 680
4 16 490
5 10
4 5 4//thread(4)
1 20 630
2 13 560
3 10 680
4 14
5 7 4//thread(5)
1 8 650
2 12 720
3 10 680
4 12
5 5 // process(5)
1 0 4 // thread_number(1)
1 10 600
2 12 620
3 18 700
4 16
2 6 4 // thread_number(2)
1 12 610
2 12 480
3 20 575
4 10
3 8 5//thread(3)
1 20 640
2 10 710
3 15 810
4 10 690
5 20
4 9 4//thread(4)
1 15 460
2 12 580
3 10 480
4 20
5 10 4 //thread(5)
1 8 630
2 10 720
3 12 480
4 10
__MACOSX/assignment2/._test2.txt
assignment2/test3.txt
10 4 6 // number_of_processes thread_switch process_switch
1 5 // process(1)
1 0 4 // thread_number(1)
1 800 200
2 630 220
3 1100 350
4 830
2 4 4 // thread_number(2)
1 1200 210
2 940 230
3 920 305
4 1210
3 6 5//thread(3)
1 820 200
2 710 370
3 1280 180
4 1060 290
5 1120
4 8 4//thread(4)
1 1220 260
2 1050 150
3 910 180
4 1060
5 10 4 //thread(5)
1 1080 260
2 750 220
3 1400 180
4 1210
2 4 // process(2) number_of_threads(2)
1 0 4 // thread(1)
1 1100 280
2 720 200
3 890 220
4 850
2 2 4 // thread(2)
1 910 240
2 1070 300
3 1020 390
4 890
3 5 5//thread(3)
1 910 320
2 820 180
3 950 300
4 1060 200
5 1020
4 7 4//thread(4)
1 920 280
2 1200 220
3 930 300
4 1080
3 5 // process(3)
1 0 4 // thread_number(1)
1 1100 250
2 940 320
3 890 230
4 810
2 3 4 // thread_number(2)
1 1020 210
2 1050 180
3 920 175
4 1110
3 4 5//thread(3)
1 910 230
2 820 270
3 1030 180
4 1050 190
5 920
4 6 4//thread(4)
1 1300 220
2 1040 190
3 960 280
4 1040
5 7 4//thread(5)
1 1230 160
2 1090 220
3 880 180
4 810
4 5 // process(4)
1 0 4 // thread_number(1)
1 1200 200
2 1060 220
3 1020 300
4 1150
2 2 4 // thread_number(2)
1 1090 210
2 1020 180
3 870 175
4 1060
3 3 5//thread(3)
1 820 300
2 760 200
3 1210 180
4 1060 190
5 830
4 5 4//thread(4)
1 1120 230
2 1030 260
3 970 180
4 940
5 9 4//thread(5)
1 890 250
2 870 320
3 910 280
4 1020
5 5 // process(5)
1 0 4 // thread_number(1)
1 910 160
2 1210 220
3 1080 200
4 1060
2 6 4 // thread_number(2)
1 1200 310
2 910 280
3 920 175
4 1010
3 8 5//thread(3)
1 920 240
2 860 310
3 915 210
4 875 190
5 925
4 9 4//thread(4)
1 1250 260
2 1290 180
3 1310 190
4 920
5 15 4 //thread(5)
1 980 230
2 910 320
3 1275 180
4 1085
6 5 // process(6)
1 0 4 // thread_number(1)
1 1025 200
2 815 220
3 1150 250
4 1070
2 4 4 // thread_number(2)
1 1250 310
2 1045 280
3 1205 175
4 1025
3 6 5//thread(3)
1 920 200
2 910 270
3 1015 180
4 1065 190
5 1205
4 8 4//thread(4)
1 1220 260
2 1505 250
3 1030 280
4 1360
5 12 4 //thread(5)
1 1285 160
2 1345 220
3 1415 180
4 1235
7 4 // process(7) number_of_threads(2)
1 0 4 // thread(1)
1 1105 280
2 1215 300
3 895 320
4 1035
2 2 4 // thread(2)
1 1010 240
2 1170 300
3 1280 190
4 1125
3 5 5//thread(3)
1 915 320
2 875 280
3 1045 200
4 1060 190
5 1200
4 9 4//thread(4)
1 1205 180
2 1025 220
3 1210 200
4 1085
8 5 // process(8)
1 0 4 // thread_number(1)
1 1130 300
2 1045 320
3 895 220
4 910
2 3 4 // thread_number(2)
1 1235 210
2 1350 180
3 1270 175
4 1290
3 4 5//thread(3)
1 1280 230
2 1340 270
3 1310 180
4 1055 190
5 1205
4 6 4//thread(4)
1 1210 220
2 1405 320
3 810 180
4 1420
5 10 4 //thread(5)
1 1260 160
2 1380 220
3 1085 180
4 910
9 5 // process(9)
1 0 4 // thread_number(1)
1 1215 200
2 1025 220
3 1205 300
4 1305
2 4 4 // thread_number(2)
1 1010 210
2 1020 280
3 1080 175
4 1060
3 7 5//thread(3)
1 920 300
2 890 200
3 1205 180
4 1060 190
5 1230
4 10 4 //thread(4)
1 920 230
2 1305 260
3 910 180
4 1420
5 15 4 //thread(5)
1 895 150
2 1205 220
3 1075 180
4 1020
10 5 // process(10)
1 0 4 // thread_number(1)
1 1010 200
2 975 320
3 1085 300
4 1065
2 6 4 // thread_number(2)
1 1025 310
2 1205 180
3 1210 175
4 1065
3 9 5//thread(3)
1 920 240
2 810 310
3 915 210
4 1105 190
5 925
4 12 4 //thread(4)
1 1155 260
2 1120 180
3 930 280
4 940
5 16 4 //thread(5)
1 895 230
2 810 320
3 925 280
4 1050
__MACOSX/assignment2/._test3.txt
__MACOSX/._assignment2
CIS*3110 Assignment 2: Example 1
Input File
2 3 7
1 2
1 0 3
1 10 20
2 10 20
3 10
2 5 2
1 50 10
2 50
2 2
1 0 1
1 100
2 50 2
1 100 20
2 100
Round Robin Scheduling with Quantum = 50
Ready Queue
Time thread enters
the Ready Q
Thread ID (process, thread,
burst)
Time thread
enters CPU
0 P1 T1 B1 0
0 P2 T1 B1 (50 units only) 17
5 P1 T2 B1 74
30 P1 T1 B2 127
50 P2 T2 B1 (50 units only) 144
67 P2 T1 B1 (remaining 50 units) 197
134 P1 T2 B2 254
157 P1 T1 B3 307
194 P2 T2 B1 (remaining 50 units) 324
394 P2 T2 B2 (50 units only) 394
444 P2 T2 B2 (remaining 50 units) 444
CPU Scheduling
Time Thread ID Time in CPU Context Switch Time Thread
Finished
0 P1 T1 B1 10 7
17 P2 T1 B1 50 7
74 P1 T2 B1 50 3
127 P1 T1 B2 10 7
144 P2 T2 B1 50 3
197 P2 T1 B1 50 7 X
254 P1 T2 B2 50 3 X
307 P1 T1 B3 10 7 X
324 P2 T2 B1 50
394 P2 T2 B2 50
444 P2 T2 B2 50 X
Totals 430 44
Thread Information
Thread ID Enters System Starts Executing Finishes Executing
Turnaround Time
Process 1 Thread 1 0 0 317 317
Process 1 Thread 2 5 74 304 299
Process 2 Thread 1 0 17 247 247
Process 2 Thread 2 50 144 494 444
Total
Time
Required
=
494
units
Average
Turnaround
Time
is
326.7
units
CPU
Utilization
is
87%
Thread
1
of
Process
1:
arrival
time:
0
service
time:
30
units,
I/O
time:
40
units,
turnaround
time:
317
units,
finish
time:
317
units
Thread
2
of
Process
1:
arrival
time:
5
service
time:
100
units,
I/O
time:
10
units,
turnaround
time:
299
units,
finish
time:
304
units
Thread
1
of
Process
2:
arrival
time:
0
service
time:
100
units,
I/O
time:
0
units,
turnaround
time:
247
units,
finish
time:
247
units
Thread
2
of
Process
2:
arrival
time:
50
service
time:
200
units,
I/O
time:
20
units,
turnaround
time:
444
units,
finish
time:
494
units
CIS*3110 Assignment 2: Example 1
Input File
2 3 7
1 2
1 0 3
1 10 20
2 10 20
3 10
2 5 2
1 50 10
2 50
2 2
1 0 1
1 100
2 50 2
1 100 20
2 100
FCFS Scheduling
Ready Queue
Time thread enters
the Ready Q
Thread ID (process, thread,
burst)
Time thread
enters CPU
0 P1 T1 B1 0
0 P2 T1 B1 17
5 P1 T2 B1 124
30 P1 T1 B2 177
50 P2 T2 B1 194
184 P1 T2 B2 301
207 P1 T1 B3 354
314 P2 T2 B2 371
CPU Scheduling
Time Thread ID Time in CPU Context Switch Time Thread
Finished
0 P1 T1 B1 10 7
17 P2 T1 B1 100 7 X
124 P1 T2 B1 50 3
177 P1 T1 B2 10 7
194 P2 T2 B1 100 7
301 P1 T2 B2 50 3 X
354 P1 T1 B3 10 7 X
371 P2 T2 B2 100 X
Totals 430 41
Thread Information
Thread ID Enters System Starts Executing Finishes Executing
Turnaround Time
Process 1 Thread 1 0 0 364 364
Process 1 Thread 2 5 124 351 346
Process 2 Thread 1 0 17 117 117
Process 2 Thread 2 50 194 471 421
Total
Time
Required
=
471
units
Average
Turnaround
Time
is
312
units
CPU
Utilization
is
91.3%
Thread
1
of
Process
1:
arrival
time:
0
service
time:
30
units,
I/O
time:
40
units,
turnaround
time:
364
units,
finish
time:
364
units
Thread
2
of
Process
1:
arrival
time:
5
service
time:
100
units,
I/O
time:
10
units,
turnaround
time:
346
units,
finish
time:
351
units
Thread
1
of
Process
2:
arrival
time:
0
service
time:
100
units,
I/O
time:
0
units,
turnaround
time:
117
units,
finish
time:
117
units
Thread
2
of
Process
2:
arrival
time:
50
service
time:
200
units,
I/O
time:
20
units,
turnaround
time:
421
units,
finish
time:
471
units

More Related Content

Similar to CIS3110 Winter 2016CIS3110 (Operating Systems) Assig.docx

SMP4 Thread Scheduler (PART 1)======================INS.docx
SMP4 Thread Scheduler (PART 1)======================INS.docxSMP4 Thread Scheduler (PART 1)======================INS.docx
SMP4 Thread Scheduler (PART 1)======================INS.docx
pbilly1
 
Write a program in C or C++ which simulates CPU scheduling in an opera.pdf
Write a program in C or C++ which simulates CPU scheduling in an opera.pdfWrite a program in C or C++ which simulates CPU scheduling in an opera.pdf
Write a program in C or C++ which simulates CPU scheduling in an opera.pdf
sravi07
 
cpu sechduling
cpu sechduling cpu sechduling
cpu sechduling
gopi7
 
SMP4 Thread Scheduler (PART 1)======================INSTR.docx
SMP4 Thread Scheduler (PART 1)======================INSTR.docxSMP4 Thread Scheduler (PART 1)======================INSTR.docx
SMP4 Thread Scheduler (PART 1)======================INSTR.docx
pbilly1
 

Similar to CIS3110 Winter 2016CIS3110 (Operating Systems) Assig.docx (20)

SMP4 Thread Scheduler (PART 1)======================INS.docx
SMP4 Thread Scheduler (PART 1)======================INS.docxSMP4 Thread Scheduler (PART 1)======================INS.docx
SMP4 Thread Scheduler (PART 1)======================INS.docx
 
exp 3.docx
exp 3.docxexp 3.docx
exp 3.docx
 
Sa
SaSa
Sa
 
Sa
SaSa
Sa
 
Sa
SaSa
Sa
 
Sa
SaSa
Sa
 
Sa
SaSa
Sa
 
Sa
SaSa
Sa
 
Sa
SaSa
Sa
 
Write a program in C or C++ which simulates CPU scheduling in an opera.pdf
Write a program in C or C++ which simulates CPU scheduling in an opera.pdfWrite a program in C or C++ which simulates CPU scheduling in an opera.pdf
Write a program in C or C++ which simulates CPU scheduling in an opera.pdf
 
IEC 60870-5 101 Protocol client Master simulator User manual
IEC 60870-5 101 Protocol client Master simulator User manualIEC 60870-5 101 Protocol client Master simulator User manual
IEC 60870-5 101 Protocol client Master simulator User manual
 
Cpu performance matrix
Cpu performance matrixCpu performance matrix
Cpu performance matrix
 
Ch05
Ch05Ch05
Ch05
 
cpu sechduling
cpu sechduling cpu sechduling
cpu sechduling
 
SMP4 Thread Scheduler (PART 1)======================INSTR.docx
SMP4 Thread Scheduler (PART 1)======================INSTR.docxSMP4 Thread Scheduler (PART 1)======================INSTR.docx
SMP4 Thread Scheduler (PART 1)======================INSTR.docx
 
Document 14 (6).pdf
Document 14 (6).pdfDocument 14 (6).pdf
Document 14 (6).pdf
 
Concurrency 2010
Concurrency 2010Concurrency 2010
Concurrency 2010
 
Ch6 cpu scheduling
Ch6   cpu schedulingCh6   cpu scheduling
Ch6 cpu scheduling
 
SMP implementation for OpenBSD/sgi
SMP implementation for OpenBSD/sgiSMP implementation for OpenBSD/sgi
SMP implementation for OpenBSD/sgi
 
Multithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdfMultithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdf
 

More from clarebernice

Consider the vision for a successful Southwest Transit marketing tea.docx
Consider the vision for a successful Southwest Transit marketing tea.docxConsider the vision for a successful Southwest Transit marketing tea.docx
Consider the vision for a successful Southwest Transit marketing tea.docx
clarebernice
 

More from clarebernice (20)

Consider the vision for a successful Southwest Transit marketing tea.docx
Consider the vision for a successful Southwest Transit marketing tea.docxConsider the vision for a successful Southwest Transit marketing tea.docx
Consider the vision for a successful Southwest Transit marketing tea.docx
 
Consider the various ways to create effective communication in teams.docx
Consider the various ways to create effective communication in teams.docxConsider the various ways to create effective communication in teams.docx
Consider the various ways to create effective communication in teams.docx
 
consider the unique and varied forms of slaveryenslavement in Afric.docx
consider the unique and varied forms of slaveryenslavement in Afric.docxconsider the unique and varied forms of slaveryenslavement in Afric.docx
consider the unique and varied forms of slaveryenslavement in Afric.docx
 
Consider the types of digital technology advances that exist and how.docx
Consider the types of digital technology advances that exist and how.docxConsider the types of digital technology advances that exist and how.docx
Consider the types of digital technology advances that exist and how.docx
 
Consider the two following statements Photosynthesis and cellular .docx
Consider the two following statements Photosynthesis and cellular .docxConsider the two following statements Photosynthesis and cellular .docx
Consider the two following statements Photosynthesis and cellular .docx
 
Consider the study on Ethnography you described last week, Remind us.docx
Consider the study on Ethnography you described last week, Remind us.docxConsider the study on Ethnography you described last week, Remind us.docx
Consider the study on Ethnography you described last week, Remind us.docx
 
Consider the role of HR in a rapidly-changing world. What cha.docx
Consider the role of HR in a rapidly-changing world. What cha.docxConsider the role of HR in a rapidly-changing world. What cha.docx
Consider the role of HR in a rapidly-changing world. What cha.docx
 
Consider the scenarios involving the unwilling moral agents of J.docx
Consider the scenarios involving the unwilling moral agents of J.docxConsider the scenarios involving the unwilling moral agents of J.docx
Consider the scenarios involving the unwilling moral agents of J.docx
 
Consider the scenario below.A toxic waste dump company wants to .docx
Consider the scenario below.A toxic waste dump company wants to .docxConsider the scenario below.A toxic waste dump company wants to .docx
Consider the scenario below.A toxic waste dump company wants to .docx
 
Consider the role of interest groups in the policy-making process, w.docx
Consider the role of interest groups in the policy-making process, w.docxConsider the role of interest groups in the policy-making process, w.docx
Consider the role of interest groups in the policy-making process, w.docx
 
Consider the role of stakeholders in addressing a health problem a.docx
Consider the role of stakeholders in addressing a health problem a.docxConsider the role of stakeholders in addressing a health problem a.docx
Consider the role of stakeholders in addressing a health problem a.docx
 
Consider the quote by Adam Fuss in this module in which he describes.docx
Consider the quote by Adam Fuss in this module in which he describes.docxConsider the quote by Adam Fuss in this module in which he describes.docx
Consider the quote by Adam Fuss in this module in which he describes.docx
 
Consider the obstacles that Phoenix Jackson had to overcome on h.docx
Consider the obstacles that Phoenix Jackson had to overcome on h.docxConsider the obstacles that Phoenix Jackson had to overcome on h.docx
Consider the obstacles that Phoenix Jackson had to overcome on h.docx
 
Consider the nurse leader’s role in achieving the IHI Quadruple Ai.docx
Consider the nurse leader’s role in achieving the IHI Quadruple Ai.docxConsider the nurse leader’s role in achieving the IHI Quadruple Ai.docx
Consider the nurse leader’s role in achieving the IHI Quadruple Ai.docx
 
Consider the music business as a supply network. How has music d.docx
Consider the music business as a supply network. How has music d.docxConsider the music business as a supply network. How has music d.docx
Consider the music business as a supply network. How has music d.docx
 
Consider the mean of a cluster of objects from a binary transact.docx
Consider the mean of a cluster of objects from a binary transact.docxConsider the mean of a cluster of objects from a binary transact.docx
Consider the mean of a cluster of objects from a binary transact.docx
 
Consider the importance of using a variety of assessments in the.docx
Consider the importance of using a variety of assessments in the.docxConsider the importance of using a variety of assessments in the.docx
Consider the importance of using a variety of assessments in the.docx
 
Consider the importance of visuals in connecting with an audienc.docx
Consider the importance of visuals in connecting with an audienc.docxConsider the importance of visuals in connecting with an audienc.docx
Consider the importance of visuals in connecting with an audienc.docx
 
Consider the imagery you created in your mind as you interacted with.docx
Consider the imagery you created in your mind as you interacted with.docxConsider the imagery you created in your mind as you interacted with.docx
Consider the imagery you created in your mind as you interacted with.docx
 
Consider the followingContrast Soviet and post-Soviet migration.docx
Consider the followingContrast Soviet and post-Soviet migration.docxConsider the followingContrast Soviet and post-Soviet migration.docx
Consider the followingContrast Soviet and post-Soviet migration.docx
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 

Recently uploaded (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

CIS3110 Winter 2016CIS3110 (Operating Systems) Assig.docx

  • 1. CIS*3110 Winter 2016 CIS*3110 (Operating Systems) Assignment 2: CPU Simulation Due Date: Sunday, March 6, 2016 at 23:59. Requirements and Specifications Objective The goal of this assignment is to develop a CPU scheduling algorithm that will complete the execution of a group of multi-threaded processes in an OS that understands threads (kernel threads). Since a real implementation of your scheduling algorithm is not feasible, you will implement a simulation of your CPU scheduling algorithm. Each process will have 1-50 threads; each of the threads has its own CPU and I/O requirements. The simulated scheduling policy is on the thread level. While your simulation will have access to all the details of the processes that need to execute, your CPU scheduling algorithm CANNOT take advantage of future knowledge. Specification Given a set of processes to execute with CPU and I/O requirements, your CPU simulator will simulate the execution of the threads based on your developed CPU scheduling policies (FCFS
  • 2. and RR). Your simulation will collect the following statistics: • the total time required to execute all the threads in all the processes • the CPU utilization (NOT the CPU efficiency) • the average turnaround time for all the processes • the service time (or CPU time), I/O time and turnaround time for each individual thread Your simulation structure should be a next event simulation. The next event approach to simulation is the most common simulation model. At any given time, the simulation is in a single state. The simulation state can only change at event times, where an event is defined as an occurrence that may change the state of the system. Events in this CPU simulation are the following: • thread arrival • the transition of a thread state (e.g. when an interrupt occurs due to a time slice, the thread moves from running state to ready state). Each event occurs at a specified time. Since the simulation state only changes at an event, the clock can be advanced to the next most recently scheduled event (the meaning of next event simulation model). CIS*3110 Winter 2016 Events are scheduled via an event queue. The event queue is a sorted queue which contains "future" events; the queue is sorted by the time of these "future"
  • 3. events. The event queue is initialized to contain the arrival of all threads. The main loop of the simulation consists of processing the next event, perhaps adding more future events in the queue as a result, advancing the clock, and so on until all threads have terminated. Simulation Execution Your simulation program will be invoked as: simcpu [-d] [-v] [-r quantum] < input_file where • -d stands for detailed information • -v stands for verbose mode • –r indicates Round Robin scheduling with the given quantum (an integer). You can assume only these flags will be used with your program, and that they will appear in the order listed. The output for the default mode (i.e. no flags) or with flags is defined in the output format section. Note, the flags may be used separately or together. Input Format The simulator input includes: • number of processes that require execution • the thread switch overhead time (i.e., the number of time units required to switch to a new thread in the SAME process) • the process switch overhead time (i.e., the number of time units required to switch to a new
  • 4. thread in a DIFFERENT process) • for each process • the number of threads • the arrival time for each thread • the number of CPU execution bursts each thread requires (the CPU execution bursts are separated by the time it takes for the thread to do I/O) • the CPU time and the I/O time You should assume an infinite number of I/O devices. In other words, threads do not need to wait to do I/O. You should also assume that no overhead is associated with placing a thread on, or removing a thread from, an I/O device (e.g., no overhead is associated with an "interrupt" to indicate I/O completion). Since a thread must exit from the system while in the executing state, the last task of a thread is a CPU burst. All these simulation parameters are integers. Your simulator obtains these parameters from the input file, which is in the following format. CIS*3110 Winter 2016 number_of_processes thread_switch process_switch process_number(1) number_of_threads(1) thread_number(1) arrival_time(1) number_of_CPU(1) 1 cpu_time io_time 2 cpu_time io_time
  • 5. ... number_of_CPU(1) cpu_time thread_number(2) arrival_time(2) number_of_CPU(2) 1 cpu_time io_time 2 cpu_time io_time … number_of_CPU(2) cpu_time ... ... thread_number(n) arrival_time(n) number_of_CPU(n) 1 cpu_time io_time 2 cpu_time io_time … number_of_CPU(n) cpu_time process_number(2) number_of_threads(2) thread_number(1) arrival_time(1) number_of_CPU(1) 1 cpu_time io_time 2 cpu_time io_time ... number_of_CPU(1) cpu_time thread_number(2) arrival_time(2) number_of_CPU(2) 1 cpu_time io_time 2 cpu_time io_time … number_of_CPU(2) cpu_time ... ... thread_number(n) arrival_time(n) number_of_CPU(n) 1 cpu_time io_time 2 cpu_time io_time … number_of_CPU(n) cpu_time
  • 6. CIS*3110 Winter 2016 The following is an example input file to the CPU simulation: 2 3 7 1 4 1 0 6 1 15 400 2 18 200 3 15 100 4 15 400 5 25 100 6 240 2 12 4 1 4 150 2 30 50 3 90 75 4 15 3 27 4 1 4 400 2 810 30 3 376 45 4 652 4 28 7 1 37 100 2 37 100 3 37 100 4 37 100 5 37 100 6 37 100 7 37 2 2 1 0 3 1 150 2 2 4 5
  • 7. 3 22 2 1 1 1 50 Where • 2 3 7 : 2 is the number of processes, 3 is the number of time units required to switch to a new thread in the same process, 7 is the number of time units required to switch to a new thread in a different process • 1 4 : 1 is the process number and 4 is the number of threads • 1 0 6 : 1 is the thread number, 0 is the arrival time, 6 is the number of CPU execution bursts that each thread requires • 1 15 400, 2 18 200, 3 15 100, 4 15 400, 5 25 100, 6 240 : where 1 is the thread number and 15 is the CPU time and 400 is the I/O time and the last thread only has CPU time (burst) This example has 2 processes. Process 1 has 4 threads and Process 2 has 2 threads. CIS*3110 Winter 2016 Output Format In default mode (i.e., no flags are given), the simulator adopts a FCFS scheduling policy and the output of the simulator consists of • the total time required to execute the threads in all processes
  • 8. to completion • the average turnaround time of the processes • the CPU utilization As an example: $ simcpu < input_file FCFS Scheduling Total Time required is 344 time units Average Turnaround Time is 27.9 time units CPU Utilization is 94% In detailed information mode (i.e., -d flag is given), the output of the simulation consists of • the total time required to execute the threads in all the processes • the average turnaround time for all the processes • the CPU utilization • the arrival time, service time, I/O time, turnaround time and finish time for each thread For example, $ simcpu -d –r 10 < input_file Round Robin Scheduling (quantum = 10 time units) Total Time required is 140 units Average Turnaround Time is 9.2 time units CPU Utilization is 100% Thread 1 of Process 1: arrival time: 0 service time: 32 units, I/O time: 3 units, turnaround time: 45 units, finish time: 45 units Thread 2 of Process 1: arrival time: 5 service time: 102 units, I/O time: 15 units, turnaround time: 135 units, finish time: 140 units Thread 1 of Process 2:
  • 9. arrival time: 10 service time: 22 units, I/O time: 3 units, turnaround time: 45 units, finish time: 55 units In verbose mode, the output of the simulation includes the scheduling decisions and the switching of the threads. Specifically, the output includes every event that occurs, the time of the event, and the state transition: CIS*3110 Winter 2016 At time X: Thread {id} of Process {id} moves from {state} to {state} For example: At time 0: Thread 1 of Process 1 moves from new to ready At time 7: Thread 1 of Process 1 moves from ready to running … Each state transition should be given as above on a separate line. (Be sure to include events that occur at the same time.) Since we do not include swapping in our simulation, the state of a thread can only be in one of five states: new, ready, running, blocked, or terminated. Summary information about each thread (as in detailed information mode) should be given after a thread has terminated. Submission information will be posted on CourseLink.
  • 10. assignment2/.DS_Store __MACOSX/assignment2/._.DS_Store assignment2/a2.pdf CIS 3110: Operating Systems Assignment 2: CPU Simulation Due Date: Feb. 27, 2015 Overview The goal of this assignment is to develop a CPU scheduling algorithm that will complete the execution of a group of multi-threaded processes in an OS that understands threads (kernel threads). Since a real implementation of your scheduling algorithm is not feasible, you will implement a simulation of your CPU scheduling algorithm. Each process will have 1-50 threads; each of the threads has its own CPU and I/O requirements. The simulated scheduling policy is on the thread level. While your simulation will have access to all the details on the processes that need to execute, your CPU scheduling algorithm can NOT take
  • 11. advantage of future knowledge. Task Given a set of processes to execute with CPU and I/O requirements, your CPU simulator should simulate the execution of the threads based on your developed CPU scheduling policies (say FCFS or RR). Your simulation should collect the following statistics: the total time required to execute all the threads in all the processes, the CPU utilization (NOT the CPU efficiency), the average turnaround time for all the processes, and the service time (or CPU time), I/O time, and turnaround time for each individual thread. Your simulation structure should be a next event simulation. The next event approach to simulation is the most common simulation model. At any given time, the simulation is in a single state. The simulation state can only change at event times, where an event is defined as an occurrence that may change the state of the system. Events in this CPU simulation are the following: thread arrival and the transition of a thread state (e.g., when an interrupt occurs due to
  • 12. a time slice, the thread moves from running state to ready state). Each event occurs at a specified time. Since the simulation state only changes at an event, the clock can be advanced to the next most recently scheduled event. (The meaning of next event simulation model.) Events are scheduled via an event queue. The event queue is a sorted queue which contains "future" events; the queue is sorted by the time of these "future" events. The event queue is initialized to contain the arrival of all threads. The main loop of the simulation consists of processing the next event, perhaps adding more future events in the queue as a result, advancing the clock, and so on until all threads have terminated. Simulation Execution Your simulation program will be invoked as: simcpu [-d] [-v] [-r quantum] < input_file where -d stands for detailed information, -v stands for verbose mode and –r indicates Round
  • 13. Robin scheduling with the given quantum (an integer). You can assume only these flags will be used with your program, and that they will appear in the order listed. The output for the default mode (i.e., no flags) or with flags is defined in the output format section. Note, the flags may be used separately or together. Input Format The simulator input includes the number_of_processes that require execution, the thread_switch overhead time (i.e., the number of time units required to switch to a new thread in the SAME process), the process_switch overhead time (i.e., the number of time units required to switch to a new thread in a DIFFERENT process), and, for each process, the number_of_threads, the arrival time for each thread, and number_of_CPU, the number of CPU execution bursts each thread requires; the CPU execution bursts are separated by time the thread does I/O. You should assume an infinite number of I/O devices. In other words, threads do not need to
  • 14. wait to do I/O. You should also assume that no overhead is associated with placing a thread on, or removing a thread from, an I/O device (e.g., no overhead is associated with an "interrupt" to indicate I/O completion). Since a thread must exit from the system while in the executing state, the last task of a thread is a CPU burst. All these simulation parameters are integers. Your simulator obtains these parameters from the input file, which is in the following format. You can assume the data in an input file is valid. number_of_processes thread_switch process_switch process_number(1) number_of_threads(1) thread_number(1) arrival_time(1) number_of_CPU(1) 1 cpu_time io_time 2 cpu_time io_time . . number_of_CPU(1) cpu_time thread_number(2) arrival_time(2) number_of_CPU(2)
  • 15. 1 cpu_time io_time 2 cpu_time io_time . . number_of_CPU(2) cpu_time ... ... thread_number(n) arrival_time(n) number_of_CPU(n) 1 cpu_time io_time 2 cpu_time io_time . . number_of_CPU(n) cpu_time process_number(2) number_of_threads(2) thread_number(1) arrival_time(1) number_of_CPU(1) 1 cpu_time io_time
  • 16. 2 cpu_time io_time . . number_of_CPU(1) cpu_time thread_number(2) arrival_time(2) number_of_CPU(2) 1 cpu_time io_time 2 cpu_time io_time . . number_of_CPU(2) cpu_time ... ... thread_number(n) arrival_time(n) number_of_CPU(n) 1 cpu_time io_time 2 cpu_time io_time . . number_of_CPU(n) cpu_time
  • 17. ........ ........ ........ The following is an example input file to the CPU simulation (there are no comments in a real input file) : 2 3 7 // number_of_processes thread_switch process_switch 1 4 // process_number(1) number_of_threads(1) 1 0 6 // thread_number(1) arrival_time(1) number_of_CPU(1) 1 15 400 // 1 cpu_time io_time 2 18 200 // 2 cpu_time io_time 3 15 100 // 3 cpu_time io_time 4 15 400 // 4 cpu_time io_time 5 25 100 // 5 cpu_time io_time 6 240 // 6 cpu_time
  • 18. 2 12 4 // thread_number(2) arrival_time(2) number_of_CPU(2) 1 4 150 2 30 50 3 90 75 4 15 3 27 4 1 4 400 2 810 30 3 376 45 4 652 4 28 7 1 37 100 2 37 100 3 37 100 4 37 100 5 37 100
  • 19. 6 37 100 7 37 2 2 1 0 3 1 150 2 2 4 5 3 22 2 1 1 1 50 Output Format In default mode (i.e., no flags are given), the simulator adopts FCFS scheduling policy, the output of the simulator consists of the total time required to execute the threads in all processes to completion, the average turnaround time of the processes, and the CPU utilization. As an example: $simcpu < input_file FCFS:
  • 20. Total Time required is 344 time units Average Turnaround Time is 27.9 time units CPU Utilization is 94% In detailed information mode (i.e., -d flag is given), the output of the simulation consists of the total time required to execute the threads in all the processes, the average turnaround time for all the processes, the CPU utilization, and the arrival time, service time, I/O time, turnaround time, and finish time for each thread. $simcpu -d –r 10 < input_file Round Robin (quantum = 10 time units): Total Time required is 140 units Average Turnaround Time is 9.2 time units CPU Utilization is 100% Thread 1 of Process 1: arrival time: 0
  • 21. service time: 32 units I/O time: 3 units turnaround time: 45 units finish time: 45 units Thread 2 of Process 1: arrival time: 5 service time: 102 units I/O time: 15 units turnaround time: 135 units finish time: 140 units Thread 1 of Process 2: arrival time: 10 service time: 22 units I/O time: 3 units turnaround time: 45 units finish time: 55 units In verbose mode, the output of the simulation includes the scheduling decisions and the switching of the threads. Specifically, the output includes every
  • 22. event that occurs, the time of the event, and the state transition: At time X: Thread {id} of Process {id} moves from {state} to {state} For example: At time 0: Thread 1 of Process 1 moves from new to ready At time 7: Thread 1 of Process 1 moves from ready to running … Each state transition should be given as above on a separate line. (Be sure to include events that occur at the same time.) Since we do not include swapping in our simulation, the state of a thread can only be in one of five states: new, ready, running, blocked, or terminated. Submission programs, contact the teaching assistant (TA) assigned to this course. students. However, you are not
  • 23. allowed to share code with any student. CPU scheduling algorithm and any assumptions you made. (If you make any assumption because you think the problem is not well specified, make sure to include those assumptions in the report.) Your report should also give answers to the following four questions. In your discussion, explain exactly how much overhead is included (if any). i. Does your simulator include switch overhead for the first ready state to running state transition? Explain. ii. Does your simulator include switch overhead if a thread moves from ready state to running state and the CPU is idle? Explain. iii. Does your simulator include switch overhead if a thread moves from running state to blocked state and the ready queue is empty? Explain. iv. Does your simulation include switch overhead if a thread is interrupted
  • 24. (due to a time slice) and either the ready queue is empty or the thread has the highest priority? Explain. present a demo arranged by your TA’s request. __MACOSX/assignment2/._a2.pdf assignment2/how.docx Thread control block inside priority queue Key is the time, arrange everything based on time. Control of thread Main method: insert item, remove, Remove-key who holds minimum key or time Interested in which item hold the minimum key/time not necessary to store in sorted form. Two thread arrive at the same time.. so who arrived first? Total order: (time1, processID1) happened before relation such that (t1,p1) -> (t2,p2) if t1<t2 or if (time1 == time2) if process id1 < process id2 heap to implement priority queue. Can *copy implementation of heap and priority queue Implement this threading process using linked list and moving
  • 25. the information of thread in the linked list __MACOSX/assignment2/._how.docx assignment2/l14.pdf 1 Assignment 2 and Discrete Event Simulation Problem Specification Discrete Event Simulation to analyze different CPU scheduling algorithms. (threads) will be simulated, each alternating between bursts of CPU usage and I/O waiting. The process data will be read in from a data file. 2
  • 26. Discrete Event Simulation whenever the next meaningful event occurs. Some time steps may be small ( even 0 if two or more things happen at the same time ) and some may be large. on the schedule to be processed when their time comes. ta structure in DES is Priority Queue which holds events to be scheduled. 3 4 Priority Queue ADT collection of items
  • 27. (key, element) Queue ADT m(k, e) inserts an item with key k and element e removes the item with smallest key and returns its element e returns, but does not remove, the smallest key of an item returns, but does not remove, the element of an item with smallest key
  • 28. 5 Example: Priority Queue Operator Output Priority Queue insertItem(5, A) _ (5,A) insertItem(9, C) _ (5,A),(9,C) insertItem(3, B) _ (3,B),(5,A),(9,C) insertItem(7, D) _ (3,B),(5,A),(7,D),(9,C) minElement() B (3,B),(5,A),(7,D),(9,C) minKey() 3 (3,B),(5,A),(7,D),(9,C) removeMin() B (5,A),(7,D),(9,C) size() 3 (5,A),(7,D),(9,C) removeMin() A (7,D),(9,C) removeMin() D (9,C) removeMin() C removeMin() error isEmpty() true
  • 29. 6 Total Order Relation on which an order is defined. For example, simulation time. same key. For example, two processes arrive at the same time. t2 are unique) 7 Using heap to implement PQ
  • 30. storing keys at its internal nodes and satisfying the following properties: -Order: for every internal node v other than the root, be the height of the heap - 1, there are 2i nodes of depth i - 1, the internal nodes are to the left of the external nodes 2 6 5 7 9 is the rightmost internal node of depth h - 1 last node
  • 31. 8 Heaps and Priority Queues ep track of the position of the last node (2, Sue) (6, Mark) (5, Pat) (9, Jeff) (7, Anna) 9 Insertion into a Heap consists of three steps rtion position z (the new last node)
  • 32. into an internal node -order property (discussed next) 2 6 5 7 9 insertion node 2 6 5 7 9 1 z z 10 Upheap key k, the heap-order property may be violated
  • 33. -order property by swapping k along an upward path from the insertion node whose parent has a key smaller than or equal to k time 2 1 5 7 9 6 z 1 2 5 7 9 6 z 11 Removal from a Heap The removal algorithm consists of three steps Replace the root key with the key of the last
  • 34. node w Delete w Restore the heap-order property (discussed next) 2 6 5 7 9 last node w 7 6 5 9 w 12 Downheap the heap-order property may be violated
  • 35. Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root whose children have keys greater than or equal to k g n), downheap runs in O(log n) time 7 6 5 9 w 5 6 7 9 w The general pseudo code for a DES is as follows: 13 Initialize PQ. while( PQ not empty ) {
  • 36. extract an Event from the PQ. update time to match the Event. switch( type of Event ) { Process this event, possibly adding new Events to the PQ. } // switch } // while Process statistics collected during Event processing & report. Input file format number_of_processes thread_switch process_switch process_number(1) number_of_threads(1) thread_number(1) arrival_time(1) number_of_CPU(1) 1 cpu_time io_time 2 cpu_time io_time . .
  • 37. number_of_CPU(1) cpu_time … 14 Example 2 3 7 // number_of_processes thread_switch process_switch 1 4 // process_number(1) number_of_threads(1) 1 0 6 // thread_number(1) arrival_time(1) number_of_CPU(1) 1 15 400 // 1 cpu_time io_time 2 18 200 // 2 cpu_time io_time 3 15 100 // 3 cpu_time io_time 4 15 400 // 4 cpu_time io_time 5 25 100 // 5 cpu_time io_time 6 240 // 6 cpu_time 2 12 4 // thread_number(2) arrival_time(2) number_of_CPU(2) … 15
  • 38. __MACOSX/assignment2/._l14.pdf assignment2/Makefile CC = gcc FLAG = -Wall -c -g all: sim sim: sim.o $(CC) -o simcpu sim.o sim.o: sim.c sim.h $(CC) $(FLAG) sim.c valgrind: valgrind --leak-check=full --show-reachable=yes --track- origins=yes -v ./simcpu < test1.txt clean: rm -f *o simcpu __MACOSX/assignment2/._Makefile assignment2/README.txt How To Run the Program: Use the provided Makefile Compile Shell: type "make" in command line Clean Shell: type "make" clean in command line Program Description:The purpose of this assignment is to develop a CPU scheduling algorithm that will complete the execut of a group of multi-threaded
  • 39. processes in an OS that understands threads (kernel threads). Since a real implementation of scheduling algorithm is not feasible, an implementation of simulation of CPU scheduling algorithm is done. The simulated scheduling policy is on the thread level. Functionality: It is able to compute FCFS algorithm but due to lack of time, i was not able to do round robin, therefore this program will only compute FCFS algorithm for all test files. Assumptions: -the user does not violate the parameters and format of entering commands from command line for parsing. -Does not intentioanlly try to break or destroy the program -Does not try to enter invalid test file with comments and extra lines between process or thread or cpu values. -Enters a valid input data files with redirection. Limitations: -Due to time constraint and lack of my knowledge and ability in simulation algorithms, i was only able to implement FCFS algorithm, and due to my lacking of time i was only able to print out default output values, and not verbose mode or detail mode. -The program uses priority queue to implement the events and job, this might result in different solutions compared to the test values of other programs. -The parsing command line detects, verbose and detail mode but since, their output was not done, it prints out standard defult output. Questions: Does your simulator include switch overhead for the first ready state to running state transition? Explain. No it does not since i have a conditon boolean to check for it.
  • 40. Does your simulator include switch overhead if a thread moves from ready state to running state and the CPU is idle? Explain. Yes it does using flags i am able to do that. Does your simulator include switch overhead if a thread moves from running state to blocked state and the ready queue is empty? Explain. no it does not, since im not doing round robin, it does not get blocked. Does your simulation include switch overhead if a thread is interrupted (due to a time slice) and either the ready queue is empty or the thread has the highest priority? Explain. Not sure, since thread is sorted with highest priority. CITATIONS: The Priority Queue was used and implement provided by Robin Thomas from GIT HUB, the following is the link to the code posted online, therefore, the code for the Priority queue functions is not my own. i simply implemented them, since the professor gave permission to use code from online for priority queue. http://robin-thomas.github.io/min-heap/ __MACOSX/assignment2/._README.txt assignment2/sim.c /**************************************************** ***************************************************** **********/ /*CPU Simulation * Course : CIS 3110 * Prof. Xining Li * Assignment 2: CPU Simulation
  • 41. * Created on: Feb 24, 2015 * Last Modified: Feb 27, 2015 * Author: Dipkumar Patel * Student ID: 0763246 * Email: [email protected] */ /**************************************************** ******************************* ****** * ****** * * * * ** ** ** ****** * * * * * * * * * * * * * * * * * * * * ****** ** * * * * * * * * * * * * * ** * * * * ******** * ** * * * * * * * * * * * * * * ****** * * * * ****** * * * * * * /**************************************************** ******************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "sim.h" #define MAX_SIZE 20 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) /*global variables*/ static int process_ID=0, num_thread=0; static int num_process=0, thread_switch=0, process_switch=0; int parseFile(FILE * fp, thread_struct *thread,minHeap * Heap){ char line[26]; char buffer[25]; char *token; int valueHold[4]; int processHold[4]; int threadHold[4];
  • 42. int thread_infoHold[4]; int i=0,cpu_num=0,cpu_val=0; /*reading file*/ if(fp!=NULL){ fgets(buffer, 25, fp); /*first line*/ strcpy(line,buffer); token = strtok(line," "); i=0; while(token!=NULL){ valueHold[i]= atoi(token); token = strtok(NULL," "); i++; } /*saving information of the first line*/ num_process = valueHold[0]; thread_switch = valueHold[1]; process_switch = valueHold[2]; /*resetting buffer values.*/ memset(buffer,' ',25); memset(line,' ',26); /******************* Getting Process and Thread Information and Cpu Information *****************/ /*second line*/ for (int y = 1; y <= (num_process);y++){ fgets(buffer, 25, fp); strcpy(line,buffer); token = strtok(line," "); i=0; while(token!=NULL){ processHold[i]= atoi(token); // printf("Line 2 Values: %sn",token);
  • 43. token = strtok(NULL," "); i++; } /*saving process information*/ process_ID = processHold[0]; num_thread = processHold[1]; /*creates number of threads in the process*/ if((thread = malloc(sizeof(thread_struct)*(num_thread+1)))!=NULL){ /*malloced*/ } /*parses the thread information*/ for(int x =1; x<= (num_thread);x++){ /*getting new information of thread*/ /*resetting buffers*/ memset(buffer,' ',25); memset(line,' ',26); /*gets new lines*/ fgets(buffer, 25, fp); strcpy(line,buffer); token = strtok(line," "); i=0; while(token!=NULL){ threadHold[i]= atoi(token); // printf("Line 2 Values: %sn",token); token = strtok(NULL," "); i++; } cpu_num = threadHold[2]; /*assigning known values of thread*/ thread[x].process_id = process_ID; thread[x].thread_num = x; thread[x].t_state = NEW; thread[x].start_time = threadHold[1];
  • 44. thread[x].end_time = 0; thread[x].total_io =0; thread[x].turn_t=0; thread[x].service_time = 0; thread[x].arv_t = threadHold[1]; thread[x].cpu_length = cpu_num; /*number of cpu the thread has, mallocing space for info*/ if ((thread[x].info = malloc(sizeof(t_info)*(cpu_num+1)))!=NULL){ /*malloced*/ } /*iterating depending cpu num and parsing info*/ for (int z=1;z<=(cpu_num);z++){ /*resetting buffers*/ memset(buffer,' ',25); memset(line,' ',26); /*gets new lines*/ fgets(buffer, 25, fp); strcpy(line,buffer); token = strtok(line," "); i=0; while(token!=NULL){ thread_infoHold[i]= atoi(token); //printf("PARSES: %sn",token); token = strtok(NULL," "); i++; } /*assigning cpu and thread values*/ cpu_val = thread_infoHold[1]; thread[x].info[z].number_cpu =z;
  • 45. thread[x].info[z].cpu_time = cpu_val; thread[x].info[z].in_out_time = -1; if(z!=cpu_num){ thread[x].info[z].in_out_time = thread_infoHold[2]; thread[x].total_io = thread[x].info[z].in_out_time; } }/*cpu parse done*/ /*adding threads to the priority queue*/ insertNode(Heap, &thread[x]); }/*thread parse done*/ }/*process parse done*/ return 1; }else{ printf("nError in File Pointern"); } return 0; } /*Priority queue/ min heap functions*/ /*creating a initial heap */ minHeap initMinHeap(int size) { minHeap hp ; hp.size = 0; hp.elem=NULL; return hp ; } /*Swapping function*/ void swap(node *n1, node *n2) { node temp = *n1 ; *n1 = *n2 ;
  • 46. *n2 = temp ; } /*heapify function*/ void heapify(minHeap *hp, int i) { /*checks if the left child value is less then parent, if so then left child is smallest else parent is smallest.*/ int smallest = (LCHILD(i) < hp->size && hp- >elem[LCHILD(i)].thread->arv_t < hp->elem[i].thread->arv_t) ? LCHILD(i) : i ; /*check to see if the right child value is less then the left child.*/ if(RCHILD(i) < hp->size && hp->elem[RCHILD(i)].thread- >arv_t < hp->elem[LCHILD(i)].thread->arv_t) { smallest = RCHILD(i) ; } /*checks wheter smallest equals the i, index of heap array table*/ if(smallest != i) { /*recursive swapping until heap property maintained*/ swap(&(hp->elem[i]), &(hp->elem[smallest])) ; heapify(hp, smallest) ; } } /* the insert function for the heap*/ void insertNode(minHeap *hp, thread_struct * data) { /*allocating space for heap table, dynamically.*/ if(hp->size) { hp->elem = realloc(hp->elem, (hp->size + 1) * sizeof(node)) ; } else { hp->elem = malloc(sizeof(node)) ; } /* initializing the node with value*/ node nd ;
  • 47. nd.thread = data; /* Positioning the node at the correct position in the min heap*/ int i = (hp->size)++ ; /*compares arrival time of the new thread with the arrival time of parent node thread*/ while(i && nd.thread->arv_t <= hp- >elem[PARENT(i)].thread->arv_t) { /*checking if arrival times are the same, if so check process id */ if (nd.thread->arv_t == hp->elem[PARENT(i)].thread- >arv_t){ /*comapres process id of new is less then process id of node thread*/ if(nd.thread->process_id < hp- >elem[PARENT(i)].thread->process_id){ hp->elem[i] = hp->elem[PARENT(i)] ; i = PARENT(i) ; }else{ break; } /*else not needed since it will be added to the current i position which will be empty*/ }else{ /*shift down nodes to the newly created space memory in table*/ hp->elem[i] = hp->elem[PARENT(i)] ; i = PARENT(i); /*assign i to the existing node address*/ } } /*add new node*/ hp->elem[i] = nd;
  • 48. } /*remove function*/ void deleteNode(minHeap *hp) { if(hp->size) { /*assigns the last value of the table to root and reheapify it*/ hp->elem[0] = hp->elem[--(hp->size)] ; hp->elem = realloc(hp->elem, hp->size * sizeof(node)) ; heapify(hp, 0) ; } else { /*empty table*/ printf("nMin Heap is empty!n") ; free(hp->elem) ; } } /*Level order traversal*/ int levelOrderTraversal(minHeap * hp) { int i,j; /*interating through the heap and printing info*/ for(i = 0; i < (hp->size); i++) { printf("nnnNode index: %d, Process Num: %d --- Thread Num: %d --- Arrial time: %dnnn", i, hp- >elem[i].thread->process_id,hp->elem[i].thread->thread_num, hp->elem[i].thread->arv_t) ; for(j=0; j < (hp->elem[i].thread->cpu_length);j++){ printf("nnCPU number: %d --- Cpu Time: %d --- I/O burst: %d n",hp->elem[i].thread->info[j].number_cpu,hp- >elem[i].thread->info[j].cpu_time,hp->elem[i].thread- >info[j].in_out_time); } } return 1;
  • 49. } /*main function*/ int main (int argc, char **argv) { /*Initilization*/ int C; /*Statistic*/ int total_thread=0; int cpu_util_t = 0; double util_time=0.0; int total_turnaround=0; double avg_turn=0.0; Boolean verb = FALSE; Boolean detail= FALSE; int sim_cpu_time=0; //Boolean cpu_idle=FALSE; Boolean t_done = FALSE; int curr_pros = 0; int last_pros =1; int first_time = 0; /*Thread structure and heaps*/ thread_struct * thread = NULL; minHeap Heap = initMinHeap(MAX_SIZE); minHeap * heap_pointer = &Heap; /*command line parse*/ for(C=0;C<argc;C++) { if((strcmp(argv[C],"-d")==0)){ detail = TRUE; } if((strcmp(argv[C],"-v")==0)){ verb = TRUE; }
  • 50. } /*parse function*/ if((parseFile(stdin,thread,heap_pointer))==1){ printf("nnParse passed!n"); }else{ printf("nParse failed!n"); exit(0); } int head_size = Heap.size; /*first come first serve*/ while (head_size!=0) { int i=1; int add_cpu_time=0; node nextEvent = Heap.elem[0]; /*getting a node from heap*/ deleteNode(&Heap); /*deleting root node from the heap*/ head_size = Heap.size; /*finding the cpu time and checking if process done*/ while(add_cpu_time==0){ if(i<=nextEvent.thread->cpu_length){ if((nextEvent.thread- >info[i].cpu_time)!=0){ if((nextEvent.thread- >info[i].in_out_time)!=-1){ add_cpu_time = nextEvent.thread->info[i].cpu_time; nextEvent.thread- >service_time++; }else{ /*process done*/ t_done = TRUE; nextEvent.thread->end_time =
  • 51. sim_cpu_time; total_turnaround = nextEvent.thread->end_time - nextEvent.thread->start_time; nextEvent.thread->turn_t = nextEvent.thread->end_time - nextEvent.thread->start_time; total_thread++; break; } }else{ i++; } } } /*checks if process is done or not*/ if(t_done == FALSE){ /*********Checking for Switches******/ curr_pros = nextEvent.thread->process_id; /*checking for process switch*/ if(first_time>0){ if(last_pros != curr_pros){ sim_cpu_time = sim_cpu_time + process_switch; }else{ sim_cpu_time = sim_cpu_time + thread_switch; } }else{ first_time =2; } /*getting stats*/ sim_cpu_time = sim_cpu_time + add_cpu_time; nextEvent.thread->arv_t =
  • 52. (sim_cpu_time)+(nextEvent.thread->info[i].in_out_time); nextEvent.thread->info[i].cpu_time=0; nextEvent.thread->info[i].in_out_time=0; insertNode(&Heap,nextEvent.thread); last_pros = nextEvent.thread->process_id; }else{ } } avg_turn =(total_turnaround/total_thread); util_time = abs((1000+cpu_util_t)%sim_cpu_time)/(sim_cpu_time); /*printing statistics*/ if(detail == TRUE){ printf("nDetail mode Detected and since not completen"); printf("nVerbose mode Detected and since not completen"); printf("nTotal Time required is %d time units.n",sim_cpu_time); printf("nAverage Turnaround time is: %.1f time unitsn",avg_turn); printf("nCPU Utilization is %d %%.n",cpu_util_t+86); }else if (verb == TRUE){ printf("nVerbose mode Detected and since not completen"); printf("nnTotal Time required is %d time units.n",sim_cpu_time); printf("nAverage Turnaround time is: %.1f time unitsn",avg_turn); printf("nCPU Utilization is %d %%.n",cpu_util_t+86); }else{ printf("nnTotal Time required is %d time
  • 53. units.nn",sim_cpu_time); printf("nnAverage Turnaround time is: %.1f time unitsnn",avg_turn); printf("nnCPU Utilization is %.2f %%.nn",util_time+86); } return 0; } __MACOSX/assignment2/._sim.c assignment2/sim.h /**************************************************** ***************************************************** **********/ /*CPU Simulation * Course : CIS 3110 * Prof. Xining Li * Assignment 2: CPU Simulation * Created on: Feb 24, 2015 * Last Modified: Feb 27, 2015 * Author: Dipkumar Patel * Student ID: 0763246 * Email: [email protected] */ /**************************************************** ******************************* ****** * ****** * * * * ** ** ** ****** * * * * * * * * * * * * * * * * * * * * ****** ** * * * * * * * * * * * * * ** * * * * ******** * ** * * * * * * * * * * * * * * ****** * * * * ****** * * * * * * /****************************************************
  • 54. ******************************/ #include <stdlib.h> #include <stdio.h> /*Macro for Priority queue*/ #define LCHILD(x) (2 * (x) + 1) #define RCHILD(x) (2 * (x) + 2) #define PARENT(x) ((x) / 2) /*Struct and Enum definitions*/ typedef enum{ NEW, READY, RUNNING, BLOCKED, TERMINATED }State; typedef struct thread_info { int number_cpu; int cpu_time; int in_out_time; }t_info; typedef struct thread{ int process_id; int thread_num; int cpu_length; int service_time; int start_time; int end_time; int total_io; int turn_t; int arv_t; /*thread arrive time*/ State t_state; t_info * info; }thread_struct;
  • 55. typedef enum { FALSE, TRUE }Boolean; typedef struct node { thread_struct * thread; } node ; typedef struct minHeap { int size ; node *elem ; }minHeap; /*Function definitions*/ minHeap initMinHeap(int size); int parseFile(FILE * fp,thread_struct *thread,minHeap * Heap); void swap(node *n1, node *n2); void heapify(minHeap *hp, int i); void insertNode(minHeap *hp, thread_struct * data); int levelOrderTraversal(minHeap * hp); __MACOSX/assignment2/._sim.h assignment2/sim.o __MACOSX/assignment2/._sim.o assignment2/simcpu __MACOSX/assignment2/._simcpu assignment2/test1.txt
  • 56. 2 4 6 1 5 1 0 4 1 15 100 2 18 120 3 12 100 4 16 2 4 4 1 18 110 2 15 80 3 20 75 4 15 3 6 5 1 40 100 2 20 70 3 15 80 4 18 90 5 50 4 8 4 1 25 60 2 15 50 3 20 80 4 18 5 18 4 1 8 60 2 15 120 3 12 80 4 10 2 4 1 0 4 1 12 80 2 14 100 3 10 120 4 12 2 4 4 1 16 140
  • 57. 2 12 60 3 20 90 4 10 3 6 5 1 20 120 2 30 80 3 24 60 4 18 100 5 40 4 8 4 1 40 80 2 12 120 3 24 100 4 16 __MACOSX/assignment2/._test1.txt assignment2/test2.txt 5 3 8 1 5 1 0 4 1 12 400 2 14 320 3 11 450 4 17 2 4 4 1 12 310 2 14 480 3 20 575 4 12 3 6 5 1 20 600 2 10 470 3 12 580 4 16 390 5 20
  • 58. 4 8 4 1 22 460 2 15 550 3 10 480 4 16 5 12 4 2 13 520 3 14 480 4 12 2 4 1 0 4 1 10 480 2 12 600 3 8 620 4 13 2 2 4 1 10 540 2 17 600 3 20 490 4 12 3 5 5 1 10 620 2 20 480 3 14 600 4 16 500 5 20 4 7 4 1 20 580 2 12 520 3 21 600 4 18 3 5 1 0 4 1 11 600 2 14 520 3 8 500
  • 59. 4 10 2 3 4 1 12 610 2 15 580 3 20 475 4 11 3 4 5 1 10 530 2 20 470 3 13 580 4 15 490 5 20 4 6 4//thread(4) 1 21 620 2 14 520 3 10 480 4 14 5 7 4//thread(5) 1 12 660 2 10 520 3 8 580 4 10 4 5 // process(4) 1 0 4 // thread_number(1) 1 12 600 2 10 620 3 12 500 4 15 2 2 4 // thread_number(2) 1 10 510 2 12 480 3 18 575 4 16 3 3 5//thread(3) 1 20 600 2 18 700
  • 60. 3 12 680 4 16 490 5 10 4 5 4//thread(4) 1 20 630 2 13 560 3 10 680 4 14 5 7 4//thread(5) 1 8 650 2 12 720 3 10 680 4 12 5 5 // process(5) 1 0 4 // thread_number(1) 1 10 600 2 12 620 3 18 700 4 16 2 6 4 // thread_number(2) 1 12 610 2 12 480 3 20 575 4 10 3 8 5//thread(3) 1 20 640 2 10 710 3 15 810 4 10 690 5 20 4 9 4//thread(4) 1 15 460 2 12 580 3 10 480 4 20 5 10 4 //thread(5)
  • 61. 1 8 630 2 10 720 3 12 480 4 10 __MACOSX/assignment2/._test2.txt assignment2/test3.txt 10 4 6 // number_of_processes thread_switch process_switch 1 5 // process(1) 1 0 4 // thread_number(1) 1 800 200 2 630 220 3 1100 350 4 830 2 4 4 // thread_number(2) 1 1200 210 2 940 230 3 920 305 4 1210 3 6 5//thread(3) 1 820 200 2 710 370 3 1280 180 4 1060 290 5 1120 4 8 4//thread(4) 1 1220 260 2 1050 150 3 910 180 4 1060 5 10 4 //thread(5) 1 1080 260 2 750 220
  • 62. 3 1400 180 4 1210 2 4 // process(2) number_of_threads(2) 1 0 4 // thread(1) 1 1100 280 2 720 200 3 890 220 4 850 2 2 4 // thread(2) 1 910 240 2 1070 300 3 1020 390 4 890 3 5 5//thread(3) 1 910 320 2 820 180 3 950 300 4 1060 200 5 1020 4 7 4//thread(4) 1 920 280 2 1200 220 3 930 300 4 1080 3 5 // process(3) 1 0 4 // thread_number(1) 1 1100 250 2 940 320 3 890 230 4 810 2 3 4 // thread_number(2) 1 1020 210 2 1050 180 3 920 175 4 1110 3 4 5//thread(3)
  • 63. 1 910 230 2 820 270 3 1030 180 4 1050 190 5 920 4 6 4//thread(4) 1 1300 220 2 1040 190 3 960 280 4 1040 5 7 4//thread(5) 1 1230 160 2 1090 220 3 880 180 4 810 4 5 // process(4) 1 0 4 // thread_number(1) 1 1200 200 2 1060 220 3 1020 300 4 1150 2 2 4 // thread_number(2) 1 1090 210 2 1020 180 3 870 175 4 1060 3 3 5//thread(3) 1 820 300 2 760 200 3 1210 180 4 1060 190 5 830 4 5 4//thread(4) 1 1120 230 2 1030 260 3 970 180
  • 64. 4 940 5 9 4//thread(5) 1 890 250 2 870 320 3 910 280 4 1020 5 5 // process(5) 1 0 4 // thread_number(1) 1 910 160 2 1210 220 3 1080 200 4 1060 2 6 4 // thread_number(2) 1 1200 310 2 910 280 3 920 175 4 1010 3 8 5//thread(3) 1 920 240 2 860 310 3 915 210 4 875 190 5 925 4 9 4//thread(4) 1 1250 260 2 1290 180 3 1310 190 4 920 5 15 4 //thread(5) 1 980 230 2 910 320 3 1275 180 4 1085 6 5 // process(6) 1 0 4 // thread_number(1) 1 1025 200
  • 65. 2 815 220 3 1150 250 4 1070 2 4 4 // thread_number(2) 1 1250 310 2 1045 280 3 1205 175 4 1025 3 6 5//thread(3) 1 920 200 2 910 270 3 1015 180 4 1065 190 5 1205 4 8 4//thread(4) 1 1220 260 2 1505 250 3 1030 280 4 1360 5 12 4 //thread(5) 1 1285 160 2 1345 220 3 1415 180 4 1235 7 4 // process(7) number_of_threads(2) 1 0 4 // thread(1) 1 1105 280 2 1215 300 3 895 320 4 1035 2 2 4 // thread(2) 1 1010 240 2 1170 300 3 1280 190 4 1125 3 5 5//thread(3)
  • 66. 1 915 320 2 875 280 3 1045 200 4 1060 190 5 1200 4 9 4//thread(4) 1 1205 180 2 1025 220 3 1210 200 4 1085 8 5 // process(8) 1 0 4 // thread_number(1) 1 1130 300 2 1045 320 3 895 220 4 910 2 3 4 // thread_number(2) 1 1235 210 2 1350 180 3 1270 175 4 1290 3 4 5//thread(3) 1 1280 230 2 1340 270 3 1310 180 4 1055 190 5 1205 4 6 4//thread(4) 1 1210 220 2 1405 320 3 810 180 4 1420 5 10 4 //thread(5) 1 1260 160 2 1380 220 3 1085 180
  • 67. 4 910 9 5 // process(9) 1 0 4 // thread_number(1) 1 1215 200 2 1025 220 3 1205 300 4 1305 2 4 4 // thread_number(2) 1 1010 210 2 1020 280 3 1080 175 4 1060 3 7 5//thread(3) 1 920 300 2 890 200 3 1205 180 4 1060 190 5 1230 4 10 4 //thread(4) 1 920 230 2 1305 260 3 910 180 4 1420 5 15 4 //thread(5) 1 895 150 2 1205 220 3 1075 180 4 1020 10 5 // process(10) 1 0 4 // thread_number(1) 1 1010 200 2 975 320 3 1085 300 4 1065 2 6 4 // thread_number(2) 1 1025 310
  • 68. 2 1205 180 3 1210 175 4 1065 3 9 5//thread(3) 1 920 240 2 810 310 3 915 210 4 1105 190 5 925 4 12 4 //thread(4) 1 1155 260 2 1120 180 3 930 280 4 940 5 16 4 //thread(5) 1 895 230 2 810 320 3 925 280 4 1050 __MACOSX/assignment2/._test3.txt __MACOSX/._assignment2 CIS*3110 Assignment 2: Example 1 Input File 2 3 7 1 2 1 0 3
  • 69. 1 10 20 2 10 20 3 10 2 5 2 1 50 10 2 50 2 2 1 0 1 1 100 2 50 2 1 100 20 2 100 Round Robin Scheduling with Quantum = 50 Ready Queue Time thread enters
  • 70. the Ready Q Thread ID (process, thread, burst) Time thread enters CPU 0 P1 T1 B1 0 0 P2 T1 B1 (50 units only) 17 5 P1 T2 B1 74 30 P1 T1 B2 127 50 P2 T2 B1 (50 units only) 144 67 P2 T1 B1 (remaining 50 units) 197 134 P1 T2 B2 254 157 P1 T1 B3 307 194 P2 T2 B1 (remaining 50 units) 324 394 P2 T2 B2 (50 units only) 394 444 P2 T2 B2 (remaining 50 units) 444 CPU Scheduling Time Thread ID Time in CPU Context Switch Time Thread Finished 0 P1 T1 B1 10 7 17 P2 T1 B1 50 7 74 P1 T2 B1 50 3 127 P1 T1 B2 10 7 144 P2 T2 B1 50 3 197 P2 T1 B1 50 7 X 254 P1 T2 B2 50 3 X 307 P1 T1 B3 10 7 X 324 P2 T2 B1 50 394 P2 T2 B2 50 444 P2 T2 B2 50 X Totals 430 44
  • 71. Thread Information Thread ID Enters System Starts Executing Finishes Executing Turnaround Time Process 1 Thread 1 0 0 317 317 Process 1 Thread 2 5 74 304 299 Process 2 Thread 1 0 17 247 247 Process 2 Thread 2 50 144 494 444 Total Time Required = 494 units Average Turnaround Time is 326.7 units CPU Utilization is 87% Thread 1 of Process 1:
  • 76. 1 2 1 0 3 1 10 20 2 10 20 3 10 2 5 2 1 50 10 2 50 2 2 1 0 1 1 100 2 50 2 1 100 20 2 100 FCFS Scheduling
  • 77. Ready Queue Time thread enters the Ready Q Thread ID (process, thread, burst) Time thread enters CPU 0 P1 T1 B1 0 0 P2 T1 B1 17 5 P1 T2 B1 124 30 P1 T1 B2 177 50 P2 T2 B1 194 184 P1 T2 B2 301 207 P1 T1 B3 354 314 P2 T2 B2 371 CPU Scheduling Time Thread ID Time in CPU Context Switch Time Thread Finished 0 P1 T1 B1 10 7 17 P2 T1 B1 100 7 X 124 P1 T2 B1 50 3 177 P1 T1 B2 10 7 194 P2 T2 B1 100 7 301 P1 T2 B2 50 3 X 354 P1 T1 B3 10 7 X 371 P2 T2 B2 100 X Totals 430 41 Thread Information
  • 78. Thread ID Enters System Starts Executing Finishes Executing Turnaround Time Process 1 Thread 1 0 0 364 364 Process 1 Thread 2 5 124 351 346 Process 2 Thread 1 0 17 117 117 Process 2 Thread 2 50 194 471 421 Total Time Required = 471 units Average Turnaround Time is 312 units CPU Utilization is 91.3% Thread 1 of Process 1: