OS RECORD (1)
OS RECORD (1)
OS RECORD (1)
Website: www.jec.ac.in
1
ANNA UNIVERSITY
JAYA ENGINEERING COLLEGE
THIRUNINRAVUR-602 024
(An ISO 9001:2000 Certified Institution)
Email:info@jec.ac.in
Website:
www.jec.ac.in
BONAFIDE CERTIFICATE
Mr./Ms. Reg.No. of
2
INDEX
LIST OF EXPERIMENTS:
EX. DATE NAME OF THE EXPERIMENT PAGE Cos POs &PSOs MARK SIGN
no NO Mapped Mapped
1. Illustration of windows OS
2. Basics of UNIX Commands
3.1 Implements System calls
fork(),exec(),getpid(),getppid(),wait()
3.2 Implement system calls opendir(),readir()
4.1 CPU Scheduling Algorithm(Round Robin
Algorithm)
4.2 SJF Algorithm
4.3 FCFS Algorithm
4.4 Priority Scheduling Algorithm
5. Implementation of Shared Memory and
IPC
6.1 To perform matrix addition in child
process using shared memory
6.2 To generate ODD number using Shared
memory.
6.3 Implementation of semaphores
7. Implementation of Bankers Algorithm for
Deadlock avoidance
8. Implementation of Deadlock Detection
Algorithm
9. Write C program to Implement Threading
&Synchronization application
3
INDEX
S NO PARTICULARS PAGE NO
4
1. VISION AND MISSION OF THE INSTITUTE
Vision of the Institute
To achieve excellence in technical education through innovative teaching and
multidisciplinary research with professionalism to serve the global society.
M1 -To provide state of art teaching and learning for Engineering and Technology,
Research and Management studies.
M3 - To associate with R&D and industries to have connectivity with the society.
5
3. PROGRAM EDUCATIONAL OBJECTIVES
Graduates can
PEO1: Apply their technical competence in computer science to solve real world problems, with
technical and people leadership.
PEO2: Conduct cutting edge research and develop solutions on problems of social relevance.
PEO3: Work in a business environment, exhibiting team skills, work ethics, adaptability and
lifelong learning.
4. PROGRAM OUTCOMES
PO2 - 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.
PO4 - Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of
the information to provide valid conclusions.
PO5 - Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering
activities with an understanding of the limitations.
PO6 - 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.
6
PO7 - Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and
need for sustainable development.
PO8 - Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
PO9 - Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
PO11 - 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.
PO12 - Life-long learning: Recognize the need for, and have the preparation and ability to engage
7
5. PROGRAMME OUTCOMES
Engineering Graduates will be able to:
PSO1: Exhibit design and programming skills to build and automate
business solutions using cutting edge technologies
PSO2: Strong theoretical foundation leading to excellence and excitement towards
research, to provide elegant solutions to complex problems.
PSO3: Ability to work effectively with various engineering fields as a team to design, build
and develop system applications.
6. COURSE OUTCOME
8
9
Ex.No : 1 INSTALLATION OF WINDOWS OPERATING
SYSTEM
DATE:
AIM:
To know how to install the windows operating system
INTRODUCTION:
Each version of Microsoft Windows is installed on a computer using similar steps.
While there are steps in the installation process that differ between versions of Windows, the
following general steps and guidelines help you install Windows on your computer. The
steps below are for all recent versions of Windows, including Windows 98, Windows ME,
Windows 2000, Windows XP, Windows Vista, Windows 7, Windows 8, Windows 10, and
Windows 11. These steps even work for earlier versions (e.g., Windows 95) as long as you
use the disc version. The floppy diskette version is similar, but it requires additional steps.
10
● If pressing F2 opens a diagnostics tool, your setup key is likely F10.
● F10 is also used for the boot menu. If pressing F10 opens a boot menu, your
setup key is likely F2.
Setup keys are to be pressed as the computer is booting up. Most users see a
message similar to the example below upon startup. Some older computers may also display
a flashing block of text to indicate when to press F1 or F2.
Press F2 to enter BIOS setup.Once you have successfully entered the CMOS
setup, a screen similar to the example below appears. Your CMOS setup may look different,
depending on the manufacturer, but it should still share a lot of the same options and
information.
Acer BIOS:
If your Acer computer cannot boot or you want to restore the BIOS to its
original settings, press and hold the F10 as you turn on the computer. While holding F10,
two beeps should be heard to indicate the settings are restored.
AMI BIOS:
Older AMI BIOS could be restored to bootable settings by pressing and holding
,Insert as the computer is booting.
12
doing this). Keep pressing the key until the computer stops booting, and you have the option
to enter setup. If this does not work, make sure your keyboard is working.
HOW TO EXIT THE BIOS OR CMOS:
There are several ways to exit the BIOS or CMOS setup depending on the
computer's type. The most common methods include the following.
● Press the Esc key to exit without saving any changes.
● Press the F10 or F12 key to save changes and exit.
● Access the Exit or Save & Exit tab in setup and select the Exit or Save
and Exit option.
If you have trouble exiting the BIOS or CMOS setup, you can try the
following methods to fix the problem.
▪ Press the F9 key to load default settings and press F10 to save and exit.
▪ Access the Exit or Save & Exit tab in setup, select the Load Optimized
Defaults option, select Yes, and press Enter. Any changes made are
reverted, and the BIOS or CMOS is set back to default settings.
▪ Turn off the computer (use only as a last resort and with caution).
RESULT:
Thus the study exercise for installing windows operating system is studied successfully
14
Ex.No : 2 UNIX COMMANDS AND SHELL PROGRAMMING
DATE:
Aim:
To study basic UNIX commands.
15
(5)Command : ls (List Files and Directories)
a. ls
[cselab@jec]$ ls
111 2.prn cpgm.c HelloWorld.class mac.c print.c
1. ccc a.out duplicate.c HelloWorld.java Makefile sample.c
1. docx array.c fib.c inc.c OS streverse.c
1. prn CP fun.c JAVA pr.c
vin [cselab@jec]$
d.[cselab@jec]$ ls –x
111 1.ccc 1.docx 1.prn 2.prn a.out
array.c CP cpgm.c duplicate.c fib.c
fun.c HelloWorld.class HelloWorld.java inc.c
JAVA mac.c
Makefile OS pr.c print.c sample.c streverse.c vin [cselab@jec]$
(5) Command : mv (Move)
a. mv (Move a File to Directory)
Syntax : mv SourceFile DestinationDirectory
[cselab@jec]$ mv coll.txt programs
[cselab@jec]$ cd programs [cselab@jec
programs]$ ls
coll.txt shellprogram
[cselab@jec programs]$
16
b. mv (Move Contents of one File to
Another File) Syntax : mv SourceFile
DestinationFile [cselab@jec]$ cat one.txt
Hi, Welcome to CSE Lab
SVCET
[cselab@jec]$ mv one.txt new.txt
[cselab@jec]$ cat new.txt
Hi, Welcome to CSE Lab
SVCET
[cselab@jec]$
1) Command : rm (Remove
a File) Syntax : rm filename
[cselab@jec]$ ls
a.out college2.txt file.txt firs.sh one programs
col1.txt college.txt fir.sh new.txt pgms
second.sh [cselab@jec]$ rm new.txt
[cselab@jec]$ ls
a.out college2.txt file.txt firs.sh pgms second.sh
col1.txt college.txt fir.sh one programs
[cselab@jec]$
2) Command : rmdir (Remove a
Diretory) Syntax : rmdir Directoryname
[cselab@jec programs]$ ls
coll.txt shellprogram
[cselab@jec programs]$ rmdir shellprogram
[cselab@jec programs]$ ls
coll.txt
[cselab@jec programs]$
3) Command : rm -rf (Remove a Diretory which is not Empty)
Syntax : rm –rf Directoryname
[cselab@jec programs]$ ls
coll.txt
[cselab@jec]$ rm -rf programs
[cselab@jec]$ ls
a.out college2.txt file.txt firs.sh pgms
col1.txt college.txt fir.sh one second.sh
[cselab@jec]$
4) Command : echo (Prints the Given String)
a) Syntax: echo “String”
[cselab@jec]$ echo
"SVCET" SVCET
[cselab@jec]$
b) Syntax: echo –n “String” (Don’t Print a
New Line) [cselab@jec]$ echo -n "SVCET"
SVCET [cselab@jec]$
5) Command : head (Prints required no. of lines in the File Counting from
the Beginning of the File)
Syntax : head –n filename
n : Number of Lines to be
displayed [cselab@jec]$ cat
one.txt Welcome to SVCET
17
Puliyangudi
CSE Dept
Syntax: cal
[cselab@jec]$
cal [cselab@jec]
$ cal
December 2016
Su Mo Tu We Th Fr Sa
123
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
18
[cselab@jec]$ cal 5 2009
May 2009
Su Mo Tu We Th Fr Sa
1 2
1) Command : grep (Displays a Line from the file Containing the Given String)
Syntax : grep “String” filename
Syntax : grep –i “String” filename
i – Ignore Case of the Given
String [cselab@jec]$ cat one.txt
Welcome to SVCET
Puliyangudi
CSE Dept
Computer Practices Laboratory
[cselab@jec]$ grep "Ramco" one.txt
Welcome to SVCET
[cselab@jec]$
Result
Thus the study and execution of UNIX commands has been completed successfully.
19
Ex.No : 3.1 Implement System Calls fork(), Exec(), getpid(), getppid(), wait()
DATE:
Aim:
To write the program using System Calls fork(), Exec(), getpid(), getppid(), wait() .
Algorithm:
Step 1 : Declare the variable pid.
Step 2 : Get the pid value using system call fork().
Step 3 : If pid value is less than zero then print as “Fork failed”.
Step 4 : Else if pid value is equal to zero include the new process in the
system‟s file using execlp system call.
Step 5 : Else if pid is greater than zero then it is the parent
process and it waits till the child completes using the system call
wait() Step 6 : Then print “Child complete”.
Program:
#include<stdio.h>
#include<sys/types.h
> #include<string.h>
#include<unistd.h>
#include<sysexits.h>
#include<stdlib.h>
main()
{
int pid;
pid=fork();
if(pid<0)
printf("error");
else if(pid>0)
{
wait(1000);
printf("\n Parent Process:\n");
printf("\n\tParent Process id:%d\t\n",getppid());
execlp("/bin/ls","ls",NULL);
}
else
{
printf("\nChild process:%d\n",getpid()); printf("\n\
tChildprocess parent id:\t %d\n",getppid());
execlp("cal","cal",NULL);
exit(0);
}
}
20
Output:
[cse6@localhost Pgm]$ cc prog4a.c
[cse6@localhost Pgm]$ ./a.out
Result
Thus the program using System Calls fork(), Exec(), getpid(), getppid(), wait() has
been completed successfully.
21
Ex.No : 3.2
Implement System calls opendir(),readdir()
DATE:
Aim:
To write the program using System Calls opendir(),readdir().
Algorithm:
Step 1 : Start.
Step 2 : In the main function pass the arguments.
Step 3 : Create structure as stat buff and the variables as integer.
Step 4 : Using the for loop,initialization.
Program:
#include<sys/types.h>
#include<dirent.h>
#include<stdio.h>
main(int c,char* arg[])
{
DIR *d;
struct dirent *r;
int i=0;
d=opendir(arg[1]);
printf("\n\t NAME OF ITEM
\n"); while((r=readdir(d)) !=
NULL)
{
printf("\t %s \n",r->d_name); i=i+1;
}
printf("\n TOTAL NUMBER OF ITEM IN THAT DIRECTORY IS %d \n",i);
}
Result:
Thus the program using System Calls opendir(),readdir() has been completed
Successfully.
22
EX.NO:4.1 CPU SCHEDULING ALGORITHM
( Round Robin Algorithm )
DATE :
AIM:
To write a C program to implement round robin scheduling algorithm.
ALGORITHM:
1. Read no. of processes and time quantum( TQ).
2. Read process name and burst time(BT) for each process.
3. Ready queue is treated as circular queue.CPU schedules all processes (according
to their of order of arrival) only up to given time quantum.
4. A timer is set to interrupt the scheduling if time quantum expires for a process.
5. If BT of process is greater than TQ then after executing upto TQ, it gets added to tail
of ready queue.
6. If BT of process is less than TQ then CPU gets released from it and schedules
next process in ready queue.
7. Set waiting time (WT) of first process as zero and turnaround time(TAT) as burst time.
8. Calculate waiting time and turnaround time of other processes
as follows: Pi (WT) = P i-1(WT) +P i-1(BT)
P i (TAT) = P i (BT) + P i (WT)
9. Calculate and display average WT and TAT.
10. Display order of execution of processes ie. Process name, burst time, WT and TAT.
PROGRAM:
#include<stdio.h>
int main()
{
int i, limit, total = 0, x, counter = 0, time_quantum;
float average_wait_time, average_turnaround_time;
printf("\nEnter Total Number of Processes:\t");
scanf("%d", &limit);
x = limit;
for(i = 0; i < limit; i++)
{
printf("\nEnter Details of Process[%d]\n", i + 1);
printf("Arrival Time:\t");
scanf("%d",
&arrival_time[i]);
printf("Burst Time:\t");
scanf("%d", &burst_time[i]);
printf("\nEnter Time Quantum:\t");
scanf("%d", &time_quantum);
printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");
for(total = 0, i = 0; x != 0;)
{
if(temp[i] <= time_quantum && temp[i] > 0)
{
23
total = total + temp[i];
temp[i] = 0;
counter = 1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - time_quantum;
total = total + time_quantum;
}
if(temp[i] == 0 && counter == 1)
{
x--;
printf("\nProcess[%d]\t\t%d\t\t %d\t\t\t %d", i + 1,
burst_time[i], total - arrival_time[i],
total - arrival_time[i] - burst_time[i]);
turnaround_time = turnaround_time + total - arrival_time[i];
counter = 0;
}
if(i == limit - 1)
{
i = 0;
}
else if(arrival_time[i + 1] <= total)
{
i++;
}
else
{
i = 0;
}
}
average_wait_time = wait_time * 1.0 / limit;
average_turnaround_time = turnaround_time * 1.0 / limit; printf("\
n\nAverage Waiting Time:\t%f", average_wait_time); printf("\
nAvg Turnaround Time:\t%f\n", average_turnaround_time); return
0;
}
24
Output
[cselab@jec ~]$ cc round.c
[cselab@jec ~]$ ./a.out
Enter Total Number of Processes: 3
Enter Details of
Process[1] Arrival Time:
1
Burst Time: 24
Enter Details of
Process[2] Arrival Time:
2
Burst Time: 3
Enter Details of
Process[3] Arrival Time:
3
Burst Time: 3
Enter Time Quantum: 3
Process ID Burst Time Turnaround Time Waiting Time
Process[2] 3 4 1
Process[3] 3 6 3
Process[1] 24 29 5
RESULT:
Thus the Round Robin CPU scheduling algorithms was executed successfully.
25
EX.NO: 4 .2
Shortest Job First Algorithm
DATE :
AIM:
To write a C program to implement SJF (Shortest Job First) scheduling algorithm.
ALGORITHM:
Step1:Read no. of processes.
Step2:Read process name and burst time for each process.
Step3:Sort the processes in ready queue according to burst time.CPU schedules
process with shortest burst time first followed by other processes.
Step4:Set waiting time(WT) of first process as zero and turnaround time(TAT) as
burst time.
Step5:Calculate waiting time and turnaround time of other processes as
follows: Pi (WT) = P i-1(WT) +P i-1(BT)
P i (TAT) = P i ( BT) + P i (WT)
Step6:Calculate and display average WT and TAT.
Step7:Display order of execution of processes ie. Process name, burst time, WT and
TAT.
PROGRAM:
#include<stdio.h>
#include<string.h>
struct jo
{
Charjn[20];
int bt;
int wt,tat;
}
j[20];
main()
{
int i,tmp,n; char
t[20];
float avgwt,avgtat;
printf("\n\t SJF SCHEDULING ALGORITHM \n\n");
printf("Enter no. of jobs\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter job name of job %d : ",i);
26
scanf("%s",j[i].jn);
printf("\nEnter burst time of job %d : ",i);
scanf("%d",&j[i].bt);
}
for(j=i+1;j<=n;j++)
{
if( j[i].bt > j[j].bt)
{
strcpy( t, j[i].jn);
strcpy(j[i].jn,
j[j].jn);
strcpy(j[j].jn, t);
tmp= j[i].bt;
j[i].bt=j[j].bt;
j[j].bt=tmp;
}
}
}
j[1].wt=0;
j[1].tat=j[1].bt;
for(i=2;i<=n;i++)
{
j[i].wt=j[i-1].wt+j[i-1].bt;
j[i].tat=j[i].wt+j[i].bt;
}
printf("\n\n\tORDER OF EXECUTION OF JOBS\n\n"); printf("\tJN\tBT\tWT\tTAT\n\
n");
for(i=1;i<=n;i++)
{
printf("\t%s\t%d\t%d\t%d\n",j[i].jn,j[i].bt,j[i].wt,j[i].tat);
avgwt=avgwt+j[i].wt;
avgtat=avgtat+j[i].tat;
}
avgwt=avgwt/n; avgtat=avgtat/n; printf("\n\nAVG
WAITING TIME: %f",avgwt);
printf("\n\nAVG TURN AROUND TIME : %f\n\n",avgtat);
}
27
Output
[cselab@jec ~]$ cc sjf.c
[cselab@jec ~]$ ./a.out
RESULT:
Thus the Shortest Job First CPU scheduling algorithms was executed successfully.
28
EX.NO: 4.3 First Come First Serve Algorithm (FCFS)
DATE :
AIM:
To write a C program to implement FCFS scheduling algorithm.
ALGORITHM:
Step1:Read no. of processes.
Step2:Read process name and burst time for each process.
Step3:CPU schedules processes according to their order of arrival in read queue (ie It
first executes process which is at head of ready queue)
Step4:Set waiting time(WT) of first process as zero and turnaround time(TAT) as burst
time.
Step5:Calculate waiting time and turnaround time of other processes as follows:
Pi (WT) = P i-1(WT) +P i-1(BT)
P i (TAT) = P i ( BT) + P i (WT)
Step6:Calculate and display average WT and TAT.
Step7:Display order of execution of processes ie. Process name, burst time, WT and TAT.
PROGRAM:
#include<stdio.h>
int main()
printf("Enter The Number of Processes To Execute:\t");
scanf("%d", &total_process);
printf("\nEnter The Burst Time of Processes:\n\n");
for(count = 0; count < total_process; count++)
{
printf("Process [%d]:", count + 1);
scanf("%f", &burst_time[count]);
}
waiting_time[0] = 0;
for(count = 1; count < total_process; count++)
{
waiting_time[count] = 0;
for(j = 0; j < count; j++)
{
waiting_time[count] = waiting_time[count] + burst_time[j];
}
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time\n");
for(count = 0; count < total_process; count++)
{
turnaround_time[count] = burst_time[count] + waiting_time[count];
average_waiting_time = average_waiting_time + waiting_time[count];
average_turnaround_time = average_turnaround_time + turnaround_time[count];
printf("\nProcess [%d]\t\t%.2f\t\t%.2f\t\t%.2f", count + 1,
burst_time[count], waiting_time[count], turnaround_time[count]);
}
printf("\n");
29
average_waiting_time = average_waiting_time / count;
average_turnaround_time = average_turnaround_time / count;
printf("\nAverage Waiting Time = %f", average_waiting_time); printf("\
nAverage Turnaround Time = %f", average_turnaround_time); printf("\n");
return 0;
}
Output
[cselab@jec ~]$ cc fcfs.c
[cselab@jec ~]$ ./a.out
Enter The Number of Processes To Execute:
Process [1]:24
Process [2]:3
Process [3]:3
RESULT:
Thus the First in First Out CPU scheduling algorithms was executed successfully.
30
EX.NO: 4 .4 Priority Scheduling Algorithm
DATE:
AIM:
To write a C program to implement priority scheduling algorithm.
ALGORITHM:
Step1:Read no. of processes.
Step2:Read process name, burst time and priority for each process.
Step3:Sort the processes in ready queue according to priority. (i.e. Process with high
priority get placed at head of ready queue) CPU schedules process with high priority
first followed by other processes.
Step4:Set waiting time (WT) of first process as zero and turnaround time (TAT) as
burst time.
Step5:Calculate waiting time and turnaround time of other processes as follows:
Pi (WT) = P i-1(WT) +P i-1(BT)
P i (TAT) = P i (BT) + P i (WT)
Step6:Calculate and display average WT and TAT.
Step7:Display order of execution of processes ie. Process name, burst time, priority,
WT and TAT.
Step8:Stop the program.
PROGRAM:
#include<stdio.h>
int main()
{
int burst_time[20], process[20], waiting_time[20], turnaround_time[20],
priority[20];
int i, j, limit, sum = 0, position, temp;
float average_wait_time, average_turnaround_time;
printf("Enter Total Number of Processes:\t");
scanf("%d", &limit);
printf("\nEnter Burst Time and Priority For %d Processes\n", limit);
for(i = 0; i < limit; i++)
{
printf("\nProcess[%d]\n", i + 1);
printf("Process Burst Time:\t");
scanf("%d", &burst_time[i]);
printf("Process Priority:\t");
scanf("%d", &priority[i]);
process[i] = i + 1;
}
for(i = 0; i < limit; i++)
{
position = i;
for(j = i + 1; j < limit; j++)
{
if(priority[j] < priority[position])
31
{
position = j;
}
}
temp = priority[i];
priority[i] =
priority[position];
priority[position] = temp;
temp = burst_time[i];
burst_time[i] = burst_time[position];
burst_time[position] = temp;
temp = process[i];
process[i] = process[position];
process[position] = temp;
}
waiting_time[0] = 0;
for(i = 1; i < limit;
i++)
{
waiting_time[i] =
0; for(j = 0; j < i; j+
+)
{
waiting_time[i] = waiting_time[i] + burst_time[j];
}
sum = sum + waiting_time[i];
}
average_wait_time = sum / limit;
sum = 0;
printf("\nProcess ID\t\tBurst Time\t Waiting Time\t Turnaround Time\
n"); for(i = 0; i < limit; i++)
{
turnaround_time[i] = burst_time[i] + waiting_time[i];
sum = sum + turnaround_time[i]; printf("\nProcess[%d]\
t\t%d\t\t %d\t\t %d\n", process[i], burst_time[i],
waiting_time[i], turnaround_time[i]);
}
average_turnaround_time = sum / limit;
printf("\nAverage Waiting Time:\t%f", average_wait_time); printf("\
nAverage Turnaround Time:\t%f\n", average_turnaround_time); return
0;
}
32
Output
[cselab@jec ~]$ cc prior.c
[cselab@jec ~]$ ./a.out
Enter Total Number of Processes: 3
Enter Burst Time and Priority For 3 Processes
Process[1]
Process Burst 5
Time: Process 3
Priority:
Process[2] 8
Process Burst
Time:
Process 2
Priority:
Process[3] 7
Process
Burst Time:
Process 1
Priority: Burst Time Waiting Turnaround
Process ID Time Time
Process[3] 7 0 7
Process[2] 8 7 15
Process[1] 5 15 20
RESULT:
Thus the Priority CPU scheduling algorithm was executed successfully.
33
EX.NO: 5 IMPLEMENTATION OF SHARED MEMORY AND
IPC DATE:
AIM:
To write a C program to implement IPC (Inter Process Communication) via message
queue.
ALGORITHM:
Sender side
Step1:Establish a message queue using msgget() with unique msg id.
Step2:Read 5 messages from user using fgets( ) and transmit it to receiver
using msgsnd( ).
Receiver side
Step1:Establish a connection with message queue of sender.
Step2:Receive and print messages from queue using msgrcv( ).
PROGRAM:
Sender Side:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h
>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<errno.h>
#include<string.h>
#define NMSGS 5
extern int errno;
struct msgbuf
{
long
mtype; char
mtext[100];
};
int main()
{
int msgid;
int i,nloop;
34
struct msgbuf msgp;
35
char tmp_msg[100];
tmp_msg[0]='\0'
msgid=msgget(9999,IPC_CREAT|0666);
if(msgid<0)
{
printf("%d:Error number is %d\n", LINE ,errno);
exit(1);
}
printf("Enter messsage(max 5lines)\n");
for(nloop=1;nloop<=NMSGS;nloop++)
{
msgp.mtype=1;
fgets(tmp_msg,100,stdin);
strncpy(msgp.mtext,tmp_msg,strlen(tmp_msg));
i=msgsnd(msgid,&msgp,strlen(tmp_msg),IPC_NOWAIT);
if(i<0)
{
printf("%d:Error number is%d\n", LINE ,errno);
exit(1);
}
tmp_msg[0]='\0';
}
return 0;
}
Receiver Side
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h
>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<errno.h>
#include<string.h>
extern int errno;
36
struct msgbuf
37
{
long mtype;
char mtext[100];
};
int main()
{
int
msgid;
int
i,nloop;
struct msgbuf msgp;
msgid=msgget(9999,0444);
if(msgid<0)
{
printf("%d:Error number is %d\n", LINE ,errno);
exit(1);
}
for(nloop=0;nloop<5;nloop++)
{
bzero(msgp.mtext,100);
i=msgrcv(msgid,&msgp,100,1,IPC_NOWAIT);
if(i<0)
{
printf("%d:Error number is %d\n", LINE ,errno);
exit(1);
}
msgp.mtext[strlen(msgp.mtext)]='\0';
fwrite(msgp.mtext,sizeof(char),strlen(msgp.mtext),stdout);
printf("Message is %s\n",msgp.mtext);
}
if(msgctl(msgid,IPC_RMID,NULL)<0)
{
printf("%d:Error number is %d\n", LINE ,errno);
exit(1);
}
return 0;
38
Output:
"ex31.c" 44L, 734C written
[cselab@jec ~]$ cc ex31.c
[cselab@jec ~]$ ./a.out Enter
messsage(max 5lines) welcome
to SVC
Welcome to OS Lab
Welcome to Second
year Good
[cselab@jec ~]$ cc ex32.c
[cselab@jec ~]$ ./a.out
welcome
Message is welcome
To SVC
Message is to SVC
Welcome to OS
Lab
Message is Welcome to OS Lab
Welcome to Second year
Message is Welcome to Second year
Good
Message is Good
[cselab@jec ~]$
RESULT:
Thus the Inter Process Communication program was executed successfully.
39
ExNo 6.1
To perform matrix addition in child process using shared memory
DATE:
AIM:
To write a C program to perform matrix addition in child process using shared memory
Algorithm
Step1:Enter the order of matrix
Step2:Using shmget create shared memory for parent and child process and attach shared
memory to processes.
Step3:Enter the element of matricse stored in to shared memory variable.
Step4:Calculate addition of matrices using shared memory.
Step5:Stop the
program Program
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
int main()
{
int
p,k,i,n,l,m,*c;
int *a,*b;
printf("enter the order of matrix");
scanf("%d",&n);
k=shmget(IPC_PRIVATE,n*sizeof(int),00700);
a=(int*) shmat(k,0,0);
l= shmget(IPC_PRIVATE,n*sizeof(int),00700);
b=(int*) shmat(k,0,0);
m= shmget(IPC_PRIVATE,n*sizeof(int),00700);
c=(int*) shmat(k,0,0);
printf("enter the elements of a");
for(i=0;i<n*n;i++)
{
40
scanf("%d",(a+i));
}
printf("enter the elements of b");
for(i=0;i<n*n;i++)
{
scanf("%d",(b+i));
}
p=fork();
if(p==0)
{
for(i=0;i<(n*n)/2;i++)
*(c+i)=*(a+i)+*(b+i);
}
printf("matrix
addition");
for(i=0;i<n*n;i++)
printf("%d\t",*(c+i));
}
if(p!=0)
{
for(i=(n*n)/2;i<n*n;i++)
{
*(c+i)=*(a+i)+*(b+i);
}
}
for(i=0;i<n*n;i++)
{
printf("%d\t",*(c+i));
return 0;
}
41
}
42
Output
[cselab@jec ~]$ cc matadd.c
[cselab@jec ~]$ ./a.out enter the
order of matrix3
enter the elements of a1 2
3456
789
345
4 1 3
3 4 5
8 10 12
4 1 3
3 4 5
Result:
Thus the program to perform matrix addition in child process using shared memory
was written and executed successfully.
43
Ex No: 6.2
To generate ODD number using Shared memory.
DATE:
AIM:
To write a C program to generate ODD number using shared memory.
Algorithm
Step1:Enter the number of elements.
Step2:Using shmget create shared memory for parent and child process and attach shared
memory to processes.
Step3:Using shared memory variable find out the odd number.
Step4:Generate odd series.
Step5:Stop the program.
Program
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
main()
{
int k;
int
*a;
int
n,i;
printf("enter the no of elements");
scanf("%d",&n);
k=shmget(IPC_PRIVATE,n*n*sizeof(int),00700);
a=shmat(k,NULL,0);
printf("enter the
elements");
for(i=0;i<n;i++)
scanf("%d",(a+i));
int b=fork();
printf("odd
44
number"); if(b!=0)
{for(i=0;i<n;i++)
45
{
if((*a+i)%2!
printf("%d",*(a+i));
}
}
if(b==0)
for(i=n/2;i<n;i++)
{
if(*(a+i)%2!=0)
printf("%d",*(a+i));
}
}
shmdt(a);
}
Output
[cselab@jec os]$ cc odd.c
[cselab@jec os]$ ./a.out enter
the no of elements3
enter the elements1
2
3
odd number13[cselab@jec os]$
Result
Thus the Program to To generate ODD number using Semaphore was written and
executed successfully.
46
EX.NO:6.3 IMPLEMENTATION OF SEMAPHORES
DATE:
AIM:
To write a C program to implement PCP (Producer Consumer Problem) using
semaphores.
ALGORITHM:
Step1:Read size of buffer
Step2:Producer process produces and buffers the items using shmget() and shmctl()
Step3:Consumer process consumes item from buffer using semop() and semrel()
Step4:Producer process waits if buffer is full and consumer process waits if buffer is
empty.
PROGRAM:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h
>
#include<sys/shm.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<stdlib.h>
union semum
{
int array[3];
};
main()
{
int
k,f,i,x,n,t,sid,num[10],j,in=0,out=0;
int *l;
union semum arg;
struct sembuf a;
sid=shmget(50010,3,IPC_CREAT | 00700);
if(sid == -1)
{
printf("Not
created"); exit(0);
}
printf("Enter the no of
number");
scanf("%d",&n);
arg.array[0]=1;
47
arg.array[1]=5;
arg.array[2]=0;
48
semctl(sid,0,SETALL,arg);
k=shmget(IPC_PRIVATE,5*sizeof(int),00700);
l=(int*) shmat(k,NULL,0);
f=fork();
printf(" fork value=%d",f);
if(f != 0)
{
for(i=0;i<n;i++)
{
printf("Enter the number\n"); scanf("%d\
n",&num[i]);
a.sem_num=1;
a.sem_op=-1;
a.sem_flg=0;
semop(sid,&a,-1);
a.sem_num=0;
a.sem_op=-1;
a.sem_flg=0;
semop(sid,&a,-1);
l[in]=num[i];
in=(in+1)%n;
a.sem_num=0;
a.sem_op=1;
a.sem_flg=0;
semop(sid,&a,1);
a.sem_num=2;
a.sem_op=1;
a.sem_flg=0;
semop(sid,&a,1);
}
shmctl(k,IPC_RMID,NULL);
}
if(f==0)
{
for(j=0;j<n;j++)
{
a.sem_num=2;
a.sem_op=-1;
a.sem_flg=0;
semop(sid,&a,1);
a.sem_num=0;
a.sem_op=-1;
a.sem_flg=0;
semop(sid,&a,1);
x=l[out];
out=(out+1)%n;
a.sem_num=0;
a.sem_op=1;
49
a.sem_flg=0;
semop(sid,&a,1);
a.sem_num=0;
a.sem_op=1;
a.sem_flg=0;
semop(sid,&a,1);
printf("\n Consumer %d",x);
}
}
}
Output
[cselab@jec ~]$ cc semaphore.c
[cselab@jec ~]$ ./a.out
Enter the no of number3
Enter the number 6 3 4
Consumer consumes 4
Consumer consumes 3
Consumer consumes 6
Result
Thus the program to implement PCP (Producer Consumer Problem) using semaphores
was written and executed successfully.
50
EX.NO: 7 Implementation of Bankers Algorithm for Deadlock Avoidance
DATE:
AIM:
A program to simulate the Bankers Algorithm for Deadlock Avoidance.
Algorithm
Step1:Get the number of processes and number of resource instances.
Step2:Get the allocation matrix and Available matrix from the user.
Step3:Calculate need matrix.
Step4:Using banker’s algorithm allocate resources to processes.
Step5:Print safe sequence of processes..
Step6:Stop the program.
PROGRAM:
#include<stdio.h>
int max[100][100];
int alloc[100]
[100]; int
need[100][100]; int
avail[100];
int n,r;
void input();
void
show();
void cal();
int main()
{
int i,j;
printf("********** Banker's Algorithm ************\n");
input();
show();
cal();
//
getch();
51
return 0;
}
void input()
52
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resources instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
53
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++
)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d
",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100]
[100],flag=1,k,c1=0; int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
54
printf("\n");
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++
)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
printf("P%d->",i);
if(finish[i]==1)
{
i=n;
}
}
}
}
}
}
for(i=0;i<n;i++)
{
if(finish[i]==1)
55
{
c1++;
}
else
{
printf("P%d->",i);
}
}
if(c1==n)
{
printf("\n The system is in safe state");
}
else
{
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}
}
56
Output
********** Banker's Algorithm
************ Enter the no of Processes
5 Enter the no of resources instances 3
Enter the Max Matrix
753
322
902
222
433
Enter the Allocation
Matrix 0 1 0
200
302
211
002
Enter the available Resources
332
Process Allocation Max Available
P1 010 753332
P2 200 322
P3 302 902
P4 211 222
P5 002 433
P1->P3->P4->P2->P0->
The system is in safe state
RESULT:
Thus the program for implementing deadlock avoidance algorithm was implemented
has been executed successfully.
57
EX.NO: 8 Implementation of Deadlock Detection Algorithm
DATE :
AIM:
To write a C program to implement the concept of deadlock detection.
ALGORITHM
1. Get the number of processes and number of resource
instances. 2.Get the allocation matrix and Available matrix
from the user. 3.Calculate need matrix.
4.Using banker’s algorithm allocate resources to processes.
5.Print”deadlock occurred or not.
6.Stop the program.
PROGRAM:
#include<stdio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
58
printf("Enter the no of resource instances\t"); scanf("%d",&r);
59
printf("Enter the Max Matrix\n"); for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n"); for(i=0;i<n;i++)
{for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n"); for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t"); for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1); for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t"); for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t"); if(i==0)
{
for(j=0;j<r;j++);
printf("%d
60
",avail[j]);
61
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0; int dead[100];
int safe[100];
int i,j; for(i=0;i<n;i+
+);
{
finish[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++);
{
need[i][j]=max[i][j]-alloc[i][j];
}
{
flag=0;
for(i=0;i<n;i++);
{
int c=0;
for(j=0;j<r;j++);
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{ c+
+;
if(c==r)
{
for(k=0;k<r;k++);
{
avail[k]+=alloc[i][j];
finish[i]=1; flag=1;
if(finish[i]==1)
62
{
i=n;
}
}
}
}
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
dead[j]=i;
j++;
flag=1;
}
}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock process are\n");
for(i=0;i<n;i++)
printf("P%d\t",dead[i]);
}}
else
{
63
Output
P1 P3 P4 P2 P0 No Deadlock Occur
RESULT:
Thus the program for implementing deadlock detection algorithm was executed
successfully
64
EX.NO:9 WRITE C PROGRAM TO IMPLEMENT
THREADING & SYNCHRONISATION
APPLICATIONS
DATE:
AIM:
To write a C program to implement thread and synchronisation applications.
ALGORITHM:
Step1. Start the program
Step2. i is initialized in the beginning of the main function.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
int c;
void *myThreadFun(void *vargp)
{
int i=0;
printf("Thread %d is processing \n",vargp);
//c+=1;
//for(i=0;i<(0xFFFFFFFF);i++);
sleep(40);
printf("Thread %d is completed\n",vargp);
pthread_exit(NULL);
}
void *mythread(void *vargp)
{
printf("Thread %d is running",vargp);
65
pthread_exit(NULL);
66
}
int main()
{
int i,x;
pthread_t
a[6];
printf("Before Thread\
n"); for(i=0;i<5;i++)
{
x=pthread_create(&a[i],NULL,myThreadFun,(void
*)i); if(x!=0)
printf("Thread not created");
}
//for(i=0;i<5;i++)
//{
//x=pthread_create(&a[i],NULL,mythread,(void *)i);
//if(x!=0)
// printf("Thread is not created\n");
//}
pthread_exit(NULL);
67
Output
Before Thread
Thread 3 is
processing Thread 4
is processing Thread
2 is processing
Thread 0 is
processing Thread 1
is processing Thread
3 is completed Thread
4 is completed Thread
2 is completed Thread
0 is completed Thread
1 is completed
[cselab@jec ~]$
RESULT:
Thus the program for implementing thread and synchronisation application was
executed successfully
68
EX.NO: 10.1 IMPLEMENTATION OF MEMORY MANAGEMENT - PAGING
DATE:
AIM:
ALGORITHM:
Step 1: Start the program.
Step 2: Enter the logical memory address i.e no of pages in memory.
Step 3: Enter the page table which has offset and page frame.
int page[MAX],i,n,f,ps,off,pno;
printf("\nEnter the no of pages
in memory"); scanf("%d",&n);
printf("\nEnter page size");
scanf("%d",&ps); printf("\
nEnter no of frames");
scanf("%d",&f); for(i=0;i<n;i+
+)
page[i]=-1;
{
printf("\n\n%d\t\t",i);
69
scanf("%d",&page[i]);
}
Output
Enter the no of pages in
memory4 Enter page size2
Enter no of frames4
Enter the page table
0 3
1 5
2 1
3 7
RESULT:
Thus C program for implementing paging concept for memory management has been
executed successfully.
70
EX.NO:10.2 MEMORY ALLOCATION METHODS FOR FIXED PARTITION
FIRST FIT ALLOCATION
DATE:
AIM:
To allocate memory requirements for processes using first fit allocation.
ALGORITHM:
1. Declare structures hole and process to hold information about set of holes
and processes respectively.
2. Get number of holes, say nh.
3. Get the size of each hole
4. Get number of processes, say np.
5. Get the memory requirements for each process.
6. Allocate processes to holes, by examining each hole as follows:
a. If hole size>process size then
i. Mark process as allocated to that hole.
ii. Decrement hole size by process size.
b. Otherwise check the next from the set of holes
7. Print the list of process and their allocated holes or unallocated status.
8. Print the list of holes, their actual and
current availability
.9.Stop the program.
PROGRAM
#include<stdio.h>
struct process
{
int size; int flag; int hole_id;
}p[10];
struct hole
{
int size;
int
actual;
}h[10];
main()
{
int i,np,nh,j;
printf("EnterthenumberofHoles:");
scanf("%d",&nh); for(i=0;i<nh;i+
+){ printf("EntersizeforholeH
%d:",i);
71
scanf("%d",&h[i].size);
h[i].actual=h[i].size;
}
printf("\nEnternumberofprocess:");
scanf("%d",&np); for(i=0;i<np;i++)
{
printf("enterthesizeofprocessP%d:",i);
scanf("%d",&p[i].size);
p[i].flag=0;
}
for(i=0;i<np;i++){
for(j=0;j<nh;j++){
if(p[i].flag!=1){
if(p[i].size<=h[j].size){
p[i].flag=1; p[i].holeid=j; h[j].size-=p[i].size;
}}}}
printf("\n\tFirstfit\n"); printf("\
nProcess\tPSize\tHole");
for(i=0;i<np;i++){ if(p[i].flag!
=1)
printf("\nP%d\t%d\tNotallocated",i,p[i].size);
else printf("\nP%d\t%d\tH
%d",i,p[i].size,p[i].holeid);
}
printf("\n\nHole\tActual\tAvailable");
for(i=0;i<nh;i++) printf("\nH%d\t%d\t
%d",i,h[i].actual,h[i].size); printf("\n");
}
72
OUTPUT:
RESULT:
Thus processes were allocated memory using first fit method.
73
EX. NO:10.3 WORST FIT
ALLOCATION DATE:
AIM:
To allocate memory requirements for processes using worst fit allocation.
ALGORITHM:
PROGRAM:
#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];
clrscr();
printf("\n\tMemory Management Scheme - First 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++)
{
74
printf("File %d:",i);
75
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
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
Enter the number of blocks: 3
Enter the number of files: 2
Enter the size of the blocks:-
Block 1: 5
Block 2: 2
Block 3: 7
Enter the size of the files:-
File 1: 1
File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment
1 1 1 5 4
2 4 3 7 3
RESULT:
Thus processes were allocated memory using the worst fit method.
76
EX. NO:10.4 BEST FIT
ALLOCATION DATE:
AIM:
To allocate memory requirements for processes using best fit allocation.
ALGORITHM:
1. Declare structures hole and process to hold information
about set of holes and processes respectively.
2. Get the number of holes, say nh.
3. Get the size of each hole
4. Get the number of processes, say np.
5. Get the memory requirements for each process.
6. Allocate processes to holes, by examining each hole as follows:
a. Sort the holes according to their sizes in ascending order
b. If hole size>process size then
i. Mark process as allocated to that hole.
ii. Decrement hole size by process size.
c. Otherwise check the next from the set of sorted holes.
7. Print the list of processes and their allocated holes or unallocated status.
8. Print the list of holes, their actual and current availability.
9. Stop
PROGRAM:
#include<stdio.h>
struct process
{
int size;
int flag;
int
holeid;
}
p[10];
struct hole
{
int hid;
int size;
int
actual;
}
h[10];
main()
{
int i,np,nh,j;
void bsort(structhole[],int);
printf("Enter the number of Holes:");
scanf("%d",&nh);
for(i=0;i<nh;i++)
{
77
printf("Enter size for holeH%d:",i);
78
scanf("%d",&h[i].size);
h[i].actual=h[i].size;
h[i].hid=i;
}
printf("\nEnter number of process:");
scanf("%d",&np);
for(i=0;i<np;i++)
{
printf("enter the size of processP%d:",i);
scanf("%d",&p[i].size);
p[i].flag=0;
}
for(i=0;i<np;i++)
{
bsort(h,nh); for(j=0;j<nh;j+
+)
{
if(p[i].flag!=1)
{
if(p[i].size<=h[j].size)
{
p[i].flag=1;
p[i].holeid=h[j].hid;
h[j].size-=p[i].size;
}}}
}
printf("\n\tBestfit\n"); printf("\
nProcess\tPSize\tHole");
for(i=0;i<np;i++)
{
if(p[i].flag!=1) printf("\nP%d\t%d\
tNotallocated",i,p[i].size); else printf("\nP%d\t
%d\tH%d",i,p[i].size,p[i].holeid);
}
printf("\n\nHole\tActual\tAvailable"); for(i=0;i<nh;i+
+) printf("\nH%d\t%d\t%d",h[i].hid,h[i].actual,
h[i].size); printf("\n");
}
Void bsort(structholebh[],intn)
{
struct
holetemp; int
i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(bh[i].size>bh[j].size)
{
temp=bh[i];
bh[i]=bh[j];
bh[j]=temp;
}
}
}
}
79
OUTPUT:
RESULT:
Thus processes were allocated memory using the best fit method.
80
EX.NO: 11.1 First In First Out
DATE:
AIM:
To write a C program to implement FIFO page replacement algorithms.
ALGORITHM:
Step1:Read the size of the frame, no. of elements and elements one by one.
Step2:Initialise the frames with value -1.
Step3:Insert each element into frame, if it's not present.
Step4:If the frame is full and the new element is not already present then replace
the oldest element by the new element.
Step5:Increment no. of page faults by one while inserting each element into
the frames.
Step6:Display the contents of frames during processing and the total no. of page
faults.
PROGRAM:
#include<stdio.h>
int main()
{
int reference_string[10], page_faults = 0, m, n, s, pages, frames; printf("\
nEnter Total Number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter values of Reference String:\n");
for(m = 0; m < pages; m++)
{
printf("Value No. [%d]:\t", m +
1); scanf("%d",
&reference_string[m]);
}
printf("\nEnter Total Number of Frames:\t");
{
scanf("%d", &frames);
}
int temp[frames];
81
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(reference_string[m] == temp[n])
{
s++;
page_faults--;
}
}
page_faults++;
if((page_faults <= frames) && (s == 0))
{
temp[m] = reference_string[m];
}
else if(s == 0)
{
temp[(page_faults - 1) % frames] = reference_string[m];
}
printf("\n");
for(n = 0; n < frames; n++)
{
printf("%d\t", temp[n]);
}
}
printf("\nTotal Page Faults:\t%d\n", page_faults);
return 0;
}
82
Output
5
Enter values of Reference String:
Value No. [1]: 3
Value No. [2]: 5
Value No. [3]: 2
Value No. [4]: 3
Value No. [5]: 4
Enter Total Number of Frames: 3
3 -1 -1
3 5 -1
3 5 2
3 5 2
4 5 2
Result:
Thus the program to implement FIFO page replacement algorithm was written and
83
executed successfully.
84
EX.NO: 11.2 Least Recently Used (LRU)
DATE:
AIM:
To write a C program to implement LRU page replacement algorithm.
ALGORITHM:
Step1:Read the size of the frame, no. of elements and elements one by one.
Step2:Initialize the frames with value -1.
Step3:Insert each element into frame, if it’s already not present.
Step4:If the frame is full and new element is not already present then replace the least
recently used element by the new element.
Step5:Increment no. of page faults by one while inserting each element into the frames.
Step6:Display the contents of frames during processing and the total no. of page faults.
PROGRAM:
#include<stdio.h>
int main()
{
int frames[10], temp[10], pages[10];
int total_pages, m, n, position, k, l, total_frames;
int a = 0, b = 0, page_fault = 0;
printf("\nEnter Total Number of Frames:\t");
scanf("%d", &total_frames);
for(m = 0; m < total_frames; m++)
{
frames[m] = -1;
}
printf("Enter Total Number of Pages:\t");
scanf("%d", &total_pages);
printf("Enter Values for Reference String:\n");
for(m = 0; m < total_pages; m++)
{
printf("Value No.[%d]:\t", m + 1);
85
scanf("%d", &pages[m]);
}
for(n = 0; n < total_pages; n++)
{
a = 0, b = 0;
for(m = 0; m < total_frames; m++)
{
if(frames[m] == pages[n])
{
a = 1;
b = 1;
break;
}
}
if(a == 0)
{
for(m = 0; m < total_frames; m++)
{
if(frames[m] == -1)
{
frames[m] = pages[n];
b = 1;
break;
}
}
}
if(b == 0)
{
for(m = 0; m < total_frames; m++)
{
temp[m] = 0;
}
for(k = n - 1, l = 1; l <= total_frames - 1; l++, k--)
{
for(m = 0; m < total_frames; m++)
86
{
if(frames[m] == pages[k])
{
temp[m] = 1;
}
}
}
for(m = 0; m < total_frames; m++)
{
if(temp[m] == 0)
position = m;
}
frames[position] = pages[n];
page_fault++;
}
printf("\n");
for(m = 0; m < total_frames; m++)
{
printf("%d\t", frames[m]);
}
}
printf("\nTotal Number of Page Faults:\t%d\n", page_fault);
return 0;
}
87
Output
Value No.[1]: 5
Value No.[2]: 6
Value No.[3]: 3
Value No.[4]: 2
Value No.[5]: 5
Value No.[6]: 1
Value No.[7]: 8
5 -1 -1
5 6 -1
5 6 3
2 6 3
2 5 3
2 5 1
8 5 1
Result
Thus the program to implement LRU page replacement algorithm was written and
executed successfully.
88
EX.NO: 11.3 OPTIMAL PAGE REPLACEMENT
ALGORITHMS DATE:
AIM:
To implement Optimal (The page which is not used for longest time) page replacement
algorithms.
ALGORITHM:
Step1:Read the size of the frame, no. of elements and elements one by one.
Step2:Initialize the frames with value -1.
Step3:Insert each element into frame, if it’s already not present.
Step4:If the frame is full and new element is not already present then replace the least
frequently used element by the new element.
Step5:Increment no. of page faults by one while inserting each element into the frames.
Step6:Display the contents of frames during processing and the total no. of page faults.
PROGRAM:
#include<stdio.h>
int main()
{
int reference_string[25], frames[25], interval[25];
int pages, total_frames, page_faults = 0;
int m, n, temp, flag, found;
int position, maximum_interval, previous_frame = -1;
printf("\nEnter Total Number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter Values of Reference String\n");
for(m = 0; m < pages; m++)
{
printf("Value No.[%d]:\t", m +
1); scanf("%d",
&reference_string[m]);
}
printf("\nEnter Total Number of Frames:\t");
scanf("%d", &total_frames);
for(m = 0; m < total_frames; m++)
{
89
frames[m] = -1;
}
for(m = 0; m < pages; m++)
{
flag = 0;
for(n = 0; n < total_frames; n++)
{
if(frames[n] == reference_string[m])
{
flag = 1;
printf("\t");
break;
}
}
if(flag == 0)
{
if (previous_frame == total_frames - 1)
{
for(n = 0; n < total_frames; n++)
{
for(temp = m + 1; temp < pages; temp++)
{
interval[n] = 0;
if (frames[n] == reference_string[temp])
{
interval[n] = temp - m;
break;
}
}
}
found = 0;
for(n = 0; n < total_frames; n++)
{
if(interval[n] == 0)
{
90
position = n;
found = 1;
break;
}
}
}
else
{
position = ++previous_frame;
found = 1;
}
if(found == 0)
{
maximum_interval = interval[0];
position = 0;
for(n = 1; n < total_frames; n++)
{
if(maximum_interval < interval[n])
{
maximum_interval = interval[n];
position = n;
}
}
}
frames[position] = reference_string[m];
printf("FAULT\t");
page_faults++;
}
for(n = 0; n < total_frames; n++)
{
if(frames[n] != -1)
{
printf("%d\t", frames[n]);
}
}
91
printf("\n");
}
printf("\nTotal Number of Page Faults:\t%d\n", page_faults);
return 0;
}
Output
String
Value No.[1]: 3
Value No.[2]: 2
Value No.[3]: 3
Value No.[4]: 5
Value No.[5]: 6
Value No.[6]: 1
FAULT 3
FAULT 3 2
3 2
FAULT 3 2 5
FAULT 3 2 5 6
FAULT 3 2 5 1
Result
Thus the program to implement optimal page replacement algorithm was written and
executed successfully.
92
EX.NO: 12.1 FILE ORGANIZATION TECHNIQUES
IMPLEMENTATION OF SINGLE LEVEL DIRECTORY
DATE:
AIM:
To write a C program to implement Single - Level directory structure in C
ALGORITHM:
1. Start
2. Declare the number, names and size of the directories and file names.
3. Get the values for the declared variables.
4. Display the files that are available in the directories.
5. Stop
PROGRAM:
#include<stdio.h>
main()
{
int master,s[20];
char f[20][20]
[20];
char d[20]
[20]; int i,j;
printf("enter number of directorios:");
scanf("%d",&master);
printf("enter names of directories:");
for(i=0;i<master;i++)
scanf("%s",&d[i]);
printf("enter size of directories:");
for(i=0;i<master;i++)
scanf("%d",&s[i]);
printf("enter the file names :");
for(i=0;i<master;i++)
for(j=0;j<s[i];j++)
scanf("%s",&f[i][j]); printf("\
n");
printf(" directory\tsize\tfilenames\n");
printf("*************************************************\n");
for(i=0;i<master;i++)
{
printf("%s\t\t%2d\t",d[i],s[i]);
for(j=0;j<s[i];j++) printf("%s\n\t\t\
t",f[i][j]); printf("\n");
}
printf("\t\n");
}
93
OUTPUT:
RESULT:
Thus the Single level directory structure is written and executed successfully.
94
EX.NO:12.2 IMPLEMENTATION OF TWO-LEVEL DIRECTORY
DATE:
AIM:
To write a C program to implement Two-level directory structure in C.
ALGORITHM:
1. Start
2. Declare the number, names and size of the directories and subdirectories and filenames.
3. Get the values for the declared variables.
4. Display the files that are available in the directories and subdirectories.
5. Stop.
PROGRAM:
#include<stdio.h>
struct st
{
char dname[10];
char sdname[10][10];
char fname[10][10]
[10]; int ds,sds[10];
}dir[10];
void
main()
{
int i,j,k,n;
clrscr();
printf("enter number of directories:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter directory %d
names:",i+1);
scanf("%s",&dir[i].dname);
printf("enter size of directories:");
scanf("%d",&dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("enter subdirectory name and size:");
scanf("%s",&dir[i].sdname[j]);
scanf("%d",&dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
{
printf("enter file name:"); scanf("%s",&dir[i].fname[j]
95
[k]);
}}}
96
printf("\ndirname\t\tsize\tsubdirname\tsize\tfiles"); printf("\
n******************************************************\n"); for(i=0;i<n;i++){
printf("%s\t\t%d",dir[i].dname,dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("\t%s\t\t%d\t",dir[i].sdname[j],dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++) printf("%s\
t",dir[i].fname[j][k]);
printf("\n\t\t");}
printf("\n"); }
}
OUTPUT:
RESULT:
97
FILE ALLOCATION STRATEGIES
AIM:
To write a C Program to implement Sequential File Allocation method.
ALGORITHM:
Step 1: Start the program.
Step 2: Enter the number of files.using for loop get the file name
Step 3: Get the starting block of each file from that allocate blocks sequentially.
Step 4: Print the File name and blocks occupied by file will be displayed.
Step 5: Stop the program.
PROGRAM:
#include<stdio.h>
main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20]
[20]; printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by
file%d",i+1); scanf("%d",&b[i]);
printf("Enter the starting block of file
%d",i+1); scanf("%d",&sb[i]); t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\
n"); for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("Enter file name:");
98
scanf("%d",&x);
printf("File name is:
%d",x);
printf("length is:%d",b[x-
1]); printf("blocks
occupied:"); for(i=0;i<b[x-
1];i++) printf("%4d",c[x-1]
[i]); getch();
}
Output
Enter no.of files:2
Enter no. of blocks occupied by
file1 5 Enter the starting block of
file1 1 Enter no. of blocks occupied
by file2 5 Enter the starting block of
file2 6 Filename Start
block length
1 1 5
2 6 5
Enter file name:1
File name is:1 length is:5 blocks occupied: 1 2 3 4 5
Result
Thus the following file allocation technique of Sequential File Allocation was written
and executed successfully.
99
EX.NO:13.2 INDEXED FILE
ALLOCATION DATE:
AIM:
ALGORITHM:
Step 1: Start.
Step 2: Enter the filename and blocks occupied by a file index block using
loop. Step 3: Allocate file to index block.
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int n,m[20],i,j,sb[20],s[20],b[20]
[20],x; printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter starting block and size of file%d:",i+1); scanf("%d
%d",&sb[i],&s[i]);
printf("Enter blocks occupied by
file%d:",i+1);
scanf("%d",&m[i]); printf("enter
blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
}
printf("\nFile\t index\tlength\
n"); for(i=0;i<n;i++)
10
0
{
10
1
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}
printf("\nEnter file
name:");
scanf("%d",&x);
printf("file name is:%d\
n",x); i=x-1;
printf("Index is:%d",sb[i]);
printf("Block
occupiedare:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
}
Output
[cselab@jec ~]$ ./a.out
Enter no. of files:2
Enter starting block and size of file1:2 5
Enter blocks occupied by file1:10
enter blocks of
file1:3 2 5 4 6 7 2 6
4
7
Enter starting block and size of file2:3 4
Enter blocks occupied by file2:5
enter blocks of file2:2 3 4 5 6
Enter file
name:1
file name
is:1
Index is:2 Block occupied are: 3 2 5 4 6 7 2 6 4 7
10
2
Result
Thus the following file allocation technique of Indexed File Allocation was written
and executed successfully.
10
3
EX.NO: 13.3 LINKED FILE
ALLOCATION DATE:
AIM:
To write a C Program to implement Linked File Allocation method.
ALGORITHM:
Step 1: Start the Program
Step 2:Obtain the required data through char and int
datatypes. Step 3:Enter the filename,starting block ending
block.
Step 4: Print the free block using loop.
Step 5:‟for‟ loop is created to print the file utilization of linked type of entered type .
Step 6: This is allocated to the unused linked allocation.
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
10
4
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n")
; for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size)
; for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j])
; printf("\n");
}
10
5
Output
Enter no. of
files:2 Enter file
name:sample
Enter starting
block:20 Enter
no.of blocks:6
Enter block
numbers:4 12
15
45
32
25
Enter file
name:raj Enter
starting block:12
Enter no.of
blocks:5 Enter
block numbers:6
5
4
3
2
File start size block
sample 20 6 4--->12--->15--->45--->32--->25
raj 12 5 6--->5--->4--->3--->2
Result:
Thus the following file allocation technique of Linked File Allocation was
written and executed successfully.
10
6
EX.NO: 14 DISK SCHEDULING
ALGORITHM DATE:
AIM :
ALGORITHM:
2. Let us one by one take the tracks in default order and calculate the
absolute distance of the track from the head.
4. Currently serviced track position now becomes the new head position.
5. Go to step 2 until all tracks in the request array have not been serviced.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>int
main()
// clrscr();
scanf("%d",&queue[i]);
10
7
position\t");scanf("%d",&head); for(i=1;i<n;i+
+) t[i]=abs(head-queue[i]);for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
if(t[i]>t[j])
for(i=1;i<n-1;i++)
seek=seek+abs(head-queue[i]);head=queue[i];
t",seek); avg=seek/(float)n;
%f\t",avg); return 0;
OUTPUT:
RESULT:
10
8
EX.NO: 15 LINUX INSTALLATION USING
VMWARE DATE:
AIM:
To install the Linux OS using VMware
Note: Log in as a root user, or use the sudo command to complete each of these steps.
3. Right Click VM in the virtual machine menu, then click Guest >
Install/Upgrade VMware Tools.
4. Click OK.
Note: In some cases, verify that the CDROM device is Connected from
within the Edit Settings option of the virtual machine.
5. To create a
mount
point, run:
mkdir
/mnt/cdrom
6. To mount the
CDROM, run:
mount
/dev/cdrom
/mnt/cdrom
/mnt/cdrom/VMwareTools-version.tar.gz /tmp/
10
9
version.
11
0
8. To determine the version of
similar to:
# VMwareTools-5.0.0-12124.tar.gz
9. To change to the tmp directory and extract the contents of the tar file into
a new directory called vmware-tools-distrib, run:
cd /tmp
tar -zxvf VMwareTools-version.tar.gz
cd vmware-tools-distrib
./vmware-install.pl
Notes:
● Complete the screen prompts to install the VMware Tools.
Options in square brackets are default choices and can be selected
by pressing Enter.
● To compile VMware Tools successfully, you need gcc
Compiler and Linux Kernel sources provided by your Linux
distribution. Consult your Linux distribution documentation for
details on methods to install these packages.
● It is normal for the console screen to go blank for a short
time during the installation when the display size changes.
● Some warnings or errors are normal, like when a files does not exist.
● Depending on the Linux distribution, your network service might
restart after installation. VMware recommends that you invoke
this command from the console and not remotely.
/usr/bin/vmware-toolbox &
umount /mnt/cdrom
11
1
14. Depending on your environment, you may need to manually end the
11
2
VMware Tools installation. To end the VMware Tools install, click VM
in the virtual machine menu, then click Guest > End VMware Tools
Install.
RESULT:
Thus the study exercise for Linux installation using VMware studied
successfully.
11
3