0% found this document useful (0 votes)
6 views

CS3461- Lab Programs

The document outlines course objectives and experiments focused on operating systems, including Windows installation and UNIX command basics. It details the steps for installing Windows and provides various UNIX commands for file manipulation and shell programming. Additionally, it includes examples of shell scripts for finding the greatest number, calculating factorials, and summing odd numbers.

Uploaded by

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

CS3461- Lab Programs

The document outlines course objectives and experiments focused on operating systems, including Windows installation and UNIX command basics. It details the steps for installing Windows and provides various UNIX commands for file manipulation and shell programming. Additionally, it includes examples of shell scripts for finding the greatest number, calculating factorials, and summing odd numbers.

Uploaded by

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

COURSE OBJECTIVES:

To install windows oper ating systems.


To under stand the basics of Unix command and shell pr ogr amming.
To implement var ious CPU scheduling algor ithms.
To implement Deadlock Avoidance and Deadlock Detection Algor ithms
To implement Page Replacement Algor ithms
To implement var ious memor y allocation methods.
To be familiar with File Or ganization and File Allocation Str ategies.
LIST OF EXPERIMENTS:
1. Installation of windows oper ating system
2. Illustr ate UNIX commands and Shell Pr ogr amming
3. Pr ocess Management using System Calls : For k, Exit, Getpid, Wait, Close
4. Wr ite C pr ogr ams to implement the var ious CPU Scheduling Algor ithms
5. Illustr ate the inter pr ocess communication str ategy
6. Implement mutual exclusion by Semaphor e
7. Wr ite C pr ogr ams to avoid Deadlock using Banker 's Algor ithm
8. Wr ite a C pr ogr am to Implement Deadlock Detection Algor ithm
9. Wr ite C pr ogr am to implement Thr eading
10. Implement the paging Technique using C pr ogr am
11. Wr ite C pr ogr ams to implement the following Memor y Allocation Methods
a. Fir st Fit b. Wor st Fit c. Best Fit
12. Wr ite C pr ogr ams to implement the var ious Page Replacement Algor ithms
13. Wr ite C pr ogr ams to Implement the var ious File Or ganization Techniques
14. Implement the following File Allocation Str ategies using C pr ogr ams
a. Sequential b. Indexed c. Linked
15. Wr ite C pr ogr ams for the implementation of var ious disk scheduling
algor ithms
16. Install any guest oper ating system like Linux using VMwar e.

TOTAL: 45 PERIODS
Ex. No. : 1 Installation of windows oper ating system
Date:

Aim
To install the windows oper ating System.

Pr er equisites
1. Backup Your Data: Befor e star ting the installation pr ocess, back up any impor tant
data on your computer to an exter nal dr ive or cloud stor age.
2. Windows Installation Media: A Windows installation USB dr ive or DVD is needed. A
bootable USB dr ive can be cr eated using the Media Cr eation Tool fr om Micr osoft's
website.
3. Pr oduct Key: Windows pr oduct key should be known.
Steps to Install Windows
Step 1: Cr eate Installation Media
1. Download the Media Cr eation Tool fr om the Micr osoft website for Windows 10 or
Windows 11.
2. Run the Tool: Follow the instr uctions to cr eate a bootable USB dr ive.
● Inser t a USB dr ive (at least 8GB).
● Select "Cr eate installation media (USB flash dr ive, DVD, or ISO file) for
another PC".
● Follow the pr ompts to complete the pr ocess.
Step 2: Boot fr om Installation Media
1. Inser t the USB Dr ive or DVD into the computer .
2. Restar t the Computer and enter the BIOS/UEFI setup.
● This typically involves pr essing a key like F2, F12, Del, or Esc immediately
after tur ning on the computer . The key var ies by manufactur er .
1. Change the Boot Or der : Set the USB dr ive or DVD dr ive as the fir st boot device.
2. Save and Exit: The computer will r estar t and boot fr om the installation media.
Step 3: Install Windows
1. Language, Time, and Keyboar d Pr efer ences: Choose the pr efer ences and click
"Next".
2. Install Now: Click the "Install Now" button.
3. Enter Pr oduct Key: Input the Windows pr oduct key when pr ompted.
4. Accept License Ter ms: Check the box to accept the license ter ms and click "Next".
5. Custom Installation: Choose "Custom: Install Windows only (advanced)" for a clean
install.
6. Select Par tition: Choose the par tition wher e to install Windows.
● If it is installed on a new dr ive, select the unallocated space and click "Next".
● If r einstalling, select the existing Windows par tition, for mat it, and then
pr oceed.
Step 4: Complete Installation
1. Windows Will Copy Files: The installer will copy files, install featur es, and updates.
2. Restar t Multiple Times: The computer will r estar t sever al times dur ing the pr ocess.
3. Per sonalize Settings: Follow the pr ompts to per sonalize the settings or use expr ess
settings.
4. Cr eate User Account: Set up a user account and passwor d.
5. Finalize Settings: Windows will finalize the settings and boot to the desktop.
Post- Installation
1. Install Updates: Go to Settings > Update & Secur ity > Windows Update and check
for updates.
2. Install Dr iver s: Ensur e all the har dwar e is r ecognized and functioning pr oper ly by
installing the latest dr iver s fr om the computer or mother boar d manufactur er 's
website.
3. Install Essential Softwar e: Install essential applications like web br owser s, office
suites, and antivir us softwar e.
By following these steps, installation of Windows should be done and r un on the computer .

1. Fir st input the windows 10 installer media you ar e using (flash or DVD)
2. Select Language to install, Time and cur r ency for mat, and Keyboar d or input method. If it
is selected, click next.

3. Then click Install now.

4. Then enter the ser ial number that you have. If you don’t have it, just click skip.
5. In this section, you can customize the Windows 10 that you will install. Adjust it to each
of your choices or needs! If it is selected, click Next.

6. Then check the I accept the license ter ms section. Then click Next

7. In this section, ther e ar e two options, which you can choose between upgr ade or custom.
Since we will r einstall our computer or laptop, then I choose custom.
8. Then we will continue with a split of par titions. Ur gent! Her e I hope you ar e ver y car eful,
especially for the fir st time. Because if it’s wr ong, your data might be lost (ther efor e, don’t
for get to backup data befor e r einstalling the computer /laptop).

9. Next ar e the steps in the pr ocess of allocating Windows files fr om the installer media to
the
har d disk. Wait for the pr ocess to finish.
10. Then the computer will ask to r estar t itself. Just click Restar t now.

11. Now almost finished, follow for the next step to the Windows 10 desktop. Then, enter
the ser ial number if asked. If you don’t have it, click Do this later .

12. Select I own it. Then click Next.

13. Cr eate an account for your computer . Enter your user name, passwor d, and HINT (for a
r eminder if you for get your passwor d), don’t leave it blank because if for get your passwor d
have to for mat the data. When it is selected, click Next.

14. Congr atulations you have successfully installed windows 10 on your computer or laptop!
Result
Thus the windows oper ating system was successfully installed.

Ex. No: 2 BASICS OF UNIX COMMANDS


Date:

AIM:
To study about the basics of UNIX Commands.
UNIX:
It is a multi- user oper ating system. It was developed at AT&T Bell Industr ies, USA, in 1969
by Ken Thompson, along with Dennis Ritchie, who developed it fr om the MULTICS
(Multiplexed Infor mation and Computing Ser vice) OS. By 1980, UNIX had been completely
r ewr itten using the C language.
LINUX:
It is similar to UNIX, which was cr eated by Linus Tor valds. All UNIX commands wor k in
Linux. Linux is open- sour ce softwar e. The main featur e of Linux is coexisting with other OSs
such as Windows and UNIX.
PROCEDURE:
1. Log on to the Ubuntu oper ating system.
2. Open the ter minal.
3. The pr ompt,$ which is called a command pr ompt is issued by the shell
4. The shell is now r eady to r eceive commands fr om the user .
5. Var ious commands ar e executed as follows.
UNIX COMMANDS
GENERAL COMMANDS
a) date
–used to check the date and time Syn:$date

b) cal
–used to display the calendar
Syntax: $cal 2 2009

c) echo
–used to pr int the message on the scr een.
Syntax: $echo “text”
d) ls
–used to list the files. Your files ar e kept in a dir ector y.
Syntax: $lsls–s
e) lp
–used to take pr intouts
Syn: $lp filename
f) man
–used to pr ovide manual help on ever y UNIX commands.
Syn: $man unix command
$man cat
g) who & whoami
–it displays data about all user s who have logged into the system cur r ently. The next
command displays about cur r ent user only.
Syn: $who
$whoami
h) uptime
–tells you how long the computer has been r unning since its last r eboot or power - off.
Syn: $uptime
i) uname
–it displays the system infor mation such as har dwar e platfor m, system name and
pr ocessor , OS type.
Syn: $uname–a
j) hostname
–displays and set system host name
Syn: $ hostname
k) bc
–stands for best calculator

FILE MANIPULATION COMMANDS


