Anda di halaman 1dari 14

Name : Ningsih Adityas

Nim : 20180801049
UTS Operating System

1. Explain the various 4 types of allocation algorithms, and describe the advantages and
disadvantages and describe the process of the four algorithms !
 First Fit
In the first fit approach is to allocate the first free partition or hole
large enough which can accommodate the process. It finishes after finding the
first suitable free partition.

Advantage
Fastest algorithm because it searches as little as possible.

Disadvantage
The remaining unused memory areas left after allocation become waste
if it is too smaller. Thus request for larger memory requirement cannot be
accomplished.

 Best Fit
The best fit deals with allocating the smallest free partition which
meets the requirement of the requesting process. This algorithm first searches
the entire list of free partitions and considers the smallest hole that is adequate.
It then tries to find a hole which is close to actual process size needed.

Advantage
Memory utilization is much better than first fit as it searches the
smallest free partition first available.

Disadvantage
It is slower and may even tend to fill up memory with tiny useless
holes.

 Worst fit
In worst fit approach is to locate largest available free portion so that
the portion left will be big enough to be useful. It is the reverse of best fit.

Advantage
Reduces the rate of production of small gaps.

Disadvantage
If a process requiring larger memory arrives at a later stage then it
cannot be accommodated as the largest hole is already split and occupied.

 Buddy's System

In buddy system, sizes of free blocks are in form of integral power of


2. E.g. 2, 4, 8, 16 etc. Up to the size of memory. When a free block of size 2k
is requested, a free block from the list of free blocks of size 2k is allocated. If
no free block of size 2k is available, the block of next larger size, 2k+1 is split
in two halves called buddies to satisfy the request.

Advantage
Buddy system is faster. When a block of size 2k is freed, a hole of 2k
memory size is searched to check if a merge is possible, whereas in other
algorithms all the hole list must be searched.

Disadvantage
It is often become inefficient in terms of memory utilization. As all
requests must be rounded up to a power of 2, a 35KB process is allocated to
64KB, thus wasting extra 29KB causing internal fragmentation. There may be
holes between the buddies causing external fragmentation.

2. What inconveniences that a user can face while interacting with a computer system,
which is without an operating system?
Operating system is a required component of the computer system.
Without an operating system computer hardware is only an inactive electronic
machine, which is inconvenient to user for execution of programs.
As the computer hardware or machine understands only the machine language.
It is difficult to develop each and every program in machine language in order to
execute it.
Thus without operating system execution of user program or to solve user
problems is extremely difficult.

3. Find out which algorithm among FCFS, SJF and Round Robin with quantum 8, would
give the minimum average time for a given workload.

Appendix question number 3 (5 process)

Process Burst Time Arrival Time Priority Quantum Time


P1 6 0 3
8
P2 4 1 4
P3 3 5 1
P4 7 4 5
P5 8 6 2

Answer:

 FCFS
o Source Code
// C++ program for implementation of FCFS
// scheduling with different arrival time
#include<iostream>
using namespace std;

// Function to find the waiting time for all


// processes
void findWaitingTime(int processes[], int n, int bt[],
int wt[], int
at[])
{
int service_time[n];
service_time[0] = 0;
wt[0] = 0;

// calculating waiting time


for (int i = 1; i < n ; i++)
{
// Add burst time of previous processes
service_time[i] = service_time[i-1] + bt[i-1];

// Find waiting time for current process =


// sum - at[i]
wt[i] = service_time[i] - at[i];

// If waiting time for a process is in negative


// that means it is already in the ready queue
// before CPU becomes idle so its waiting time is 0
if (wt[i] < 0)
wt[i] = 0;
}
}

// Function to calculate turn around time


