You are on page 1of 10

21CSC202J- Operating Systems Lab

Lab
Exp. No. 7 Reader-Writer Problem

AIM: To Write a C program to simulate reader-writer problem using


semaphores.

DESCRIPTION: The reader-writer problem is a classic synchronization problem in


operating systems where multiple processes require access to a shared resource. In this
problem, some processes may only read the resource while others may write to it. The
goal is to ensure that multiple reader processes can access the resource
simultaneously, but only one writer process can access the resource at a time to avoid
data inconsistency. One possible solution to the reader-writer problem is to use a
mutex lock and a semaphore. The mutex lock ensures mutual exclusion while
updating a variable that keeps track of the number of processes performing the read
operation.

Code

#include<semaphore.h>
#include<stdio.h>
#include<stdlib.h>
sem_t x,y;
pthread_t tid;
pthread_t writerthreads[100],readerthreads[100];
int readercount;

void *reader(void* param)


{
sem_wait(&x);
readercount++;
if(readercount==1)
sem_wait(&y);
sem_post(&x);
printf("\n%d reader is inside",readercount);
sem_wait(&x);
readercount--;
if(readercount==0)
{
sem_post(&y);
}
sem_post(&x);
printf("\n%d Reader is leaving",readercount+1);
}

void *writer(void* param)


{
printf("\nWriter is trying to enter");
sem_wait(&y);

O SRMIST
21CSC202J- Operating Systems Lab
printf("\nWriter has entered"); Lab
sem_post(&y);
printf("\nWriter is leaving");
}

