OS Manual LBS
OS Manual LBS
OS Manual LBS
2019 Scheme
Vision
Mission
Imparting value based technical education for transforming young women to professionals
excelling globally in academics, research & development and industry meeting societal
challenges.
Vision
To be a renowned academic and research center in Computer Science and allied domains.
Mission
2. Problem analysis: Identify, formulate, review research Literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modelling to complex engineering
activities with an understanding of the Limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to
the professional engineering practice.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one's own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage
in independent and life-long learning in the broadest context of technological change.
CONTENTS
1 FAMILIARISATION
2 CREATION OF PROCESS
5 IMPLEMENTING SEMAPHORES
Introduction
Operating System
Operating System is a type of System Software and is a collection (set) of programs, which
performs two specific functions. First, it provides a user Interface so that human user can
interact with the machine. Second, the operating system manages computer resources
.
Computer Resources
Computer resources are physical devices, which an operating system accesses, and manages.
Printers, memories, input/output devices, files, etc., are examples of computer resources. So
why an operating is called a resource manager.Some of important tasks, which a typical modern
operating system has to perform, are given below:
Processes Scheduling
Interprocess Communication
Synchronization
Memory Management (physical memory allocation, virtual memory etc.)
Resource Management
Directory and File Management
Communication
Linux
Linux is an operating system, which is a flavor of Unix. Linux is a multiuser and multitasking
operating system. It is a leading operating system on servers and
other big iron systems such as mainframe computers and super computers. More than 90%of
today's 500 fastest supercomputers run some variant of Linux,
Including the 10 fastest. The Android system in wide use on mobile devices is built on the
Linux kernel. Since with the likeness with UNIX, all the programs written for UNIX can be
compiled and run under Linux. Linux operating system runes
on verity of machines like 486/Pentium, Sun Sparces, PowerPC, etc.
Linus Torvalds, principal author, at the University of Helsinki, Finland, wrote Linux Kernel.
UNIX programmers around the world in the development of Linux assisted him. Popular Linux
distributions include: Debian (Knoppix, Ubuntu) , Fedora, Gentoo, openSUSE etc.
Linux System
Kernel is the core of Linux Operating System, while the system is operational, it keeps on
running. The kernel is the part of the Linux Operating system which consist of routines,
which interact with underlying hardware, and routines which include system call handling,
process management, scheduling, pipes, signals, paging, swapping, the file system, and high
level part of the I/O system. So shell accepts commands from user interpret them and deliver
these interpreted commands to kernel for execution. After execution, the shell displays result
of executed commands.
Files
File is a mechanism through which we store information. Normally, there are two modes of
storing information.
i)File
ii)Directories
i) File- A simple file stores some type of information. The information it has may
be in text format, or in binary format.
ii) Directories- are special types of files owned the operating system, which
contains information about files, and may contain other directories (called
Subdirectories). So directories are also files, which contains some vital
information about the files, and other directories. There’s a file (management)
system in operating system, which manipulates file and directories. The major
operations, which can be performed on files and directories, are given below:
Create
Delete
Open
Close
Read
Write
Append
Seek
Rename
Get Attributes
Set Attributes
System Calls
A system call is a mechanism that provides the interface between a process and the
operating system. It is a programmatic method in which a computer program
requests a service from the kernel of the OS. System call offers the services of the
operating system to the user programs via API (Application Programming
Interface). System calls are the only entry points for the kernel system.
Process Control :
This system calls perform the task of process creation, process termination, etc.
The Linux System calls under this are fork() , exit() , exec().
fork()
⮚ A new process is created by the fork() system call.
⮚ A new process may be created with fork() without a new program being run-
the new sub-process simply continues to execute exactly the same program that
the first (parent) process was running.
⮚ It is one of the most widely used system calls under process management.
exit()
⮚ The exit() system call is used by a program to terminate its execution.
⮚ The operating system reclaims resources that were used by the process after the
exit() system call.
exec()
⮚ A new program will start executing after a call to exec()
⮚ Running a new program does not require that a new process be created first:
any process may call exec() at any time. The currently running program is
immediately terminated, and the new program starts executing in the context of
the existing process.
⮚
File Management :
File management system calls handle file manipulation jobs like creating a file,
reading, and writing, etc. The Linux System calls under this are open(), read(),
write(), close().
open():
⮚ It is the system call to open a file.
⮚ This system call just opens the file, to perform operations such as read and
write, we need to execute different system call to perform the operations.
read():
⮚ This system call opens the file in reading mode
⮚ We can not edit the files with this system call.
⮚ Multiple processes can execute the read() system call on the same file
simultaneously.
write():
⮚ This system call opens the file in writing mode
⮚ We can edit the files with this system call.
⮚ Multiple processes can not execute the write() system call on the same file
simultaneously.
close():
⮚ This system call closes the opened file.
Device Management :
Device management does the job of device manipulation like reading from device
buffers, writing into device buffers, etc. The Linux System calls under this is ioctl().
ioctl():
⮚ ioctl() is referred to as Input and Output Control.
⮚ ioctl is a system call for device-specific input/output operations and other
operations which cannot be expressed by regular system calls.
Information Maintenance:
It handles information and its transfer between the OS and the user program. In
addition, OS keeps the information about all its processes and system calls are used to
access this information. The System calls under this are getpid(), alarm(), sleep().
getpid():
⮚ getpid stands for Get the Process ID.
⮚ The getpid() function shall return the process ID of the calling process.
⮚ The getpid() function shall always be successful and no return value is reserved
to indicate an error.
alarm():
⮚ This system call sets an alarm clock for the delivery of a signal that when it has
to be reached.
⮚ It arranges for a signal to be delivered to the calling process
sleep():
⮚ This System call suspends the execution of the currently running process for
some interval of time
⮚ Meanwhile, during this interval, another process is given chance to execute
Communication
These types of system calls are specially used for inter-process communications.
Two models are used for inter-process communication
1. Message Passing(processes exchange messages with one another)
2. Shared memory(processes share memory region to communicate)
The system calls under this are pipe() , shmget() ,mmap().
pipe():
⮚ The pipe() system call is used to communicate between different Linux
processes.
⮚ It is mainly used for inter-process communication.
⮚ The pipe() system function is used to open file descriptors.
shmget():
⮚ shmget stands for shared memory segment.
⮚ It is mainly used for Shared memory communication.
⮚ This system call is used to access the shared memory and access the messages
in order to communicate with the process.
mmap():
⮚ This function call is used to map or unmap files or devices into memory.
⮚ The mmap() system call is responsible for mapping the content of the file to the
virtual memory space of the process.
INTRODUCTION TO OPERATING SYSTEMS
An Operating System is a program that manages the Computer hardware. It controls and
coordinates the use of the hardware among the various application programs for the various users.
Apart from the program code, it includes the current activity represented by
• Program Counter,
• Process Stack which contains temporary data like function parameters, return
addresses and local variables
• Data section which contains global variables
A Multi-programmed system can have many processes running simultaneously with the CPU
multiplexed among them. By switching the CPU between the processes, the OS can make the computer
more productive. There is Process Scheduler which selects the process among many processes that are
ready, for program execution on the CPU. Switching the CPU to another process requires performing a
state save of the current process and a state restore of new process, this is Context Switch.
Scheduling Algorithms
CPU Scheduler can select processes from ready queue based on various scheduling algorithms.
Different scheduling algorithms have different properties, and the choice of a particular algorithm may
favour one class of processes over another. The scheduling criteria include
• CPU utilization:
• Throughput: The number of processes that are completed per unit time.
• Turnaround time: The interval between the time of submission of process to the time
of completion.
• Response time: The time from submission of a request until the first response is produced.
• The lesser the arrival time, the sooner will the process gets the CPU.
• The Turnaround time and the waiting time are calculated by using the following formula.
0 0 2 2 2 0
1 1 6 8 7 1
2 2 4 12 8 4
3 3 9 21 18 9
4 4 12 33 29 17
Avg Waiting Time=31/5
• The job with the shortest burst time will get the CPU first.
• The lesser the burst time, the sooner will the process get the CPU.
• However, it is very difficult to predict the burst time needed for a process hence this
algorithm is very difficult to implement in the system.
• In the following example, there are five jobs named as P1, P2, P3, P4 and P5. Their arrival
time and burst time are given in the table below.
1 1 7 8 7 0
2 3 3 13 10 7
3 6 2 10 4 2
4 7 10 31 24 14
5 9 8 21 12 4
Since, No Process arrives at time 0 hence; there will be an empty slot in the Gantt chart from
time 0 to 1 (the time at which the first process arrives)
.
• According to the algorithm, the OS schedules the process which is having the lowest burst
time among the available processes in the ready queue.
• Till now, we have only one process in the ready queue hence the scheduler will schedule this
to the processor no matter what is its burst time.
• This will be executed till 8 units of time.
• Till then we have three more processes arrived in the ready queue hence the scheduler will
choose the process with the lowest burst time.
• Among the processes given in the table, P3 will be executed next since it is having the lowest
burst time among all the available processes.
• It is the pre-emptive form of SJF. In this algorithm, the OS schedules the Job according to
the remaining time of the execution
4. Priority Scheduling
•The higher the priority, the sooner will the process get the CPU.
• If the priority of the two processes is same then they will be scheduled according to their
arrival time.
•A multi-level queue scheduling algorithm partitions the ready queue into several separate
queues.
•The processes are permanently assigned to one queue, generally based on some property of the
process, such as memory size, process priority, or process type. •Each queue has its own
scheduling algorithm.
EXPERIMENT-2
DESCRIPTION:
Used to create new processes. The new process consists of a copy of the address space of the
original process. The value of process id for the child process is zero, whereas the value of
process
id for the parent is an integer value greater than zero.
Syntax:
Fork ( );
ALGORITHM:
1: Start the program.
2: Declare the variables pid and child id.
3: Get the child id value using system call fork().
4: If child id value is greater than zero then print as “i am in the parent process”.
5: If child id! = 0 then using getpid() system call get the process id.
6: Print “i am in the parent process” and print the process id.
7: If child id! = 0 then using getppid() system call get the parent process id.
8: Print “i am in the parent process” and print the parent process id.
9: Else If child id value is less than zero then print as “i am in the child process”.
10: If child id! = 0 then using getpid() system call get the process id.
11: Print “i am in the child process” and print the process id.
12: If child id! = 0 then using getppid() system call get the parent process id.
13: Print “i am in the child process” and print the parent process id.
14: Stop the program.
PROGRAM :
SOURCE CODE:
/* fork system call */
#include<stdio.h>
#include <unistd.h>
#include<sys/types.h>
int main()
{
int id,childid;
id=getpid();
if((childid=fork())>0)
{
printf("\n i am in the parent process %d",id);
printf("\n i am in the parent process %d",getpid());
printf("\n i am in the parent process %d\n",getppid());
}
else
{
printf("\n i am in child process %d",id);
printf("\n i am in the child process %d",getpid());
printf("\n i am in the child process %d",getppid());
}
}
OUTPUT:
$ vi fork.c
$ cc fork.c
$ ./a.out
i am in child process 3765
i am in the child process 3766
i am in the child process 3765
i am in the parent process 3765
i am in the parent process 3765
i am in the parent process 3680
RESULT:
Thus the program was executed and verified successfully
2.B) Wait () and Exit () System Calls:
AIM:To write the program to implement the system calls wait ( ) and exit ( ).
DESCRIPTION:
i. fork ( )
Used to create new process. The new process consists of a copy of the address space of the
original process. The value of process id for the child process is zero, whereas the value of
process
id for the parent is an integer value greater than zero.
Syntax: fork ( );
ii. wait ( )
The parent waits for the child process to complete using the wait system call. The wait
system
call returns the process identifier of a terminated child, so that the parent can tell which of its
possibly many children has terminated.
Syntax: wait (NULL);
iii. exit ( )
A process terminates when it finishes executing its final statement and asks the operating
system to delete it by using the exit system call. At that point, the process may return data
(output)
to its parent process (via the wait system call).
Syntax: exit (0);
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables pid and i as integers.
Step 3: Get the child id value using the system call fork ().
Step 4: If child id value is less than zero then print “fork failed”.
Step 5: Else if child id value is equal to zero, it is the id value of the child and then start the
child
process to execute and perform Steps 7 & 8.
Step 6: Else perform Step 9.
Step 7: Use a for loop for almost five child processes to be called.
Step 8: After execution of the for loop then print “child process ends”.
Step 9: Execute the system call wait ( ) to make the parent to wait for the child process to get
over.
Step 10: Once the child processes are terminated, the parent terminates and hence prints
“Parent
process ends”.
Step 11: After both the parent and the child processes get terminated it execute the wait ( )
system
call to permanently get deleted from the OS.
Step 12: Stop the program.
PROGRAM:
2.B.1) SOURCE CODE:
#include<stdio.h>
#include<unistd.h>
int main( )
{
int i, pid;
pid=fork( );
if(pid== -1)
{
printf("fork failed");
exit(0);
}
else if(pid==0)
{
printf("\n Child process starts");
for(i=0; i<5; i++)
{
printf("\n Child process %d is called", i);
}
printf("\n Child process ends");
}
else
{
wait(0);
printf("\n Parent process ends");
}
exit(0);
}
OUTPUT:
$ vi waitexit.c
$ cc waitexit.c
$ ./a.out
Child process starts
Child process 0 is called
Child process 1 is called
Child process 2 is called
Child process 3 is called
Child process 4 is called
Child process ends
Parent process ends
RESULT:
Thus the program was executed and verified successfully
2.B.2 ) WAIT () AND EXIT () SYSTEM CALLS
PROGRAM:
SOURCE CODE:
/* wait system call */
#include <stdlib.h>
#include <errno.h>
#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
main()
{
pid_t pid;
int rv;
switch(pid=fork())
{
case -1:
perror("fork");
exit(1);
case 0:
printf("\n CHILD: This is the child process!\n");
fflush(stdout);
printf("\n CHILD: My PID is %d\n", getpid());
printf("\n CHILD: My parent's PID is %d\n",getppid());
printf("\n CHILD: Enter my exit status (make it small):\n ");
printf("\n CHILD: I'm outta here!\n");
scanf(" %d", &rv);
exit(rv);
default:
printf("\nPARENT: This is the parent process!\n");
printf("\nPARENT: My PID is %d\n", getpid());
fflush(stdout);
wait(&rv);
fflush(stdout);
printf("\nPARENT: My child's PID is %d\n", pid);
RESULT:
Thus the program was executed and verified successfully
EXPERIMENT-3
3.A) Program to Implement System Calls
DESCRIPTION: A system call is a method for a computer program to request a service from
the kernel of the operating system on which it is running. A system call is a method of
interacting with the operating system via programs. A system call is a request from computer
software to an operating system's kernel.
write():It is used to write data from a user buffer to a device like a file. This system call is one
way for a program to generate data.
AIM: To write a program to read maximum of 15 characters from the user and print them on
screen
ALGORITHM:
1.Start the program
2.Declare the buffer size
3.Read user from standard input device to the buffer using read() system call.
4.Print on screen using write() system call.
5.Stop the program.
PROGRAM:
SOURCE CODE:
#include<unistd.h>
int main()
{
int n;
char buff[15];
n=read(0,buff,15);
write(1,buff,n);
}
OUTPUT:
HELLO
HELLO.
RESULT:
Thus the program was executed and verified successfully
F2.txt;
abcd
F3.txt:
abcd
123
RESULT:
Thus the program was executed and verified successfully
}
dp=readdir(p);
while(p!=NULL)
{
printf("%d%s",dp->d_ino,dp->d_name);
dp=readdir(p);
}
}
OUTPUT:
2753482.2753486A.OUT
236356..2753496PROCESS.C
2754390PROCESS2.C
2753489F1.txt
275349F2.txt
27533501PROCESS3.C
RESULT:
Thus the program was executed and verified successfully
EXPERIMENT-4
4) INTER PROCESS COMMUNICATION USING SHARED MEMORY
DESCRIPTION: Shared memory is a memory shared between two or more processes. Each
process has its own address space; if any process wants to communicate with some information
from its own address space to other processes, then it is only possible with IPC (inter-process
communication) techniques.
Shared memory is the fastest inter-process communication mechanism. The operating system
maps a memory segment in the address space of several processes to read and write in that
memory segment without calling operating system functions.
SENDER PROGRAM:
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)1122,1024,0666|IPC_CREAT);
printf("Key of shared memory is %d",shmid);
shared_memory=shmat(shmid,NULL,0);
printf("Process attached at %p\n",shared_memory);
printf("Enter some data to write to shared memory");
read(0,buff,100);
strcpy(shared_memory,buff);
printf("You write :%s",(char *)shared_memory);
}
RECEIVER PROGRAM:
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)1122,1024,0666);
printf("Key of shared memory is %d",shmid);
shared_memory=shmat(shmid,NULL,0);
printf("Process attached at %p\n",shared_memory);
printf("Data read from shared memory is :%s\n",(char *)shared_memory);
}
OUTPUT(SENDER):
Key of shared memory is : 33
Process attached at 0x7ff3eabf900
Enter source data to write to shared memory: You are home
You write: You are home
OUTPUT(RECEIVER):
Key of shared memory is : 33
Process attached at 0x7ff3eabf300
Data read from shared memory: You are home
RESULT:
Thus the program was executed and verified successfully
EXPERIMENT-5
5) IMPLEMENT SEMAPHORES
DESCRIPTION: Semaphore in OS is an integer value that indicates whether the resource
required by the process is available or not. The value of a semaphore is modified by wait() or
signal() operation where the wait() operation decrements the value of semaphore and the
signal() operation increments the value of the semaphore.
AIM: To write a C program to implement semaphores
ALGORITHM:
1. Start the program.
2. Get the no of jobs from the user.
3. When job1 is processing, job 2 is also starts processing.
4. When job 1 enters critical section, next job starts processing.
5. When job1 comes out of the critical section, the other job enters the critical section.
6. The above 3 steps are performed for various programs.
7. End the program.
PROGRAM:
SOURCE CODE:
#include<stdio.h>
main()
{
int i,a=1,h=2,n;
printf("\n Enter the no of jobs");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(a==1)
{
printf("processing %d......! \n", i+1);
a++;
}
if(h>1)
{
if(i+2<=n)
{
printf("\n processing %d.....! \n",i+2);
}
printf("\n Process %d Enters Critical section", i+1);
printf("\n Process %d Leaves Critical section", i+1);
}
h+1;
}
}
OUTPUT:
ENTER THE NUMBER OF JOBS:4
Processing 1…….!
Processing 2……..!
Process 1 enters critical condition.
Process 1 leaves critical condition.
Processing 3……..!
Process 2 enters critical condition.
Process 2 leaves critical condition.
Processing 4……..!
Process 3 enters critical condition.
Process 3 leaves critical condition.
Process 4 enters critical condition.
Process 4 leaves critical condition.
RESULT:
Thus the program was executed and verified successfully
EXPERIMENT-6
6) Write a C program to simulate the following non-preemptive CPU scheduling algorithms
to find turnaround time and waiting time for the above problem.
a) FCFS
b) SJF
c) ROUNDROBIN
d) PRIORITY
PROGRAM 6.A:
SOURCE CODE:
#include <stdio.h>
int waitingtime(int proc[], int n,int burst_time[], int wait_time[]) {
wait_time[0] = 0;
for (int i = 1; i < n ; i++ )
wait_time[i] = burst_time[i-1] + wait_time[i-1] ;
return 0;
}
int turnaroundtime( int proc[], int n,
int burst_time[], int wait_time[], int tat[]) {
int i;
for ( i = 0; i < n ; i++)
tat[i] = burst_time[i] + wait_time[i];
return 0;
}
int avgtime( int proc[], int n, int burst_time[]) {
int wait_time[n], tat[n], total_wt = 0, total_tat = 0;
int i;
waitingtime(proc, n, burst_time, wait_time);
turnaroundtime(proc, n, burst_time, wait_time, tat);
printf("Processes Burst Waiting Turn around ");
for ( i=0; i<n; i++) {
total_wt = total_wt + wait_time[i];
total_tat = total_tat + tat[i];
printf(" %d\t %d\t\t %d \t%d
", i+1, burst_time[i], wait_time[i], tat[i]);
}
printf("Average waiting time = %f
", (float)total_wt / (float)n);
printf("Average turn around time = %f
", (float)total_tat / (float)n);
return 0;
}
int main() {
int proc[] = { 1, 2, 3};
int n = sizeof proc / sizeof proc[0];
int burst_time[] = {5, 8, 12};
avgtime(proc, n, burst_time);
return 0;
}
OUTPUT:
Processes Burst Waiting Turn around
1 5 0 5
2 8 5 13
3 12 13 25
Average Waiting time = 6.000000
Average turn around time = 14.333333
RESULT:
Thus the program was executed and verified successfully
PROGRAM 6.B:
SOURCE CODE:
#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:
Enter the number of process:4
Enter process name, arrival time& burst time:
1
1
3
Enter process name, arrival time& burst time:
2
2
4
Enter process name, arrival time& burst time:
3
1
2
Enter process name, arrival time& burst time:
4
4
4
RESULT:
Thus the program was executed and verified successfully
PROGRAM 6.C:
SOURCE CODE:
#include<stdio.h>
int main()
{
int cnt,j,n,t,remain,flag=0,tq;
int wt=0,tat=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(cnt=0;cnt<n;cnt++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d :",cnt+1);
scanf("%d",&at[cnt]);
scanf("%d",&bt[cnt]);
rt[cnt]=bt[cnt];
}
printf("Enter Time Quantum:\t");
scanf("%d",&tq);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(t=0,cnt=0;remain!=0;)
{
if(rt[cnt]<=tq && rt[cnt]>0)
{
t+=rt[cnt];
rt[cnt]=0;
flag=1;
}
else if(rt[cnt]>0)
{
rt[cnt]-=tq;
t+=tq;
}
if(rt[cnt]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",cnt+1,t-at[cnt],t-at[cnt]-bt[cnt]);
wt+=t-at[cnt]-bt[cnt];
tat+=t-at[cnt];
flag=0;
}
if(cnt==n-1)
cnt=0;
else if(at[cnt+1]<=t)
cnt++;
else
cnt=0;
}
printf("\nAverage Waiting Time= %f\n",wt*1.0/n);
printf("Avg Turnaround Time = %f",tat*1.0/n);
return 0;
}
OUTPUT:
Enter Total Process: 4
Enter Arrival Time and Burst Time for Process Process Number 1 :0 5
Enter Arrival Time and Burst Time for Process Process Number 2 :1 4
Enter Arrival Time and Burst Time for Process Process Number 3 :2 2
Enter Arrival Time and Burst Time for Process Process Number 4 :4 1
Enter Time Quantum: 2
Process Turnaround Time Waiting Time
P[3] | 4 | 2
P[4] | 3 | 2
P[2] | 10 | 6
P[1] | 12 | 7
PROGRAM 6.D:
SOURCE PROGRAM:
#include <stdio.h>
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int n;
printf("Enter Number of Processes: ");
scanf("%d",&n);
int b[n],p[n],index[n];
for(int i=0;i<n;i++)
{
printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
scanf("%d %d",&b[i],&p[i]);
index[i]=i+1;
}
for(int i=0;i<n;i++)
{
int a=p[i],m=i;
for(int j=i;j<n;j++)
{
if(p[j] > a)
{
a=p[j];
m=j;
}
}
swap(&p[i], &p[m]);
swap(&b[i], &b[m]);
swap(&index[i],&index[m]);
}
int t=0;
printf("Order of process Execution is\n");
for(int i=0;i<n;i++)
{
printf("P%d is executed from %d to %d\n",index[i],t,t+b[i]);
t+=b[i];
}
printf("\n");
printf("Process Id Burst Time Wait Time TurnAround Time\n");
int wait_time=0;
for(int i=0;i<n;i++)
{
printf("P%d %d %d %d\n",index[i],b[i],wait_time,wait_time + b[i]);
wait_time += b[i];
}
return 0;
}
OUTPUT:
Enter Number of Processes: 3
Enter Burst Time and Priority Value for Process 1: 10 2
Enter Burst Time and Priority Value for Process 2: 5 0
Enter Burst Time and Priority Value for Process 3: 8 1
Order of process Execution is
P1 is executed from 0 to 10
P3 is executed from 10 to 18
P2 is executed from 18 to 23
EXPERIMENT-7
7.A)Implementation of the Memory Allocation Methods for fixed partition*
First Fit
DESCRIPTION: In the first fit, the partition is allocated which is first sufficient from the top
of Main Memory. Its advantage is that it is the fastest search as it searches only the first block
i.e. enough to assign a process. The first fit memory allocation scheme checks the empty
memory block in a sequential manner. This means that the memory block found empty at the
first attempt is checked for size.If the size is not less than the required size, it is allocated.
One of the biggest issues in this memory allocation scheme is, when a process is allocated to
a comparatively larger space than needed, it creates huge chunks of memory space.
AIM: To write a C program for implementation memory allocation methods for fixed
partition using first fit.
ALGORITHM:
1:Define the max as 25.
2: Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp, highest=0, bf[max],ff[max].
3: Get the number of blocks,files,size of the blocks using for loop.
4: In for loop check bf[j]!=1, if so temp=b[j]-f[i]
5: Check highest
PROGRAM:
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
INPUT
Enter the number of blocks: 3
Enter the number of files: 2
OUTPUT
File No File Size Block No Block Size Fragment
1 1 1 5 4
2 4 3 7 3
RESULT:
Thus the program was executed and verified successfully
AIM:To write a C program for implementation of FCFS and SJF scheduling algorithms.
ALGORITHM:
1:Define the max as 25.
2: Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp, highest=0, bf[max],ff[max].
3: Get the number of blocks,files,size of the blocks using for loop.
4: In for loop check bf[j]!=1, if so temp=b[j]-f[i]
5: Check temp>=0,if so assign ff[i]=j break the for loop.
6: Assign frag[i]=temp,bf[ff[i]]=1;
7: Repeat step 4 to step 6.
8: Print file no,size,block no,size and fragment.
9: Stop the program.
PROGRAM:
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
INPUT:
OUTPUT:
File No File Size Block No Block Size Fragment
1 1 3 7 6
2 4 1 5 1
RESULT:
Thus the program was executed and verified successfully
AIM:To write a C program for implementation of FCFS and SJF scheduling algorithms.
ALGORITHM:
1:Define the max as 25.
2: Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp, highest=0, bf[max],ff[max].
3: Get the number of blocks,files,size of the blocks using for loop.
4: In for loop check bf[j]!=1, if so temp=b[j]-f[i]
5: Check lowest>temp,if so assign ff[i]=j,highest=temp
6: Assign frag[i]=lowest, bf[ff[i]]=1,lowest=10000
7: Repeat step 4 to step 6.
8: Print file no,size,block no,size and fragment.
9: Stop the program.
PROGRAM:
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
INPUT
Enter the number of blocks: 3
Enter the number of files: 2
OUTPUT
File No File Size Block No Block Size Fragment
1 1 2 2 1
2 4 1 5 1
RESULT:
Thus the program was executed and verified successfully
EXPERIMENT-8
8.A)IMPLEMENTATION OF FIFO PAGE REPLACEMENT ALGORITHM
DESCRIPTION: FIFO which is also known as First In First Out is one of the types of page
replacement algorithm. The FIFO algorithm is used in the paging method for memory
management in an operating system that decides which existing page needs to be replaced in
the queue. FIFO algorithm replaces the oldest (First) page which has been present for
the longest time in the main memory
ALGORITHM:
4. Check the need of replacement from old page to new page in memory
PROGRAM:
SOURCE CODE:
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
OUTPUT:
RESULT:
Thus the program was executed and verified successfully
ALGORITHM :
PROGRAM:
SOURCE CODE:
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}
OUTPUT:
Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
7 5
7 5 9
4 5 9
4 3 9
4 3 7
9 3 7
9 6 7
9 6 2
1 6 2
RESULT:
Thus the program was executed and verified successfully
PROGRAM:
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
main()
{
int rs[50], i, j, k, m, f, cntr[20], a[20], min, pf=0;
clrscr();
printf("\nEnter number of page references -- ");
scanf("%d",&m);
printf("\nEnter the reference string -- ");
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
printf("\nEnter the available no. of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
cntr[i]=0; a[i]=-1;
}
printf(“\nThe Page Replacement Process is – \n“);
for(i=0;i<m;i++)
{
for(j=0;j<f;j++)
if(rs[i]==a[j])
{
cntr[j]++;
break;
if(j==f)
{ min = 0;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min])
min=k;
a[min]=rs[i]; cntr[min]=1;
pf++;
}
printf("\n");
for(j=0;j<f;j++)
printf("\t%d",a[j]);
if(j==f)
}
printf(“\tPF No. %d”,pf);}
printf("\n\n Total number of page faults -- %d",pf);
getch();
}
INPUT
Enter number of page references -- 10
Enter the reference string -- 1 2 3 4 5 2 5 2 5 1 4 3
Enter the available no. of frames -- 3
OUTPUT
The Page Replacement Process is –
1 -1 -1 PF No. 1
1 2 -1 PF No. 2
1 2 3 PF No. 3
4 2 3 PF No. 4
5 2 3 PF No. 5
523
523
5 2 1 PF No. 6
5 2 4 PF No. 7
5 2 3 PF No. 8
RESULT:
Thus the program was executed and verified successfully
EXPERIMENT-9
ALGORITHM:
1. Start the program
2. Declare the memory for the process
3. Read the no of processes, resources, allocation matrix and available matrix
4. Calculate the need matrix ;Need = Max- Allocation
5. Compare each and every proceses using the bankers algorithm.
6. If the process in the safe state , then it is not a deadlock process otherwise it is a deadlock
process.
7. Process the result of state of process.
8. Stop the program
SAFETY ALGORITHM:
1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi <= Work
if no such i exists goto step (4)
3) Work = Work + Allocation[i]
Finish[i] = true
goto step (2)
4) if Finish [i] = true for all i
then the system is in a safe state
RESOURCE REQUEST ALGORITHM:
PROGRAM:
SOURCE CODE:
#include <stdio.h>
int main()
{
int n, m, i, j, k, y,alloc[20][20],max[20][20],avail[50],ind=0;
printf("Enter the no of Proceses:");
scanf("%d",&n);
printf("Enter the no of Resources:");
scanf("%d",&m);
printf("Enter the Allocation Matrix:");
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
scanf("%d",&alloc[i][j]);
}
printf("Enter the Max Matrix:");
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
scanf("%d",&max[i][j]);
}
printf("Enter the Available Matrix");
for(i=0;i<m;i++)
scanf("%d",&avail[i]);
int finish[n], safesequence[n],work[m],need[n][m];
//calculating NEED matrix
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
printf("NEED matrix is");
for (i = 0; i < n; i++)
{
printf("\n");
for (j = 0; j < m; j++)
printf(" %d ",need[i][j]);
}
for(i=0;i<m;i++)
{
work[i]=avail[i];
}
for (i = 0; i < n; i++) {
finish[i] = 0;
}
for (k = 0; k < n; k++) {
for (i = 0; i < n; i++)
{
if (finish[i] == 0)
{
int flag = 0;
for (j = 0; j < m; j++)
{
if (need[i][j] > work[j])
{
flag = 1;
break;
}
}
if (flag == 0) {
safesequence[ind++] = i;
for (y = 0; y < m; y++)
work[y] += alloc[i][y];
finish[i] = 1;
}
}
}
}
printf("\nFollowing is the SAFE Sequence\n");
for (i = 0; i <= n - 1; i++)
printf(" P%d ", safesequence[i]);
}
OUTPUT:
RESULT:
Thus the program was executed and verified successfully
EXPERIMENT-10
10)SIMULATING DISK SCHEDULING ALGORITHM
10.A: FCFS
DESCRIPTION: First Come First Serve (FCFS) is an operating system scheduling algorithm
that automatically executes queued requests and processes in order of their arrival. It is the
easiest and simplest CPU scheduling algorithm. In this type of algorithm, processes which
requests the CPU first get the CPU allocation first
AIM: To write a c program to implement FCFS disk scheduling algorithm.
ALGORITHM
1: Start the program
2: Let the request array represent an array storing indexes of tracks that have been requested in
ascending order of their time of arrival. ‘initial’ is their position of disk head.
3: Let us one by one take the tracks in default order and calculate the absolute distance of the track
from the central.
4: Increment the total head moment with this distance.
5: Currently serviced track position now becomes the new head positions.
6: Go to step 3 until all tracks in request array have been serviced.
7: Stop the program.
PROGRAM:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main(){
int TotalHeadMoment=0,initial,RQ[100],n,i;
printf("Enter the number of requests:");
scanf("%d",&n);
printf("Enter the request sequence:");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter the initial head position:");
scanf("%d",&initial);
//logic for fcfs disk scheduling
for(i=0;i<n;i++){
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial = RQ[i];
}
printf("Total head moment is %d",TotalHeadMoment);
return 0;
}
OUTPUT:
Enter the number of requests:8
Enter the request sequence:95
180
34
119
11
123
62
64
Enter the initial head position:50
Total head moment is 644
10.B:SCAN
DESCRIPTION: In SCAN disk scheduling algorithm, head starts from one end of the disk and
moves towards the other end, servicing requests in between one by one and reach the other
end. Then the direction of the head is reversed and the process continues as head
continuously scan back and forth to access the disk. So, this algorithm works as an elevator
and hence also known as the elevator algorithm. As a result, the requests at the midrange are
serviced more and those arriving behind the disk arm will have to wait.
AIM: To write a c program to implement scan scheduling algorithm.
ALGORITHM:
1: Start the program.
2: Let the request array represent an array of string indices of tracks that have been requested in
ascending order of their time.
3: Let the direction represent whether the initial is moving towards the left or right.
4: In the direction in which the initial is moving service all tracks one by one.
5: Calculate the absolute distance of the track from the head.
6: Increment the total head moment with this distance.
7: Currently serviced track position now becomes the new initial position.
8: Go to step 4 until we reach at one of the ends of the disk.
9: If we reach at the end of the disk, reverse the direction, and go to step 3 until all tracks in the
request array have not been serviced.
10: Stop the program.
PROGRAM:
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
void main(){
int TotalHeadMoment=0,initial,index,move,rq[20],temp,n,size,i=0;
printf("Enter the number of requests:");
scanf("%d",&n);
printf("Enter the request sequence:");
for(i=0;i<n;i++)
scanf("%d",&rq[i]);
printf("Enter the initial head position:");
scanf("%d",&initial);
printf("Enter the disk size:");
scanf("%d",&size);
printf("Enter the head moment direction (for high:1 and for low:0) :");
scanf("%d",&move);
for(i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
if(rq[j+1]<rq[j]){
temp=rq[j];
rq[j]=rq[j+1];
rq[j+1]=temp;
}
}
}
for(i=0;i<n;i++){
if(initial<rq[i]){
index=i;
break;
}
}
OUTPUT:
Enter the number of requests:8
Enter the request sequence:95
180
34
119
11
123
62
64
Enter the initial head position:50
Enter the disk size:200
Enter the head moment direction (for high:1 and for low:0) :1
Total head movement=337
10.C: C-SCAN
ALGORITHM:
1: Start the program.
2: Let the request array represent an array, storing indices of tracks that have been requested in
ascending order of their time of arrival. ‘initial’ is the position of the disk head.
3: The initial sevices only in the right direction from 0 to the size of the disk.
4: While moving in the left direction do not service any of the tracks.
5: When we reach the beginning (left end) reverse the direction.
6: While moving in the right direction it services all tracks one by one.
7: While moving in the right direction calculate the absolute distance of the tracks from the initial.
8: Increment the total head moment with this distance.
9: Currently serviced track position now becomes the new initial position.
10: Go to step 7 until we reach the right end of the disk.
11: If we reach the right end of the disk reverse the direction and go to step 4 until all tracks in the
request array have been serviced.
12: Stop the program.
PROGRAM:
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,ref[10],max,initial,n,TotalHeadMoment=0,mov,j,index;
printf("Enter the number of requests:");
scanf("%d",&n);
printf("Enter the request sequence:");
for(i=0;i<n;i++){
scanf("%d",&ref[i]);
}
printf("Enter the initial head position:");
scanf("%d",&initial);
printf("Enter the max range:");
scanf("%d",&max);
printf("Enter the head moment direction (for high:1 and for low:0) :");
scanf("%d",&mov);
OUTPUT:
Enter the number of requests:8
Enter the request sequence:95
180
34
119
11
123
62
64
Enter the initial head position:50
Enter the max range:200
Enter the head moment direction (for high:1 and for low:0) :1
Total Head Moment:382
VIVA QUESTIONS:
During regular intervals that are set by the operating system, processes can be copied from
main memory to a backing store, and then copied back later. Swapping allows more operations
to be
run that can fit into memory at one time.
During regular intervals that are set by the operating system, processes can be copied from
main memory to a backing store, and then copied back later. Swapping allows more operations
to be run that can fit into memory at one time.
A real time operating system is used when rigid time requirement have been placed on the
operation of a processor or the flow of the data; thus, it is often used as a control device in a
dedicated application. Here the sensors bring data to the computer. The computer must analyze
the data and possibly adjust controls to modify the sensor input.
They are of two types:
1. Hard real time OS
2. Soft real time OS
Hard-real-time OS has well-defined fixed time constraints. But soft real time operating systems
have less stringent timing constraints.