Dr. B.R. Ambedkar National Institute of Technology Jalandhar
Dr. B.R. Ambedkar National Institute of Technology Jalandhar
Theory:
Usually shells are interactive that mean, they accept command as input from users and
execute them. However, some time we want to execute a bunch of commands
routinely, so we have type in all commands each time in terminal.
As shell can also take commands as input from file we can write these commands in a
file and can execute them in shell to avoid this repetitive work. These files are called
Shell Scripts or Shell Programs. Shell scripts are similar to the batch file in MS-DOS.
Each shell script is saved with .sh file extension for eg. myscript.sh
A shell script have syntax just like any other programming language. If you have any
prior experience with any programming language like Python, C/C++ etc. it would be
very easy to get started with it.
if[“$test1”=”$test2”]
then
echo ”Both are same”
else
echo “Both are not same”
fi
Lab 2
Aim: Write a program for first come first serve CPU scheduling.
Theory:
First in, first out (FIFO), also known as first come, first served (FCFS), is the simplest
scheduling algorithm. FIFO simply queues processes in the order that they arrive in the ready
queue.
In this, the process that comes first will be executed first and next process starts only after
the previous gets fully executed. When the CPU is free, it is allocated to the process at
the head of the queue. The running process is then removed from the queue. FCFS is a
non-preemptive scheduling algorithm.
Algorithm:
1- Input the processes along with their burst time (bt).
2- Find waiting time (wt) for all processes.
3- As first process that comes need not to wait so waiting time for process 1 will
be 0 i.e. wt[0] = 0.
4- Find waiting time for all other processes i.e. for process i ->
wt[i] = bt[i-1] + wt[i-1] .
5- Find turnaround time = waiting_time + burst_time for all processes.
6- Find average waiting time =
i. total_waiting_time / no_of_processes.
7- Similarly, find average turnaround time =
ii. total_turn_around_time / no_of_processes.
Program:
#include<iostream>
#include<vector>
using namespace std;
for(int i=0;i<n;i++)
{
cout<<"\n"<<process[i]<<"\t\t"<<at[i]<<"\t\t"<<bt[i]<<"\t"<<ct[i]<<"\t"<<tat[i
]<<"\t"<<wt[i
]<<"\n";
}
}
int main()
{
int process[]={1,2,3};
int at[]={0,3,5};
int bt[]={3,9,12};
int n=sizeof process/sizeof process[0];
cout<<"size: "<<n;
fcfs(process,at,bt,n);
}
Lab 3
Aim: Write a program for shortest job first CPU scheduling.
Theory:
Shortest Job First scheduling works on the process with the shortest burst
time or duration first. This is the best approach to minimize waiting time.This is used
in Batch Systems. It is of two types:
1. Non Pre-emptive
2. Pre-emptive
To successfully implement it, the burst time/duration time of the processes should be known
to the processor in advance, which is practically not feasible all the time.
This scheduling algorithm is optimal if all the jobs/processes are available at the same time.
(either Arrival time is 0 for all, or Arrival time is same for all)
Process which have the shortest burst time are scheduled first.If two processes have the
same bust time then FCFS is used to break the tie. It is a non-preemptive scheduling
algorithm.
Algorithm:
1. Sort all the process according to the arrival time.
2. Then select that process which has minimum arrival time and minimum
Burst time.
3. After completion of process make a pool of process which after till the
completion of previous process and select that process among the pool
which is having minimum Burst time.
Program:
#include<bits/stdc++.h>
using namespace std;
struct Process
{
int pid;
int bt;
};
findWaitingTime(proc, n, wt);
findTurnAroundTime(proc, n, wt, tat);
int main()
{
Process proc[] = {{1, 21}, {2, 3}, {3, 6}, {4, 2}};
int n = sizeof proc / sizeof proc[0];
findAverageTime(proc, n);
return 0;
}
Lab 4
Theory:
Round Robin is a CPU scheduling algorithm where each process is assigned a fixed
time slot in a cyclic way.
It is simple, easy to implement, and starvation-free as all processes get fair
share of CPU.
One of the most commonly used technique in CPU scheduling as a core.
It is preemptive as processes are assigned CPU only for a fixed slice of time
at most.
The disadvantage of it is more overhead of context switching.
Algorithm:
Program:
#include<iostream>
using namespace std;
void findWaitingTime(int processes[], int n,
int bt[], int wt[], int quantum)
{
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
int t = 0;
while (1)
{
bool done = true;
for (int i = 0 ; i < n; i++)
{
if (rem_bt[i] > 0)
{
done = false;
if (rem_bt[i] > quantum)
{
t += quantum;
rem_bt[i] -= quantum;
}
else
{
t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}
if (done == true)
break;
}
}
void findTurnAroundTime(int processes[], int n,
int bt[], int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
void findavgTime(int processes[], int n, int bt[], int quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
cout << "Processes "<< " Burst time "
<< " Waiting time " << " Turn around time\n";
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;
}