void findTurnAroundTime(int processes[], int n, int bt[],
int
wt[], int tat[])
{
// Calculating turnaround time by adding bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}

// Function to calculate average waiting and turn-around


// times.
void findavgTime(int processes[], int n, int bt[], int at[])
{
int wt[n], tat[n];

// Function to find waiting time of all processes


findWaitingTime(processes, n, bt, wt, at);

// Function to find turn around time for all processes


findTurnAroundTime(processes, n, bt, wt, tat);

// Display processes along with all details


cout << "Processes " << " Burst Time " << " Arrival Time "
<< " Waiting Time " << " Turn-Around Time "
<< " Completion Time \n";
int total_wt = 0, total_tat = 0;
for (int i = 0 ; i < n ; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
int compl_time = tat[i] + at[i];
cout << " " << i+1 << "\t\t" << bt[i] << "\t\t"
<< at[i] << "\t\t" << wt[i] << "\t\t "
<< tat[i] << "\t\t " << compl_time << endl;
}

cout << "Average waiting time = "


<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}

// Driver code
int main()
{
// Process id's
int processes[] = {1, 2, 3,4,5};
int n = sizeof processes / sizeof processes[0];

// Burst time of all processes


int burst_time[] = {6,4,3,7,8};

// Arrival time of all processes


int arrival_time[] = {0,1,5,4,6};

findavgTime(processes, n, burst_time, arrival_time);

return 0;
}
o Output

 SJF Preemptive
o Source Code
// C++ program to implement Shortest Job first with Arrival Time
#include <iostream>
using namespace std;
int mat[10][6];

void swap(int *a, int *b)


{
int temp = *a;
*a = *b;
*b = temp;
}

void arrangeArrival(int num, int mat[][6])


{
for(int i=0; i<num; i++)
{
for(int j=0; j<num-i-1; j++)
{
if(mat[j][1] > mat[j+1][1])
{
for(int k=0; k<5; k++)
{
swap(mat[j][k], mat[j+1][k]);
}
}
}
}
}

void completionTime(int num, int mat[][6])


{
int temp, val;
mat[0][3] = mat[0][1] + mat[0][2];
mat[0][5] = mat[0][3] - mat[0][1];
mat[0][4] = mat[0][5] - mat[0][2];

for(int i=1; i<num; i++)


{
temp = mat[i-1][3];
int low = mat[i][2];
for(int j=i; j<num; j++)
{
if(temp >= mat[j][1] && low >= mat[j][2])
{
low = mat[j][2];
val = j;
}
}
mat[val][3] = temp + mat[val][2];
mat[val][5] = mat[val][3] - mat[val][1];
mat[val][4] = mat[val][5] - mat[val][2];
for(int k=0; k<6; k++)
{
swap(mat[val][k], mat[i][k]);
}
}
}

int main()
{
int num, temp;

cout<<"Enter number of Process: ";


cin>>num;

cout<<"...Enter the process ID...\n";


for(int i=0; i<num; i++)
{
cout<<"...Process "<<i+1<<"...\n";
cout<<"Enter Process Id: ";
cin>>mat[i][0];
cout<<"Enter Arrival Time: ";
cin>>mat[i][1];
cout<<"Enter Burst Time: ";
cin>>mat[i][2];
}

cout<<"Before Arrange...\n";
cout<<"Process ID\tArrival Time\tBurst Time\n";
for(int i=0; i<num; i++)
{
cout<<mat[i][0]<<"\t\t"<<mat[i][1]<<"\t\t"<<mat[i][2]<<"\n";
}
arrangeArrival(num, mat);
completionTime(num, mat);
cout<<"Final Result...\n";
cout<<"Process ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround
Time\n";
for(int i=0; i<num; i++)
{

cout<<mat[i][0]<<"\t\t"<<mat[i][1]<<"\t\t"<<mat[i][2]<<"\t\t"<<mat[i][4]<
<"\t\t"<<mat[i][5]<<"\n";
}
}

o Output

 SJF Non Preemptive

o Source Code

// C++ program to implement Shortest Remaining


// Time First
#include <bits/stdc++.h>
using namespace std;

struct Process {
int pid; // Process ID
int bt; // Burst Time
int art; // Arrival Time
};

// Function to find the waiting time for all


// processes
void findWaitingTime(Process proc[], int n,
int wt[])
{
int rt[n];

// Copy the burst time into rt[]


for (int i = 0; i < n; i++)
rt[i] = proc[i].bt;

int complete = 0, t = 0, minm = INT_MAX;


int shortest = 0, finish_time;
bool check = false;

// Process until all processes gets


// completed
while (complete != n) {

// Find process with minimum


// remaining time among the
// processes that arrives till the
// current time`
for (int j = 0; j < n; j++) {
if ((proc[j].art <= t) &&
(rt[j] < minm) && rt[j] > 0) {
minm = rt[j];
shortest = j;
check = true;
}
}

if (check == false) {
t++;
continue;
}

// Reduce remaining time by one


rt[shortest]--;

// Update minimum
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;

// If a process gets completely


// executed
if (rt[shortest] == 0) {

// Increment complete
complete++;
check = false;

// Find finish time of current


// process
finish_time = t + 1;
// Calculate waiting time
wt[shortest] = finish_time -
proc[shortest].bt -
proc[shortest].art;

if (wt[shortest] < 0)
wt[shortest] = 0;
}
// Increment time
t++;
}
}

// Function to calculate turn around time


void findTurnAroundTime(Process proc[], int n,
int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}

// Function to calculate average time


void findavgTime(Process proc[], int n)
{
int wt[n], tat[n], total_wt = 0,
total_tat = 0;

// Function to find waiting time of all


// processes
findWaitingTime(proc, n, wt);

// Function to find turn around time for


// all processes
findTurnAroundTime(proc, n, wt, tat);

// Display processes along with all


// details
cout << "Processes "
<< " Burst time "
<< " Waiting time "
<< " Turn around time\n";

// Calculate total waiting time and


// total turnaround time
for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << proc[i].pid << "\t\t"
<< proc[i].bt << "\t\t " << wt[i]
<< "\t\t " << tat[i] << endl;
}

cout << "\nAverage waiting time = "


<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}