int main()
{
int n2,i;
printf("Enter the number of readers:");
scanf("%d",&n2);
int n1[n2];
sem_init(&x,0,1);
sem_init(&y,0,1);
for(i=0;i<n2;i++)
{
pthread_create(&writerthreads[i],NULL,reader,NULL);
pthread_create(&readerthreads[i],NULL,writer,NULL);
}
for(i=0;i<n2;i++)
{
pthread_join(writerthreads[i],NULL);
pthread_join(readerthreads[i],NULL);
}

O SRMIST
21CSC202J- Operating Systems Lab
Lab
Exp. No. 8 FCFS and SJF Scheduling

FCFS Scheduling

DESCRIPTION: FCFS Scheduling algorithm automatically executes the queued processes


and requests in the order of their arrival. It allocates the job that first arrived in the queue to
the CPU, then allocates the second one, and so on. FCFS is the simplest and easiest CPU
scheduling algorithm, managed with a FIFO queue. FIFO stands for First In First Out. The
FCFS scheduling algorithm places the arriving processes/jobs at the very end of the queue.

Code

#include<iostream>
#define MAX_PROCESS 10
using namespace std;
class process
{
public:
int process_num;
int burst_time;
int arrival_time;
int response_time;
int waiting_time;
int turnaround_time;
void input_process(int);
int get_at()
{
return arrival_time;
}
};
void process::input_process(int count)
{
process_num=count+1;
cout<<"\nENTER BURST TIME FOR PROCESS "<<count+1<<" : ";
cin>>burst_time;
cout<<"ENTER ARRIVAL TIME FOR PROCESS "<<count+1<<" : ";
cin>>arrival_time;
}
void calc_wait_tat(process*,int);
void average(process*,int);
void display(process*,int);
int main()
{
process p[MAX_PROCESS],temp;
int num,i,j;
cout<<"ENTER NUMBER OF PROCESSES : ";
cin>>num;
for(i=0;i<num;++i)
p[i].input_process(i);
for(i=0;i<num;++i)
{

O SRMIST
21CSC202J- Operating Systems Lab
for(j=i+1;j<num;++j) Lab
{
if(p[i].get_at()>p[j].get_at())
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
calc_wait_tat(p,num);
display(p,num);
return 0;
}
void calc_wait_tat(process *p,int n)
{
int i;
p[0].response_time=0;
for(i=1;i<n;++i)
{
p[i].response_time=p[i-1].burst_time+p[i-1].response_time;
if(p[i].response_time<p[i].arrival_time)
p[i].response_time=p[i].arrival_time;
}
p[0].waiting_time=0;
for(i=1;i<n;++i)
p[i].waiting_time=p[i].response_time-p[i].arrival_time;
for(i=0;i<n;++i)
p[i].turnaround_time=p[i].waiting_time+p[i].burst_time;
}
void average(process *p,int n)
{
float avg_wt=0,avg_tat=0;
for(int i=0;i<n;++i)
{
avg_wt+=(float)p[i].waiting_time;
avg_tat+=(float)p[i].turnaround_time;
}
avg_wt/=n;
avg_tat/=n;
cout<<"\n\nAVERAGE WAITING TIME : "<<avg_wt;
cout<<"\nAVERAGE TURN AROUND TIME : "<<avg_tat;
}
void display(process *p,int n)
{
cout<<"Processes "<<" Burst time "<<" Arrival time "<<" Waiting time "<<" Turn around
time\n";
for (int i=0;i<n;i++)
{
cout<<"\n "<<p[i].process_num<<"\t\t"<<p[i].burst_time<<"\t\t"<<p[i].arrival_time<<"\t\t
"<<p[i].waiting_time<<"\t\t"<<p[i].turnaround_time;
}
average(p,n);
}

O SRMIST
21CSC202J- Operating Systems Lab
Lab

O SRMIST
21CSC202J- Operating Systems Lab
Lab
Exp. No.9(a) FCFS and SJF Scheduling

SJF Scheduling(Non-Primitive)

DESCRIPTION: The shortest job first (SJF) or shortest job next, is a scheduling
policy that selects the waiting process with the smallest execution time to execute
next. SJN, also known as Shortest Job Next (SJN), can be preemptive or non-
preemptive.

Code 1

#include <iostream>
using namespace std;

int main() {

// Matrix for storing Process Id, Burst


// Time, Average Waiting Time & Average
// Turn Around Time.
int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;

cout << "Enter number of process: ";


cin >> n;

cout << "Enter Burst Time:" << endl;

// User Input Burst Time and alloting Process Id.


for (i = 0; i < n; i++) {
cout << "P" << i + 1 << ": ";
cin >> A[i][1];
A[i][0] = i + 1;
}

// Sorting process according to their Burst Time.


for (i = 0; i < n; i++) {
index = i;
for (j = i + 1; j < n; j++)
if (A[j][1] < A[index][1])
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;

temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}

A[0][2] = 0;

O SRMIST
21CSC202J- Operating Systems Lab
// Calculation of Waiting Times Lab
for (i = 1; i < n; i++) {
A[i][2] = 0;
for (j = 0; j < i; j++)
A[i][2] += A[j][1];
total += A[i][2];
}

avg_wt = (float)total / n;
total = 0;
cout << "P BT WT TAT" << endl;

// Calculation of Turn Around Time and printing the


// data.
for (i = 0; i < n; i++) {
A[i][3] = A[i][1] + A[i][2];
total += A[i][3];
cout << "P" << A[i][0] << " " << A[i][1] << " " << A[i][2] << " " << A[i][3] << endl;
}

avg_tat = (float)total / n;
cout << "Average Waiting Time= " << avg_wt << endl;
cout << "Average Turnaround Time= " << avg_tat << endl;
}

O SRMIST
21CSC202J- Operating Systems Lab
Lab
Exp. No.9(b) FCFS and SJF Scheduling

SJF Scheduling(Primitive)

Code

#include<iostream>

using namespace std;


int main()
{
int n,temp,tt=0,min,d,i,j;
float atat=0,awt=0,stat=0,swt=0;

cout<<"enter no of process"<<endl;
cin>>n;
int a[n],b[n],e[n],tat[n],wt[n];

for(i=0;i<n;i++)
{
cout<<"enter arival time "; //input
cin>>a[i];
}
for(i=0;i<n;i++)
{
cout<<"enter brust time "; //input
cin>>b[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(b[i]>b[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;

temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
min=a[0];
for(i=0;i<n;i++)

O SRMIST
21CSC202J- Operating Systems Lab
{ Lab
if(min>a[i])
{
min=a[i];
d=i;
}
}
tt=min;
e[d]=tt+b[d];
tt=e[d];

for(i=0;i<n;i++)
{
if(a[i]!=min)
{
e[i]=b[i]+tt;
tt=e[i];
}
}
for(i=0;i<n;i++)
{

tat[i]=e[i]-a[i];
stat=stat+tat[i];
wt[i]=tat[i]-b[i];
swt=swt+wt[i];
}
atat=stat/n;
awt=swt/n;
cout<<"Process Arrival-time(s) Burst-time(s) Waiting-time(s) Turnaround-
time(s)\n";

for(i=0;i<n;i++)
{
cout<<"P"<<i+1<<" "<<a[i]<<" "<<b[i]<<"
"<<wt[i]<<" "<<tat[i]<<endl;
}

cout<<"awt="<<awt<<" atat="<<atat; //average waiting time and turn around time


}

O SRMIST
21CSC202J- Operating Systems Lab
Lab

O SRMIST

You might also like