a) cat–this cr eate, view and concatenate files.
Cr eation:
Syn:$cat>filename
Viewing:
Syn:$cat filename
Add text to an existing file:
Syn:$cat>>filename
Concatenate:
Syn:$catfile1file2>file3
$catfile1file2>>file3 (no over wr iting of file3)
b) gr ep–used to sear ch a par ticular wor d or patter n r elated to that wor d fr om the file.
Syn:$gr ep sear ch wor d filename
Eg:$gr ep anu student
c) r m–deletes a file fr om the file system
Syn:$r m filename
d) touch–used to cr eate a blank file.
Syn:$touch file names
e) cp–copies the files or dir ector ies
Syn: $cpsour ce file destination file
Eg: $cp student stud
f) mv–to r ename the file or dir ector y
Syn: $mv old file new file
Eg: $mv–i student student list (- i pr ompt when over wr ite)
g) ut–it cuts or pickup a given number of char acter or fields of the file.
Syntax: $cut<option><filename>
Eg: $cut –c filename
$cut–c1- 10emp
$cut–f 3,6emp
$ cut –f 3- 6 emp
- c cutting columns
- f cutting fields
h) head–displays10 lines fr om the head (top)of a given file
Syntax: $head filename
Eg: $head student
Syn: $head- 2student
i) tail–displays last 10 lines of the file
Syn: $tail filename
Eg: $tail student
To display the bottom two lines;
Syn: $ tail - 2 student
j) chmod–used to change the per missions of a file or dir ector y.
Syn: $ch mod categor y oper ation per mission file
Wher e,
Categor y–is the user type
Oper ation–is used to assign or r emove per mission
Per mission–is the type of per mission
File–ar e used to assign or r emove per mission all
Examples:
$chmodu- wx student
Removes wr ite and execute per mission for user s
$ch modu+r w, g+r wstudent
Assigns r ead and wr ite per mission for user s and gr oups
$chmodg=r wx student
Assigns absolute per mission for gr oups of all r ead, wr ite and execute per missions
k) wc–it counts the number of lines, wor ds, char acter in a specified file(s) with the options as

l,- w,- c

Syn: $wc –l filename


$wc –w filename
$wc–c filename
RESULT
Thus the basics of UNIX Commands have been executed successfully.

Ex. No. : 2 Shell Pr ogr amming


Date :

Aim
To wr ite simple shell pr ogr ams by using conditional, br anching and looping statements

Descr iption
The shell pr ovides an inter face to the UNIX system. It gather s input fr om user and executes
pr ogr ams based on that input. A shell is an envir onment in which we can r un our commands,
pr ogr ams, and shell scr ipts. Ther e ar e differ ent flavor s of shells, just as ther e ar e differ ent
flavor s of oper ating systems. Shell Types ar e
The Bour ne shell- If you ar e using a Bour ne- type shell, the default pr ompt is the $
char acter .
The C shell- If you ar e using a C- type shell, the default pr ompt is the % char acter .

1. GREATEST AMONG THREE NUMBERS


Aim:
To wr ite shell pr ogr am to find the gr eatest number among thr ee number s.
Algor ithm:
STEP 1: Star t the pr ogr am.
STEP 2: Read 3 number s a, b, c.
STEP 3: If (a > b) and (a > c) echo value of a
STEP 4: elif (b > c) echo value of b
STEP 5: Other wise, echo value of c
STEP 6: End the pr ogr am.
Pr ogr am:
echo "ENTER THREE NUMBERS"
r ead a b c
if [ $a - gt $b ] && [ $a - gt $c ]
then
echo "$a is gr eater "
elif [ $b - gt $c ]
then
echo "$b is gr eater "
else
echo "$c is gr eater "
fi
Output:
ENTER THREE NUMBERS
3 12 5
12 is gr eater

2. FACTORIAL OF A GIVEN NUMBER


Aim:
To wr ite a Shell pr ogr am to find the factor ial of a number
Algor ithm:
STEP 1: Star t the pr ogr am.
STEP 2: Read the value of n.
STEP 3: If the value of n is gr eater than 1 then calculate fact=`expr $fact \* $n `
and n=`expr $n - 1`
STEP 4: Pr int the factor ial of the given number .
STEP 5: End the pr ogr am.
Pr ogr am:
echo "ENTER THE NUMBER:"
r ead n
fact=1
while [ $n - gt 1 ]
do
fact=`expr $fact \* $n `
n=`expr $n - 1`
done
echo "FACTORIAL OF THE GIVEN NUMBER IS $fact"
Output:
ENTER THE NUMBER:
5
FACTORIAL OF THE GIVEN NUMBER IS 120

3. SUM OF ODD NUMBERS UPTO N


Aim:
To wr ite shell pr ogr am for calculating the sum of odd number s upto N
Algor ithm:
STEP 1: Star t the pr ogr am.
STEP 2: Get N (Total Number s).
STEP 3: Get N number s using loop.
STEP 4: Calculate the sum.
STEP 5: Pr int the r esult.
STEP 6: End the pr ogr am.
Pr ogr am:
echo "enter the r ange"
r ead n
x=1
sum=0
while [ $x - le $n ]
do
sum=`expr $sum + $x`
x=`expr $x + 2`
done
echo "sum=$sum"
Output:
enter the r ange
10
sum=25
4. GENERATION OF FIBONACCI NUMBERS
Aim:
To wr ite shell pr ogr am for the gener ation of Fibonacci number s
Algor ithm:
Step 1: Star t the pr ogr am
Step 2: Declar e var iable a,b,n,i,fn
Step 3: Initialize var iable a=0, b=1
Step 4: Read N fr om user
Step 5: Pr int a and b
Step 6: Repeat until i less than N
Pr int a
fn=a+b
a=b, b=fn
i=i+1
Stop 7: End the pr ogr am.
Pr ogr am:
echo "Enter the value of N: "
r ead N
a=0
b=1
echo - e "the Fibonacci ser ies is :\n "
for ((i=0;i<N;i++))
do
echo "$a"
fn=$((a+b))
a=$b
b=$fn
done
Output:
Enter the value of N:
5
the Fibonacci ser ies is :
0
1
1
2

5. The Given number is even or odd


Aim:
To wr ite a Shell pr ogr am to check the given number is even or odd
Algor ithm:
STEP 1: Star t the pr ogr am.
STEP 2: Read the value of n.
STEP 3: Calculate r =expr $n%2
STEP 4: If the value of r equals 0 then pr int the number is even
STEP 5: If the value of r not equal to 0 then pr int the number is odd.
STEP 6: End the pr ogr am.
Pr ogr am:
echo "Enter the Number "
r ead n
r =`expr $n % 2`
if [ $r - eq 0 ] then
echo "$n is Even number "
else
echo "$n is Odd number "
fi
Output:
Enter a no:
5
Number is odd
Enter a no:
2
Number is even
6. The Given year is leap year or not
Pr ogr am:
Aim:
To wr ite a Shell pr ogr am to check the given year is leap year or not
Algor ithm:
STEP 1: Star t the pr ogr am.
STEP 2: Read the value of year .
STEP 3: Calculate b=expr $y%4
STEP 4: If the value of b is equivalent to 0 then pr int the year is a leap year
STEP 5: If the value of r is not equal to 0 then pr int the year is not a leap year .
STEP 6: End the pr ogr am.
Pr ogr am:
echo "Enter the year "
r ead y
b=`expr $y % 4`
if [ $b - eq 0 ] then
echo "$y is a leap year "
else
echo "$y is not a leap year "
fi
Output:
Enter the year :
2000
2000 is a leap year
7. Swap the two integer s
Aim:
To wr ite a Shell pr ogr am to swap the two integer s
Algor ithm:
SEPT 1: Star t the pr ogr am.
STEP 2: Read the value of a, b.
STEP 3: Calculate the swapping of two values by using a tempor ar y var iable temp.
STEP 4: Pr int the value of a and b.
STEP 5: End the pr ogr am.
Pr ogr am:
echo "Enter Two Number s"
r ead a b
temp=$a
a=$b
b=$temp
echo "after swapping"
echo $a $b
Output:
Enter Two Number s:
24
after swapping
42
Result:
Thus the Shell pr ogr ams have been ver ified successfully.

Ex. No.3a for k() System call

Pr ogr am:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
pid_ t pid; int x = 5;
pid = for k();
x++;

if (pid < 0)
{
pr intf("Pr ocess cr eation er r or ");
exit(- 1);
}
else if (pid == 0)
{
pr intf("Child pr ocess:");
pr intf("\nPr ocess id is %d", getpid());
pr intf("\nValue of x is %d", x);
pr intf("\nPr ocess id of par ent is %d\n", getppid());
}
else
{
pr intf("\nPar ent pr ocess:");
pr intf("\nPr ocess id is %d", getpid());
pr intf("\nValue of x is %d", x);
pr intf("\nPr ocess id of shell is %d\n", getppid());
}
r etur n 0;
}

Output:

Par ent pr ocess:


Pr ocess id is 22699
Value of x is 6
Pr ocess id of shell is 22692
Child pr ocess:
Pr ocess id is 22700
Value of x is 6
Pr ocess id of par ent is 1

