Rtos Lab Manual
Rtos Lab Manual
Rtos Lab Manual
School of Engineering
Syllabus Contents
w.e.f. 2024-25
6th semester
School of Engineering
Syllabus Contents
w.e.f. 2024-25
TABLE OF CONTENTS
Experiment Page
List of Experiments
No. No.
CPU Scheduling Algorithms 3-13
School of Engineering
Syllabus Contents
w.e.f. 2024-25
School of Engineering
Syllabus Contents
w.e.f. 2024-25
School of Engineering
Syllabus Contents
w.e.f. 2024-25
( )
Average Waiting Time (Avg. W.T.) = = = = 3.8
( )
Average Turn Around Time (Avg. T.A.T.) = = = = 6.8
Code:
#include<stdio.h>
int main()
{
int bt[10]={0},at[10]={0},tat[10]={0},wt[10]={0},ct[10]={0};
int n;
float totalTAT=0,totalWT=0;
printf("Enter the no of processes\n");
scanf("%d",&n);
printf("\nEnter arrival time and burst time for each process:- \n");
for(int i=0;i<n;i++)
{
printf("Arriva time of process[%d]\t",i+1);
scanf("%d",&at[i]);
printf("Burst time of process[%d]\t",i+1);
scanf("%d",&bt[i]);
printf("\n");
}
//calculating completion time
int sum=at[0];
for(int j=0;j<n;j++)
{
sum=sum+bt[j];
ct[j]=sum;
}
//calculate turnaround time and waiting time
for(int k=0;k<n;k++)
{
tat[k]=ct[k]-at[k];
totalTAT=totalTAT+tat[k];
}
for(int k=0;k<n;k++)
{
wt[k]=tat[k]-bt[k];
totalWT=totalWT+wt[k];
}
printf("p#\t AT\t BT\t CT\t TAT\t WT\t\n\n");
for(int i=0;i<n;i++)
{
School of Engineering
Syllabus Contents
w.e.f. 2024-25
Output:
School of Engineering
Syllabus Contents
w.e.f. 2024-25
Real-World Example:
In sports teams like those found in the NFL, NBA, MLB or NHL, a round-robin is a method of
scheduling where each participant plays against all other participants in the event. Like in Cricket
World Cups where a team plays against all other teams in the League Stage.
School of Engineering
Syllabus Contents
w.e.f. 2024-25
( )
Average Waiting Time (Avg. W.T.) = = = = 11.833
( )
Average Turn Around Time (Avg. T.A.T.) = = = = 15.833
School of Engineering
Syllabus Contents
w.e.f. 2024-25
Code:
#include<stdio.h>
int main()
{
int count, j, n, time, remain, flag=0,timeQuantum;
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 :\n",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&timeQuantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=timeQuantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=timeQuantum;
time+=timeQuantum;
}
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;
}
Kadaganchi, Aland Road, Kalaburagi 585 367, Karnataka, India.
Website: www.cuk.ac.in
Central University of Karnataka
Dept.: ECE Dept.
School of Engineering
Syllabus Contents
w.e.f. 2024-25
Output:
School of Engineering
Syllabus Contents
w.e.f. 2024-25
Real-World Example:
In a hospital, patients are attended to based on the severity of their conditions, not just their arrival time.
Scheduling our day-to-day tasks based on priority (or importance).
Step 1: At time=0, Process P1 and P2 arrive. P1 has higher priority than P2. The execution begins with
process P1, which has burst time 4. At time=1, no new process arrive. Execution continues with P1.
Step 2: At time 2, no new process arrives, so you can continue with P1. P2 is in the waiting queue.
Step 3: At time 3, no new process arrives so you can continue with P1. P2 process still in the waiting.
Step 4: At time 4, P1 has finished its execution. P2 starts execution.
Step 5: At time= 5, no new process arrives, so we continue with P2.
School of Engineering
Syllabus Contents
w.e.f. 2024-25
Step 6: At time=6, P3 arrives. P3 is at a higher priority (1) compared to P2 having priority (2). P2 is
preempted, and P3 begins its execution. Like this continue till all the processes are executed.
Turnaround Waiting
Process Burst Time Priority
Time Time
A 4 1 4 0
C 7 1 11 4
B 3 2 14 11
E 2 2 16 14
D 4 3 20 16
( )
Average Waiting Time (Avg. W.T.) = = = = 9.00
( )
Average Turn Around Time (Avg. T.A.T.) = = = = 13.00
Code:
#include<stdio.h>
int main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
printf("Enter the number of processes --- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i] = i;
printf("Enter the Burst Time & Priority of Process %d --- \n",i);
scanf("%d %d",&bt[i], &pri[i]);
}
for(i=0;i<n;i++)
{
for(k=i+1;k<n;k++)
{
if(pri[i] > pri[k])
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
School of Engineering
Syllabus Contents
w.e.f. 2024-25
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;
}
}
}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
School of Engineering
Syllabus Contents
w.e.f. 2024-25
FreeRTOS is a real-time
operating system (RTOS) kernel
for embedded devices. It is open
source and is distributed under
the MIT License. FreeRTOS is
designed to make it easier to
program, deploy, secure, connect,
and manage small, low-power
edge devices.
School of Engineering
Syllabus Contents
w.e.f. 2024-25
School of Engineering
Syllabus Contents
w.e.f. 2024-25
School of Engineering
Syllabus Contents
w.e.f. 2024-25
Fixed partitioning is a memory management technique that divides memory into fixed, non-
overlapping sizes. It's the oldest and simplest technique for loading multiple processes into main
memory. In fixed partitioning, the partitions are created in the main memory before execution or
during the system configuration. The number of partitions in RAM is fixed, but the sizes of the
partitions can vary.
In fixed partitioning concept, RAM is divided into set of fixed partition of equal Size
Programs having the Size Less than the partition size are loaded into Memory
Programs Having Size more then the size of Partitions Size is rejected
The program having the size less than the partition size will lead to internal Fragmentation.
If all partitions are allocated and a new program is to be loaded, the program that lead to
Maximum Internal Fragmentation can be replaced.
School of Engineering
Syllabus Contents
w.e.f. 2024-25
Internal fragmentation: Internal fragmentation occurs when memory blocks are allocated to the
process more than their requested size. Due to this some unused space is left over and creating an
internal fragmentation problem. Example: Suppose there is a fixed partitioning used for memory
allocation and the different sizes of blocks 3MB, 6MB, and 7MB space in memory. Now a new
process p4 of size 2MB comes and demands a block of memory. It gets a memory block of 3MB but
1MB block of memory is a waste, and it can not be allocated to other processes too. This is called
internal fragmentation.
External fragmentation: In External Fragmentation, we have a free memory block, but we can not
assign it to a process because blocks are not contiguous. Example: Suppose (consider the above
example) three processes p1, p2, and p3 come with sizes 2MB, 4MB, and 7MB respectively. Now
they get memory blocks of size 3MB, 6MB, and 7MB allocated respectively. After allocating the
process p1 process and the p2 process left 1MB and 2MB. Suppose a new process p4 comes and
demands a 3MB block of memory, which is available, but we can not assign it because free memory
space is not contiguous. This is called external fragmentation.
Real-World Example:
Download software, transfer data, Google Chrome, MS Excel, Firefox browser, and many other apps
are instances of multiprogramming operating systems.
Characteristics of MFT:
Simple
A fixed number of jobs at a time and this is
known
No frequent switching of boundary registers
Fences are fixed
The OS is catered for a number of PCB's
The strategy can be used along with
swapping.
School of Engineering
Syllabus Contents
w.e.f. 2024-25
Code:
#include<stdio.h>
Int main()
{
int i,p=0;
int ms, bs, nob, ef,n, mp[10],tif=0;
printf("Enter the total memory available (in Bytes) -- ");
scanf("%d",&ms);
printf("Enter the block size (in Bytes) -- ");
scanf("%d", &bs);
nob=ms/bs;
ef=ms - nob*bs;
printf("\nEnter the number of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter memory required for process %d (in Bytes)-- ",i+1);
scanf("%d",&mp[i]);
}
printf("\nNo. of Blocks available in memory -- %d",nob);
printf("\n\nPROCESS\tMEMORY REQUIRED\tALLOCATED INTERNAL
FRAGMENTATION");
for(i=0;i<n && p<nob;i++)
{
printf("\n %d\t\t%d",i+1,mp[i]);
if(mp[i] > bs)
printf("\tNO\t\t---");
else
{
printf("\tYES\t\t%d",bs-mp[i]);
tif = tif + bs-mp[i];
p++;
}
}
if(i<n)
printf("\nMemory is Full, Remaining Processes cannot be accomodated");
printf("\n\nTotal Internal Fragmentation is %d",tif);
printf("\nTotal External Fragmentation is %d",ef);
}
Explanation:
The program calculates the number of blocks available (nob) based on the total memory and block
size. It also calculates the external fragmentation (ef) by subtracting the total memory occupied by
allocated blocks from the total memory available.
Then, the program iterates through each process, asking for the memory required for each. For each
process, it checks if the memory requirement is greater than the block size. If it is, it prints "NO"
Kadaganchi, Aland Road, Kalaburagi 585 367, Karnataka, India.
Website: www.cuk.ac.in
Central University of Karnataka
Dept.: ECE Dept.
School of Engineering
Syllabus Contents
w.e.f. 2024-25
indicating that the process cannot be accommodated in a single block. If the memory requirement is
less than or equal to the block size, it prints "YES" and the allocated internal fragmentation, which is
the difference between the block size and the memory requirement.
The program keeps track of the total internal fragmentation (tif) by adding up the internal
fragmentation for each successfully allocated process. The variable p is used to count the number of
processes that have been successfully allocated so far.
Finally, the program prints the total internal and external fragmentation. If there are still processes
remaining after all the available blocks are filled, it prints a message stating that the memory is full,
and the remaining processes cannot be accommodated.
Output:
School of Engineering
Syllabus Contents
w.e.f. 2024-25
Characteristics of MVT:
It divides memory into fixed-size
partitions, and each partition is
assigned to a process.
It is a memory management
technique in which each job
receives the amount of memory it
needs.
It uses variable fences, while MFT
uses fixed fences.
It significantly reduces internal
fragmentation and can correct
external fragmentation.
Code:
#include<stdio.h>
int main()
{
char ch = 'y';
int ms,mp[10],i, temp,n=0;
printf("\nEnter the total memory available (in Bytes)-- ");
scanf("%d",&ms);
temp=ms;
for(i=0;ch=='y';i++,n++)
{
printf("\nEnter memory required for process %d (in Bytes) -- ",i+1);
scanf("%d",&mp[i]);
if(mp[i]<=temp)
{
printf("\nMemory is allocated for Process %d ",i+1);
temp = temp - mp[i];
}
else
{
printf("\nMemory is Full");
break;
}
printf("\nDo you want to continue(y/n) -- ");
scanf(" %c", &ch);
}
Kadaganchi, Aland Road, Kalaburagi 585 367, Karnataka, India.
Website: www.cuk.ac.in
Central University of Karnataka
Dept.: ECE Dept.
School of Engineering
Syllabus Contents
w.e.f. 2024-25
printf("\n\nTotal Memory Available -- %d", ms);
printf("\n\n\tPROCESS\t\t MEMORY ALLOCATED ");
for(i=0;i<n;i++)
printf("\n \t%d\t\t%d",i+1,mp[i]);
printf("\n\nTotal Memory Allocated is %d",ms-temp);
printf("\nTotal External Fragmentation is %d",temp);
}
Output:
School of Engineering
Syllabus Contents
w.e.f. 2024-25
Advantages:
Improved System Stability
Better Resource Utilization
Easy Implementation (like Wait-For Graph)
Disadvantages:
Performance Overhead
Complex (some algorithms like Resource Allocation Graph, etc)
School of Engineering
Syllabus Contents
w.e.f. 2024-25
Types of Semaphore:-
Binary Semaphore: Here, there are only two values of Semaphore in Binary Semaphore Concept.
The two values are 1 and 0. 1 Process has capability to enter critical section area.
0 Process does not have the capability to enter the critical section area.
Counting Semaphore: Here, there are two sets of values of Semaphore in Counting Semaphore
Concept. The two types of values are values greater than and equal to one and other type is value
equal to zero. If the Value of Binary Semaphore is greater than or equal to 1, then the process has
the capability to enter the critical section area. If the value of Binary Semaphore is 0 then the
process does not have the capability to enter the critical section area.
Code:
#include<stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;
in = 0;
out = 0;
bufsize = 2;
while(choice !=3)
{
printf("\n1. Produce \t 2. Consume \t3. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
if((in+1)%bufsize==out)
printf("\nBuffer is Full");
else
{
printf("\nEnter the value: ");
Kadaganchi, Aland Road, Kalaburagi 585 367, Karnataka, India.
Website: www.cuk.ac.in
Central University of Karnataka
Dept.: ECE Dept.
School of Engineering
Syllabus Contents
w.e.f. 2024-25
scanf("%d", &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
}
break;
case 2:
if(in == out)
printf("\nBuffer is Empty");
else
{
consume = buffer[out];
printf("\nThe consumed value is %d", consume);
out = (out+1)%bufsize;
}
break;
}
}
}
Output:
School of Engineering
Syllabus Contents
w.e.f. 2024-25
To prevent the hold and wait condition, processes must be prevented from holding one or more
resources while simultaneously waiting for one or more others. One possibility for this is to require
that all processes request all resources at one time.
Banker’s Algorithm:-
1. Initialize the system
2. Define a request
3. Check if the request can be granted
4. Check if the system is in a safe state
5. Release the Resources
School of Engineering
Syllabus Contents
w.e.f. 2024-25
Code:
#include<stdio.h>
struct file
{
int all[10];
int max[10];
int need[10];
int flag;
};
void main()
{
struct file f[10];
int fl;
int i, j, k, p, b, n, r, g, cnt=0, id, newr;
int avail[10],seq[10];
printf("Enter number of processes -- ");
scanf("%d",&n);
printf("Enter number of resources -- ");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("Enter details for P%d",i);
printf("\nEnter allocation\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].all[j]);
printf("Enter Max\t\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].max[j]);
f[i].flag=0;
}
printf("\nEnter Available Resources\t -- \t");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
printf("\nEnter New Request Details -- ");
printf("\nEnter pid \t -- \t");
scanf("%d",&id);
printf("Enter Request for Resources \t -- \t");
for(i=0;i<r;i++)
{
scanf("%d",&newr);
f[id].all[i] += newr;
avail[i]=avail[i] - newr;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
Kadaganchi, Aland Road, Kalaburagi 585 367, Karnataka, India.
Website: www.cuk.ac.in
Central University of Karnataka
Dept.: ECE Dept.
School of Engineering
Syllabus Contents
w.e.f. 2024-25
{
f[i].need[j]=f[i].max[j]-f[i].all[j];
if(f[i].need[j]<0)
f[i].need[j]=0;
}
}
cnt=0;
fl=0;
while(cnt!=n)
{
g=0;
for(j=0;j<n;j++)
{
if(f[j].flag==0)
{
b=0;
for(p=0;p<r;p++)
{
if(avail[p]>=f[j].need[p])
b=b+1;
else
b=b-1;
}
if(b==r)
{
printf("\nP%d is visited",j);
seq[fl++]=j;
f[j].flag=1;
for(k=0;k<r;k++)
avail[k]=avail[k]+f[j].all[k];
cnt=cnt+1;
printf("(");
for(k=0;k<r;k++)
printf("%3d",avail[k]);
printf(")"); g=1;
}
}
}
if(g==0)
{
printf("\n REQUEST NOT GRANTED -- DEADLOCK OCCURRED");
printf("\n SYSTEM IS IN UNSAFE STATE");
goto y;
}
}
printf("\nSYSTEM IS IN SAFE STATE");
printf("\nThe Safe Sequence is -- (");
Kadaganchi, Aland Road, Kalaburagi 585 367, Karnataka, India.
Website: www.cuk.ac.in
Central University of Karnataka
Dept.: ECE Dept.
School of Engineering
Syllabus Contents
w.e.f. 2024-25
for(i=0;i<fl;i++)
printf("P%d ",seq[i]);
printf(")");
y: printf("\nProcess\t\tAllocation\t\tMax\t\t\tNeed\n");
for(i=0;i<n;i++)
{
printf("P%d\t",i);
for(j=0;j<r;j++)
printf("%6d",f[i].all[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].max[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].need[j]);
printf("\n");
}
}
Output:
School of Engineering
Syllabus Contents
w.e.f. 2024-25
10. Simulate the concept of Dining Philosophers Problem
The dining philosophers problem was introduced by Dijkstra. Five philosophers live in a house,
where a table is laid for them. The life of each philosopher consists principally of thinking and
eating, and through years of thought, all of the philosophers had agreed that the only food that
contributed to their thinking efforts was spaghetti. Due to a lack of manual skill, each
philosopher requires two forks to eat spaghetti.
The eating arrangements are simple: a round table on which is set a large serving bowl of
spaghetti, five plates, one for each philosopher, and five forks. A philosopher wishing to eat goes
to his or her assigned place at the table and, using the two forks on either side of the plate, takes
and eats some spaghetti.
The problem: devise a ritual (algorithm) that will allow the philosophers to eat. The algorithm
must satisfy mutual exclusion (no two philosophers can use the same fork at the same time)
while avoiding deadlock and starvation. This problem may not seem important or relevant in
itself. However, it does illustrate basic problems in deadlock and starvation. Furthermore,
attempts to develop solutions reveal many of the difficulties in concurrent programming. In
addition, the dining philosophers problem can be seen as representative of problems dealing with
the coordination of shared resources, which may occur when an application includes concurrent
threads of execution. Accordingly, this problem is a standard test case for evaluating approaches
to synchronization.
School of Engineering
Syllabus Contents
w.e.f. 2024-25
Code:
int tph, philname[20], status[20], howhung, hu[20], cho;
main()
{
int i;
printf("\n\nDINING PHILOSOPHER PROBLEM");
printf("\nEnter the total no. of philosophers: ");
scanf("%d",&tph);
for(i=0;i<tph;i++)
{
philname[i] = (i+1);
status[i]=1;
}
printf("How many are hungry : ");
scanf("%d", &howhung);
if(howhung==tph)
{
printf("\nAll are hungry..\nDead lock stage will occur");
printf("\nExiting..");
}
else
{
for(i=0;i<howhung;i++)
{
printf("Enter philosopher %d position: ",(i+1)); scanf("%d", &hu[i]);
status[hu[i]]=2;
}
do
{
printf("1.One can eat at a time\t2.Two can eat at a time\t3.Exit\nEnter your choice:");
scanf("%d", &cho);
switch(cho)
{
case 1: one();
break;
case 2: two();
break;
case 3: exit(0);
default:
printf("\nInvalid option..");
}
}
while(1);
}
}
School of Engineering
Syllabus Contents
w.e.f. 2024-25
one()
{
int pos=0, x, i;
printf("\nAllow one philosopher to eat at any time\n");
for(i=0;i<howhung; i++, pos++)
{
printf("\nP %d is granted to eat", philname[hu[pos]]);
for(x=pos;x<howhung;x++)
printf("\nP %d is waiting", philname[hu[x]]);
}
}
two()
{
int i, j, s=0, t, r, x;
printf("\n Allow two philosophers to eat at same time\n");
for(i=0;i<howhung;i++)
{
for(j=i+1;j<howhung;j++)
{
if(abs(hu[i]-hu[j])>=1&& abs(hu[i]-hu[j])!=4)
{
printf("\n\ncombination %d \n", (s+1));
t=hu[i];
r=hu[j];
s++;
printf("\nP %d and P %d are granted to eat", philname[hu[i]],philname[hu[j]]);
for(x=0;x<howhung;x++)
{
if((hu[x]!=t)&&(hu[x]!=r))
printf("\nP %d is waiting", philname[hu[x]]);
}
}
}
}
}
School of Engineering
Syllabus Contents
w.e.f. 2024-25
Output: