RTOS Lab Workbook
RTOS
Lab Work Book
Dr. Anu Tonk
School of Engineering & Technology
Department
of
Multidisciplinary Engineering
Name Sparsh Agrawal
Roll No. 21ECU024
Branch EECE
Section A
The NorthCap University
Gurugram, Haryana
RTOS Lab Workbook
INDEX
[Link] Experiment Pag Date of Date of Mark CO Sign
e Experiment Submiss s Covered
No. ion
1.
2.
3.
4.
5.
6.
7.
RTOS Lab Workbook
Experiment No.- 1
OBJECTIVE : Introduction to FreeRTOS and setting up of different IDEs.
SOFTWARE : Arduino IDE, Visual Studio
INSTALLATION :
Arduino IDE & Free RTOS Library :-
Arduino IDE (Integrated Development Environment) is an open-source software platform
used for writing, compiling, and uploading code to Arduino boards. Arduino boards are
widely used microcontroller platforms that allow enthusiasts, hobbyists, and professionals to
create interactive electronic projects. The IDE provides a user-friendly interface for
programming Arduino boards, abstracting many complexities associated with embedded
systems development.
As for FreeRTOS, it is a popular real-time operating system kernel designed for embedded
systems. It provides multitasking support, task scheduling, and synchronization primitives,
making it suitable for applications that require real-time performance. Integrating FreeRTOS
with Arduino allows developers to create more complex and responsive applications on
Arduino boards.
Here's a brief guide on installing the FreeRTOS library on Arduino IDE:
- Open Arduino IDE :
Start by opening the Arduino IDE on your computer.
- Install Arduino SAMD Boards (if necessary) :
If you are using an Arduino board based on the SAMD architecture (e.g., Arduino
Zero, MKR series), you may need to install the SAMD board support package. Go to
"Tools" > "Board" > "Boards Manager," search for "Arduino SAMD Boards," and
install the latest version.
- Install FreeRTOS Library :
Go to "Sketch" > "Include Library" > "Manage Libraries" in the Arduino IDE.
In the Library Manager, type "FreeRTOS" in the search bar.
Look for the "FreeRTOS by Richard Barry" library and click "Install."
Configure FreeRTOS in Your Sketch:
RTOS Lab Workbook
After installing the library, you can include the FreeRTOS.h header file in your Arduino
sketch.
You can then define tasks and use FreeRTOS functions to manage task execution,
synchronization, and other real-time features.
Opening the Ardunio IDE -
Downloading the library –
Visual Studio & Free RTOS :-
RTOS Lab Workbook
Visual Studio is a powerful integrated development environment (IDE) developed by
Microsoft. It supports a wide range of programming languages and provides features such as
code editing, debugging, and project management. Visual Studio is widely used for
developing various types of applications, including desktop software, web applications,
mobile apps, and embedded systems.
To install the FreeRTOS library in Visual Studio for an embedded system project, you
typically need to follow these general steps:
- Create a New Project:
Open Visual Studio and create a new project for your embedded system. Select the
appropriate project template based on your target platform and programming language
(C or C++).
- Configure Your Project:
Set up the project settings, including the target architecture, compiler options, and any
other relevant configurations for your embedded system.
Download FreeRTOS Source Code:
- Add FreeRTOS Source Code to Your Project:
Extract the FreeRTOS source code to a directory on your computer.
In Visual Studio, add the FreeRTOS source code files to your project. This can be
done by right-clicking on the project in the Solution Explorer, selecting "Add," and
then choosing "Existing Item" to add the FreeRTOS source files.
- Configure FreeRTOS for Your Target Platform:
Modify the FreeRTOS configuration files to match the specifications of your
embedded system. This may include configuring the scheduler, memory allocation,
and other parameters in the FreeRTOSConfig.h file.
- Build and Debug:
Build your project to compile the FreeRTOS source code along with your application
code.
Debug your embedded system project using the debugging features provided by
Visual Studio.
- Verify and Test:
RTOS Lab Workbook
Test your application on the target hardware to ensure that FreeRTOS is functioning
correctly and meeting the real-time requirements of your system.
It's important to note that the specific steps and configurations can vary depending on
the target architecture, compiler, and toolchain you are using. Refer to the FreeRTOS
documentation and the documentation for your specific embedded platform for
detailed instructions and guidelines. Additionally, Visual Studio may have different
versions and support for various toolchains, so ensure compatibility with your
development environment.
Visual Studio & Free RTOS
RTOS Lab Workbook
Experiment No.- 2
OBJECTIVE : Create and delete a task in FreeRTOS and Arduino/Visual
Studio/Eclipse-based IDE.
SOFTWARE : Arduino IDE
CODE :
#include <Arduino_FreeRTOS.h> // include the free rtos library
#include "task.h" // include task.h(a library for handling tasks)
TaskHandle_t TaskHandle_2; // handler for Task2
void setup()
{
[Link](9600); // Enable serial communication.
pinMode(4, OUTPUT); // define LED1 pin as a digital output
pinMode(5, OUTPUT); // define LED2 pin as a digital output
//Create the first task at priority 1
// Name of task is "LED1"
// Stack size is set to 100
// we do not pass any value to Task1. Hence, third agument is NULL
// Set the priority of task to one
// Task1 handler is not used. Therefore set to Null
xTaskCreate(Task1, "LED1", 100, NULL, 1, NULL); // xtaskcreate is used to create a fresh
task
// Start FreeRTOS scheduler in Preemptive timing silicing mode
vTaskStartScheduler(); // starts real time kernel
}
void loop()
{
// Do nothing as schduler will allocated CPU to Task1 and Task2 automatically
}
/* Task1 with priority 1 */
void Task1(void* pvParameters) // void* means we are pointing to an unknown type of data
{
while(1)
{
[Link]("Task1 Running"); // print "Task1 Running" on Arduino Serial Monitor
digitalWrite(4, HIGH); // sets the digital pin 4 on
digitalWrite(5, LOW); // sets the digital pin 5 off
RTOS Lab Workbook
xTaskCreate(Task2, "LED2", 100, NULL, 2, &TaskHandle_2); // create task2 with priority
2
vTaskDelay( 100 / portTICK_PERIOD_MS ); // create delay between tasks, here wait for
one second
}
}
/* Task2 with priority 2 */
void Task2(void* pvParameters)
{
//digitalWrite(5, HIGH); // sets the digital pin 5 high
//digitalWrite(4, LOW); // sets the digital pin 4 low
[Link]("Task2 is runnig and about to delete itself");
vTaskDelete(TaskHandle_2); //Delete own task by passing NULL(TaskHandle_2 can also
be used)
}
OUTPUT :
RTOS Lab Workbook
Experiment No.- 3
OBJECTIVE : Creating C programs
SOFTWARE : Dev C++
CODE : (1) Calculator in C
#include <stdio.h>
int main() {
int num1, num2;
float answer;
char oper;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &oper);
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (oper == '+') {
answer = num1 + num2;
printf("%d + %d = %.2f", num1, num2, answer);
}
else if (oper == '-') {
answer = num1 - num2;
printf("%d - %d = %.2f", num1, num2, answer);
}
else if (oper == '*') {
answer = num1 * num2;
printf("%d * %d = %.2f", num1, num2, answer);
}
else if (oper == '/') {
if (num2 != 0) {
answer = (float)num1 / num2;
printf("%d / %d = %.2f", num1, num2, answer);
} else {
printf("Error! Division by zero."); return 1;
}
}else {
printf("Error! Invalid operator.");
return 1;
}return 0;
}
RTOS Lab Workbook
OUTPUT :
(2) Check whether the entered string is palindrome or not?
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = { "naman" };
int l = 0;
int h = strlen(str) - 1;
while (h > l) {
if (str[l++] != str[h--]) {
printf("%s is not a palindrome\n", str);
return 0;
}
}
printf("%s is a palindrome\n", str);
return 0;
}
RTOS Lab Workbook
OUTPUT :
(3) Find the largest number in a array.
#include <stdio.h>
int main()
{
int arr[] = {25, 11, 7, 75, 56};
int length = sizeof(arr)/sizeof(arr[0]);
int max = arr[0];
for (int i = 0; i < length; i++) {
if(arr[i] > max)
max = arr[i];
}
printf("Largest element present in given array: %d\n", max);
return 0;
}
OUTPUT :
RTOS Lab Workbook
(4) (A) Write a program to print fibbonaci series.
#include <stdio.h>
int main() {
int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
OUTPUT :
RTOS Lab Workbook
(4) (B) Write a program to check whether the year entered is leap or not.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
else {
printf("%d is not a leap year.", year);
}
return 0;
}
OUTPUT :
RTOS Lab Workbook
RTOS Lab Workbook
Experiment No.- 4
OBJECTIVE : Creating a program in C to verify the FCFS algorithm.
SOFTWARE : Dev C++
CODE :
#include<stdio.h>
void findWaitingTime(int processes[], int n, int bt[], int wt[])
{
wt[0] = 0;
for (int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}
void findTurnAroundTime( int processes[], int n,
int bt[], int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
void findavgTime( int processes[], int n, int bt[])
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt);
findTurnAroundTime(processes, n, bt, wt, tat);
printf("Processes Burst time Waiting time Turn around time\n");
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d ",(i+1));
printf(" %d ", bt[i] );
printf(" %d",wt[i] );
RTOS Lab Workbook
printf(" %d\n",tat[i] );
}
float s=(float)total_wt / (float)n;
float t=(float)total_tat / (float)n;
printf("Average waiting time = %f",s);
printf("\n");
printf("Average turn around time = %f ",t);
}
int main()
{
int processes[] = { 1, 2, 3, 4, 5, 6};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {3, 2, 1, 4, 5, 2};
findavgTime(processes, n, burst_time);
return 0;
}
OUTPUT :
RTOS Lab Workbook
Experiment No.- 5
OBJECTIVE : Create a FreeRTOS task to Blink LED in Arduino Uno IDE
SOFTWARE : Arduino IDE
CODE :
#include <Arduino_FreeRTOS.h>
void TaskBlink1( void *pvParameters );
void TaskBlink2( void *pvParameters );
void Taskprint( void *pvParameters );
void setup() {
[Link](9600);
xTaskCreate(TaskBlink1, "task1" , 128 , NULL, 1 , NULL );
xTaskCreate(TaskBlink2, "task2", 128 , NULL, 1 , NULL );
xTaskCreate(Taskprint, "task3", 128 , NULL, 1 , NULL );
vTaskStartScheduler();
void loop()
{
void TaskBlink1(void *pvParameters) {
pinMode(12, OUTPUT);
while(1)
[Link]("Task1");
RTOS Lab Workbook
digitalWrite(12, HIGH);
vTaskDelay( 200 / portTICK_PERIOD_MS );
digitalWrite(12, LOW);
vTaskDelay( 200 / portTICK_PERIOD_MS );
}
void TaskBlink2(void *pvParameters)
{
pinMode(7, OUTPUT);
while(1)
[Link]("Task2");
digitalWrite(7, HIGH);
vTaskDelay( 300 / portTICK_PERIOD_MS );
digitalWrite(7, LOW);
vTaskDelay( 300 / portTICK_PERIOD_MS );
}
}
void Taskprint(void *pvParameters) {
int counter = 0;
while(1)
{
counter++;
[Link](counter);
vTaskDelay(500 / portTICK_PERIOD_MS); }
}
RTOS Lab Workbook
OUTPUT :
RTOS Lab Workbook
Experiment No.- 6
OBJECTIVE : To Suspend and Resuma a task in FreeRTOS
SOFTWARE : Arduino IDE
CODE :
#include <Arduino_FreeRTOS.h>
TaskHandle_t Task1;
TaskHandle_t Task2;
TaskHandle_t Task3;
TaskHandle_t Task4;
void setup (){
[Link] (9600);
[Link](F(" IN Setup Function"));
xTaskCreate(MyTask1, "Task1", 100, NULL, 1, &Task1);
xTaskCreate(MyTask2, "Task2", 100, NULL, 2, &Task2);
xTaskCreate(MyTask3, "Task3", 100, NULL, 3, &Task3);
xTaskCreate(MyTask4, "Task4", 100, NULL, 4, &Task4);
}
void loop (){
[Link] ("loop Function");
delay (50);
}
static void MyTask1(void* pvParameters){
[Link](F("Task1 Resuming Task2"));
vTaskResume(Task2);
[Link](F("Task1 Resuming Task3"));
vTaskResume(Task3);
[Link](F("Task1 Resuming Task4"));
vTaskResume(Task4);
[Link](F("Task1 Deleting Itself"));
vTaskDelete(Task1);
RTOS Lab Workbook
static void MyTask2(void* pvParameters){
[Link](F("Task2, Deleting itself"));
vTaskDelete(NULL); //Delete own task by passing NULL(TaskHandle_2
can also be used)
}
static void MyTask3(void* pvParameters){
[Link](F("Task3, Deleting Itself"));
vTaskDelete(NULL); //Delete own task by passing NULL(TaskHandle_3
can also be used)
}
static void MyTask4(void* pvParameters){
[Link](F("Task4 Running, Suspending all tasks"));
vTaskSuspend(Task2); //Suspend Task2/3
vTaskSuspend(Task3);
vTaskSuspend(NULL); //Suspend Own Task
[Link](F("Back in Task4, Deleting Itself"));
vTaskDelete(Task4);
}
OUTPUT :
Experiment No.- 7
RTOS Lab Workbook
OBJECTIVE : To implement Round Robin scheduling in FreeRTOS.
SOFTWARE : Dev c++
CODE :
#include<stdio.h>
#include<limits.h>
#include<stdbool.h>
struct P{
int AT,BT,ST[20],WT,FT,TAT,pos;
};
int quant;
int main(){
int n,i,j;
// Taking Input
printf("Enter the no. of processes :");
scanf("%d",&n);
struct P p[n];
printf("Enter the quantum \n");
scanf("%d",&quant);
printf("Enter the process numbers \n");
for(i=0;i<n;i++)
scanf("%d",&(p[i].pos));
printf("Enter the Arrival time of processes \n");
for(i=0;i<n;i++)
scanf("%d",&(p[i].AT));
printf("Enter the Burst time of processes \n");
for(i=0;i<n;i++)
scanf("%d",&(p[i].BT));
// Declaring variables
int c=n,s[n][20];
float time=0,mini=INT_MAX,b[n],a[n];
// Initializing burst and arrival time arrays
int index=-1;
for(i=0;i<n;i++){
b[i]=p[i].BT;
a[i]=p[i].AT;
for(j=0;j<20;j++){
RTOS Lab Workbook
s[i][j]=-1;
}
}
int tot_wt,tot_tat;
tot_wt=0;
tot_tat=0;
bool flag=false;
while(c!=0){
mini=INT_MAX;
flag=false;
for(i=0;i<n;i++){
float p=time+0.1;
if(a[i]<=p && mini>a[i] && b[i]>0){
index=i;
mini=a[i];
flag=true;
}
}
// if at =1 then loop gets out hence set flag to false
if(!flag){
time++;
continue;
}
//calculating start time
j=0;
while(s[index][j]!=-1){
j++;
}
if(s[index][j]==-1){
s[index][j]=time;
p[index].ST[j]=time;
}
if(b[index]<=quant){
time+=b[index];
b[index]=0;
}
else{
time+=quant;
b[index]-=quant;
RTOS Lab Workbook
}
if(b[index]>0){
a[index]=time+0.1;
}
// calculating arrival,burst,final times
if(b[index]==0){
c--;
p[index].FT=time;
p[index].WT=p[index].FT-p[index].AT-p[index].BT;
tot_wt+=p[index].WT;
p[index].TAT=p[index].BT+p[index].WT;
tot_tat+=p[index].TAT;
}
} // end of while loop
// Printing output
printf("Process number ");
printf("Arrival time ");
printf("Burst time ");
printf("\tStart time");
j=0;
while(j!=10){
j+=1;
printf(" ");
}
printf("\t\tFinal time");
printf("\tWait Time ");
printf("\tTurnAround Time \n");
for(i=0;i<n;i++){
printf("%d \t\t",p[i].pos);
printf("%d \t\t",p[i].AT);
printf("%d \t",p[i].BT);
j=0;
int v=0;
while(s[i][j]!=-1){
printf("%d ",p[i].ST[j]);
j++;
v+=3;
}
while(v!=40){
RTOS Lab Workbook
printf(" ");
v+=1;
}
printf("%d \t\t",p[i].FT);
printf("%d \t\t",p[i].WT);
printf("%d \n",p[i].TAT);
}
//Calculating average wait time and turnaround time
double avg_wt,avg_tat;
avg_wt=tot_wt/(float)n;
avg_tat=tot_tat/(float)n;
//Printing average wait time and turnaround time
printf("The average wait time is : %lf\n",avg_wt);
printf("The average TurnAround time is : %lf\n",avg_tat);
return 0;
}
OUTPUT :
RTOS Lab Workbook
Experiment No.- 8
OBJECTIVE : To implement Priority scheduling in FreeRTOS.
SOFTWARE : Dev c++
CODE :
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct {
int pid;
int arrival_time;
int burst_time;
int priority;
int remaining_time;
int waiting_time;
int turnaround_time;
} Process;
void calculate_times(Process *processes, int n) {
int current_time = 0;
int total_waiting_time = 0;
int total_turnaround_time = 0;
int completed = 0;
int min_priority = INT_MAX;
int selected_process = -1;
while (completed < n) {
min_priority = INT_MAX;
selected_process = -1;
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time &&
processes[i].remaining_time > 0) {
if (processes[i].priority < min_priority) {
RTOS Lab Workbook
min_priority = processes[i].priority;
selected_process = i;
}
}
}
if (selected_process == -1) {
current_time++;
continue;
}
processes[selected_process].remaining_time--;
if (processes[selected_process].remaining_time == 0) {
completed++;
processes[selected_process].turnaround_time = current_time + 1 -
processes[selected_process].arrival_time;
processes[selected_process].waiting_time =
processes[selected_process].turnaround_time -
processes[selected_process].burst_time;
total_waiting_time += processes[selected_process].waiting_time;
total_turnaround_time += processes[selected_process].turnaround_time;
}
current_time++;
}
// Calculate average waiting time and average turnaround time
float avg_waiting_time = (float)total_waiting_time / n;
float avg_turnaround_time = (float)total_turnaround_time / n;
// Print results
printf("PID\tArrival Time\tBurst Time\tPriority\tWaiting Time\tTurnaround
Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].pid,
processes[i].arrival_time,
processes[i].burst_time, processes[i].priority,
RTOS Lab Workbook
processes[i].waiting_time,
processes[i].turnaround_time);
}
printf("Average Waiting Time: %.2f\n", avg_waiting_time);
printf("Average Turnaround Time: %.2f\n", avg_turnaround_time);
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
Process *processes = (Process *)malloc(n * sizeof(Process));
printf("Enter PID, Arrival Time, Burst Time, and Priority for each process:\
n");
for (int i = 0; i < n; i++) {
printf("Process %d:\n", i + 1);
printf("PID: ");
scanf("%d", &processes[i].pid);
printf("Arrival Time: ");
scanf("%d", &processes[i].arrival_time);
printf("Burst Time: ");
scanf("%d", &processes[i].burst_time);
printf("Priority: ");
scanf("%d", &processes[i].priority);
processes[i].remaining_time = processes[i].burst_time;
}
calculate_times(processes, n);
free(processes);
return 0;
}
RTOS Lab Workbook
OUTPUT :
RTOS Lab Workbook
Experiment No.- 9
OBJECTIVE : To implement SJF scheduling in FreeRTOS.
SOFTWARE : Dev c++
CODE :
#include<stdio.h>
#include<string.h>
int main()
{
int bt[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
double awt,ata;
char pn[10][10],t[10];
//clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter process name, arrival time& burst time:");
scanf("%s%d%d",pn[i],&at[i],&bt[i]);
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(bt[i]<bt[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
RTOS Lab Workbook
}
}
for(i=0; i<n; i++)
{
if(i==0)
st[i]=at[i];
else
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+bt[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
}
awt=(double)totwt/n;
ata=(double)totta/n;
printf("\nProcessname\tarrivaltime\tbursttime\twaitingtime\
tturnaroundtime");
for(i=0; i<n; i++)
{
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],bt[i],wt[i],ta[i]);
}
printf("\nAverage waiting time: %f",awt);
printf("\nAverage turnaroundtime: %f",ata);
return 0;
}
OUTPUT :