Ex. No. 3b wait() System call

Pr ogr am:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int i, status; pid_ t pid;

pid = for k();


if (pid < 0)
{
pr intf("\nPr ocess cr eation failur e\n"); exit(- 1);
}
else if(pid > 0)
{
wait(NULL);
pr intf ("\nPar ent star ts\nEven Nos: "); for (i=2;i<=10;i+=2)
pr intf ("%3d",i);
pr intf ("\nPar ent ends\n");
}
else if (pid == 0)
{
pr intf ("Child star ts\nOdd Nos: "); for (i=1;i<10;i+=2)
pr intf ("%3d",i); pr intf ("\nChild ends\n");
}
r etur n 0;}

Output:

Child star ts
Odd Nos: 1 3 5 7 9
Child ends

Par ent star ts


Even Nos: 2 4 6 8 10
Par ent ends

Ex.No. 3c exec() system call

Pr ogr am:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
pid_ t pid;
switch(pid = for k())
{
case - 1:
per r or ("For k failed");
exit(- 1);

case 0:
pr intf("Child pr ocess\n");
execlp("/bin/date", "date",0);
exit(0);
default:
wait(NULL);
pr intf("Child Ter minated\n");
exit(0);
}
r etur n 0;
}

Output:

Child pr ocess
Wed Apr 17 06:20:01 UTC 2024
Child Ter minated

Ex.No. 3d close() System call

Pr ogr am:

#include <er r no.h>


#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int fd1 = open("foo.txt", O_ RDONLY);
if (fd1 < 0) {
per r or ("c1");
exit(1);
}
pr intf("opened the fd = % d\n", fd1);

// Using close system Call


if (close(fd1) < 0) {
per r or ("c1");
exit(1);
}
pr intf("closed the fd.\n");
r etur n 0;
}

Output:

opened the fd = 3
closed the fd.
Ex.No. 4a FCFS Scheduling Algor ithm

Pr ogr am:

