OS 1 AND 2
OS 1 AND 2
Description:
First in, first out (FIFO), also known as first come, first served (FCFS), is the simplest
scheduling algorithm. FIFO simply queues processes in the order that they arrive in the ready
queue. In this, the process that comes first will be executed first and next process starts only
after the previous gets fully executed. We are given with the n number of processes i.e. P1,
P2, P3,.......,Pn and their corresponding burst times. The task is to find the average waiting
time and average turnaround time using FCFS CPU Scheduling algorithm. Here we are
considering that arrival time for all processes is 0.
Turnaround Time is the time interval between the submission of a process and its
completion.
Turnaround Time = completion of a process – submission of a process
Waiting Time is the difference between turnaround time and burst time
Waiting Time = turnaround time – burst time
Algorithm:
1. Start
2. Declare the following array to maximum size 15.
a. WT[ MAX ] to store waiting time of each process.
b. BT[ MAX ] to store Burst time of each process.
c. TT [ MAX ] to store turnaround time of each process.
3. Read the number of processes.
4. Read the Burst time for all the processes.
5. Calculate the waiting time of each process
a. WT [ i ] = WT [ i-1] + BT [i-1]
6. Calculate the Turnaround time of each process
a. TT [ i ] = WT [ i ] + BT [ i ]
7. Calculate average waiting time and average turnaround time.
8. Display all the values
9. Stop.
Program:
#include<stdio.h>
int main()
{
int processes[] = { 1, 2, 3};
int n =3;
int bt[] = {10, 5, 8};
wt[0] = 0;
return 0;
}
OUTPUT
RUN 1:
10
2 5 10
15
3 8 15
23
VIVA Questions:
1. What are the features of FCFS scheduling algorithm?
The process may contain multiple threads. Suppose you have notepad application installed on
your system. You click on notepad icon to note down some points. The process starts
running. Then you open another instance of notepad to save other data. The two threads are
created to run two instances of the notepad. You can work on any of the opened notepad
windows independently.
Description: Shortest Job First (SJF) is an algorithm in which the process having the smallest
execution time is chosen for the next execution. This scheduling method can be preemptive or non-
preemptive. It significantly reduces the average waiting time for other processes awaiting execution.
The full form of SJF is Shortest Job First.
Algorithm:
10. Start
11. Declare Process structure with following members.
a. PID : to Store Process ID
b. WT : to store waiting time of process.
c. BT : to store Burst time of process.
d. TT : to store turnaround time of process.
12. Read the number of processes.
13. Read the Burst time and Process Id for all the processes.
14. Sort the Array of process structure based on Burt time.
15. Calculate the waiting time of each process
a. After sorting first process in the list will have waiting time 0
i. Proc[0] .wt = 0
b. Find Waiting time for all other process
i. Proc[ i ] . wt = Proc[ i-1] .wt + Pro [ i-1] .bt
16. Calculate the Turnaround time of each process
i. Proc[ i ] . tt = Proc[ i] .wt + Pro [ i] .bt
17. Calculate average waiting time
a. Total_waitingTime / Number of Process
18. Calculate average turnaround time.
a. Total_waitingTime / Number of Process
19. Display all the values
20. Stop.
Program:
#include <stdio.h>
#define MAX 15
struct process
{
int pid;
int bt;
int wt;
int tt;
};
int main()
{
struct process proc[MAX], temp;
int n , i , j;
int total_wt=0 , total_tt=0;
float avg_wt, avg_tt;
printf(" Process ID Burst time Waiting Time Turn Around Time \n");
return 0;
OUTPUT
RUN1 :
enter number of process (max of 15) 4
2 45
3 56
1 78
47
4 7 0 7
2 45 7 52
3 56 52 108
1 78 108 186
Viva Questions:
3. Shortest job first is also known as shortest job next (SJN) and shortest process
next (SPN).
7. What is starvation ?
Starvation or indefinite blocking is phenomenon associated with the Priority
scheduling algorithms, in which a process ready to run for CPU can wait indefinitely
because of low priority. In heavily loaded computer system, a steady stream of
higher-priority processes can prevent a low-priority process from ever getting the
CPU.
Program 1.3: Write a C Program to simulate the RR (
Round Robin ) CPU scheduling algorithm.
ALGORITHM:
21. Start
22. Declare Process structure with following members.
a. PID : to Store Process ID
b. WT : to store waiting time of process.
c. BT : to store Burst time of process.
d. TT : to store turnaround time of process.
e. rem_bt : to store remaining burst time
23. Read the number of processes.
24. Read the Burst time and Process Id for all the processes.
25. Read the time quantum value.
26. The process are selected from the queue in first come first serve order and
following steps are repeated until remaining burst time for all process is zero.
a. If Burst time > time quantum
i. The process is preempted after time quantum and added to the end
of the queue. Remaing burst time is recorded. CPU execution time
is also recorded
1. t = t + quantum
2. Proc[i] . rem_bt = Proc[i].rem_bt - quantum
b. If Burst Time <= time quantum
i. Process remaining burst time is set to zero and waiting time is
calculated
1. t = t + Proc[i] . rem_bt
2. Proc[i] . rem_bt = 0
3. Proc[ i ] .wt = t - Proc [ i ] .bt
27. Calculate the Turnaround time of each process
i. Proc[ i ] . tt = Proc[ i] .wt + Pro [ i] .bt
28. Calculate average waiting time
a. Total_waitingTime / Number of Process
29. Calculate average turnaround time.
a. Total_waitingTime / Number of Process
30. Display all the values
31. Stop.
Program:
#include <stdio.h>
#define MAX 15
struct process
{
int pid;
int bt;
int wt;
int tt;
int rem_bt;
};
int main()
{
struct process proc[MAX] ;
int n , i , j ,quantum , t;
int total_wt=0 , total_tt=0;
float avg_wt, avg_tt;
int done=0;
t = 0;
// Keep traversing processes in round robin manner until all of them are not done.
printf("Order of Process Excecution is \n");
do
{
done = 1;
for (int i = 0 ; i < n; i++) // Traverse all processes one by one repeatedly
{
// If burst time of a process is greater than 0 then only need to process further
if (proc[i].rem_bt > 0)
{
done = 0; // There is a pending process
printf(" %d : ",proc[i].pid);
if (proc[i].rem_bt > quantum)
{
// Increase the value of t which shows how much time a process has been processed
t += quantum;
// As the process gets fully executed make its remaining burst time = 0
proc[i].rem_bt = 0;
}
}
}
}while(done != 1);
printf("\n");
for (i = 0; i < n; i++)
{
total_wt = total_wt + proc[i].wt; // total waiting time calculation
proc[i].tt = proc[i].wt + proc[i].bt ; // turn around time calculation
total_tt = total_tt + proc[i].tt; // total turn around time calculation
}
avg_wt = (float) total_wt/n;
avg_tt = (float) total_tt/n;
}
enter number of process (max of 15)
4
Enter Process Id and Brust time of the process
1 4
2 3
3 5
4 6
Enter time Quantum
2
Order of Process Excecution is
1: 2: 3: 4: 1: 2: 3: 4: 3: 4:
Processes are executed in following order
PID | BT | WT | TaT
1 : 4 : 6 : 10
2 : 3 : 8 : 11
3 : 5 : 11 : 16
4 : 6 : 12 : 18
Average waiting time 9.250000
Average Turnaround time 13.750000
VIVA QUESTIONS
1. What are the characteristic of RR?
a. Round Robin is a CPU scheduling algorithm where each process is assigned a fixed
time slot in a cyclic way.
b. It is simple, easy to implement, and starvation-free as all processes get fair share of
CPU.
c. One of the most commonly used technique in CPU scheduling as a core.
d. It is preemptive as processes are assigned CPU only for a fixed slice of time at most.
e. The disadvantage of it is more overhead of context switching.
4. What does Round Robin Scheduling (RRS) mean?Round robin scheduling (RRS) is a
job-scheduling algorithm that is considered to be very fair, as it uses time slices that are
assigned to each process in the queue or line. Each process is then allowed to use the CPU
for a given amount of time, and if it does not finish within the allotted time, it is
preempted and then moved at the back of the line so that the next process in line is able to
use the CPU for the same amount of time.
Description: In priority scheduling, a priority is associated with each process, and the CPU
is allocated to the process with the highest priority. Equal-priority processes are scheduled in
FCFS order. SJF is simply a priority algorithm where the priority (p) is the inverse of the
(predicted) next CPU burst. The large the CPU burst, the lower the priority. Priorities are
generally some fixed range of numbers, such as 0 to 7. In this text, we assume that low
numbers represent high priority.
Algorithm:
a. Start
b. Declare Process structure with following members.
i. PID : to Store Process ID
ii. Priority : to store priority of the process
iii. WT : to store waiting time of the process.
iv. BT : to store Burst time of the process.
v. TT : to store turnaround time of the process.
c. Read the number of processes.
d. Read the Process Id, Burst time and Priority for all the processes.
e. Sort the Array of process structure based on Priority.
f. Calculate the waiting time of each process
a. After sorting first process in the list will have waiting time 0
i. Proc[0] .wt = 0
b. Find Waiting time for all other process
i. Proc[ i ] . wt = Proc[ i-1] .wt + Pro [ i-1] .bt
32. Calculate the Turnaround time of each process
i. Proc[ i ] . tt = Proc[ i] .wt + Pro [ i] .bt
33. Calculate average waiting time
a. Total_waitingTime / Number of Process
34. Calculate average turnaround time.
a. Total_waitingTime / Number of Process
35. Display all the values
36. Stop.
#include <stdio.h>
#define MAX 15
struct process
{
int pid;
int priority;
int bt;
int wt;
int tt;
};
int main()
{
struct process proc[MAX], temp;
int n , i , j;
int total_wt=0 , total_tt=0;
float avg_wt, avg_tt;
printf("enter number of process (max of 15) \n");
scanf("%d",&n);
printf(" Enter Process Id and Brust time and priority of the process \n");
for( i=0; i< n; i++)
{
scanf("%d%d%d", &proc[i].pid , &proc[i].bt , &proc[i].priority);
}
//Bubble sort, to sort the process
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (proc[j].priority > proc[j+1].priority)
{
temp=proc[j];
proc[j]= proc[j+1];
proc[j+1]=temp;
}
printf(" | Process ID | Priority | Burst time | Waiting Time | Turn Around Time | \n");
OUTPUT:
VIVA Questions
4. What is aging?
A remedy to starvation is aging, which is a technique used to gradually
increase the priority of those processes that wait for long periods in the system.
Description:
System Calls:
The way that programs talk to the operating system is via ``system calls.''
A system call looks like a procedure call , but it's different -- it is a request to the operating
system to perform some activity.
System calls are expensive. While a procedure call can usually be performed in a few
machine instructions, a system call requires the computer to save its state, let the
operating system take control of the CPU, have the operating system perform some
function, have the operating system save its state, and then have the operating system
give control of the CPU back to you.
System calls are system dependent. Knowing this, it would not be a good idea to
directly use system calls when portability cannot be neglected. System calls are also
quite complex. Most often, it involves the duo of TRAP and RET (or some variations
of those two). To implement system call, one would need specialized knowledge of
I/O registers, the sequence of operations needed to use them; and most important of
all, implement enough protection because I/O resources are generally shared among
multiple users and/or processes.
AIM: To write a C program to copy content from one file to another file.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
int main(void)
{
int n,fd1,fd2;
char buff[BUFF_SIZE];
return 0;
}
INPUT &OUTPUT:
lseek (C System Call): lseek is a system call that is used to change the location of the
read/write pointer of a file descriptor. The location can be set either in absolute or relative
terms.
The lseek() function shall set the file offset for the open file description associated with the
file descriptor fildes, as follows:
The output will be the first 10 characters “1234567890” followed by “fghijxxxxx”. The
inbetween 5 characters are skipped because we used lseek to reposition the pointer 5
characters ahead from the current (SEEK_CUR) position.
Description:
Stat system call is a system call in Linux to check the status of a file such as to check
when the file was accessed. The stat() system call actually returns file attributes. The file
attributes of an inode are basically returned by Stat() function. An inode contains the
metadata of the file. An inode contains: the type of the file, the size of the file, when the file
was accessed (modified, deleted) that is time stamps, and the path of the file, the user ID and
the group ID, links of the file, and physical address of file content.
To use the stat system call in C programming language we should include the following
header file:
#include <sys/stat.h>
int stat(const char *path, struct stat *buf)
The return type of the function in int, if the function is executed successfully, 0 is
returned if there are any errors, -1 will be returned.
const char *path specifies the name of the file.
stat structure The data or information about the file is stored which uses a pointer named
buf, which is passed in as a paramteter and filled in during the execution of the call and
readable by the user after the call.
Program :
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include<unistd.h>
struct stat statbuf;
char dirpath[256];
{
stat(dp->d_name, &statbuf);
printf("the file name is %s \n", dp->d_name);
printf("dir = %d\n", S_ISDIR(statbuf.st_mode));
printf("file size is %ld in bytes \n", statbuf.st_size);
printf("last modified time is %ld in seconds \n", statbuf.st_mtime);
printf("last access time is %ld in seconds \n", statbuf.st_atime);
printf("The device containing the file is %ld\n", statbuf.st_dev);
printf("File serial number is %ld\n\n", statbuf.st_ino);
}
}
OUTPUT:
Description:
The opendir() function shall open a directory stream corresponding to the directory named by
the dirname argument. Directory entries represent files or subdirectory.
#include <dirent.h>
DIR *opendir(const char *dirname);
The readdir() function shall return a pointer to a structure representing the directory entry at
the current position in the directory stream specified by the argument dirp, and position the
directory stream at the next entry. It shall return a null pointer upon reaching the end of the
directory stream.
* readdir(DIR *dirp);
* Upon successful completion, readdir() shall return a pointer to an object of type
struct dirent. When an error is encountered, a null pointer shall be returned and errno shall
be set to indicate the error.
The closedir() function shall close the directory stream referred to by the argument dirp.
Upon successful completion, closedir() shall return 0; otherwise, -1 shall be returned and
errno set to indicate the error.
PROGRAM:
#include<stdio.h>
#include<dirent.h>
struct dirent *dptr;
int main(int argc, char *argv[])
{
char buff[100];
DIR *dirp;
printf("\n\n ENTER DIRECTORY NAME");
scanf("%s", buff);
if( ( dirp=opendir( buff ) ) == NULL)
{
printf("The given directory does not exist");
exit(1);
}
while(dptr = readdir ( dirp ) )
{
printf("%s\n",dptr->d_name);
} closedir(dirp); }
Program 2.5: C Program for fork() and getpid() system call
Description: fork() creates a new process by duplicating the calling process.The new
process is referred to as the child process. The calling process is referred to as the parent
process. The child has its own unique process ID.The child's parent process ID is the same as
the parent's process ID.
#include <unistd.h>
pid_t fork(void);
On success, the PID of the child process is returned in the parent, and 0 is returned in the
child. On failure, -1 is returned in the parent, no child process is created, and errno is set
to indicate the error.
Getpid() is the function used to get the process ID of the process that calls that function.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<unistd.h>
main()
{
int pid,pid1,pid2;
pid=fork();
if(pid== -1)
{
printf("ERROR IN PROCESS CREATION \n");
exit(1);
}
if(pid!=0)
{
pid1=getpid();
printf("\n the parent process ID is %d\n", pid1);
}
else
{
pid2=getpid();
printf("\n the child process ID is %d\n", pid2);
}}
2.6 C program to execute another C program using exec
system call.
Description:
The exec family of functions replaces the current running process with a new process. It can
be used to run a C program by using another C program. It comes under the header file
unistd.h. There are many members in the exec family which are shown below with
examples.
execvp : Using this command, the created child process does not have to run the same
program as the parent process does. The exec type system calls allow a process to run
any program files, which include a binary executable or a shell script . Syntax:
file: points to the file name associated with the file being executed.
argv: is a null terminated array of character pointers.
We will have two .C files , EXEC.c and execDemo.c and we will replace the execDemo.c
with EXEC.c by calling execvp() function in execDemo.c .
//EXEC.c
#include<stdio.h>
#include<unistd.h>
int main()
{
int i;
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
//A null terminated array of character
//pointers
char *args[]={"./EXEC",NULL};
execvp(args[0],args);
return 0;
}