0% found this document useful (0 votes)
23 views10 pages

OS Lab Programs

The document contains multiple C programs demonstrating different CPU scheduling algorithms including FCFS, SJF, and Round Robin, as well as implementations of the Producer-Consumer problem, Readers-Writers problem, and Dining Philosophers problem using semaphores. Each section includes code snippets, user inputs, and sample outputs for better understanding. The programs illustrate fundamental concepts in operating systems related to process scheduling and synchronization.

Uploaded by

mohdkashif6305
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views10 pages

OS Lab Programs

The document contains multiple C programs demonstrating different CPU scheduling algorithms including FCFS, SJF, and Round Robin, as well as implementations of the Producer-Consumer problem, Readers-Writers problem, and Dining Philosophers problem using semaphores. Each section includes code snippets, user inputs, and sample outputs for better understanding. The programs illustrate fundamental concepts in operating systems related to process scheduling and synchronization.

Uploaded by

mohdkashif6305
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

//4.

a FCFS CPU Scheduling


#include<stdio.h>
int main(){
int n,i;
int wt[20],bt[20],tat[20],Twt=0,Ttat=0;
float Avgwt=0,Avgtat=0;
system("clear");
printf("Enter the number of processes\n");
scanf("%d",&n);
printf("Enter the burst time for each process\n");
for(i=0;i<n;i++){
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=1;i<n;i++){
wt[i]=wt[i-1]+bt[i-1];
Twt+=wt[i];
}
for(i=0;i<n;i++){
tat[i]=wt[i]+bt[i];
Ttat+=tat[i];
}
Avgwt=(float)Twt/n;
Avgtat=(float)Ttat/n;
printf("P no\tB.T\tW.T\tT.A.T\n");
for(i=0;i<n;i++){
printf("%d\t%d\t%d\t%d\n",i+1,bt[i],wt[i],tat[i]);
}
printf(" \t \t%d\t%d\n",Twt,Ttat);
printf(" \t \t%.2f\t%.2f\t\n",Avgwt,Avgtat);
}

OUTPUT:-
[cse22151@cseserver os]$./a.out
Enter the number of processes
4
Enter the burst time for each process
1
3
4
5
P no B.T W.T T.A.T
1 1 0 1
2 3 1 4
3 4 4 8
4 5 8 13
13 26
3.25 6.50
//4.b sjf
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i=0,temp=0,j=0;
system("clear");
int wt[20],bt[20],tat[20],pno[20],Twt=0,Ttat=0;
float Avgwt=0,Avgtat=0;
system("clear");
printf("Enter the number of processes\n");
scanf("%d",&n);
printf("Enter the burst time for each process\n");
for(i=0;i<n;i++){
scanf("%d",&bt[i]);
pno[i]=i;
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(bt[j]<bt[i]){
temp=bt[j];
bt[j]=bt[i];
bt[i]=temp;
temp=0;
temp=pno[j];
pno[j]=pno[i];
pno[i]=temp;
}
}
}
wt[0]=0;
for(i=1;i<n;i++){
wt[i]=wt[i-1]+bt[i-1];
Twt+=wt[i];
}
for(i=0;i<n;i++){
tat[i]=wt[i]+bt[i];
Ttat+=tat[i];
}
Avgwt=(float)Twt/n;
Avgtat=(float)Ttat/n;
printf("P no\tB.T\tW.T\tT.A.T\n");
for(i=0;i<n;i++){
printf("%d\t%d\t%d\t%d\n",pno[i],bt[i],wt[i],tat[i]);
}
printf(" \t \t%d\t%d\n",Twt,Ttat);
printf(" \t \t%.2f\t%.2f\t\n",Avgwt,Avgtat);
}
OUTPUT:-
[cse22151@cseserver os]$./a.out
Enter the number of processes
3
Enter the burst time for each process
3
2
6
P no B.T W.T T.A.T

1 2 0 2

0 3 2 5

2 6 5 11

__ __ 7 18