#include <stdio.h>
#include<conio.h>
str uct pr ocess
{
int pid; int btime; int wtime; int ttime;
} p[ 10] ;
void main()
{
int i,j,k,n,ttur ,twat; float awat,atur ;
clr scr ();
pr intf("Enter no. of pr ocess : "); scanf("%d", &n);
for (i=0; i<n; i++)
{
pr intf("Bur st time for pr ocess P%d (in ms) : ",(i+1)); scanf("%d", &p[ i] .btime);
p[ i] .pid = i+1;
}
p[ 0] .wtime = 0; for (i=0; i<n; i++)
{
p[ i+1] .wtime = p[ i] .wtime + p[ i] .btime; p[ i] .ttime = p[ i] .wtime + p[ i] .btime;
}
ttur = twat = 0; for (i=0; i<n; i++)
{
ttur += p[ i] .ttime; twat += p[ i] .wtime;
}
awat = (float)twat / n; atur = (float)ttur / n;
pr intf("\n FCFS Scheduling\n\n"); for (i=0; i<28; i++)
pr intf("- ");
pr intf("\nPr ocess B- Time T- Time W- Time\n"); for (i=0; i<28; i++)
pr intf("- ");
for (i=0; i<n; i++)
pr intf("\n P%d\t%4d\t%3d\t%2d", p[ i] .pid,p[ i] .btime,p[ i] .ttime,p[ i] .wtime);
pr intf("\n"); for (i=0; i<28; i++)
pr intf("- ");
pr intf("\n\nAver age waiting time : %5.2fms", awat); pr intf("\nAver age tur n ar ound time :
%5.2fms\n", atur );

pr intf("\n\nGANTT Char t\n"); pr intf("- ");


for (i=0; i<(p[ n- 1] .ttime + 2*n); i++) pr intf("- ");
pr intf("\n");
pr intf("|"); for (i=0; i<n; i++)
{
k = p[ i] .btime/2; for (j=0; j<k; j++)
pr intf(" "); pr intf("P%d",p[ i] .pid); for (j=k+1; j<p[ i] .btime; j++)
pr intf(" ");
pr intf("|");
}
pr intf("\n");
pr intf("- ");
for (i=0; i<(p[ n- 1] .ttime + 2*n); i++) pr intf("- ");
pr intf("\n");
pr intf("0"); for (i=0; i<n; i++)
{
for (j=0; j<p[ i] .btime; j++) pr intf(" ");
pr intf("%2d",p[ i] .ttime);
}
getch();
}

Output

Enter no. of pr ocess : 3


Bur st time for pr ocess P1 (in ms) : 10
Bur st time for pr ocess P2 (in ms) : 5
Bur st time for pr ocess P3 (in ms) : 15

FCFS Scheduling

-----------------------------------------
----------
Pr ocess B- Time T- Time W- Time
-----------------------------------------
----------
P 10 10 0
P2 5 15 10
P3 15 30 15
-----------------------------------------
----------

Aver age waiting time : 8.33ms


Aver age tur n ar ound time : 18.33ms

GANTT Char t
-------------------------------------
| P1 | P2 | P3 |
-------------------------------------
0 10 15 30

Ex. No. 4b SJF Scheduling


Pr ogr am

#include <stdio.h>
str uct pr ocess
{
int pid; int btime; int wtime; int ttime;
} p[ 10] , temp;

main()
{
int i,j,k,n,ttur ,twat; float awat,atur ;
clr scr ();
pr intf("Enter no. of pr ocess : "); scanf("%d", &n);
for (i=0; i<n; i++)
{
pr intf("Bur st time for pr ocess P%d (in ms) : ",(i+1));
scanf("%d", &p[ i] .btime);
p[ i] .pid = i+1;
}
for (i=0; i<n- 1; i++)
{
for (j=i+1; j<n; j++)
{
if((p[ i] .btime > p[ j] .btime) ||
(p[ i] .btime == p[ j] .btime && p[ i] .pid > p[ j] .pid))
{
temp = p[ i] ; p[ i] = p[ j] ; p[ j] = temp;
}

}
}
p[ 0] .wtime = 0; for (i=0; i<n; i++)
{
p[ i+1] .wtime = p[ i] .wtime + p[ i] .btime; p[ i] .ttime = p[ i] .wtime + p[ i] .btime;
}
ttur = twat = 0;

for (i=0; i<n; i++)


{
ttur += p[ i] .ttime; twat += p[ i] .wtime;
}
awat = (float)twat / n; atur = (float)ttur / n;

pr intf("\n SJF Scheduling\n\n"); for (i=0; i<28; i++)


pr intf("- ");
pr intf("\nPr ocess B- Time T- Time W- Time\n"); for (i=0; i<28; i++)
pr intf("- "); for (i=0; i<n; i++)
pr intf("\nP%- 4d\t%4d\t%3d\t%2d", p[ i] .pid,p[ i] .btime,p[ i] .ttime,p[ i] .wtime);

pr intf("\n"); for (i=0; i<28; i++)


pr intf("- ");
pr intf("\n\nAver age waiting time : %5.2fms", awat); pr intf("\nAver age tur n ar ound time :
%5.2fms\n", atur );

pr intf("\n\nGANTT Char t\n"); pr intf("- ");


for (i=0; i<(p[ n- 1] .ttime + 2*n); i++) pr intf("- ");
pr intf("\n|"); for (i=0; i<n; i++)
{
k = p[ i] .btime/2; for (j=0; j<k; j++)
pr intf(" "); pr intf("P%d",p[ i] .pid); for (j=k+1; j<p[ i] .btime; j++)
pr intf(" ");
pr intf("|");
}
pr intf("\n- ");
for (i=0; i<(p[ n- 1] .ttime + 2*n); i++) pr intf("- ");
pr intf("\n0"); for (i=0; i<n; i++)
{
for (j=0; j<p[ i] .btime; j++) pr intf(" ");
pr intf("%2d",p[ i] .ttime);
}
clr scr ();
}

Output
Enter no. of pr ocess : 3
Bur st time for pr ocess P1 (in ms) : 10
Bur st time for pr ocess P2 (in ms) : 5
Bur st time for pr ocess P3 (in ms) : 15

SJF Scheduling
---------------------------------------------------------
------------
Pr ocess B- Time T- Time W- Time
---------------------------------------------------
------------------
P2 5 5 0
P1 10 15 5
P3 15 30 15
---------------------------------------------------
-----------------

Aver age waiting time : 6.67ms


Aver age tur n ar ound time : 16.67ms

GANTT Char t
-------------------------------------
| P2 | P1 | P3 |
-------------------------------------
0 5 15 30

Ex.No. 4c Pr ior ity scheduling

Pr ogr am:
#include <stdio.h>
str uct pr ocess
{
int pid; int btime; int pr i; int wtime; int ttime;
} p[ 10] , temp;

main()
{
int i,j,k,n,ttur ,twat; float awat,atur ;
clr scr ();
pr intf("Enter no. of pr ocess : "); scanf("%d", &n);
for (i=0; i<n; i++)
{
pr intf("Bur st time for pr ocess P%d (in ms) : ", (i+1));
scanf("%d", &p[ i] .btime);
pr intf("Pr ior ity for pr ocess P%d : ", (i+1));
scanf("%d", &p[ i] .pr i);
p[ i] .pid = i+1;
}

for (i=0; i<n- 1; i++)


{
for (j=i+1; j<n; j++)
{
if((p[ i] .pr i > p[ j] .pr i) ||
(p[ i] .pr i == p[ j] .pr i && p[ i] .pid > p[ j] .pid) )
{
temp = p[ i] ; p[ i] = p[ j] ; p[ j] = temp;
}

}
}
p[ 0] .wtime = 0; for (i=0; i<n; i++)
{
p[ i+1] .wtime = p[ i] .wtime + p[ i] .btime; p[ i] .ttime = p[ i] .wtime + p[ i] .btime;
}
ttur = twat = 0; for (i=0; i<n; i++)
{
ttur += p[ i] .ttime; twat += p[ i] .wtime;
}
awat = (float)twat / n; atur = (float)ttur / n;
pr intf("\n\t Pr ior ity Scheduling\n\n"); for (i=0; i<38; i++)
pr intf("- ");
pr intf("\nPr ocess B- Time Pr ior ity T- Time W- Time\n"); for (i=0; i<38; i++)
pr intf("- "); for (i=0; i<n; i++)
pr intf("\nP%- 4d\t%4d\t%3d\t%4d\t%4d", p[ i] .pid,p[ i] .btime,p[ i] .pr i,p[ i] .ttime,p[ i] .wtime);
pr intf("\n"); for (i=0; i<38; i++)
pr intf("- ");
pr intf("\n\nAver age waiting time : %5.2fms", awat);
pr intf("\nAver age tur n ar ound time : %5.2fms\n", atur );
pr intf("\n\nGANTT Char t\n"); pr intf("- ");
for (i=0; i<(p[ n- 1] .ttime + 2*n); i++) pr intf("- ");
pr intf("\n|"); for (i=0; i<n; i++)
{
k = p[ i] .btime/2; for (j=0; j<k; j++)
pr intf(" "); pr intf("P%d",p[ i] .pid); for (j=k+1; j<p[ i] .btime; j++)
pr intf(" ");
pr intf("|");
}
pr intf("\n- ");
for (i=0; i<(p[ n- 1] .ttime + 2*n); i++) pr intf("- ");
pr intf("\n0"); for (i=0; i<n; i++)
{
for (j=0; j<p[ i] .btime; j++) pr intf(" ");
pr intf("%2d",p[ i] .ttime);
}
getch();
r etur n 0;
}

Output:
Enter no. of pr ocess : 3
Bur st time for pr ocess P1 (in ms) : 10
Pr ior ity for pr ocess P1 : 5
Bur st time for pr ocess P2 (in ms) : 15
Pr ior ity for pr ocess P2 : 2
Bur st time for pr ocess P3 (in ms) : 15
Pr ior ity for pr ocess P3 : 1

Pr ior ity Scheduling


---------------------------------------------------------------------
---------------------
Pr ocess B- Time Pr ior ity T- Time W- Time
------------------------------------------------------------------
------------------------
P3 15 1 15 0
P2 15 2 30 15
P1 10 5 40 30
------------------------------------------------------------------
------------------------
Aver age waiting time : 15.00ms
Aver age tur n ar ound time : 28.33ms

GANTT Char t
-----------------------------------------
------
| P3 | P2 | P1 |
-----------------------------------------
------
0 15 30 40
Ex.No. 5a IPC using shar ed memor y

Pr ogr am:

Ser ver :
#include <stdio.h>
#include <stdlib.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define shmsize 27
main()
{
char c; int shmid;
key_ t key = 2013; char *shm, *s;
if ((shmid = shmget(key, shmsize, IPC_ CREAT|0666)) < 0)
{
per r or ("shmget"); exit(1);
}
pr intf("Shar ed memor y id : %d\n", shmid);
if ((shm = shmat(shmid, NULL, 0)) == (char *) - 1)
{
per r or ("shmat"); exit(1);
}
memset(shm, 0, shmsize); s = shm;
pr intf("Wr iting (a- z) onto shar ed memor y\n"); for (c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = '\0';
while (*shm != '*');
pr intf("Client finished r eading\n");
if(shmdt(shm) != 0)
fpr intf(stder r , "Could not close memor y segment.\n");
shmctl(shmid, IPC_ RMID, 0);
}

Client:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define shmsize 27
main()
{
int shmid;
key_ t key = 2013; char *shm, *s;

if ((shmid = shmget(key, shmsize, 0666)) < 0)


{
pr intf("Ser ver not star ted\n"); exit(1);
}
else
pr intf("Accessing shar ed memor y id : %d\n",shmid);
if ((shm = shmat(shmid, NULL, 0)) == (char *) - 1)
{
per r or ("shmat"); exit(1);
}
pr intf("Shar ed memor y contents:\n"); for (s = shm; *s != '\0'; s++)
putchar (*s); putchar ('\n');
*shm = '*';
}
Output:

Ser ver :
Shar ed memor y id : 0
Wr iting (a- z) onto shar ed memor y
Client finished r eading

Client:
Accessing shar ed memor y id : 0
Shar ed memor y contents:
abcdefghijklmnopqr stuvwxyz
Ex.No.5b IPC using Message Passing

Pr ogr am:

Ser ver :
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// str uctur e for message queue
str uct msg_ buffer {
long msg_ type;
char msg[ 100] ;
} message;
main() {
key_ t my_ key;
int msg_ id;
my_ key = ftok("pr ogfile", 65); //cr eate unique key
msg_ id = msgget(my_ key, 0666 | IPC_ CREAT); //cr eate message queue and r etur n id
message.msg_ type = 1;
pr intf("Wr ite Message : ");
fgets(message.msg, 100, stdin);
msgsnd(msg_ id, &message, sizeof(message), 0); //send message
pr intf("Sent message is : %s \n", message.msg);
}

Client:
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// Define message queue str uctur e
str uct msg_ buffer {
long msg_ type;
char msg[ 100] ;
} message;
main() {
key_ t my_ key;
int msg_ id;
my_ key = ftok("pr ogfile", 65); //cr eate unique key
msg_ id = msgget(my_ key, 0666 | IPC_ CREAT); //cr eate message queue and r etur n id
msgr cv(msg_ id, &message, sizeof(message), 1, 0); //used to r eceive message
// display the message
pr intf("Received Message is : %s \n", message.msg);
msgctl(msg_ id, IPC_ RMID, NULL); //destr oy the message queue
r etur n 0;
}

Output:

Ser ver :
Wr ite Message : Hai Hello
Sent message is : Hai Hello
Client:
Received Message is : Hai Hello
Ex.No.6 Mutual exclusion by Semaphor e

Pr ogr am:

#include <stdio.h>
#include <pthr ead.h>
#include <semaphor e.h>
#include <unistd.h>
sem_ t mutex;
void* thr ead(void* ar g)
{
//wait sem_ wait(&mutex);
pr intf("\nEnter ed..\n");
//cr itical section
sleep(4);
//signal
pr intf("\nJust Exiting...\n");
sem_ post(&mutex);
}
int main()
{
sem_ init(&mutex, 0, 1);
pthr ead_ t t1,t2;
pthr ead_ cr eate(&t1,NULL,thr ead,NULL);
sleep(2);
pthr ead_ cr eate(&t2,NULL,thr ead,NULL);
pthr ead_ join(t1,NULL);
pthr ead_ join(t2,NULL);
sem_ destr oy(&mutex);
r etur n 0;
}

Output:

Enter ed..

Enter ed..

Just Exiting...

Just Exiting...

Ex.No. 7 Deadlock avoidance using Banker ’s Algor ithm


Pr ogr am:

#include <stdio.h>
#include<conio.h>
void main()
{
int r [ 1] [ 10] , av[ 1] [ 10] ;
int all[ 10] [ 10] , max[ 10] [ 10] , ne[ 10] [ 10] , w[ 10] ,safe[ 10] ;
int i=0, j=0, k=0, l=0, np=0, nr =0, count=0, cnt=0;
pr intf("enter the number of pr ocesses in a system");
scanf("%d", &np);
pr intf("enter the number of r esour ces in a system");
scanf("%d",&nr );
for (i=1; i<=nr ; i++)
{
pr intf("Enter no. of instances of r esour ce R%d " ,i);
scanf("%d", &r [ 0] [ i] );
av[ 0] [ i] = r [ 0] [ i] ;
}

for (i=1; i<=np; i++) for (j=1; j<=nr ; j++)


all[ i] [ j] = ne[ i] [ j] = max[ i] [ j] = w[ i] =0;

pr intf("Enter the allocation matr ix");


for (i=1; i<=np; i++)
{
for (j=1; j<=nr ; j++)
{
scanf("%d", &all[ i] [ j] );
av[ 0] [ j] = av[ 0] [ j] - all[ i] [ j] ;
}
}
pr intf("Enter the maximum matr ix");
for (i=1; i<=np; i++)
{
for (j=1; j<=nr ; j++)
{
scanf("%d",&max[ i] [ j] );
}
}

for (i=1; i<=np; i++)


{
for (j=1; j<=nr ; j++)
{
ne[ i] [ j] = max[ i] [ j] - all[ i] [ j] ;
}
}
for (i=1; i<=np; i++)
{
pr intf("pocess P%d", i); for (j=1; j<=nr ; j++)
{
pr intf("\n allocated %d\t",all[ i] [ j] );
pr intf("maximum %d\t",max[ i] [ j] );
pr intf("need %d\t",ne[ i] [ j] );
}
pr intf("\n \n");
}

pr intf("\nAvailability ");
for (i=1; i<=nr ; i++)
pr intf("R%d %d\t", i, av[ 0] [ i] );
pr intf("\n ");
pr intf("\n safe sequence");

for (count=1; count<=np; count++)


{
for (i=1; i<=np; i++)
{
int cnt = 0;
for (j=1; j<=nr ; j++)
{
if(ne[ i] [ j] <= av[ 0] [ j] && w[ i] ==0) cnt++;
}
if(cnt == nr )
{
k++;
safe[ k] = i; for (l=1; l<=nr ; l++)
av[ 0] [ l] = av[ 0] [ l] + all[ i] [ l] ;
pr intf("\n P%d ",safe[ k] );
pr intf("\t Availability "); for (l=1; l<=nr ; l++)
pr intf("R%d %d\t", l, av[ 0] [ l] ); w[ i] =1;
}
}
}
getch();
}
Output:

enter the number of pr ocesses in a system


3
enter the number of r esour ces in a system
3

Enter no. of instances of r esour ce R1 10


Enter no. of instances of r esour ce R2 7
Enter no. of instances of r esour ce R3 7

Enter the allocation matr ix


321
112
412

Enter the maximum matr ix


444
345
524

pocess P1
allocated 3 maximum 4 need 1
allocated 2 maximum 4 need 2
allocated 1 maximum 4 need 3
pocess P2
allocated 1 maximum 3 need 2
allocated 1 maximum 4 need 3
allocated 2 maximum 5 need 3

pocess P3
allocated 4 maximum 5 need 1
allocated 1 maximum 2 need 1
allocated 2 maximum 4 need 2

Availability R1 2 R2 3 R3 2

safe sequence
P3 Availability R1 6 R2 4 R3 4
P1 Availability R1 9 R2 6 R3 5
P2 Availability R1 10 R2 7 R3 7

Ex.No.8 Deadlock Detection Algor ithm

Pr ogr am:

#include <stdio.h>
#include <stdbool.h>

#define MAX_ PROCESSES 10


#define MAX_ RESOURCES 10

int available[ MAX_ RESOURCES] ;


int allocation[ MAX_ PROCESSES] [ MAX_ RESOURCES] ;
int r equest[ MAX_ PROCESSES] [ MAX_ RESOURCES] ;
bool finish[ MAX_ PROCESSES] ;

// Function to initialize the ar r ays


void initialize(int m, int n) {
// Initialize finish ar r ay to false
for (int i = 0; i < m; i++)
finish[ i] = false;

// Initialize allocation and r equest matr ices to zer o


for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
allocation[ i] [ j] = 0;
r equest[ i] [ j] = 0;
}
}
}

// Function to check if r equest can be gr anted


bool isSafe(int pr ocesses[ ] , int available[ ] , int max[ ] [ MAX_ RESOURCES] , int need[ ]
[ MAX_ RESOURCES] , int m, int n) {
int wor k[ n] ;
bool finish[ m] ;

// Initialize wor k and finish ar r ays


for (int i = 0; i < n; i++)
wor k[ i] = available[ i] ;
for (int i = 0; i < m; i++)
finish[ i] = false;

// Find an index i such that both


// finish[ i] is false and need[ i] [ 0]
// to need[ i] [ n- 1] is smaller than wor k[ 0]
// to wor k[ n- 1]
int count = 0;

while (count < m) {


bool found = false;
for (int i = 0; i < m; i++) {
if (!finish[ i] ) {
int j;
for (j = 0; j < n; j++)
if (need[ i] [ j] > wor k[ j] )
br eak;
if (j == n) {
for (int k = 0; k < n; k++)
wor k[ k] += allocation[ i] [ k] ;
finish[ i] = tr ue;
found = tr ue;
pr ocesses[ count++] = i;
}
}
}
if (!found)
br eak;
}

// If all pr ocesses ar e not finished


if (count < m)
r etur n false;
r etur n tr ue;
}

// Function to detect deadlock


bool detectDeadlock(int m, int n) {
int pr ocesses[ MAX_ PROCESSES] ;

// Cr eate a need matr ix


int need[ m] [ n] ;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
need[ i] [ j] = r equest[ i] [ j] ;
}
}

// Check if system is in a safe state


r etur n isSafe(pr ocesses, available, allocation, need, m, n);
}

int main() {
int m, n;
pr intf("Enter the number of pr ocesses: ");
scanf("%d", &m);

pr intf("Enter the number of r esour ces: ");


scanf("%d", &n);
pr intf("Enter the available instances of each r esour ce:\n");
for (int i = 0; i < n; i++)
scanf("%d", &available[ i] );

pr intf("Enter the allocation matr ix:\n");


for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d", &allocation[ i] [ j] );

pr intf("Enter the r equest matr ix:\n");


for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d", &r equest[ i] [ j] );

// Initialize ar r ays
initialize(m, n);

// Detect deadlock
if (detectDeadlock(m, n))
pr intf("No deadlock detected.\n");
else
pr intf("Deadlock detected.\n");

r etur n 0;
}

Output:

Enter the number of pr ocesses:


3
Enter the number of r esour ces: 3
Enter the available instances of each r esour ce:
120
Enter the allocation matr ix:
333
203
124
Enter the r equest matr ix:
035
230
220
Deadlock detected.

Ex.No.9 Thr eading and Synchr onization

Pr ogr am
#include <stdio.h>
#include <str ing.h>
#include <pthr ead.h>
#include <stdlib.h>
#include <unistd.h>
pthr ead_ t tid[ 2] ; int counter ;
pthr ead_ mutex_ t lock;
void* tr ythis(void *ar g)
{
pthr ead_ mutex_ lock(&lock);
unsigned long i = 0; counter += 1;
pr intf("\n Job %d has star ted\n", counter );
for (i=0; i<(0xFFFFFFFF);i++);
pr intf("\n Job %d has finished\n", counter );
pthr ead_ mutex_ unlock(&lock);
r etur n NULL;
}
void main()
{
int i = 0; int er r or ;
if (pthr ead_ mutex_ init(&lock, NULL) != 0)
{
pr intf("\n mutex init has failed\n");
}
while(i < 2)
{
er r or = pthr ead_ cr eate(&(tid[ i] ), NULL,
&tr ythis, NULL);
if (er r or != 0)
pr intf("\nThr ead can't be cr eated :[ %s] ",
str er r or (er r or ));
i++;
}
pthr ead_ join(tid[ 0] , NULL);
pthr ead_ join(tid[ 1] , NULL);
pthr ead_ mutex_ destr oy(&lock);
}
Output

Job 1 has star ted

Job 1 has finished

Job 2 has star ted

Job 2 has finished


Ex.No.10 Paging Technique

Pr ogr am
#include <stdio.h>
#include <stdlib.h>
#define PAGE_ SIZE 1024
#define FRAME_ SIZE 1024
#define MEMORY_ SIZE 8192
#define PAGE_ TABLE_ SIZE
(MEMORY_ SIZE / FRAME_ SIZE)
// Str uctur e to r epr esent a page table entr y
typedef str uct {
int fr ame_ number ;
int valid;
} PageTableEntr y;
// Function to initialize page table with invalid
entr ies
void initialize_ page_ table(PageTableEntr y
*page_ table, int size) {
for (int i = 0; i < size; i++) {
page_ table[ i] .valid = 0;
}
}
// Function to simulate page lookup and page
fault handling
void page_ lookup(PageTableEntr y *page_ table,
int page_ number ) {
if (page_ table[ page_ number ] .valid) {
pr intf("Page %d is alr eady in memor y.
Fr ame number : %d\n", page_ number ,

page_ table[ page_ number ] .fr ame_ number );


}
else {
pr intf("Page %d is not in memor y. Page
fault occur r ed.\n", page_ number );
int fr ame_ number = page_ number ;
page_ table[ page_ number ] .fr ame_ number
= fr ame_ number ;
page_ table[ page_ number ] .valid = 1;
pr intf("Page %d loaded into fr ame %d.\n",
page_ number , fr ame_ number );
}
}
int main() {
PageTableEntr y
page_ table[ PAGE_ TABLE_ SIZE] ;
initialize_ page_ table(page_ table,
PAGE_ TABLE_ SIZE);
// Simulate memor y accesses by r efer encing
page number s
int access_ sequence[ ] = { 0, 1, 2, 3, 0, 4, 1, 5,
2, 6, 3, 7} ;
int num_ accesses = sizeof(access_ sequence) /
sizeof(access_ sequence[ 0] );
pr intf("Simulating memor y accesses...\n");
for (int i = 0; i < num_ accesses; i++) {
int page_ number = access_ sequence[ i] ;
page_ lookup(page_ table, page_ number );
}
r etur n 0;
}
Output

Simulating memor y accesses...


Page 0 is not in memor y. Page fault occur r ed.
Page 0 loaded into fr ame 0.
Page 1 is not in memor y. Page fault occur r ed.
Page 1 loaded into fr ame 1.
Page 2 is not in memor y. Page fault occur r ed.
Page 2 loaded into fr ame 2.
Page 3 is not in memor y. Page fault occur r ed.
Page 3 loaded into fr ame 3.
Page 0 is alr eady in memor y. Fr ame number : 0
Page 4 is not in memor y. Page fault occur r ed.
Page 4 loaded into fr ame 4.
Page 1 is alr eady in memor y. Fr ame number : 1
Page 5 is not in memor y. Page fault occur r ed.
Page 5 loaded into fr ame 5.
Page 2 is alr eady in memor y. Fr ame number : 2
Page 6 is not in memor y. Page fault occur r ed.
Page 6 loaded into fr ame 6.
Page 3 is alr eady in memor y. Fr ame number : 3
Page 7 is not in memor y. Page fault occur r ed.
Page 7 loaded into fr ame 7.

Ex. No. 11 Memor y Allocation Methods

Pr ogr am
#include <stdio.h>
#define MAX_ HOLES 10
#define MAX_ PROCESSES 10
intch;
str uct Hole {
int size;
};

str uct Pr ocess {


int size;
};

void fir stFit(str uct Pr ocess pr ocesses[ ] , intnumPr ocesses, str uct Hole
holes[ ] , intnumHoles) {
pr intf("Fir st fit\n");
pr intf("Pr ocess\tPSize\tHole\n");
for (inti = 0; i<numPr ocesses; i++) {
int allocated = 0;
for (int j = 0; j <numHoles; j++) {
if (holes[ j] .size>= pr ocesses[ i] .size) {
pr intf("P%d\t%d put in H%d %d\n", i, pr ocesses[ i] .size,j, holes[ j] .size);
allocated = 1;
holes[ j] .size=holes[ j] .size- pr ocesses[ i] .size;
br eak;
}
}
if (!allocated) {
pr intf("P%d\t%d\tNot allocated\n", i+1, pr ocesses[ i] .size);
}
}
}

void wor stFit(str uct Pr ocess pr ocesses[ ] , intnumPr ocesses, str uct Hole
holes[ ] , intnumHoles) {
str uct Hole c;
pr intf("Wor st fit\n");
pr intf("Pr ocess\tPSize\tHole\n");
for (inti=0;i<numHoles- 1;i++)
for (int j=i+1;j<numHoles;j++)
if(holes[ i] .size<holes[ j] .size)
{
c=holes[ i] ;
holes[ i] =holes[ j] ;
holes[ j] =c;
}
for (inti = 0; i<numPr ocesses; i++) {
int allocated = 0;
for (int j = 0; j <numHoles; j++) {
if (holes[ j] .size>= pr ocesses[ i] .size) {
pr intf("P%d\t%d put in H%d %d\n", i, pr ocesses[ i] .size,j, holes[ j] .size);
allocated = 1;
holes[ j] .size=holes[ j] .size- pr ocesses[ i] .size;
br eak;
}
}
if (!allocated) {
pr intf("P%d\t%d\tNot allocated\n", i+1, pr ocesses[ i] .size);
}
}}
void bestFit(str uct Pr ocess pr ocesses[ ] , intnumPr ocesses, str uct Hole
holes[ ] , intnumHoles) {
str uct Hole c;
pr intf("Best fit\n");
pr intf("Pr ocess\tPSize\tHole\n");
for (inti=0;i<numHoles- 1;i++)
for (int j=i+1;j<numHoles;j++)
if(holes[ i] .size>holes[ j] .size)
{
c=holes[ i] ;
holes[ i] =holes[ j] ;
holes[ j] =c;
}
for (inti = 0; i<numPr ocesses; i++) {
int allocated = 0;
for (int j = 0; j <numHoles; j++) {
if (holes[ j] .size>= pr ocesses[ i] .size) {
pr intf("P%d\t%d put in H%d %d\n", i, pr ocesses[ i] .size,j, holes[ j] .size);
allocated = 1;
holes[ j] .size=holes[ j] .size- pr ocesses[ i] .size;
br eak;
}
}
if (!allocated) {
pr intf("P%d\t%d\tNot allocated\n", i+1, pr ocesses[ i] .size);
}
}
}

intmain() {
intnumHoles, numPr ocesses;
pr intf("Enter the number of Holes : ");
scanf("%d", &numHoles);
str uct Hole holes[ MAX_ HOLES] ;
for (inti = 0; i<numHoles; i++) {
pr intf("Enter size for hole H%d : ", i);
scanf("%d", &holes[ i] .size);
}

pr intf("Enter number of pr ocess : ");


scanf("%d", &numPr ocesses);
str uct Pr ocess pr ocesses[ MAX_ PROCESSES] ;
for (inti = 0; i<numPr ocesses; i++) {
pr intf("enter the size of pr ocess P%d : ", i);
scanf("%d", &pr ocesses[ i] .size);
}

pr intf("1.Fir st Fit 2.Best fit and 3. Wor st fit\n");


scanf("%d",&ch);
switch(ch)
{ case 1:
fir stFit(pr ocesses, numPr ocesses, holes, numHoles);
br eak;
case 2:
bestFit(pr ocesses, numPr ocesses, holes, numHoles);
br eak;
case 3:
wor stFit(pr ocesses, numPr ocesses, holes, numHoles);
br eak;
default:
br eak;
}
r etur n 0;
}
Output
Enter the number of Holes : 2
Enter size for hole H0 : 500
Enter size for hole H1 : 300
Enter number of pr ocess : 2
enter the size of pr ocess P0 : 300
enter the size of pr ocess P1 : 250
1.Fir st Fit 2.Best fit and 3. Wor st fit
3
Wor st fit
Pr ocess PSize Hole
P0 300 put in H0 500
P1 250 put in H1 300
Ex. No. 12 Page Replacement Algor ithms

Pr ogr am

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#define MAX_ FRAMES 10
int fifo(int pages[ ] , int n, int capacity) {
int fr ame[ MAX_ FRAMES] , fr ameCount = 0,
pageFaults = 0, i, j, flag;

for (i = 0; i < n; i++) {


flag = 0;
for (j = 0; j < fr ameCount; j++) {
if(fr ame[ j] == pages[ i] ) {
flag = 1;
br eak;
}
}

if(flag == 0) {
if(fr ameCount < capacity) {
fr ame[ fr ameCount++] = pages[ i] ;
}
else {
for (j = 0; j < capacity - 1; j++) {
fr ame[ j] = fr ame[ j + 1] ;
}
fr ame[ j] = pages[ i] ;
}
pageFaults++;
}
}

r etur n pageFaults;
}

int optimal(int pages[ ] , int n, int capacity) {


int fr ame[ MAX_ FRAMES] , fr ameCount = 0,
pageFaults = 0, i, j, k, far thest, flag,
temp[ MAX_ FRAMES] ;

for (i = 0; i < n; i++) {


flag = 0;
for (j = 0; j < fr ameCount; j++) {
if(fr ame[ j] == pages[ i] ) {
flag = 1;
br eak;
}
}
if(flag == 0) {
if(fr ameCount < capacity) {
fr ame[ fr ameCount++] = pages[ i] ;
}
else {
for (j = 0; j < capacity; j++) {
temp[ j] = INT_ MAX;
for (k = i + 1; k < n; k++) {
if(pages[ k] == fr ame[ j] ) {
temp[ j] = k;
br eak;
}
}
}

far thest = 0;
for (j = 1; j < capacity; j++) {
if(temp[ j] > temp[ far thest] )
far thest = j;
}

fr ame[ far thest] = pages[ i] ;


}
pageFaults++;
}
}

r etur n pageFaults;
}

int main() {
int pages[ ] = { 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3,
2} ;
int n = sizeof(pages) / sizeof(pages[ 0] );
int capacity = 4;
pr intf("FIFO Page Replacement Algor ithm\n");
pr intf("Number of Page Faults: %d\n",
fifo(pages, n, capacity));

pr intf("\nOptimal Page Replacement


Algor ithm\n");
pr intf("Number of Page Faults: %d\n",
optimal(pages, n, capacity));

r etur n 0;
}

Output
FIFO Page Replacement Algor ithm
Number of Page Faults: 7

Optimal Page Replacement Algor ithm


Number of Page Faults: 6
Ex. No. 13 a Single level dir ector y

Pr ogr am

#include <stdio.h>
#include <stdlib.h>
#include <str ing.h>

str uct File {


char name[ 50] ;
};

str uct File files[ 100] ;


int totalFiles = 0;

void cr eateFile(const char *name) {


if (totalFiles < 100) {
str cpy(files[ totalFiles] .name, name);
totalFiles++;
pr intf("File '%s' cr eated successfully.\n",
name);
} else {
pr intf("Maximum number of files r eached.
Cannot cr eate file.\n");
}
}

void displayFiles() {
pr intf("Files in the dir ector y:\n");
for (int i = 0; i < totalFiles; i++) {
pr intf("%s\n", files[ i] .name);
}
}
int main() {
cr eateFile("file1.txt");
cr eateFile("file2.txt");
cr eateFile("file3.txt");
displayFiles();
r etur n 0;
}
Output

File 'file1.txt' cr eated successfully.


File 'file2.txt' cr eated successfully.
File 'file3.txt' cr eated successfully.
Files in the dir ector y:
file1.txt
file2.txt
file3.txt
Ex. No. 13 b Two level dir ector y

Pr ogr am

#include <stdio.h>
#include <stdlib.h>
#include <str ing.h>

str uct File {


char name[ 50] ;
};

str uct Dir ector y {


char name[ 50] ;
str uct File files[ 100] ;
int totalFiles;
};

str uct Dir ector y master Dir ector y[ 100] ;


int totalDir ector ies = 0;

void cr eateUser Dir ector y(const char *user name)


{
if (totalDir ector ies < 100) {

str cpy(master Dir ector y[ totalDir ector ies] .name,


user name);

master Dir ector y[ totalDir ector ies] .totalFiles = 0;


totalDir ector ies++;
pr intf("User dir ector y '%s' cr eated
successfully.\n", user name);
} else {
pr intf("Maximum number of dir ector ies
r eached. Cannot cr eate user dir ector y.\n");
}
}

void cr eateFileInDir ector y(const char


*user name, const char *filename) {
int i;
for (i = 0; i < totalDir ector ies; i++) {
if (str cmp(master Dir ector y[ i] .name,
user name) == 0) {
if (master Dir ector y[ i] .totalFiles < 100) {

str cpy(master Dir ector y[ i] .files[ master Dir ector y


[ i] .totalFiles] .name, filename);
master Dir ector y[ i] .totalFiles++;
pr intf("File '%s' cr eated successfully in
dir ector y '%s'.\n", filename, user name);
r etur n;
} else {
pr intf("Maximum number of files
r eached in dir ector y '%s'. Cannot cr eate file.\n",
user name);
r etur n;
}
}
}
pr intf("User dir ector y '%s' not found.\n",
user name);
}

void displayFilesInDir ector y(const char


*user name) {
int i;
for (i = 0; i < totalDir ector ies; i++) {
if (str cmp(master Dir ector y[ i] .name,
user name) == 0) {
pr intf("Files in dir ector y '%s':\n",
user name);
for (int j = 0; j <
master Dir ector y[ i] .totalFiles; j++) {
pr intf("%s\n",
master Dir ector y[ i] .files[ j] .name);
}
r etur n;
}
}
pr intf("User dir ector y '%s' not found.\n",
user name);
}

int main() {
cr eateUser Dir ector y("user 1");
cr eateUser Dir ector y("user 2");
cr eateFileInDir ector y("user 1", "file1.txt");
cr eateFileInDir ector y("user 1", "file2.txt");
cr eateFileInDir ector y("user 2", "file3.txt");
displayFilesInDir ector y("user 1");
displayFilesInDir ector y("user 2");
r etur n 0;
}

Output
User dir ector y 'user 1' cr eated successfully.
User dir ector y 'user 2' cr eated successfully.
File 'file1.txt' cr eated successfully in dir ector y
'user 1'.
File 'file2.txt' cr eated successfully in dir ector y
'user 1'.
File 'file3.txt' cr eated successfully in dir ector y
'user 2'.
Files in dir ector y 'user 1':
file1.txt
file2.txt
Files in dir ector y 'user 2':
file3.txt
Ex. No. 14 File Allocation methods

Pr ogr am

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

#define DISK_ SIZE 10


#define INDEX_ BLOCK_ SIZE 10

bool disk[ DISK_ SIZE] = { 0} ; // Initialize all


blocks as fr ee

typedef str uct {


int indexBlock;
int fileBlocks[ INDEX_ BLOCK_ SIZE] ;
} IndexedFile;

typedef str uct {


int star tBlock;
} LinkedFile;

void pr intDisk() {
for (int i = 0; i < DISK_ SIZE; i++) {
pr intf("%d", disk[ i] );
}
pr intf("\n");
}

// Sequential File Allocation


bool allocateSequential(int star t, int length) {
for (int i = star t; i < star t + length; i++) {
if (i >= DISK_ SIZE || disk[ i] ) {
r etur n false; // Not enough space or
block alr eady occupied
}
}
for (int i = star t; i < star t + length; i++) {
disk[ i] = tr ue;
}
r etur n tr ue;
}

void deallocateSequential(int star t, int length) {


for (int i = star t; i < star t + length; i++) {
if (i < DISK_ SIZE) {
disk[ i] = false;
}
}
}

// Indexed File Allocation


IndexedFile* cr eateIndexedFile(int fileSize) {
IndexedFile* file =
(IndexedFile*)malloc(sizeof(IndexedFile));
file- >indexBlock = - 1;

for (int i = 0; i < DISK_ SIZE; i++) {


if (!disk[ i] ) {
file- >indexBlock = i;
disk[ i] = tr ue;
br eak;
}
}

if (file- >indexBlock == - 1) {
fr ee(file);
r etur n NULL;
}

int allocated = 0;
int fileBlockIndex = 0;
for (int i = 0; i < DISK_ SIZE && allocated <
fileSize; i++) {
if (!disk[ i] ) {
file- >fileBlocks[ fileBlockIndex++] = i;
disk[ i] = tr ue;
allocated++;
}
}

if (allocated < fileSize) {


disk[ file- >indexBlock] = false;
fr ee(file);
r etur n NULL;
}

r etur n file;
}

void deallocateIndexedFile(IndexedFile* file, int


fileSize) {
if (!file) r etur n;
disk[ file- >indexBlock] = false;

for (int i = 0; i < fileSize; i++) {


disk[ file- >fileBlocks[ i] ] = false;
}

fr ee(file);
}

// Linked File Allocation


typedef str uct Block {
int nextBlock;
} Block;

Block diskBlocks[ DISK_ SIZE] ;

LinkedFile* cr eateLinkedFile(int fileSize) {


LinkedFile* file =
(LinkedFile*)malloc(sizeof(LinkedFile));
file- >star tBlock = - 1;

int pr eviousBlock = - 1;

for (int i = 0, allocatedBlocks = 0; i <


DISK_ SIZE && allocatedBlocks < fileSize; i++) {
if (!disk[ i] ) {
disk[ i] = tr ue;
if (pr eviousBlock != - 1) {
diskBlocks[ pr eviousBlock] .nextBlock =
i;
} else {
file- >star tBlock = i;
}
pr eviousBlock = i;
diskBlocks[ i] .nextBlock = - 1;
allocatedBlocks++;
}
}

if (pr eviousBlock == - 1) {
fr ee(file);
r etur n NULL;
}

r etur n file;
}

void deallocateLinkedFile(LinkedFile* file) {


if (!file) r etur n;
int block = file- >star tBlock;

while (block != - 1) {
int nextBlock = diskBlocks[ block] .nextBlock;
disk[ block] = false;
block = nextBlock;
}

fr ee(file);
}

int main() {
int choice, star t, length, fileSize;
IndexedFile* indexedFile = NULL;
LinkedFile* linkedFile = NULL;

while (1) {
pr intf("Choose file allocation str ategy:\n");
pr intf("1. Sequential\n");
pr intf("2. Indexed\n");
pr intf("3. Linked\n");
pr intf("4. Exit\n");
pr intf("Enter choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
pr intf("Enter star t block and length: ");
scanf("%d %d", &star t, &length);
if (allocateSequential(star t, length)) {
pr intf("Sequential file allocated fr om
block %d to %d.\n", star t, star t + length - 1);
} else {
pr intf("Failed to allocate sequential
file.\n");
}
pr intDisk();
pr intf("Deallocating sequential file...\n");

deallocateSequential(star t, length);
pr intDisk();
br eak;
case 2:
pr intf("Enter file size: ");
scanf("%d", &fileSize);
indexedFile = cr eateIndexedFile(fileSize);
if (indexedFile) {
pr intf("Indexed file allocated with
index block at %d.\n", indexedFile- >indexBlock);
} else {
pr intf("Failed to allocate indexed file.
\n");
}
pr intDisk();
if (indexedFile) {
pr intf("Deallocating indexed file...\n");
deallocateIndexedFile(indexedFile,
fileSize);
}
pr intDisk();
br eak;
case 3:
pr intf("Enter file size: ");
scanf("%d", &fileSize);
linkedFile = cr eateLinkedFile(fileSize);
if (linkedFile) {
pr intf("Linked file allocated star ting
at block %d.\n", linkedFile- >star tBlock);
} else {
pr intf("Failed to allocate linked file.
\n");
}
pr intDisk();
if (linkedFile) {
pr intf("Deallocating linked file...\n");
deallocateLinkedFile(linkedFile);
}
pr intDisk();
br eak;
case 4:
exit(0);
br eak;
default:
pr intf("Invalid choice. Tr y again.\n");
}
}

r etur n 0;
}
Output

Choose file allocation str ategy:


1. Sequential
2. Indexed
3. Linked
4. Exit
Enter choice: 1
Enter star t block and length: 2
2
Sequential file allocated fr om block 2 to 3.
0011000000
Deallocating sequential file...
0000000000
Choose file allocation str ategy:
1. Sequential
2. Indexed
3. Linked
4. Exit
Enter choice: 2
Enter file size: 2
Indexed file allocated with index block at 0.
1110000000
Deallocating indexed file...
0000000000
Choose file allocation str ategy:
1. Sequential
2. Indexed
3. Linked
4. Exit
Enter choice: 3
Enter file size: 3
Linked file allocated star ting at block 0.
1110000000
Deallocating linked file...
0000000000
Choose file allocation str ategy:
1. Sequential
2. Indexed
3. Linked
4. Exit
Enter choice:
Ex. No. 15a FCFS Disk Scheduling Algor ithm

Pr ogr am
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,n,r eq[ 50] ,mov=0,cp;
pr intf("enter the cur r ent position\n");
scanf("%d",&cp);
pr intf("enter the number of r equests\n");
scanf("%d",&n);
pr intf("enter the r equest or der \n");
for (i=0;i<n;i++)
{
scanf("%d",&r eq[ i] );
}
mov=mov+abs(cp- r eq[ 0] ); // abs is used to
calculate the absolute value
pr intf("%d - > %d",cp,r eq[ 0] );
for (i=1;i<n;i++)
{
mov=mov+abs(r eq[ i] - r eq[ i- 1] );
pr intf(" - > %d",r eq[ i] );
}
pr intf("\n");
pr intf("total head movement = %d\n",mov);
}
Output
enter the cur r ent position
40
enter the number of r equests
5
enter the r equest or der
23
51
48
75
82
40 - > 23 - > 51 - > 48 - > 75 - > 82
total head movement = 82

Ex. No. 15b SSTF Disk Scheduling Algor ithm

Pr ogr am
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int
i,n,k,r eq[ 50] ,mov=0,cp,index[ 50] ,min,a[ 50] ,j=0,
mini,cp1;
pr intf("enter the cur r ent position\n");
scanf("%d",&cp);
pr intf("enter the number of r equests\n");
scanf("%d",&n);
cp1=cp;
pr intf("enter the r equest or der \n");
for (i=0;i<n;i++)
{
scanf("%d",&r eq[ i] );
}
for (k=0;k<n;k++)
{
for (i=0;i<n;i++)
{
index[ i] =abs(cp- r eq[ i] ); // calculate distance
of each r equest fr om cur r ent position
}
// to find the near est r equest
min=index[ 0] ;
mini=0;
for (i=1;i<n;i++)
{
if(min>index[ i] )
{
min=index[ i] ;
mini=i;
}
}
a[ j] =r eq[ mini] ;
j++;
cp=r eq[ mini] ; // change the cur r ent position
value to next r equest
r eq[ mini] =999;
} // the r equest that is pr ocessed its value is
changed so that it is not pr ocessed again
pr intf("Sequence is : ");
pr intf("%d",cp1);
mov=mov+abs(cp1- a[ 0] ); // head movement
pr intf(" - > %d",a[ 0] );
for (i=1;i<n;i++)
{
mov=mov+abs(a[ i] - a[ i- 1] ); ///head
movement
pr intf(" - > %d",a[ i] );
}
pr intf("\n");
pr intf("total head movement = %d\n",mov);
}
Output
enter the cur r ent position
40
enter the number of r equests
5
enter the r equest or der
50
30
70
60
20
Sequence is : 40 - > 50 - > 60 - > 70 - > 30 - > 20
total head movement = 80

Ex.No. 16 Guest oper ating system installation using VMwar e.


Steps to Installing Linux on VMwar e
1. Download a Linux ISO File
Fir st, download the ISO file for the Linux distr ibution to be installed. For example, download
Ubuntu fr om Ubuntu's official website.
2. Open VMwar e Wor kstation or VMwar e Player
Launch VMwar e Wor kstation or VMwar e Player on the computer .
3. Cr eate a New Vir tual Machine
● Click on File > New Vir tual Machine or simply select Cr eate a New Vir tual Machine
fr om the home scr een.
● Choose Typical (r ecommended) and click Next.
4. Select the Installation Media
● Select Installer disc image file (iso).
● Br owse and select the Linux ISO file that was downloaded ear lier .
● Click Next.
5. Name the Vir tual Machine
● Give the vir tual machine a name (e.g., "Ubuntu VM").
● Choose a location wher e to save the vir tual machine files.
● Click Next.
6. Specify Disk Capacity
● Specify the maximum disk size (e.g., 20 GB).
● Choose Stor e vir tual disk as a single file for better per for mance, or Split vir tual
disk into multiple files to move the VM to another system easily.
● Click Next.
7. Customize Har dwar e (Optional)
● Click Customize Har dwar e to adjust settings such as the amount of RAM, number
of CPU cor es, or networ k configur ation.
● After customizing, click Close and then Finish.
8. Star t the Vir tual Machine
● Then, a new vir tual machine will appear in the VMwar e Wor kstation or Player
window.
● Select the vir tual machine and click Power on this vir tual machine.

9. Install Linux
● The vir tual machine will boot fr om the ISO file.
● Follow the on- scr een instr uctions to install the Linux distr ibution. This typically
involves:
● Selecting the installation language.
● Choosing the installation type (e.g., "Er ase disk and install Ubuntu").
● Setting up user infor mation (user name, passwor d).
10. Complete Installation
● After the installation pr ocess completes, the vir tual machine will pr ompt to
r estar t.
● Once r estar ted, need to r emove the ISO fr om the vir tual CD/DVD dr ive to pr event
it fr om booting fr om the ISO again.
11. Install VMwar e Tools
● After the OS is installed, it’s r ecommended to install VMwar e Tools for better
per for mance and additional featur es.
● In the VM window, click VM > Install VMwar e Tools.
● Follow the pr ompts within the Linux VM to install the tools. This often involves
mounting the VMwar e Tools ISO and r unning the installer fr om a ter minal.

Content beyond the syllabus

Ex.No.17 Dining Philosopher s Pr oblem

Pr ogr am
#include <pthr ead.h>
#include <semaphor e.h>
#include <stdio.h>
#include <unistd.h>

#define NUM_ PHILOSOPHERS 5

// Semaphor es for the for ks


sem_ t for ks[ NUM_ PHILOSOPHERS] ;

void *philosopher (void *num) {


int id = *(int *)num;
int left = id;
int r ight = (id + 1) % NUM_ PHILOSOPHERS;

while (1) {
// Thinking
pr intf("Philosopher %d is thinking.\n", id);
sleep(1);

// Hungr y
pr intf("Philosopher %d is hungr y.\n", id);

// Picking up left for k


sem_ wait(&for ks[ left] );
pr intf("Philosopher %d picked up left for k %d.\n", id, left);

// Picking up r ight for k


sem_ wait(&for ks[ r ight] );
pr intf("Philosopher %d picked up r ight for k %d.\n", id, r ight);

// Eating
pr intf("Philosopher %d is eating.\n", id);
sleep(1);

// Putting down r ight for k


sem_ post(&for ks[ r ight] );
pr intf("Philosopher %d put down r ight for k %d.\n", id, r ight);

// Putting down left for k


sem_ post(&for ks[ left] );
pr intf("Philosopher %d put down left for k %d.\n", id, left);
}
r etur n NULL;
}

int main() {
pthr ead_ t philosopher s[ NUM_ PHILOSOPHERS] ;
int ids[ NUM_ PHILOSOPHERS] ;

// Initialize semaphor es
for (int i = 0; i < NUM_ PHILOSOPHERS; i++) {
sem_ init(&for ks[ i] , 0, 1);
}

// Cr eate philosopher thr eads


for (int i = 0; i < NUM_ PHILOSOPHERS; i++) {
ids[ i] = i;
pthr ead_ cr eate(&philosopher s[ i] , NULL, philosopher , &ids[ i] );
}

// Join philosopher thr eads


for (int i = 0; i < NUM_ PHILOSOPHERS; i++) {
pthr ead_ join(philosopher s[ i] , NULL);
}

// Destr oy semaphor es
for (int i = 0; i < NUM_ PHILOSOPHERS; i++) {
sem_ destr oy(&for ks[ i] );
}

r etur n 0;
}
Output

Philosopher 0 is thinking.
Philosopher 1 is thinking.
Philosopher 3 is thinking.
Philosopher 2 is thinking.
Philosopher 4 is thinking.
Philosopher 3 is hungr y.
Philosopher 3 picked up left for k 3.
Philosopher 3 picked up r ight for k 4.
Philosopher 3 is eating.
Philosopher 0 is hungr y.
Philosopher 0 picked up left for k 0.
Philosopher 0 picked up r ight for k 1.
….

You might also like