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

20BCS3CP

The C program implements the dining philosophers problem using threads and semaphores. It defines N philosophers that share M chopsticks, where each philosopher needs to acquire the chopsticks on either side of them to eat. The program initializes semaphores for mutual exclusion and chopstick resources, creates threads for each philosopher, and implements the logic where a philosopher will think, become hungry, acquire both forks or wait if they are unavailable, eat, then put the forks down and think again. The output verifies the philosophers are correctly acquiring and releasing the shared resources to solve the dining philosophers problem in a synchronized manner.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views38 pages

20BCS3CP

The C program implements the dining philosophers problem using threads and semaphores. It defines N philosophers that share M chopsticks, where each philosopher needs to acquire the chopsticks on either side of them to eat. The program initializes semaphores for mutual exclusion and chopstick resources, creates threads for each philosopher, and implements the logic where a philosopher will think, become hungry, acquire both forks or wait if they are unavailable, eat, then put the forks down and think again. The output verifies the philosophers are correctly acquiring and releasing the shared resources to solve the dining philosophers problem in a synchronized manner.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Subject Subject Title Credit Lecture Tutorial Practical Type

Code

20BCS3CP Practical - Operating 5 5 Practical


System Lab

Course Outcome:

CO1 : Understand basic Structure of the Operating System


CO2 : Understand programs using implementation of system calls
CO3 : To exercise and see the file operation
CO4 : Understand the programs using problem concept
CO5 : Understand by using the methods In Operating System
1. C Program for Implementation Of System Calls.
2. C Program for File Permissions.
3. C Program for File Operations.
4. C Program for File Copy and Move.
5. C Program for Dining Philosophers Problem.
6. C Program for Producer – Consumer Problem concept.
7. C Program for First In First Serve Algorithm.
8. C Program for Shortest Job First Scheduling Algorithm.
9. C Program for Round Robin Scheduling Method.
10. C Program for Priority Scheduling Algorithm.
1. C Program for Implementation Of System Calls.

Aim:
To Write a C program to implement system calls.

Procedure:

1. Read the input variables and assign the value


2. Print the various system calls which we are going to perform
3. Using the case operator assign the various functions for the System call.
4. Check the values for all the corresponding operations.
5. Print the result and stop the execution

Program:

#include<stdio.h>

#include<conio.h>

#include<dir.h>

#include<dos.h>

void main()

int ch;

clrscr();

do

printf("\n\t\t\tMAIN MENU\n\t\t-------------------------\n");

printf("1.To Display List of Files\n");

printf("2.To Create New Directory\n");

printf("3.To Change the Working Directory\n");

printf("4.Exit\n");

printf("Enter the Number:");

scanf("%d",&ch);

switch(ch)
{

case 1:

list_file();

break;

case 2:

directory();

break;

case 3:

change_dir();

break;

case 4:

exit(0);

} }while(ch<=4);

int list_file()

int l;

char e[]="*.*";

clrscr();

printf("\t\tLIST FILE DETAIL\n\t--------------------------------------\n");

printf("1.List All Files\n2.List of Extention Files\n3.List of Name Wise\n");

scanf("%d",&l);

switch(l)

