You are on page 1of 20

Operating System Programs

File.
INDEX
1. Implementation of CPU Scheduling using FCFS.
2. Implementation of CPU Scheduling using SJF.
3. Implementation of CPU Scheduling using Priority.
4. Implementation of CPU Scheduling using Round Robin.
5. Solution of Producer-Consumer Problem.
6. Solution of Reader-Writer Problem.
7. Implementation of Banker’s Algorithm.
8. Implementation of File Storage Allocation using continuous
allocation.
9. Implementation of File Storage Allocation using linked list.
10. Implementation of File Storage Allocation using indexing.
Program 1
Implementation of CPU Scheduling using FCFS .
1. #include <stdio.h>
2. int main()
3. {
4. int pid[15];
5. int bt[15];
6. int n;
7. printf("Enter the number of processes: ");
8. scanf("%d",&n);
9.
10. printf("Enter process id of all the processes: ");
11. for(int i=0;i<n;i++)
12. {
13. scanf("%d",&pid[i]);
14. }
15.
16. printf("Enter burst time of all the processes: ");
17. for(int i=0;i<n;i++)
18. {
19. scanf("%d",&bt[i]);
20. }
21.
22. int i, wt[n];
23. wt[0]=0;
24.
25. //for calculating waiting time of each process
26. for(i=1; i<n; i++)
27. {
28. wt[i]= bt[i-1]+ wt[i-1];
29. }
30.
31. printf("Process ID Burst Time Waiting Time TurnAround
Time\n");
32. float twt=0.0;
33. float tat= 0.0;
34. for(i=0; i<n; i++)
35. {
36. printf("%d\t\t", pid[i]);
37. printf("%d\t\t", bt[i]);
38. printf("%d\t\t", wt[i]);
39.
40. //calculating and printing turnaround time of each process
41. printf("%d\t\t", bt[i]+wt[i]);
42. printf("\n");
43.
44. //for calculating total waiting time
45. twt += wt[i];
46.
47. //for calculating total turnaround time
48. tat += (wt[i]+bt[i]);
49. }
50. float att,awt;
51.
52. //for calculating average waiting time
53. awt = twt/n;
54.
55. //for calculating average turnaround time
56. att = tat/n;
57. printf("Avg. waiting time= %f\n",awt);
58. printf("Avg. turnaround time= %f",att);
59. }

Program 2
Implementation of CPU Scheduling using SJF.
1. #include<stdio.h>
2. int main()
3. {
4. int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,totalT=0,pos,temp;
5. float avg_wt,avg_tat;
6. printf("Enter number of process:");
7. scanf("%d",&n);
8.
9. printf("\nEnter Burst Time:\n");
10. for(i=0;i<n;i++)
11. {
12. printf("p%d:",i+1);
13. scanf("%d",&bt[i]);
14. p[i]=i+1;
15. }
16.
17. //sorting of burst times
18. for(i=0;i<n;i++)
19. {
20. pos=i;
21. for(j=i+1;j<n;j++)
22. {
23. if(bt[j]<bt[pos])
24. pos=j;
25. }
26.
27. temp=bt[i];
28. bt[i]=bt[pos];
29. bt[pos]=temp;
30.
31. temp=p[i];
32. p[i]=p[pos];
33. p[pos]=temp;
34. }
35.
36. wt[0]=0;
37.
38. //finding the waiting time of all the processes
39. for(i=1;i<n;i++)
40. {
41. wt[i]=0;
42. for(j=0;j<i;j++)
43. //individual WT by adding BT of all previous completed processes 44.
wt[i]+=bt[j];
45.
46. //total waiting time
47. total+=wt[i];
48. }
49.
50. //average waiting time 51.
avg_wt=(float)total/n;
52.
53. printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
54. for(i=0;i<n;i++)
55. {
56. //turnaround time of individual processes
57. tat[i]=bt[i]+wt[i];
58.
59. //total turnaround time
60. totalT+=tat[i];
61. printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]); 62. }
63.
64. //average turnaround time
65. avg_tat=(float)totalT/n;
66. printf("\n\nAverage Waiting Time=%f",avg_wt);
67. printf("\nAverage Turnaround Time=%f",avg_tat); 68. }
Program 3
Implementation of CPU Scheduling using Priority .
1. #include <stdio.h>
2.
3. //Function to swap two variables
4. void swap(int *a,int *b)
5. {
6. int temp=*a;
7. *a=*b;
8. *b=temp;
9. }
10. int main()
11. {
12. int n;
13. printf("Enter Number of Processes: ");
14. scanf("%d",&n);
15.
16. // b is array for burst time, p for priority and index for process id
17. int b[n],p[n],index[n];
18. for(int i=0;i<n;i++)
19. {
20. printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
21. scanf("%d %d",&b[i],&p[i]);
22. index[i]=i+1;
23. }
24. for(int i=0;i<n;i++)
25. {
26. int a=p[i],m=i;
27.
28. //Finding out highest priority element and placing it at its
desired position
29. for(int j=i;j<n;j++)
30. {
31. if(p[j] > a)
32. {
33. a=p[j];
34. m=j;
35. }
36. }
37.
38. //Swapping processes
39. swap(&p[i], &p[m]);
40. swap(&b[i], &b[m]);
41. swap(&index[i],&index[m]);
42. }
43.
44. // T stores the starting time of process
45. int t=0;
46.
47. //Printing scheduled process
48. printf("Order of process Execution is\n");
49. for(int i=0;i<n;i++)
50. {
51. printf("P%d is executed from %d to %d\n",index[i],t,t+b[i]);
52. t+=b[i];
53. }
54. printf("\n");
55. printf("Process Id Burst Time Wait Time TurnAround Time\n");
56. int wait_time=0;
57. for(int i=0;i<n;i++)
58. {
59. printf("P%d %d %d
%d\n",index[i],b[i],wait_time,wait_time + b[i]);
60. wait_time += b[i];
61. }
62. return 0;
63. }

Program 4
Implementation of CPU Scheduling using Round Robin.

1. #include<stdio.h>
2.
3. int main()
4. {
5. //Input no of processed
6. int n;
7. printf("Enter Total Number of Processes:");
8. scanf("%d", &n);
9. int wait_time = 0, ta_time = 0, arr_time[n], burst_time[n],
temp_burst_time[n]; 10. int x = n;
11.
12. //Input details of processes
13. for(int i = 0; i < n; i++)
14. {
15. printf("Enter Details of Process %d \n", i + 1);
16. printf("Arrival Time: ");
17. scanf("%d", &arr_time[i]);
18. printf("Burst Time: ");
19. scanf("%d", &burst_time[i]);
20. temp_burst_time[i] = burst_time[i];
21. }
22.
23. //Input time slot
24. int time_slot;
25. printf("Enter Time Slot:");
26. scanf("%d", &time_slot);
27.
28. //Total indicates total time
29. //counter indicates which process is executed
30. int total = 0, counter = 0,i;
31. printf("Process ID Burst Time Turnaround Time Waiting
Time\n");
32. for(total=0, i = 0; x!=0; )
33. {
34. // define the conditions
35. if(temp_burst_time[i] <= time_slot && temp_burst_time[i] > 0) 36.
{
37. total = total + temp_burst_time[i];
38. temp_burst_time[i] = 0;
39. counter=1;
40. }
41. else if(temp_burst_time[i] > 0)
42. {
43. temp_burst_time[i] = temp_burst_time[i] - time_slot;
44. total += time_slot;
45. }
46. if(temp_burst_time[i]==0 && counter==1)
47. {
48. x--; //decrement the process no.
49. printf("\nProcess No %d \t\t %d\t\t\t\t %d\t\t\t %d", i+1,
burst_time[i],
50. total-arr_time[i], total-arr_time[i]-burst_time[i]);
51. wait_time = wait_time+total-arr_time[i]-burst_time[i];
52. ta_time += total -arr_time[i];
53. counter =0;
54. }
55. if(i==n-1)
56. {

57. i=0;
58. }
59. else if(arr_time[i+1]<=total)
60. {
61. i++;
62. }
63. else
64. {
65. i=0;
66. }
67. }
68. float average_wait_time = wait_time * 1.0 / n;
69. float average_turnaround_time = ta_time * 1.0 / n;
70. printf("\nAverage Waiting Time:%f", average_wait_time);
71. printf("\nAvg Turnaround Time:%f", average_turnaround_time);
72. return 0;
73. }

Program 5
Implementation of the solution for Bounded Buffer
(producerconsumer) using inter process communication
techniques Semaphores.
#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
Program 6
Implementation of the solution for Reader-Writer Problem
using inter process communication techniques -Semaphores.
#include
< pthread.h>
#include <semaphore.h>
#include <stdio.h>

/*
This program provides a possible solution for first readers writers
problem using mutex and semaphore.
I have used 10 readers and 5 producers to demonstrate the solution. You
can always play with these values.
*/

sem_t wrt;
pthread_mutex_t mutex;
int cnt = 1; int numreader =
0;

void *writer(void *wno)


{
sem_wait(&wrt); cnt =
cnt*2;
printf("Writer %d modified cnt to %d\n",(*((int *)wno)),cnt);
sem_post(&wrt);

}
void *reader(void *rno)
{
// Reader acquire the lock before modifying numreader
pthread_mutex_lock(&mutex); numreader++;
if(numreader == 1) {
sem_wait(&wrt); // If this id the first reader, then it will block
the writer
}
pthread_mutex_unlock(&mutex); //
Reading Section
printf("Reader %d: read cnt as %d\n",*((int *)rno),cnt);

// Reader acquire the lock before modifying numreader


pthread_mutex_lock(&mutex); numreader--;
if(numreader == 0) {
sem_post(&wrt); // If this is the last reader, it will wake up the
writer.
}
pthread_mutex_unlock(&mutex);
}
int main()
{

pthread_t read[10],write[5];
pthread_mutex_init(&mutex, NULL); sem_init(&wrt,0,1);

int a[10] = {1,2,3,4,5,6,7,8,9,10}; //Just used for numbering the


producer and consumer

for(int i = 0; i < 10; i++) { pthread_create(&read[i], NULL,


(void *)reader, (void *)&a[i]);
}
for(int i = 0; i < 5; i++) { pthread_create(&write[i], NULL,
(void *)writer, (void *)&a[i]);
}

for(int i = 0; i < 10; i++) {


pthread_join(read[i], NULL);
}
for(int i = 0; i < 5; i++) {
pthread_join(write[i], NULL);
}

pthread_mutex_destroy(&mutex); sem_destroy(&wrt);

return 0;

Program 7
Implementation of Banker’s Algorithm.
#include <stdio.h>

int current[5][5], maximum_claim[5][5], available[5];


int allocation[5] = {0, 0, 0, 0, 0}; int maxres[5],
running[5], safe = 0; int counter = 0, i, j, exec,
resources, processes, k = 1;

int main()
{ printf("\nEnter number of processes:
"); scanf("%d", &processes);

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


{
running[i] = 1;
counter++;
}

printf("\nEnter number of resources: ");


scanf("%d", &resources);
printf("\nEnter Claim Vector:");
for (i = 0; i < resources; i++)
{
scanf("%d", &maxres[i]);
}

printf("\nEnter Allocated Resource Table:\n");


for (i = 0; i < processes; i++)
{
for(j = 0; j < resources; j++)
{
scanf("%d", &current[i][j]);
}
}

printf("\nEnter Maximum Claim Table:\n");


for (i = 0; i < processes; i++)
{
for(j = 0; j < resources; j++)
{
scanf("%d", &maximum_claim[i][j]);
}
}

printf("\nThe Claim Vector is: ");


for (i = 0; i < resources; i++)
{
printf("\t%d", maxres[i]);
}
printf("\nThe Allocated Resource Table:\n");
for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
printf("\t%d", current[i][j]);
}
printf("\n");
}

printf("\nThe Maximum Claim Table:\n");


for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
printf("\t%d", maximum_claim[i][j]);
}
printf("\n");
}

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


{
for (j = 0; j < resources; j++)
{
allocation[j] += current[i][j];
}
}
printf("\nAllocated resources:");
for (i = 0; i < resources; i++)
{
printf("\t%d", allocation[i]);
}

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


{
available[i] = maxres[i] - allocation[i];
}
printf("\nAvailable resources:");
for (i = 0; i < resources; i++)
{
printf("\t%d", available[i]);
}
printf("\n");

while (counter != 0)
{
safe = 0; for (i = 0; i <
processes; i++)
{
if (running[i])
{
exec = 1; for (j =
0; j < resources; j++)
{
if (maximum_claim[i][j] - current[i][j] > available[j])
{
exec = 0;
break;
}
} if
(exec)
{
printf("\nProcess%d is executing\n", i + 1);
running[i] = 0;
counter--; safe
= 1;

for (j = 0; j < resources; j++)


{
available[j] += current[i][j];
}
break;
}
}
} if
(!safe)
{
printf("\nThe processes are in unsafe state.\n");
break;
}
else
{
printf("\nThe process is in safe state");
printf("\nAvailable vector:");

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


{
printf("\t%d", available[i]);
}

printf("\n");
}
}
return 0;
}

