You are on page 1of 16

DR. B.R.

AMBEDKAR NATIONAL INSTITUTE OF TECHNOLOGY


JALANDHAR

Computer Networks Laboratory CSX-321


Session: July-Dec 2019

SUBMITTED TO: SUBMITTED BY:

Dr. D K Gupta Gaurav Sachdeva


Asst. Professor 17103032
Dept of CSE G-2
Lab 1
Aim: Introduction to shell scripting and write a shell script which will display all the
files in the current directory which are having permission of read, write and execute.

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.

A shell script comprises following elements –

 Shell Keywords – if, else, break etc.


 Shell commands – cd, ls, echo, pwd, touch etc.
 Functions
 Control flow – if...then...else, case and shell loops etc.
Shell Script 1:
To concatenate two strings
echo Enter first string:
read s1
echo Enter second string:
read s2
s3=$s1$s2
len=`echo $s3 | wc -c`
len=`expr $len - 1`
echo Concatinated stringis $s3 of length $len

Shell Script 2:To compare two strings


echo “Enter first word”
read test1
echo “Enter second word”
read test2

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;

void completiontime(int ct[],int bt[],int n)


{
ct[0]=bt[0];
for(int i=1;i<n;i++)
{
ct[i]=ct[i-1]+bt[i];
}
}

void tatime(int tat[],int ct[],int at[],int n)


{
int avg=0;
for(int i=0;i<n;i++)
{
tat[i]=ct[i]-at[i];
avg+=tat[i];
}

cout<<"\naverage turn around time: "<<avg/n;


}

void waitingtime(int wt[],int tat[],int bt[],int n)


{
int avg=0;
for(int i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
avg+=wt[i];
}
cout<<"\nWaiting average time: "<<avg/n;
}

void fcfs(int process[],int at[],int bt[],int n)


{
int ct[n],tat[n],wt[n];
completiontime(ct,bt,n);
tatime(tat,ct,at,n);
waitingtime(wt,tat,bt,n);
cout<<"\nprocess \t arrival\tburst\tct\ttat\twt";

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;
};

bool comparison(Process a, Process b)


{
return (a.bt < b.bt);
}

void findWaitingTime(Process proc[], int n, int wt[])


{
wt[0] = 0;
for (int i = 1; i < n ; i++)
{
wt[i] = proc[i-1].bt + wt[i-1] ;
}
}

void findTurnAroundTime(Process proc[], int n, int wt[], int tat[])


{ for (int i = 0; i < n ; i++)
{
tat[i] = proc[i].bt + wt[i];
}
}

void findAverageTime(Process proc[], int n)


{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

findWaitingTime(proc, n, wt);
findTurnAroundTime(proc, n, wt, tat);

cout << "\nProcesses "<< " 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 << " " << proc[i].pid << "\t\t"
<< proc[i].bt << "\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;

int main()
{
Process proc[] = {{1, 21}, {2, 3}, {3, 6}, {4, 2}};
int n = sizeof proc / sizeof proc[0];

sort(proc, proc + n, comparison);

cout << "Order in which process gets executed\n";


for (int i = 0 ; i < n; i++)
{
cout << proc[i].pid <<" ";
}

findAverageTime(proc, n);

return 0;
}
Lab 4

Aim: Write a program for round robin CPU scheduling.

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:

1- Create an array rem_bt[] to keep track of remaining


burst time of processes. This array is initially a
copy of bt[] (burst times array)
2- Create another array wt[] to store waiting times
of processes. Initialize this array as 0.
3- Initialize time : t = 0
4- Keep traversing the all processes while all processes
are not done. Do following for i'th process if it is
not done yet.
a- If rem_bt[i] > quantum
(i) t = t + quantum
(ii) bt_rem[i] -= quantum;
c- Else // Last cycle for this process
(i) t = t + bt_rem[i];
(ii) wt[i] = t - bt[i]
(ii) bt_rem[i] = 0; // This process is over

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;
}

cout << "Average waiting time = "


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

int processes[] = { 1, 2, 3};


int n = sizeof processes / sizeof processes[0];
int burst_time[] = {10, 5, 8};
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}
Lab 5
Aim: Write a program for shortest time remaining next job first CPU scheduling.
Theory:
It is pre-emptive mode of LJF algorithm in which we give priority to the process
having largest burst time remaining.
Algorithm:
1. First, sort the processes in increasing order of their Arrival Time.
2. Choose the process having least arrival time but with most Burst Time. Then
process it for 1 unit. Check if any other process arrives upto that time of
execution or not.
3. Repeat the above both steps until execute all the processes.
Program:
#include<iostream>
#include<iomanip>
using namespace std;
class srtf_alg
{
int ar[10],id[10],exe[10];
int n;
void sort(int *f,int *mid,int *last);
public:
void getdata();
void display();
void cal_wt_tt();
};
void srtf_alg::getdata()
{
cout<<"How many process to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution time and arrival time of "<<i+1<<" process : ";
cin>>exe[i]>>ar[i]; id[i]=i+1;
}
}
void srtf_alg::display()
{
cout<<endl<<"Process ID\tExecution time\tArrival Time "<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<id[i]<<setw(15)<<exe[i]<<setw(15)<<ar[i]<<endl;
}
void srtf_alg::sort(int *f,int *mid,int *last)
{
int temp;
for(int y=0;y<n-1;y++)
{
for(int z=0;z<n-1;z++)
if(f[z]>f[z+1])
{
temp=f[z];
f[z]=f[z+1];
f[z+1]=temp;
temp=mid[z];
mid[z]=mid[z+1];
mid[z+1]=temp;
temp=last[z];
last[z]=last[z+1];
last[z+1]=temp;
}
}
}
void srtf_alg::cal_wt_tt()
{
int exe2[10],flag=1;
int at=0,ind,wt,tnt,min,max=exe[0];
float avg=0,avtnt=0;
sort(ar,id,exe);
for(int i=0;i<n;i++)
{
exe2[i]=exe[i];
if(max<exe[i])
max=exe[i];
}
at=ar[0];
min=max+1;
cout<<"\nProcess ID \tWaiting time \tTurn Around time "<<endl;
while(flag)
{
for(int i=0;i<n;i++)
{
if(at>=ar[i]&&min>exe[i]&&id[i]>0)
{
ind=i;
min=exe[i];
}}
at++;
exe[ind]--;
min=max+1;
if(exe[ind]==0)
{
wt=at-exe2[ind]-ar[ind];
tnt=at-ar[ind];
cout<<setw(5)<<id[ind]<<setw(15)<<wt<<setw(15)<<tnt<<endl;
id[ind]=-1;
avg+=wt;
avtnt+=tnt;
}
flag=0;
for(int k=0;k<n;k++)
if(id[k]!=-1)
flag=1;
}
avg=avg/(float)n;
avtnt/=(float)n;
cout<<"\nAverage Waiting time : "<<avg;
cout<<"\nAverage turn Around time : "<<avtnt<<endl;
}
int main()
{
srtf_alg srtf;
srtf.getdata();
srtf.display();
srtf.cal_wt_tt();
return 0;
}

You might also like