// Driver code
int main()
{
Process proc[] = { { 1, 6, 0 }, { 2, 4, 1 },
{ 3, 3, 5 }, { 4, 7, 4}, {5,8,6} };
int n = sizeof(proc) / sizeof(proc[0]);

findavgTime(proc, n);
return 0;
}

o Output

 Round Robin
o Source Code
// C++ program for implementation of RR scheduling
#include<iostream>
using namespace std;

// Function to find the waiting time for all


// processes
void findWaitingTime(int processes[], int n,
int bt[], int wt[], int quantum)
{
// Make a copy of burst times bt[] to store remaining
// burst times.
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];

int t = 0; // Current time


// Keep traversing processes in round robin manner
// until all of them are not done.
while (1)
{
bool done = true;

// Traverse all processes one by one repeatedly


for (int i = 0 ; i < n; i++)
{
// If burst time of a process is greater than 0
// then only need to process further
if (rem_bt[i] > 0)
{
done = false; // There is a pending process

if (rem_bt[i] > quantum)


{
// Increase the value of t i.e. shows
// how much time a process has been
processed
t += quantum;

// Decrease the burst_time of current


process
// by quantum
rem_bt[i] -= quantum;
}

// If burst time is smaller than or equal to


// quantum. Last cycle for this process
else
{
// Increase the value of t i.e. shows
// how much time a process has been
processed
t = t + rem_bt[i];

// Waiting time is current time minus


time
// used by this process
wt[i] = t - bt[i];

// As the process gets fully executed


// make its remaining burst time = 0
rem_bt[i] = 0;
}
}
}

// If all processes are done


if (done == true)
break;
}
}

// Function to calculate turn around time


void findTurnAroundTime(int processes[], int n,
int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}

// Function to calculate average time


void findavgTime(int processes[], int n, int bt[],
int
quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

// Function to find waiting time of all processes


findWaitingTime(processes, n, bt, wt, quantum);

// Function to find turn around time for all processes


findTurnAroundTime(processes, n, bt, wt, tat);

// Display processes along with all details


cout << "Processes "<< " Burst time "
<< " Waiting time " << " Turn around time\n";

// Calculate total waiting time and total turn


// around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
<< wt[i] <<"\t\t " << tat[i] <<endl;
}

cout << "Average waiting time = "


<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}

// Driver code
int main()
{
// process id's
int processes[] = { 1, 2, 3,4,5};
int n = sizeof processes / sizeof processes[0];

// Burst time of all processes


int burst_time[] = {6,4,3,7,8};

// Time quantum
int quantum = 8;
findavgTime(processes, n, burst_time, quantum);
return 0;
}

o Output

4. Please describe the block diagram (figure) below

Answer:
o The following Figure shows the overall structure of the linux. The kernel sits
directly on the hardware and enables the interactions wit I/O devices and
memory management
o At the lowests level it containts interrupt handlesrs which are the primary way
for interacting with the device, and low level dispatching mechanism
o The dispatching occurs when an interrupt happen and also when the kernel
completes some operations and it is time to start up a user process again
o Now we devide the various kernel subsystems into the three main components.
The I/O component contains the kernel places and responsible for interacting
with the devices and performing the network and strage I/O operations.
o At the highest level the I/O operations are all intergrated under a virtual file
system and at lowest level, all I/O operations pass through some device driver
o All Linux drivers are classified as either a character device driver or block
device driver, with the main main differcnce that random accesses are allowed
on the block devices and not on the character devices
o Technically network devices are really character devices, but they are handled
somewhat differently, so It is preferable to separate them
o The layer above the network drivers handle a kind of the routing function and
making sure that the right packet goes to the right devices
o Sockets interface which allows the program to create sockets for the particular
network
o On the top of the disk drivers is the I/O scheduler who is responsible for ordering
and issuing disk operations request in a way that tries to converse wate full disk
head movement
o At the very top of the block device column are the file system Linux may have
the multiple file system excising concurrently, block device layer provides an
abstraction lyer used by allfile system
o At the right side of the figure there are two key components of the Linux kernel
these are responsible for the memory and process management tasks
o Memory management tasks include maintaining the virtual to physical memory
mappings, maintaining a cache of the recently accessed pages and implementing
a good page replacement policy
o The key responsibility of the process management component is the creation ad
termination of the process, it also includes the process scheduler and code for the
signal handling
o Finally at the very top is the system call interface in to the kernel. All system
calls come here. Causing a trap which switches the execution from the user
mode in to the kernel Mode.

Anda mungkin juga menyukai