Program 8
Implement File Storage Allocation Technique using continuous
allocation (Array).
#include < stdio.h>
#include<conio.h>
void main()
{
int f[50], i, st, len, j, c, k, count = 0;
clrscr(); for(i=0;i<50;i++) f[i]=0;
printf("Files Allocated are : \n"); x:
count=0;
printf(“Enter starting block and length of files: ”);
scanf("%d%d", &st,&len); for(k=st;k<(st+len);k++)
if(f[k]==0) count++;
if(len==count)
{
for(j=st;j<(st+len);j++) if(f[j]==0)
{ f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf(” The file is allocated to disk\n");
} else
printf(” The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)"); scanf("%d",
&c);
if(c==1)
goto x;
else exit();
getch();
}

Program 9
Implement File Storage Allocation Technique using Linked list.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], p,i, st, len, j, c, k, a;
clrscr(); for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: "); scanf("%d",&p);
printf("Enter blocks already allocated: "); for(i=0;i<p;i++)
{
scanf("%d",&a); f[a]=1;
}
x: printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len); k=len; if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{
if(f[j]==0)
{ f[j]=1; printf("%d--------
>%d\n",j,f[j]);
} else
{
printf("%d Block is already allocated \n",j); k++;
}
}}
else
printf("%d starting block is already allocated \n",st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c); if(c==1) goto x; else exit(0); getch(); }

Program 10
Implement File Storage Allocation Technique using Indexing.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr(); for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind); if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n", ind);
scanf("%d",&n);
} else
{
printf("%d index is already allocated \n",ind); goto
x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++) f[index[j]]=1; printf("Allocated\n");
printf("File Indexed\n"); for(k=0;k<n;k++) printf("%d-
------->%d : %d\n",ind,index[k],f[index[k]]);
} else
{ printf("File in the index is already allocated
\n"); printf("Enter another file indexed"); goto
y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c); if(c==1) goto x; else exit(0); getch();
}

You might also like