{
case 1:

printf("List of All(*.*) Files\n");

subfun(e);

break;

case 2:

printf("Enter the Extention:");

scanf("%s",&e);

subfun(e);

break;

case 3:

printf("Enter the Name wise(eg:moha*.*):");

scanf("%s",&e);

subfun(e);

break;

return 0;

int directory()

struct ffblk ffblk;

unsigned attrib;

int d;

char name[10],buffer[MAXPATH];

printf("Enter the Directory name:");


scanf("%s",&name);

getcwd(buffer,MAXPATH);

printf("Current directory:%s\n",buffer);

if(_dos_getfileattr(name,&attrib)==0)

printf("%s has already available",name);

return 0;

else

mkdir(name);

printf("%s Directory Successfully Created\n",name);

return 0;

int change_dir()

char buffer[MAXPATH];

int d,d1;

printf("\nCurrent Directory:%s\n",getcwd(buffer,MAXPATH));

printf("\t\tChange Directory\n\t\t-----------------\n");

printf("\n1.Step by Step Backward\n2.Goto Root Directory\n3.Forward Directory


\nEnter the number:");

scanf("%d",&d);

switch(d)
{

case 1:

chdir("..");

break;

case 2:

chdir("\\");

break;

case 3:

printf("Please enter the Filename:");

scanf("%s",buffer);

chdir(buffer);

break;

printf("\nCurrent Directory:%s",getcwd(buffer,MAXPATH));

return 0;

int subfun(s)

char s[10];

struct ffblk ffblk;

int d,p=0,i=0;

d=findfirst(s,&ffblk,0);

while(!d)
{

printf("%s\n",ffblk.ff_name);

d=findnext(&ffblk);

i++;

p=p+1;

if(p>=22)

printf("Press any key to continue:\n");

getch();

p=0;

printf("\nTotal File:%d",i);

return 0;

}
Output

Result:

Thus the C program to perform various system call using case is executed and output
is verified successfully.
2.C Program for File Permissions.
Aim:
To Write a C program for file permissions.

Procedure:

1.
Read the input variables and assign the value
2.
Print the values which we are going to perform
3.
Using the structure ,it performs the operation.
4.
Check the values for all the corresponding operations.
5.
Print the result and stop the execution
Program:

#include "time.h"

#include "sys\stat.h"

#include "stdio.h"

void main(){

struct stat status;

FILE *fp;

stat("test.txt",&status);

clrscr();

if (status.st_mode & S_IREAD)

printf("You have read permission.\n");

if (status.st_mode & S_IWRITE)

printf("You have write permission.");

getch();

struct stat {

short st_dev, st_ino;

short st_mode, st_nlink;

int st_uid, st_gid;


short st_rdev;

long st_size, st_atime;

long st_mtime, st_ctime;

};

Result:

The above aim of the program has been achieved successfully


3.C Program for File Operations.

Aim:
To Write a C program for file operations.

Procedure:

1.
Read the input variables and assign the value
2.
Open the existing files. if the specified file is not existing ,the corresponding
messages will be displayed
3. If the file is exist, it get input from user and the input written in the specified file
4. Finally the file will be closed
5. stop the execution
Program:

# include <stdio.h>

# include <string.h>

int main( )

FILE *fp ;

char data[50];

printf( "Opening the file test.c in write mode" ) ;

fp = fopen("test.c", "w") ;

if ( fp == NULL )

printf( "Could not open file test.c" ) ;

return 1;

printf( "\n Enter some text from keyboard” \

“ to write in the file test.c" ) ;

while ( strlen ( gets( data ) ) > 0 )

{
fputs(data, fp) ;

fputs("\n", fp) ;

printf("Closing the file test.c") ;

fclose(fp) ;

return 0;

OUTPUT:
Opening the file test.c in write mode

Enter some text from keyboard to write in the file test.c


Hai, How are you?
Closing the file test.c

Result:

The above aim of the program has been achieved successfully


4.C Program for File Copy and Move.

Aim:
To Write a C program for file copy and move.

Procedure:

1.
Read the input variables and assign the value
2.
Open the target files. if the specified file is not existing ,the corresponding messages
will be displayed
3. If the file is exist, it get input from user and the input written in the specified file
4. Finally the file will be closed
5. stop the execution
Program:

#include <stdio.h>

#include <stdlib.h>

main()

char ch, source_file[20], target_file[20];

FILE *source, *target;

printf("Enter name of file to copy\n");

gets(source_file);

source = fopen(source_file, "r");

if( source == NULL )

printf("Press any key to exit...\n");

exit(EXIT_FAILURE);
}

printf("Enter name of target file\n");

gets(target_file);

target = fopen(target_file, "w");

if( target == NULL )

fclose(source);

printf("Press any key to exit...\n");

exit(EXIT_FAILURE);

while( ( ch = fgetc(source) ) != EOF )

fputc(ch, target);

printf("File copied successfully.\n");

fclose(source);

fclose(target);

return 0;

}
Result:

The above aim of the program has been achieved successfully


5.C Program for Dining Philosophers Problem.

Aim:
To Write a C program for Dining Philosophers Problem.

Algorithm:

Step 1: Start

Step 2: Define the number of philosophers

Step 3: Declare one thread per philosopher

Step 4: Declare one semaphore (represent chopsticks) per philosopher

Step 5: When a philosopher is hungry

See if chopsticks on both sides are free

Acquire both chopsticks

Eat

Restore the chopstick

If chopsticks aren‟t free

Step 6: Wait till they are available

Step 7: Stop

Program:

#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>

#define N 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
#define LEFT (ph_num+4)%N
#define RIGHT (ph_num+1)%N

sem_t mutex;
sem_t S[N];

void * philospher(void *num);


void take_fork(int);
void put_fork(int);
void test(int);

int state[N];
int phil_num[N]={0,1,2,3,4};

int main()
{
int i;
pthread_t thread_id[N];
sem_init(&mutex,0,1);
for(i=0;i<N;i++)
sem_init(&S[i],0,0);
for(i=0;i<N;i++)
{
pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);
printf("Philosopher %d is thinkingn",i+1);
}
for(i=0;i<N;i++)
pthread_join(thread_id[i],NULL);
}

void *philospher(void *num)


{
while(1)
{
int *i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
}
}

void take_fork(int ph_num)


{
sem_wait(&mutex);
state[ph_num] = HUNGRY;
printf("Philosopher %d is Hungryn",ph_num+1);
test(ph_num);
sem_post(&mutex);
sem_wait(&S[ph_num]);
sleep(1);
}

void test(int ph_num)


{
if (state[ph_num] == HUNGRY && state[LEFT] != EATING && state[RIGHT] !=
EATING)
{
state[ph_num] = EATING;
sleep(2);
printf("Philosopher %d takes fork %d and %dn",ph_num+1,LEFT+1,ph_num+1);
printf("Philosopher %d is Eatingn",ph_num+1);
sem_post(&S[ph_num]);
}
}

void put_fork(int ph_num)


{
sem_wait(&mutex);
state[ph_num] = THINKING;
printf("Philosopher %d putting fork %d and %d downn",ph_num+1,LEFT+1,ph_num+1);
printf("Philosopher %d is thinkingn",ph_num+1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}

Result:

The above aim of the program has been achieved successfully


6.C Program for Producer – Consumer Problem concept.

#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);

Output

1.Producer
2.Consumer
3.Exit
Enter your choice:1

Producer produces the item 1


Enter your choice:2

Consumer consumes item 1


Enter your choice:2
Buffer is empty!!
Enter your choice:1
Producer produces the item 1
Enter your choice:1

Producer produces the item 2


Enter your choice:1

Producer produces the item 3


Enter your choice:1
Buffer is full!!
Enter your choice:3

Result:

The above aim of the program has been achieved successfully


7.C Program for First In First Serve Algorithm.

Aim:

To write a c program to implement the First In First Serve algorithm.

Algorithm.

Step 1: Start

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time

Step 4: Set the waiting of the first process as 0 and its burst time as its turn around time

Step 5: for each process in the Ready Q calculate

Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)

Turn around time for Process(n)= Turn around time of Process(n-1)+ Burst time for
process(n)

Step 6: Calculate

Average waiting time = Total waiting Time / Number of process

Average Turnaround time = Total Turnaround Time / Number of process

Step 7: Stop

#include<stdio.h>

int main(){

int bt[10]={0},at[10]={0},tat[10]={0},wt[10]={0},ct[10]={0};

int n,sum=0;

float totalTAT=0,totalWT=0;
printf("Enter number of processes ");

scanf("%d",&n);

printf("Enter arrival time and burst time for each process\n\n");

for(int i=0;i<n;i++)

printf("Arrival time of process[%d] ",i+1);

scanf("%d",&at[i]);

printf("Burst time of process[%d] ",i+1);

scanf("%d",&bt[i]);

printf("\n");

//calculate completion time of processes

for(int j=0;j<n;j++)

sum+=bt[j];

ct[j]+=sum;

//calculate turnaround time and waiting times


for(int k=0;k<n;k++)

tat[k]=ct[k]-at[k];

totalTAT+=tat[k];

for(int k=0;k<n;k++)

wt[k]=tat[k]-bt[k];

totalWT+=wt[k];

printf("Solution: \n\n");

printf("P#\t AT\t BT\t CT\t TAT\t WT\t\n\n");

for(int i=0;i<n;i++)

printf("P%d\t %d\t %d\t %d\t %d\t %d\n",i+1,at[i],bt[i],ct[i],tat[i],wt[i]);

printf("\n\nAverage Turnaround Time = %f\n",totalTAT/n);

printf("Average WT = %f\n\n",totalWT/n);

return 0;

}
Output
Arrival time of process[3] 0
Burst time of process[3] 3

Solution:

P# AT BT CT TAT WT

P1 0 24 24 24 0
P2 0 3 27 27 24
P3 0 3 30 30 27

Average Turnaround Time = 27.000000


Average WT = 17.000000

Result:

The above aim of the program has been achieved successfully


8.C Program for Shortest Job First Scheduling Algorithm.

Aim:

Write a C program to implement the SJF Scheduling Algorithm.

Algorithm:

Step 1: Start

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time

Step 4: Start the Ready Q according the shortest Burst time by sorting according to lowest
to highest burst time.

Step 5: Set the waiting time of the first process as „0‟ and its turnaround time as its burst
time.

Step 6: For each process in the ready queue, calculate

Waiting time for process(n)= waiting time of process (n-1) + Burst time of process (n-1)
Turn around time for Process(n)= Turn around time of Process (n-1)+ Burst time for
process (n)

Step 6: Calculate

Average waiting time = Total waiting Time / Number of process

Average Turnaround time = Total Turnaround Time / Number of process

Step 7: Stop
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

//sorting burst time in ascending order using selection sort

for(i=0;i<n;i++)

pos=i;

for(j=i+1;j<n;j++)

if(bt[j]<bt[pos])
pos=j;

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

//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=(float)total/n; //average waiting time

total=0;
printf("\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];

printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);

avg_tat=(float)total/n; //average turnaround time

printf("\n\nAverage Waiting Time=%f",avg_wt);

printf("\nAverage Turnaround Time=%f\n",avg_tat);

Output
Result:

The above aim of the program has been achieved successfully


9.C Program for Round Robin Scheduling Method.

Aim:

Write a C program to implement the Round Robin Scheduling Algorithm

Algorithm:

Step 1: Start

Step 2: Accept the number of processes in the ready Queue and time quantum

Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time

Step 4: Calculate the no. of time slices for each process where No. of time slice for
process(n) = burst time process(n)/time slice

Step 5: If the burst time is less than the time slice then the no. of time slices =1.

Step 6: Consider the ready queue is a circular Q, calculate

Waiting time for process(n) = waiting time of process(n-1)+ burst time of process(n-1 ) +
the time difference in getting the CPU from process(n-1)

Turn around time for process (n) = waiting time of the process (n) + burst time of
process(n)+ the time difference in getting CPU from process(n).

Step 7: Calculate

Average waiting time = Total waiting Time / Number of process

Average Turnaround time = Total Turnaround Time / Number of process

Step 8: Stop

Program:

#include<stdio.h>

int main()

int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];

printf("Enter Total Process:\t ");

scanf("%d",&n);

remain=n;

for(count=0;count<n;count++)

printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);

scanf("%d",&at[count]);

scanf("%d",&bt[count]);

rt[count]=bt[count];

printf("Enter Time Quantum:\t");

scanf("%d",&time_quantum);

printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");

for(time=0,count=0;remain!=0;)

if(rt[count]<=time_quantum && rt[count]>0)

time+=rt[count];

rt[count]=0;

flag=1;

else if(rt[count]>0)

rt[count]-=time_quantum;

time+=time_quantum;
}

