0% found this document useful (0 votes)
48 views24 pages

NEWAthar Experiment ALL Rtos

The document describes a lab workbook for experiments with the Real Time Operating System FreeRTOS. It includes instructions for installing FreeRTOS and various IDEs, as well as examples of creating and managing tasks in FreeRTOS on development boards like Arduino.

Uploaded by

athulya21ecu008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views24 pages

NEWAthar Experiment ALL Rtos

The document describes a lab workbook for experiments with the Real Time Operating System FreeRTOS. It includes instructions for installing FreeRTOS and various IDEs, as well as examples of creating and managing tasks in FreeRTOS on development boards like Arduino.

Uploaded by

athulya21ecu008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

RTOS Lab Workbook

Real Time Operating System(RTOS)


Lab Work Book
Dr. Anu Tonk

School of Engineering & Technology

Department
of
Multidisciplinary Engineering

Name Athar Mushtaque

Roll No. 21ECU012

Branch EECE

Section A

The NorthCap University


Gurugram, Haryana
INDEX Student Name: Athulya Gopal Roll No: 21ECU008

S. Name of Experiment Date of Total Faculty’s


No. Performance Marks Signature
(In the sequence of performance) with Date
(10)
1. Installing Arduino IDE and installing free 2/02/2024
RTOS library

2. Create and delete a task in FreeRTOS


and Arduino/Visual Studio/Eclipse-
based IDE.

3.

4.

5.

6.

7.

8.

9.

10.

11.
Experiment No.- 1

Aim: Introduction to Free RTOS and setting up of different


IDEs

Theory/Commands/Circuit diagram:
Step 1 - Installation

Requirements:
1. Development board (e.g., STM32, Arduino, Raspberry Pi, etc.).
2. Computer with internet access.
3. Appropriate development environment (e.g., Eclipse, Visual Studio Code, etc.).
4. USB cable for programming the development board.
Steps:
1. Download Free RTOS:
- Visit the official Free RTOS website: [https://www.freertos.org/]
(https://www.freertos.org/)
- Navigate to the "Quick Start Guide" or "Downloads" section.
- Download the Free RTOS source code or pre-built binaries suitable for your development
board.

2. Extract the Free RTOS files:


- If you downloaded the source code, extract the files to a suitable location on your
computer.

3. Set up the Development Environment:


- Install the required toolchain and development environment based on your development
board and Free RTOS port.
- Refer to the Free RTOS documentation for specific instructions on setting up the
development environment.

4. Configure Free RTOS:


- Navigate to the Free RTOS source code directory.
- Modify the configuration files according to your requirements (e.g., `FreeRTOSConfig.h`).

5. Run a Simple Example:


- Create a new project or use an existing example provided by Free RTOS.
- Implement a simple Free RTOS task for example program.
- Build and flash the program onto the development board.

6. Documentation and Feedback:


- Document the steps taken, and any issues encountered during the installation process.
- Provide feedback to the Free RTOS community if necessary.

Conclusion:
The experiment aims to successfully download, install, and run FreeRTOS on a development
board. Through this process, you gain hands-on experience with real-time operating systems
and can explore building more complex applications using FreeRTOS.
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()
{
Serial.begin(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)
{
Serial.println("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
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
Serial.println("Task2 is runnig and about to delete itself");
vTaskDelete(TaskHandle_2); //Delete own task by passing NULL(TaskHandle_2 can also
be used)
}
OUTPUT :
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;
}
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;
}
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;
}
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] );
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 :
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() {

Serial.begin(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)

Serial.println("Task1");

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)

Serial.println("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++;

Serial.println(counter);

vTaskDelay(500 / portTICK_PERIOD_MS); }

}
OUTPUT :
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 (){


Serial.begin (9600);
Serial.println(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 (){


Serial.println ("loop Function");
delay (50);
}

static void MyTask1(void* pvParameters){


Serial.println(F("Task1 Resuming Task2"));
vTaskResume(Task2);
Serial.println(F("Task1 Resuming Task3"));
vTaskResume(Task3);
Serial.println(F("Task1 Resuming Task4"));
vTaskResume(Task4);
Serial.println(F("Task1 Deleting Itself"));
vTaskDelete(Task1);
}

static void MyTask2(void* pvParameters){


Serial.println(F("Task2, Deleting itself"));
vTaskDelete(NULL); //Delete own task by passing NULL(TaskHandle_2 can also be
used)
}

static void MyTask3(void* pvParameters){


Serial.println(F("Task3, Deleting Itself"));
vTaskDelete(NULL); //Delete own task by passing NULL(TaskHandle_3 can also be
used)
}