__ __ 2.33 6.00
//4.c Roundrobin
#include<stdio.h>
#include<sys/stat.h>
int main(){
int n,i,count=0,t=0,ts, wt[20],bt[20],tat[20],Twt=0,Ttat=0,RT[15];
float Avgwt=0,Avgtat=0;
system("clear");
printf("Enter the number of processes\n");
scanf("%d",&n);
printf("Enter the time slice\n");
scanf("%d",&ts);
printf("Enter the burst time for each process\n");
for(i=1;i<=n;i++){
scanf("%d",&bt[i]);
RT[i]=bt[i];
}
while(1){
for(i=1;i<=n;i++){
if(RT[i]>0){
if(RT[i]>ts){
RT[i]-=ts;
t=t+ts;
}
else{
t=t+RT[i];
RT[i]=0;
tat[i]=t;
count++;
}
}
}
if(count==n){
break;
}
}
for(i=1;i<=n;i++){
wt[i]=tat[i]-bt[i];
Twt+=wt[i];
}
for(i=1;i<=n;i++){
Ttat+=tat[i];
}
Avgwt=(float)T
wt/n;
Avgtat=(float)Tt
at/n;
printf("P no\tB.T\tW.T\tT.A.T\
n"); for(i=1;i<=n;i++){
printf("%d\t%d\t%d\t%d\n",i,bt[i],wt[i],tat[i]);
}
printf(" \t \t%d\t%d\n",Twt,Ttat);
printf(" \t \t%.2f\t%.2f\t\n",Avgwt,Avgtat);
}

OUTPUT:-
[cse22151@cseserver
os]$./a.out Enter the number
of processes
4
Enter the time
slice 2
Enter the burst time for each
process
3
4
5
6
P no B.T W.T T.A.T
1 3 6 9
2 4 7 11
3 5 11 16
4 6 12 18
36 54
9.00 13.50
(a) To implement Producer &Consumer problem using semaphore

#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
}

int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}

void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}

void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
(a) To implement Readers and Writer problem using semaphore

#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t mutex,writeblock;
int data = 0,rcount = 0;
void *reader(void *arg)
{
int f;
f = ((int)arg);
sem_wait(&mutex);
rcount = rcount + 1;
if(rcount==1)
sem_wait(&writeblock);
sem_post(&mutex);
printf("Data read by the reader%d is %d\n",f,data);
sleep(1);
sem_wait(&mutex);
rcount = rcount - 1;
if(rcount==0)
sem_post(&writeblock);
sem_post(&mutex);
}

void *writer(void *arg)


{
int f;
f = ((int) arg);
sem_wait(&writeblock);
data++;
printf("Data writen by the writer%d is %d\n",f,data);
sleep(1);
sem_post(&writeblock);
}

main()
{
int i,b;
pthread_t rtid[5],wtid[5];
sem_init(&mutex,0,1);
sem_init(&writeblock,0,1);
for(i=0;i<=2;i++)
{
pthread_create(&wtid[i],NULL,writer,(void *)i);
pthread_create(&rtid[i],NULL,reader,(void *)i);
}
for(i=0;i<=2;i++)
{
pthread_join(wtid[i],NULL);
pthread_join(rtid[i],NULL);
}
}

(a) To implement Dining Philosopher’s Problem using semaphore

#include<stdio.h>
#include<pthread.h>
pthread_mutex_t chopstick[5];
pthread_t philosopher[5];
void* runner(void* arg)
{
int i=*(int*)arg;
printf("Philosopher %d is thinking \n",i);
sleep(2);
pthread_mutex_lock(&chopstick[i]);
pthread_mutex_lock(&chopstick[(i+1)%5]);
printf("philosopher %d is eating \n",i);
sleep(3);
pthread_mutex_unlock(&chopstick[i]);
pthread_mutex_unlock(&chopstick[(i+1)%5]);
printf("Philosopher %d finished eating \n",i);
}
int main()
{
int i;
for(i=0;i<5;i++)
{
pthread_create(&philosopher[i],NULL,runner,&i);
sleep(1);
}
for(i=0;i<5;i++)
pthread_join(philosopher[i],NULL);
}

You might also like