if(rt[count]==0 && flag==1)

remain--;

printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);

wait_time+=time-at[count]-bt[count];

turnaround_time+=time-at[count];

flag=0;

if(count==n-1)

count=0;

else if(at[count+1]<=time)

count++;

else

count=0;

printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);

printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);

return 0;

Output
Result:

The above aim of the program has been achieved successfully


10.C Program for Priority Scheduling Algorithm.

Aim:

Write a C program to implement the Priority Scheduling Algorithm.

Algorithm:

Step 1: Start

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept the CPU
bursttime

Step 4: Sort the ready queue according to the priority number

Step 5: Set the waiting of the first process as „0‟ and its burst time as its turn around time
Step 6: For each process in the Ready Q Calculate

Waiting time for process(n)= waitingtime of process (n-1) + Burst time of process(n-1)
Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
Calculate

Average waiting time = Total waiting Time / Number of process

Average Turnaround time = Total Turnaround Time / Number of process

Step 7: Stop

Program:

#include<stdio.h>

#include<conio.h>

void main()

int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;

printf("Enter the number of process : ");

scanf("%d",&n);

printf("\n Enter process : time priorities \n");

for(i=0;i<n;i++)
{

printf("\nProcess no %d : ",i+1);

scanf("%d %d",&pt[i],&pp[i]);

p[i]=i+1;

for(i=0;i<n-1;i++)

for(int j=i+1;j<n;j++)

if(pp[i]<pp[j])

x=pp[i];

pp[i]=pp[j];

pp[j]=x;

x=pt[i];

pt[i]=pt[j];

pt[j]=x;

x=p[i];

p[i]=p[j];

p[j]=x;

w[0]=0;

awt=0;

t[0]=pt[0];
atat=t[0];

for(i=1;i<n;i++)

w[i]=t[i-1];

awt+=w[i];

t[i]=w[i]+pt[i];

atat+=t[i];

printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time Priority \n");

for(i=0;i<n;i++)

printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d \n",p[i],pt[i],w[i],t[i],pp[i]);

awt/=n;

atat/=n;

printf("\n Average Wait Time : %d \n",awt);

printf("\n Average Turn Around Time : %d \n",atat);

getch();

Result:

The above aim of the program has been achieved successfully.

You might also like