static void MyTask4(void* pvParameters){


Serial.println(F("Task4 Running, Suspending all tasks"));
vTaskSuspend(Task2); //Suspend Task2/3
vTaskSuspend(Task3);
vTaskSuspend(NULL); //Suspend Own Task
Serial.println(F("Back in Task4, Deleting Itself"));
vTaskDelete(Task4);
}

OUTPUT :
Experiment No.- 7

OBJECTIVE : Creating a program for Shortest Job First Scheduling

SOFTWARE : Online C Compiler


THEORY:
The shortest job first (SJF) or shortest job next, is a scheduling policy that
selects the waiting process with the smallest execution time to execute next.
SJN, also known as Shortest Job Next (SJN), can be preemptive or non-
preemptive.
Characteristics of SJF Scheduling:
 Shortest Job first has the advantage of having a minimum average waiting
time among all scheduling algorithms.
 It may cause starvation if shorter processes keep coming. This problem
can be solved using the concept of ageing.
 It is practically infeasible as Operating System may not know burst times
and therefore may not sort them. While it is not possible to predict
execution time, several methods can be used to estimate the execution
time for a job, such as a weighted average of previous execution times.
 SJF can be used in specialized environments where accurate estimates of
running time are available.
Algorithm:
 Sort all the processes according to the arrival time.
 Then select that process that has minimum arrival time and minimum
Burst time.
 After completion of the process make a pool of processes that arrives
afterward till the completion of the previous process and select that
process among the pool which is having minimum Burst time.

CODE:
#include <stdio.h>
int main(){
int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;
printf("Enter number of process: ");
scanf("%d", &n);
printf("Enter Burst Time:\n");
for (i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;}
for (i = 0; i < n; i++) {
index = i;
for (j = i + 1; j < n; j++)
if (A[j][1] < A[index][1])
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;}
A[0][2] = 0;
for (i = 1; i < n; i++) {
A[i][2] = 0;
for (j = 0; j < i; j++)
A[i][2] += A[j][1];
total += A[i][2];}
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");
for (i = 0; i < n; i++) {
A[i][3] = A[i][1] + A[i][2];
total += A[i][3];
printf("P%d %d %d %d\n", A[i][0],
A[i][1], A[i][2], A[i][3]); }
avg_tat = (float)total / n;
printf("Average Waiting Time= %f", avg_wt);
printf("\nAverage Turnaround Time= %f", avg_tat);
}
OUTPUT :
Experiment No.- 8

OBJECTIVE : Creating a program for Priority Scheduling

SOFTWARE : Online C Compiler

THEORY:
Priority scheduling is one of the most common scheduling algorithms in batch
systems. Each process is assigned a priority. The process with the highest
priority is to be executed first and so on. Processes with the same priority are
executed on a first-come first served basis. Priority can be decided based on
memory requirements, time requirements or any other resource requirement.
Also priority can be decided on the ratio of average I/O to average CPU burst
time.
Implementation:
1- First input the processes with their burst time and priority.
2- Sort the processes, burst time and priority according to the priority.
3- Now simply apply FCFS algorithm.

CODE :
#include <bits/stdc++.h>
using namespace std;
struct Process {
int pid;
int bt;
int priority;
};
bool comparison(Process a, Process b)
{
return (a.priority > b.priority);
}
void findWaitingTime(Process proc[], int n, int wt[])
{
wt[0] = 0;
for (int i = 1; i < n; i++)
wt[i] = proc[i - 1].bt + wt[i - 1];
}

void findTurnAroundTime(Process proc[], int n, int wt[],


int tat[])
{
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}
void findavgTime(Process proc[], int n)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(proc, n, wt);
findTurnAroundTime(proc, n, wt, tat);
cout << "\nProcesses "
<< " 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];
cout << " " << proc[i].pid << "\t\t" << proc[i].bt
<< "\t " << wt[i] << "\t\t " << tat[i]
<< endl;
}
cout << "\nAverage waiting time = "
<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}
void priorityScheduling(Process proc[], int n)
{
sort(proc, proc + n, comparison);
cout << "Order in which processes gets executed \n";
for (int i = 0; i < n; i++)
cout << proc[i].pid << " ";
findavgTime(proc, n);
}
int main()
{
Process proc[]
= { { 1, 10, 2 }, { 2, 5, 0 }, { 3, 8, 1 } };
int n = sizeof proc / sizeof proc[0];
priorityScheduling(proc, n);
return 0; }
OUTPUT :
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);
}
}
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 :

You might also like