Sarthak Lab File
Sarthak Lab File
(Code – 21CSC202J)
B.Tech[CSE(DS)]
2nd Year - 3rd Semester
Program
Basic Linux Commands
1. Basics
i. echo SRM: to display the string SRM
ii. clear: to clear the screen
iii. date: to display the current date and time
iv. cal 2022: to display the calendar for the year 2022
v. cal 6 2022: to display the calendar for the June-2022
vi. passwd: to change password
5. I/O Redirection
Input redirection
wc –l < ex1: To find the number of lines of the file ‘ex1’
Output redirection
6. Piping
7. Environment variables
echo $HOME display the path of the home directory
echo $PS2 display the second prompt string ( > symbol by default )
Method-1
Syntax : chmod [ugo] [+/-] [ rwxa ] filename
Method-2
Syntax : chmodoctnum file1
Lab 2: Process Creation using fork() and Usage of getpid(), getppid(), wait() functions
Syntax for process creation
int fork();
Virtual fork
vfork() function is similar to fork but both processes shares the same address space.
#include <stdio.h>
#include<unistd.h>
int main()
{
int a=5,b=10,pid;
printf("Before fork a=%d b=%d \n",a,b); pid=fork();
if(pid==0)
{
a=a+1; b=b+1;
printf("In child a=%d b=%d \n",a,b);
}
else
{
sleep(1); a=a-1; b=b-1;
printf("In Parent a=%d b=%d \n",a,b);
}
return 0;
}
Output:
Program:
#include
<stdio.h> #include<unistd.h>
int main()
{
fork(); fork();
fork();
printf(“SRMIST\n”);
return 0;
}
Output:
How many child processes are created for the following code?
#include <stdio.h>
#include<unistd.h>
intmain()
{ fork();
fork()&&fork()||fork();
fork();
printf(“Yes ”); return 0;
}
Output:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main( )
{
int pid;
pid=fork( );
if(pid== -1)
{
perror("fork failed");
exit(0);
}
if(pid==0)
{
printf("\nChild process is under execution");
printf("\nProcess id of the child process is %d", getpid());
printf("\nProcess id of the parent process is %d", getppid());
}
else
{
printf("\nParent process is under execution");
printf("\nProcess id of the parent process is %d", getpid());
printf("\nProcess id of the parent of parent is %d", getppid());
}
return(0);
}
Output:
Program:
: Multithreading
C++ program to demonstrate
multithreading using three
different callables.
#include <iostream>
#include <thread>
using namespace std;
A dummy function
void foo(int Z)
{
for (int i = 0; i < Z; i++)
{
cout << "Thread using function"
" pointer as callable\n";
}
}
A callable object
class thread_obj {
public:
void operator()(int x)
{
for (int i = 0; i < x; i++)
cout << "Thread using function"
" object as callable\n";
}
};
Driver code
int main()
{
cout << "Threads 1 and 2 and 3 "
"operating independently" << endl;
USING SEMAPHORE
const int Delayx = 60;
int i;
int restroom = 0;
int Menwaiting = 0;
int Womenwaiting = 0;
semaphore max_capacity;
semaphore woman;
semaphore man;
semaphore mutex;
semaphore restroomcount;
void Delay(void)
{
int DelayTime;
DelayTime = random(Delayx);
for (i = 0; i<DelayTime; i++);
}
void Woman(void)
{
for(;;){
Womenwaiting++;
wait(mutex);
wait(woman);
wait(max_capacity);
wait(woman);
wait(mutex);
wait(restroomcount);
cout << "A Woman has entered Restroom"<<endl;
cout << "People in the Restroom:" << restroom++ <<endl <<endl;
signal(restroomcount);
Womenwaiting--;
Delay();
wait(restroomcount);
cout << "A woman has exited Restroom"<<endl;
cout << "People in the Restroom:" << restroom-- <<endl<<endl;
signal(restroomcount);
signal(mutex);
signal(max_capacity);
if(Menwaiting > Womenwaiting){
signal(man);
}
else{
signal(woman);
}
signal(max_capacity);
signal(man);
}
}
void Man(void)
{
for(;;){
Menwaiting++;
wait(mutex);
wait(man);
wait(max_capacity);
wait(man);
wait(mutex); wait(restroomcount); cout <<"A Man has entered the
Restroom"<<endl; cout <<"People in the Restroom:" << restroom++
<<endl<<endl; signal(restroomcount); Menwaiting--; signal(mutex);
Delay(); wait(mutex); wait(restroomcount); cout << "A man has
exited the Restroom"<<endl; cout <<"People in the Restroom:" <<
restroom-- <<endl<<endl; signal(restroomcount); signal(mutex);
signal(max_capacity); if(Womenwaiting > Menwaiting){
signal(woman);
}
else{
signal(man);
}
signal(max_capacity);
signal(woman);
} } void
main() {
initialsem(woman,1);
initialsem(man,1);
initialsem(max_capacity,4);
initialsem(mutex,1);
initialsem(restroomcount,1);
cobegin { }
Woman(); Woman(); Woman(); Woman(); Woman(); Man(); Man(); Man(); Man(); Man();
}
This generates the following output:
A Man has entered the Restroom
People in the Restroom:1
A man has exited the Restroom
People in the Restroom:0
A Man has entered the Restroom
People in the Restroom:1
A man has exited the Restroom
People in the Restroom:0
A Woman has entered Restroom
People in the Restroom:1
A woman has exited Restroom
People in the Restroom:0
A Woman has entered Restroom
People in the Restroom:1
A woman has exited Restroom
People in the Restroom:0
USING MONITOR
include <iostream>
#include <mutex>
#include <condition_variable>
#include <thread>
class NinjaMonitor {
public:
std::unique_lock<std::mutex> lock(mtx_);
SharedResource = new_value;
CondVar.notify_one();
}
private:
int SharedResource;
std::mutex mtx_;
std::condition_variable CondVar;
};
int main() {
NinjaMonitor monitor;
std::thread t(NinjaThread, std::ref(monitor));
std::this_thread::sleep_for(std::chrono::seconds(1));
monitor.UpdateSharedResource(67);
t.join();
return 0;
}
USING MONITOR
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <thread>
class NinjaMonitor {
public:
Constructor to initialize the shared resource and synchronization variables
NinjaMonitor() : SharedResource(0) {}
std::unique_lock<std::mutex> lock(mtx_);
SharedResource = new_value;
CondVar.notify_one();
}
private:
int SharedResource;
std::mutex mtx_;
std::condition_variable CondVar;
};
int main() {
NinjaMonitor monitor;
std::thread t(NinjaThread, std::ref(monitor));
std::this_thread::sleep_for(std::chrono::seconds(1));
monitor.UpdateSharedResource(67);
t.join();
return 0;
}
Using Semaphores :-
Here, readers have higher priority than writer. If a writer wants to write to the resource, it must wait until
there are no readers currently accessing that resource.
Here, we use :-
one mutex m and a semaphore w.
An integer variable read_count :- used to maintain the number of readers currently accessing the
resource. The variable read_count is initialized to 0.
waiting.
It exits the critical section.
3.
while(TRUE)
{
wait(w);
/* perform the write operation */
signal(w);
}
b. Reader Process :
1. Reader requests the entry to critical section.
2. If allowed:
i. it increments the count of number of readers inside the critical section. If this reader is the first reader
entering, it locks the w semaphore to restrict the entry of writers if any reader is inside.
ii.It then, signals mutex as any other reader is allowed to enter while others are already reading.
iii. After performing reading, it exits the critical section. When exiting, it checks if no more reader is
inside, it signals the semaphore w as now, writer can enter the critical section.
acquire lock
wait(m);
read_count--;
if(read_count == 0)
signal(w);
release lock
signal(m);
}
Thus, the semaphore w is queued on both readers and writers in a manner such that preference is given to
readers if writers are also there. Thus, no reader is waiting simply because a writer has requested to enter
the critical section.
operation takeForks(integer i)
if(fork[i]!=2)
waitC(OKtoEat[i])
fork[i+1]<- fork[i+1]-1
fork[i-1] <- fork[i-1]-1
operation releaseForks(integer i)
fork[i+1] <- fork[i+1]+1
fork[i-1] <- fork[i-1]
if(fork[i+1]==2)
signalC(OKtoEat[i+1])
if(fork[i-1]==2)
signalC(OKtoEat[i-1])
For each Philosopher –
loop forever :
p1 : think
p2 : takeForks(i)
p3 : eat
p4 : releaseForks(i)
Here Monitor will ensure all such needs mentioned above.
Implementation in C++ :
Philosopher index
int phil[N];
int times = 200;
class monitor {
public:
Test for the desired condition
i.e. Left and Right philosopher are not reading
void test(int phnum)
{
if (state[(phnum + 1) % 5] != EATING
and state[(phnum + 4) % 5] != EATING
and state[phnum] == HUNGRY) {
state[phnum] = EATING;
pthread_cond_signal(&phcond[phnum]);
}
}
pthread_mutex_lock(&condLock);
Indicates it is hungry
state[phnum] = HUNGRY;
pthread_mutex_unlock(&condLock);
}
Put Fork function
void put_fork(int phnum)
{
pthread_mutex_lock(&condLock);
constructor
monitor()
{
pthread_mutex_init(&condLock, NULL);
}
destructor
~monitor()
{
pthread_mutex_destroy(&condLock);
}
}
Initialization...
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,
PTHREAD_CREATE_JOINABLE);
for (int i = 0; i < N; i++) {
phil[i] = i;
}
Creating...
for (int i = 0; i < N; i++) {
pthread_create(&thread_id[i], &attr, philosopher,
&phil[i]);
cout << "Philosopher " << i + 1 << " is thinking..."
<< endl;
}
Joining....
for (int i = 0; i < N; i++) {
pthread_join(thread_id[i], NULL);
}
Destroying
pthread_attr_destroy(&attr);
pthread_exit(NULL);
return 0;
}
Output :
Output: Program successfully executed.
Experiment no-7
#include<bits/stdc++.h>
using namespace std;
int allocation[10][3],need[10][3],Max[10][3],available[10][3];
int p,current[3];
bool executed[10],come;
void IMP(){
come=false;
for (int i = 0; i < 10; ++i)
{}
executed[i]=false;
}
void Calculate(){
IMP();
int i,j;
for (i = 0; i < p; ++i)
{
for (j = 0; j < p; ++j)
{
while(executed[j] && j<p-1){
j++;
}
if (need[j][0]<=current[0]&&need[j][1]<=current[1]&&need[j][2]<=current[2])
{
if (!executed[j])
{
executed[j]=true;
current[0]+=allocation[j][0];current[1]+=allocation[j][1];current[2]+=allocation[j][2];
cout<<"\nProcess P"<<j+1;
cout<<"\nCurrent: "<<current[0]<<" "<<current[1]<<"
"<<current[2]<<"\n";
cout<<"\nProcess executed without deadlock";
come=true;
break;
}
}
}
if (!come)
{
cout<<"\n Dead lock\n\n";
break;
}else{
} come=false;
}
}
int main (){
int keepon = 1;
cout<<"Enter No. of processes: ";
cin>>p;
cout<<"\n";
cout<<"Enter the current resources: ";
cin>>current[0]>>current[1]>>current[2];
for (int i = 0; i < p; ++i)
{
cout<<"\n\n Process P"<<i+1<<" Details\n";
cout<<"Enter Allocation : ";
cin>>allocation[i][0]>>allocation[i][1]>>allocation[i][2];
cout<<"Enter Max :";
cin>>Max[i][0]>>Max[i][1]>>Max[i][2];
need[i][0]=Max[i][0]-allocation[i][0];need[i][1]=Max[i][1]-allocation[i][1];need[i][2]=Max[i][2]-
allocation[i][2];
}
cout<<"\n\n Table for Bankers Algo\n\n";
cout<<"Initial Resources: "<<current[0]<<" "<<current[1]<<" "<<current[2]<<"\n\n";
cout<<"Process Max Allocation Need\n";
for (int i = 0; i < p; ++i)
{
cout<<" P"<<i+1<<" ";
cout<<" "<<Max[i][0]<<" "<<Max[i][1]<<" "<<Max[i][2]<<" ";
cout<<" "<<allocation[i][0]<<" "<<allocation[i][1]<<" "<<allocation[i][2]<<" ";
cout<<" "<<need[i][0]<<" "<<need[i][1]<<" "<<need[i][2];
cout<<"\n";
}
cout<<"\n\n";
Calculate();
while(keepon){
int val,pro;
cout<<"\n\nSelect Below oprations:\n\n";
cout<<"1.Change Max of process: \n";
cout<<"2.Change Allocation of process\n";
cout<<"3.Change Initial Resources\n";
cout<<"4.Exit\n\n";
cin>>val;
if (val==1)
{
cout<<"\n\nEnter Process No: ";
cin>>pro;
cout<<"\nEnter New Max: ";
cin>>Max[pro-1][0]>>Max[pro-1][1]>>Max[pro-1][2];
}
else if (val==2)
{
cout<<"\n\nEnter Process No: ";
cin>>pro;
cout<<"\nEnter New Allocation: ";
cin>>allocation[pro-1][0]>>allocation[pro-1][1]>>allocation[pro-1][2];
}
else if (val==3)
{
cout<<"\nEnter Initial Resources: ";
cin>>current[0]>>current[1]>>current[2];
}
else{
} break;
Calculate();
}
return 0
ALGO FCFS
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 =
total_waiting_time / no_of_processes.
7- Similarly, find average turnaround time =
total_turn_around_time / no_of_processes.
PROGRAM
#include<iostream>
using namespace std;
wt[0] = 0;
bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
Driver code
int main()
{
process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
findavgTime(processes, n, burst_time);
return 0;
}
OUT PUT
Processes Burst time Waiting time Turn around time //The Output
is Wrong please correct it
1 10 0 10
2 5 10 15
3 8 15 23
Average waiting time = 8.33333
Average turn around time = 16
ALGO SJF
the steps to perform the SJF scheduling program in c.
Firstly, we will begin the procedure.
After that, we will Consider the number of elements to be inserted.
Then, we will choose the process with the shortest burst time and will execute the first process.
We will check that if both processes have the same burst time length then the FCFS scheduling algorithm
is used.
We will make the average waiting time for the next process.
We will begin with the first process, make the above selection, and place the other processes in a queue.
We will calculate burst time.
We will display the values that are related.
In the end, we will stop the process.
Likewise, we will run all the steps until all the processes are executed.
PROGRAM
#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
waiting time for first process will be zero
total+=wt[i];
}
PRIORITY SCHEDULING
#include<iostream>
using namespace std;
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
cout<<"Enter Total Number of Process:";
cin>>n;
cout<<"\nEnter Burst Time and Priority\n";
for(i=0;i<n;i++)
{
cout<<"\nP["<<i+1<<"]\n";
cout<<"Burst Time:";
cin>>bt[i];
cout<<"Priority:";
cin>>pr[i];
p[i]=i+1; contains process number
}
sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process is zero
calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n; //average waiting time
total=0;
cout<<"\nProcess\t Burst Time \tWaiting Time\tTurnaround Time";
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
cout<<"\nP["<<p[i]<<"]\t\t "<<bt[i]<<"\t\t "<<wt[i]<<"\t\t\t"<<tat[i];
}
avg_tat=total/n; //average turnaround time
cout<<"\n\nAverage Waiting Time="<<avg_wt;
cout<<"\nAverage Turnaround Time="<<avg_tat;
return 0;
PROGRAM
C program for FIFO page replacement algorithm
#include<stdio.h>
int main()
{
int incomingStream[] = {4, 1, 2, 4, 5}; int pageFaults = 0; int
frames = 3; int m, n, s, pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
s = 0;
}
void printFrame(int queue[], int occupied)
{
for(int i = 0; i < occupied; i++)
printf("%d\t\t\t",queue[i]);
}
int main()
{
int n = sizeof(incomingStream)/sizeof(incomingStream[0]);
int frames = 3;
int queue[n];
int distance[n];
int occupied = 0;
int pagefault = 0;
printf("Page\t Frame1 \t Frame2 \t Frame3\n");
printFrame(queue, occupied);
}
else{
if(queue[j] == incomingStream[k])
break;
}
return 0;
Result: Program successfully executed.
Experiment-11
Program:
#include<stdio.h>
#include<limits.h>
int checkHit(int incomingPage, int queue[], int occupied){
int main()
{
int n = sizeof(incomingStream)/sizeof(incomingStream[0]);
int frames = 3;
int queue[n];
int distance[n];
int occupied = 0;
int pagefault = 0;
printf("Page\t Frame1 \t Frame2 \t Frame3\n");
printFrame(queue, occupied);
}
else{
return 0;
}
OUTPUT
Page 1: 2: Frame1 Frame2 Frame3
3: 2: 1: 5: 2: 1 1
1: 6: 2: 5: 6: 1 1 2
3: 1: 3: 11 2 3
11 2 3
11 2 3
5 2 5
5 2 5
5 2 5
11 2 6
2 6
2 6
2 6
3 6
3 6
3 6
Page Fault: 8
Experiment no-12
AIM : Write a program of Best fit and Worst fit memory management policies
BEST FIT
#include <stdio.h>
void implimentBestFit(int blockSize[], int blocks, int processSize[], int proccesses)
{
Driver code
int main()
{
int blockSize[] = {100, 50, 30, 120, 35};
int processSize[] = {40, 10, 30, 60};
int blocks = sizeof(blockSize)/sizeof(blockSize[0]);
int proccesses = sizeof(processSize)/sizeof(processSize[0]);
return 0 ;
OUTPUT
if (indexPlaced == -1)
indexPlaced = j;
Driver code
int main()
{
int blockSize[] = {100, 50, 30, 120, 35};
int processSize[] = {40, 10, 30, 60};
int blocks = sizeof(blockSize)/sizeof(blockSize[0]);
int processes = sizeof(processSize)/sizeof(processSize[0]);
return 0;
}
Output
Process No. Process Size Block no.
1 40 4 1 2 Not
2 10 Allocated
3 30
4 60
FCFS is the simplest of all the Disk Scheduling Algorithms. In FCFS, the requests are addressed in the order they
arrive in the disk queue. Example: Given the following queue -- 95, 180, 34, 119, 11, 123, 62, 64 with the Read-
write head initially at the track 50 and the tail track being at 199.
#include<stdio.h>
#include<stdlib.h>
int main()
int RQ[100],i,n,TotalHeadMoment=0,initial;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
scanf("%d",&initial);
for(i=0;i<n;i++)
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
return 0;
}
OUTPUT
It is also called as Elevator Algorithm. In this algorithm, the disk arm moves into a particular direction till
the end, satisfying all the requests coming in its path, and then it turns backend moves in the reverse
direction satisfying requests coming in its path.
It works in the way an elevator works, elevator moves in a direction completely till the last floor of that
direction and then turns back.
Example:- Given the following queue -- 95, 180, 34, 119, 11, 123, 62, 64 with the Read-write head initially
at the track 50 and the tail track being at 199. head movement is towards low value.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
}
}
if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
initial =0;
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}
Output:-
Enter the number of Request
8
Enter the Requests Sequence
95 180 34 119 11 123 62 64
Enter initial head position
50
Enter total disk size
200
Enter the head movement direction for high 1 and for low 0
1
Total head movement is 337
Program Output:
Enter the index block: 5
Enter no of blocks needed and no of files for the index 5 on the disk :
4
1234
Allocated
File Indexed
5-------->1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0)1
Enter the index block: 4
4 index is already allocated
Enter the index block: 6
Enter no of blocks needed and no of files for the index 6 on the disk :
2
78
A5llocated
File Indexed
6-------->7 : 1
6-------->8 : 1
Do you want to enter more file(Yes - 1/No - 0)0