Vaibhav Thakral UE223111
Experiment No. 8
Aim : Write a MATLAB CODE which show operating system process states.
Introduction:
Process scheduling is a fundamental function of any modern operating system, enabling efficient and fair use
of CPU resources among processes. The First-Come, First-Served (FCFS) scheduling algorithm is one of
the simplest scheduling techniques, where the process that arrives first is executed first. This experiment
demonstrates the working of FCFS using a visual simulation in MATLAB, where processes pass through
different states like New, Ready, Running, and Terminated.
Objective :
To simulate the First-Come, First-Served (FCFS) scheduling algorithm using MATLAB and visualize the
transition of processes through various operating system states.
Theory :
Process States
A process in an operating system can be in one of the following states:
• New: Process is being created.
• Ready: Process is ready to run but waiting for CPU.
• Running: Process is currently being executed on CPU.
• Waiting: Process is waiting for some event (I/O etc.) — not used in FCFS.
• Terminated: Process has completed execution.
First-Come, First-Served (FCFS) Scheduling
• Non-preemptive scheduling algorithm.
• Processes are scheduled in the order of arrival.
• Once a process starts execution, it runs until completion.
• Simple to implement but may lead to convoy effect and poor average waiting time.
Vaibhav Thakral UE223111
Code :
clc;
clear;
states = {'New', 'Ready', 'Running', 'Terminated'};
positions = [1 4; 3 4; 5 3; 3 1]; % x-y positions for each state
% Setup figure
figure('Color', 'w');
axis off;
hold on;
xlim([0 6]);
ylim([0 5]);
for i = 1:length(states)
rectangle('Position', [positions(i,1)-0.5, positions(i,2)-0.5, 1, 1], ...
'EdgeColor', 'k', 'LineWidth', 2);
text(positions(i,1), positions(i,2), states{i}, ...
'HorizontalAlignment', 'center', 'FontSize', 12, 'FontWeight', 'bold');
end
numProcesses = 5;
colors = lines(numProcesses);
arrivalTimes = [0, 1, 2, 3, 4]; % Each process arrives at different times
burstTimes = randi([2, 4], 1, numProcesses); % Random burst times
currentTime = 0;
processQueue = []; % FIFO queue
processStates = repmat("New", numProcesses, 1);
processDots = gobjects(numProcesses, 1);
for i = 1:numProcesses
processDots(i) = plot(positions(1,1), positions(1,2), 'o', ...
'MarkerSize', 10, 'MarkerFaceColor', colors(i,:), ...
'MarkerEdgeColor', 'k');
end
getPosition = @(stateName) positions(strcmp(states, stateName), :);
Vaibhav Thakral UE223111
while any(processStates ~= "Terminated")
pause(0.5);
for p = 1:numProcesses
if processStates(p) == "New" && arrivalTimes(p) <= currentTime
processStates(p) = "Ready";
processQueue(end+1) = p;
% Animate to Ready
currentPos = getPosition("New");
newPos = getPosition("Ready");
for step = linspace(0,1,10)
interpolated = (1-step)*currentPos + step*newPos;
set(processDots(p), 'XData', interpolated(1), 'YData', interpolated(2));
drawnow;
end
end
end
% Run the front of the queue if CPU is free
if ~isempty(processQueue)
currentP = processQueue(1);
processQueue(1) = []; % Remove from queue
processStates(currentP) = "Running";
% Animate to Running
currentPos = getPosition("Ready");
newPos = getPosition("Running");
for step = linspace(0,1,10)
interpolated = (1-step)*currentPos + step*newPos;
set(processDots(currentP), 'XData', interpolated(1), 'YData', interpolated(2));
drawnow;
end
% Simulate burst time
for bt = 1:burstTimes(currentP)
pause(0.5);
currentTime = currentTime + 1;
Vaibhav Thakral UE223111
% Check for newly arrived processes during burst
for p = 1:numProcesses
if processStates(p) == "New" && arrivalTimes(p) <= currentTime
processStates(p) = "Ready";
processQueue(end+1) = p;
% Animate to Ready
currentPos = getPosition("New");
newPos = getPosition("Ready");
for step = linspace(0,1,10)
interpolated = (1-step)*currentPos + step*newPos;
set(processDots(p), 'XData', interpolated(1), 'YData', interpolated(2));
drawnow;
end
end
end
end
% Move to Terminated
processStates(currentP) = "Terminated";
currentPos = getPosition("Running");
newPos = getPosition("Terminated");
for step = linspace(0,1,10)
interpolated = (1-step)*currentPos + step*newPos;
set(processDots(currentP), 'XData', interpolated(1), 'YData', interpolated(2));
drawnow;
end
else
% No process running, increment time
currentTime = currentTime + 1;
end
end
title('FCFS Scheduling Simulation Complete');
Vaibhav Thakral UE223111
Output :
Conclusion :
The simulation successfully demonstrates the working of the FCFS scheduling algorithm. It clearly shows
how processes are handled in the order of arrival without preemption. Although FCFS is simple, it is not
always efficient due to longer waiting times for later processes.