OSprakash
OSprakash
OSprakash
TECHNOLOGY
Operating Systems
Practical File
BRANCH -AIDS
INDEX
THEORY:
FCFS is the first algorithm of CPU Process Scheduling Algorithm. In First Come First Serve
Algorithm what we do is to allow the process to execute in linear manner.
This means that whichever process enters process enters the ready queue first is executed
first. This shows that First Come First Serve Algorithm follows First In First Out (FIFO)
principle. The First Come First Serve Algorithm can be executed in Pre Emptive and Non Pre
Emptive manner.
1. Implementation is simple.
2. Does not cause any causalities while using
3. It adopts a non pre-emptive and pre-emptive strategy.
4. It runs each procedure in the order that they are received.
5. Arrival time is used as a selection criterion for procedures.
OUTPUT:
(EXP-1 : FCFS Scheduling)
EXPERIMENT – 2
AIM: C program to implement a round robin scheduling algorithm.
THEORY:
Round Robin Scheduling is a CPU scheduling algorithm in which each process is executed
for a fixed time slot. Since the resources are snatched after the time slot, round robin is pre-
emptive.
Pre-emptive: In this type, resources can be voluntarily snatched.
Non-Pre-emptive: In this type, if a process is once started, it will execute completely i.e
resources cannot be snatched.
Waiting Time: Time Difference between turnaround time and burst time.
Waiting Time = Turnaround Time – Burst Time
PROGRAM:
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. // initlialize the variable name
6. int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];
7. float avg_wt, avg_tat;
8. printf(" Total number of process in the system: ");
9. scanf("%d", &NOP);
10. y = NOP; // Assign the number of process to variable y
11.
12. // Use for loop to enter the details of the process like Arrival time and the Burst Time
13. for(i=0; i<NOP; i++)
14. {
15. printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
16. printf(" Arrival time is: \t"); // Accept arrival time
17. scanf("%d", &at[i]);
18. printf(" \nBurst time is: \t"); // Accept the Burst time
19. scanf("%d", &bt[i]);
20. temp[i] = bt[i]; // store the burst time in temp array
21. }
22. // Accept the Time qunat
23. printf("Enter the Time Quantum for the process: \t");
24. scanf("%d", &quant);
25. // Display the process No, burst time, Turn Around Time and the waiting time
26. printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
27. for(sum=0, i = 0; y!=0; )
28. {
29. if(temp[i] <= quant && temp[i] > 0) // define the conditions
30. {
31. sum = sum + temp[i];
32. temp[i] = 0;
33. count=1;
34. }
35. else if(temp[i] > 0)
36. {
37. temp[i] = temp[i] - quant;
38. sum = sum + quant;
39. }
40. if(temp[i]==0 && count==1)
41. {
42. y--; //decrement the process no.
43. printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-
at[i]-bt[i]);
44. wt = wt+sum-at[i]-bt[i];
45. tat = tat+sum-at[i];
46. count =0;
47. }
48. if(i==NOP-1)
49. {
50. i=0;
51. }
52. else if(at[i+1]<=sum)
53. {
54. i++;
55. }
56. else
57. {
58. i=0;
59. }
60. }
61. // represents the average waiting time and Turn Around time
62. avg_wt = wt * 1.0/NOP;
63. avg_tat = tat * 1.0/NOP;
64. printf("\n Average Turn Around Time: \t%f", avg_wt);
65. printf("\n Average Waiting Time: \t%f", avg_tat);
66. getch();
67. }
OUTPUT:
(EXP-2 : Round Robin Scheduling)
EXPERIMENT – 3
AIM: Implementation of the following Memory Allocation Methods for fixed partition
a) First Fit b) Worst Fit c) Best Fit.
THEORY:
1. First Fit: In the first fit, the partition is allocated which is the first sufficient block
from the top of Main Memory. It scans memory from the beginning and chooses the
first available block that is large enough. Thus it allocates the first hole that is large
enough.
2. Best Fit Allocate the process to the partition which is the first smallest sufficient
partition among the free available partition. It searches the entire list of holes to find
the smallest hole whose size is greater than or equal to the size of the process.
3. Worst Fit Allocate the process to the partition which is the largest sufficient among
the freely available partitions available in the main memory. It is opposite to the
best-fit algorithm. It searches the entire list of holes to find the largest hole and
allocate it to process.
Program:
// Driver code
int main()
{
int m; //number of blocks in the memory
int n; //number of processes in the input queue
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
m = sizeof(blockSize) / sizeof(blockSize[0]);
n = sizeof(processSize) / sizeof(processSize[0]);
return 0 ;
}
Output:
(EXP-3 : First Fit Memory Allocation)
Program:
#include <stdio.h>
#include <stdlib.h>
if (wstIdx != -1) {
allocation[i] = wstIdx;
blockSize[wstIdx] -= processSize[i];
}
}
free(allocation);
}
int main() {
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);
worstFit(blockSize, m, processSize, n);
return 0;
}
Output:
(EXP-3 : Worst Fit Memory Allocation)
Program:
#include<stdio.h>
int allocation[n];
if (bestIdx != -1) {
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++) {
printf(" %d\t\t%d\t\t", i+1, processSize[i]);
if (allocation[i] != -1)
printf("%d", allocation[i] + 1);
else
printf("Not Allocated");
printf("\n");
}
}
int main() {
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 42};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);
bestFit(blockSize, m, processSize, n);
return 0;
}
Output:
(EXP-3 : Best Fit Memory Allocation)
EXPERIMENT – 4
pthread_exit(NULL);
}
OUTPUT:
(EXP-4 : reader/writer problems using semaphores.)
EXPERIMENT – 5
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4
int max[5][3] = { { 7, 5, 3 }, // P0 // MAX Matrix
{ 3, 2, 2 }, // P1
{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // P4
int avail[3] = { 3, 3, 2 }; // Available Resources
int f[n], ans[n], ind = 0;
for (k = 0; k < n; k++) {
f[k] = 0;
}
int need[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
int y = 0;
for (k = 0; k < 5; k++) {
for (i = 0; i < n; i++) {
if (f[i] == 0) {
int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > avail[j]){
flag = 1;
break;
}
}
if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}
int flag = 1;
for(int i=0;i<n;i++)
{
if(f[i]==0)
{
flag=0;
printf("The following system is not safe");
break;
}
}
if(flag==1)
{
printf("Following is the SAFE Sequence\n");
for (i = 0; i < n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
}
return (0);
OUTPUT:
(EXP-5 : Banker’s algorithm)
EXPERIMENT – 6
AIM: Process Management
Program to implement execv function using C.
THEORY:
execv():
The `execv()` function is used to replace the current process image with a new
program.
It loads and runs a new program by specifying its path and command-line arguments.
The old program's code, data, and stack segments are replaced by the new program.
OUTPUT:
EXPERIMENT – 7
SJF (Shortest Job First) is a scheduling strategy that gives the process with the
quickest CPU burst time to the CPU first. As this technique is non-preemptive, once a
process has begun to run, it cannot be stopped until it has finished. The SJF scheduling
method is ideal since it reduces the average waiting time for a set of processes.
In this blog post, we will learn how to implement the SJF scheduling algorithm in C
programming language. We will start by discussing the algorithm and its working, followed
by the implementation details in C. We will also provide a sample code with syntax, example,
and output to help you understand the concept better.
Algorithm:
Steps:
1. We first take input from the user for the number of processes, arrival time,
and burst time of each process.
2. We create an array of structures to store the information about each process.
3. We use the qsort() function to sort the processes based on their burst time.
4. We calculate the waiting time of each process using the SJF scheduling algorithm.
5. We calculate the average waiting time and average turnaround time of all
processes.
6. Finally, we print the results in tabular form.
Program:
1. struct Process {
2. int arrival_time;
3. int burst_time;
4. int waiting_time;
5. };
6.
7. int compare(const void *a, const void *b) {
8. struct Process *p1 = (struct Process *)a;
9. struct Process *p2 = (struct Process *)b;
10. return p1->burst_time - p2->burst_time;
11. }
12.
13. int main() {
14. int n, i, j;
15. float avg_waiting_time = 0, avg_turnaround_time = 0;
16. printf("Enter the number of processes: ");
17. scanf("%d", &n);
18. struct Process processes[n];
19. for (i = 0; i< n; i++) {
20. printf("Enter arrival time and burst time of process %d: ", i+1);
21. scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time);
22. }
23. qsort(processes, n, sizeof(struct Process), compare);
24. processes[0].waiting_time = 0;
25. for (i = 1; i< n; i++) {
26. processes[i].waiting_time = 0;
27. for (j = 0; j <i; j++)
28. {
29.
30. processes[i].waiting_time += processes[j].burst_time;
31.
32. }
33.
34. avg_waiting_time += processes[i].waiting_time;
35.
36. }
37.
38. avg_waiting_time /= n;
39.
40. for (i = 0; i< n; i++) {
41.
42. avg_turnaround_time += processes[i].burst_time + processes[i].waiting_time;
43.
44. }
45.
46. avg_turnaround_time /= n;
47.
48. printf("\nProcess\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
49.
50. for (i = 0; i< n; i++) {
51.
52. printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i+1, processes[i].arrival_time, processes[i].burst_time, processes[i].w
aiting_time, processes[i].burst_time+processes[i].waiting_time);
53.
54. }
55.
56. printf("\nAverage Waiting Time: %f\n", avg_waiting_time);
57.
58. printf("Average Turnaround Time: %f\n", avg_turnaround_time);
59.
60. return 0;
61.
62. }
Output:
EXPERIMENT – 8
THEORY:
Priority scheduling is a CPU scheduling algorithm where processes are assigned priorities,
and the process with the highest priority is selected for execution. In priority scheduling, the
scheduler chooses the process with the highest priority number from the ready queue and
allocates the CPU to that process. The priority of a process is usually determined by its
importance, the amount of CPU time it needs, or its deadline.
Priority scheduling can be divided into two types: preemptive and non-preemptive. In
preemptive priority scheduling, the currently running process may be interrupted by a higher
priority process, while in non-preemptive priority scheduling, once a process is assigned the
CPU, it will continue to execute until it finishes or is blocked.
Priority scheduling is a widely used algorithm for CPU scheduling in OS. It assigns
priorities to different processes and chooses the process with the highest priority for
execution. In this blog post, we provided a sample program in C for priority scheduling that
sorts the processes based on their priority and calculates the waiting time and turnaround time
for each process.
Program:
1. #include<stdio.h>
2. #include<stdlib.h>
3.
4. struct process {
5. int process_id;
6. int burst_time;
7. int priority;
8. int waiting_time;
9. int turnaround_time;
10. };
11.
12. void find_waiting_time(struct process[], int, int[]);
13. void find_turnaround_time(struct process[], int, int[], int[]);
14.
15. void find_average_time(struct process[], int);
16.
17. void priority_scheduling(struct process[], int);
18.
19. int main()
20. {
21. int n, i;
22. struct process proc[10];
23.
24. printf("Enter the number of processes: ");
25. scanf("%d", &n);
26.
27. for(i = 0; i< n; i++)
28. {
29. printf("\nEnter the process ID: ");
30. scanf("%d", &proc[i].process_id);
31.
32. printf("Enter the burst time: ");
33. scanf("%d", &proc[i].burst_time);
34.
35. printf("Enter the priority: ");
36. scanf("%d", &proc[i].priority);
37. }
38.
39. priority_scheduling(proc, n);
40. return 0;
41. }
42.
43. void find_waiting_time(struct process proc[], int n, int wt[])
44. {
45. int i;
46. wt[0] = 0;
47.
48. for(i = 1; i< n; i++)
49. {
50. wt[i] = proc[i - 1].burst_time + wt[i - 1];
51. }
52. }
53.
54. void find_turnaround_time(struct process proc[], int n, int wt[], int tat[])
55. {
56. int i;
57. for(i = 0; i< n; i++)
58. {
59. tat[i] = proc[i].burst_time + wt[i];
60. }
61. }
62.
63. void find_average_time(struct process proc[], int n)
64. {
65. int wt[10], tat[10], total_wt = 0, total_tat = 0, i;
66.
67. find_waiting_time(proc, n, wt);
68. find_turnaround_time(proc, n, wt, tat);
69.
70. printf("\nProcess ID\tBurst Time\tPriority\tWaiting Time\tTurnaround Time");
71.
72. for(i = 0; i< n; i++)
73. {
74. total_wt = total_wt + wt[i];
75. total_tat = total_tat + tat[i];
76. printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", proc[i].process_id, proc[i].burst_time, proc[i].priority, wt[i], tat[i]);
77. }
78.
79. printf("\n\nAverage Waiting Time = %f", (float)total_wtotal_wt/n);
80. printf("\nAverage Turnaround Time = %f\n", (float)total_tat/n);
81. }
82.
83. void priority_scheduling(struct process proc[], int n)
84. {
85. int i, j, pos;
86. struct process temp;
87. for(i = 0; i< n; i++)
88. {
89. pos = i;
90. for(j = i + 1; j < n; j++)
91. {
92. if(proc[j].priority< proc[pos].priority)
93. pos = j;
94. }
95.
96. temp = proc[i];
97. proc[i] = proc[pos];
98. proc[pos] = temp;
99. }
100.
101. find_average_time(proc, n);
102. }
Output: