0% found this document useful (0 votes)
13 views72 pages

OS Lab Manual

Uploaded by

Anbuchelvan VK
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)
13 views72 pages

OS Lab Manual

Uploaded by

Anbuchelvan VK
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/ 72

1

Ex.No : 1 Basics of UNIX commands


DATE:
Aim:
To study basic UNIX commands.
1) Command : cat
a) To Create a New File:
Syntax: cat > filename
[cselab@svclinux]$ cat > one.txt
Hi
Welcome to CSE Lab
SVCET
Puliyangudi
Pressing Ctrl+z to save the file
[2]+ Stopped cat >one.txt
[cselab@svclinux]$

b) To Display the Contents of the File :


Syntax : cat filename
[cselab@svclinux]$ cat one.txt
Hi
Welcome to CSE Lab
SVCET
Puliyangudi
[cselab@svclinux]$

2) Command : mkdir (Make directory)


Syntax : mkdir filename
[cselab@svclinux]$ mkdir programs
[cselab@svclinux]$ ls
a.out college.txt fir.sh new.txt second.sh
col1.txt coll.txt firs.sh one pgms
college2.txt file.txt one.txt programs
[cselab@svclinux]$

3) Command : cd (change directory)


Syntax : cd directory name
[cselab@svclinux]$ cd programs
[cselab@svclinux programs]$ cd ..
[cselab@svclinux]$ cd programs
[cselab@svclinux programs]$ mkdir shellprogram
[cselab@svclinux programs]$ cd shellprogram
[cselab@svclinux shellprogram]$ cd
[cselab@svclinux]$

4) Command : pwd (Print Working Directory)


[cselab@svclinux shellprogram]$ pwd
/home/cselab/programs/shellprogram
[cselab@svclinux shellprogram]$ cd
[cselab@svclinux]$ pwd
/home/cselab
[cselab@svclinux]$
2

5) Command : ls (List Files and Directories)


a) ls
[cselab@svclinux]$ 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@svclinux]$

b) ls –l : Lists Files and Directories in Long Listing Mode


[cselab@svclinux]$ ls -l
total 112
-rw-rw-r-- 1 cselab cselab 35 Jul 21 16:31 111
-rw-r--r-- 1 cselab cselab 0 Sep 3 13:12 1.ccc
-rw-rw-r-- 1 cselab cselab 6857 Aug 9 16:30 1.docx
-rw-rw-r-- 1 cselab cselab 785 Jul 21 16:32 1.prn
-rw-rw-r-- 1 cselab cselab 166 Jul 21 16:33 2.prn
-rwxrwxr-x 1 cselab cselab 4777 Nov 22 11:00 a.out
-rw-rw-r-- 1 cselab cselab 346 Jul 15 2016 array.c
drwxrwxr-x 2 cselab cselab 4096 Sep 2 13:22 CP
-rw-rw-r-- 1 cselab cselab 81 Aug 3 16:47 cpgm.c
-rw-rw-r-- 1 cselab cselab 842 Aug 6 12:21 duplicate.c
-rw-rw-r-- 1 cselab cselab 265 Oct 31 13:26 fib.c
[cselab@svclinux]$

c) [cselab@svclinux]$ ls –a (To show Hidden files)


. 2.prn .bashrc fun.c mac.c sample.c
.. a.out CP .gnome2 Makefile .ssh
111 array.c cpgm.c HelloWorld.class .mozilla streverse.c
1.ccc .bash_history duplicate.c HelloWorld.java OS .vim
1.docx .bash_logout .emacs inc.c pr.c .viminfo
1.prn .bash_profile fib.c JAVA print.c vin [cselab@svclinux]$

d) [cselab@svclinux]$ 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@svclinux]$

6) Command : mv (Move)
a) mv (Move a File to Directory)
Syntax : mv SourceFile DestinationDirectory
[cselab@svclinux]$ mv coll.txt programs
[cselab@svclinux]$ cd programs
[cselab@svclinux programs]$ ls
coll.txt shellprogram
[cselab@svclinux programs]$
b) mv (Move Contents of one File to Another File)
Syntax : mv SourceFile DestinationFile
[cselab@svclinux]$ cat one.txt
Hi, Welcome to CSE Lab
SVCET
[cselab@svclinux]$ mv one.txt new.txt
[cselab@svclinux]$ cat new.txt
3

Hi, Welcome to CSE Lab


SVCET
[cselab@svclinux]$
7) Command : rm (Remove a File)
Syntax : rm filename
[cselab@svclinux]$ ls
a.out college2.txt file.txt firs.sh one programs
col1.txt college.txt fir.sh new.txt pgms second.sh
[cselab@svclinux]$ rm new.txt
[cselab@svclinux]$ ls
a.out college2.txt file.txt firs.sh pgms second.sh
col1.txt college.txt fir.sh one programs
[cselab@svclinux]$

8) Command : rmdir (Remove a Diretory)


Syntax : rmdir Directoryname
[cselab@svclinux programs]$ ls
coll.txt shellprogram
[cselab@svclinux programs]$ rmdir shellprogram
[cselab@svclinux programs]$ ls
coll.txt
[cselab@svclinux programs]$

9) Command : rm -rf (Remove a Diretory which is not Empty)


Syntax : rm –rf Directoryname
[cselab@svclinux programs]$ ls
coll.txt
[cselab@svclinux]$ rm -rf programs
[cselab@svclinux]$ ls
a.out college2.txt file.txt firs.sh pgms
col1.txt college.txt fir.sh one second.sh
[cselab@svclinux]$
10) Command : echo (Prints the Given String)
a) Syntax: echo “String”
[cselab@svclinux]$ echo "SVCET"
SVCET
[cselab@svclinux]$
b) Syntax: echo –n “String” (Don’t Print a New Line)
[cselab@svclinux]$ echo -n "SVCET"
SVCET [cselab@svclinux]$

11) 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@svclinux]$ cat one.txt
Welcome to SVCET
Puliyangudi
CSE Dept
Computer Practices Laboratory
[cselab@svclinux]$ head -2 one.txt
Welcome to SVCET
Puliyangudi
[cselab@svclinux]$
4

12) Command : tail (Prints required no. of lines in the File Counting from
the End of the File)
Syntax : tail –n filename
n : Number of Lines to be displayed
[cselab@svclinux]$ cat one.txt
Welcome to SVCET
Puliyangudi
CSE Dept
Computer Practices Laboratory
[cselab@svclinux]$ tail -2 one.txt
CSE Dept
Computer Practices Laboratory
[cselab@svclinux]$

13) Command : who (Displays the Users Who Logged into the System)
Syntax: who
[cselab@svclinux]$ who
root tty7 2015-01-06 02:14 (:0)
cselab pts/20 2016-12-01 16:07 (172.16.67.30)
[cselab@svclinux]$
Command : who am i (Displays the Name of the Current User of this
System)
Syntax: who am i
[cselab@svclinux]$ who am i
cselab pts/20 2016-12-01 16:07 (172.16.67.30)
[cselab@svclinux]$

14) Command : date (Displays the Current Date and Time)


Syntax: date
[cselab@svclinux]$ date
Thu Dec 1 16:07:49 IST 2016
[cselab@svclinux]$

15) Command : cal (Displays the Calendar)

Syntax: cal
[cselab@svclinux]$ cal
[cselab@svclinux]$ cal
December 2016
Su Mo Tu We Th Fr Sa
1 2 3
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

[cselab@svclinux]$ cal 5 2009


May 2009
Su Mo Tu We Th Fr Sa
1 2
3 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 [cselab@svclinux]$
5

16) 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@svclinux]$ cat one.txt
Welcome to SVCET
Puliyangudi
CSE Dept
Computer Practices Laboratory
[cselab@svclinux]$ grep "Ramco" one.txt
Welcome to SVCET
[cselab@svclinux]$

Result
Thus the study and execution of UNIX commands has been completed successfully.
6

Ex.No : 2.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);
}
}

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.
7

Ex.No : 2.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);
}

Output:
first.c
pk6.c f2
abc FILE1

Result
Thus the program using System Calls opendir(),readdir() has been completed
successfully.
8

Ex.No : 3.1 Write C programs to simulate UNIX commands ‘ls’


DATE:

AIM: To write a C program to simulate the operation of “ls” commands in Unix.

ALGORITHM:
Step 1 : Include the necessary header files.
Step 2 : Define the buffer size as 1024.
Step 3 : Get the file name which has been created already.
Step 4 : Open the file in read mode.
Step 5 :.Read the contents of the file and store it in the buffer.
Step 6 : Print the contents stored in the buffer.
Step 7 : Close the file.
Program:
#include<stdio.h>
#include<dirent.h>
int main()
{
struct dirent **namelist;
int n,i;
char pathname[100];
scanf("%s",&pathname);
getcwd(pathname);
n=scandir(pathname,&namelist,0,alphasort);
if(n<0)
printf("Error");
else
for(i=0;i<n;i++)
printf("%s\n",namelist[i]->d_name);
}
Output:
Enter the file name: Auto.txt

Result:
Thus the program was executed successfully
9

Ex.No : 3.2 Write C programs to simulate UNIX commands like ‘grep’


DATE:

AIM: To write a C program to simulate the operation of “grep” commands in Unix.

ALGORITHM:

Step 1 : Include the necessary header files.


Step 2 : Define the buffer size.
Step 3 : Get the file name which has been created already.
Step 4 : Open the file in read mode.
Step 5 :.Read the contents of the file and store it in the buffer.
Step 6 : Print the contents stored in the buffer.
Step 7 : Close the file.

Program:

#include<stdio.h>
#include<stdlib.h>
main()
{
FILE *fp;
char c[100],pat[10];
int l,i,j=0,count=0,len,k,flag=0;
printf("\n enter the pattern");
scanf("%s",pat);
len=strlen(pat);
fp=fopen("nn.txt","r");
while(!feof(fp))
{
fscanf(fp,"%s",c);
l=strlen(c);
count++;
for(i=0;i<count;i++)
{
if(c[i]==pat[j])
{
flag=0;
for(k=1;k<i;k++)
{
if(c[i+k]!=pat[k])
flag=1;
}
if(flag==0)
printf("\n the pattern %s is present in word %d",pat,count);
}}}}

Output:
Enter the pattern: svc
The pattern svc is present in word 5 at count.

Result:

Thus the program was executed successfully


10

Shell Programming
EX.NO:4.1 SUM OF “n” NATURAL NUMBERS
DATE:
AIM:
To write a shell script to find the sum of “N” natural numbers.
ALGORITHM:
Step1:Start the program.
Step2:Enter the number.
Step3:Assign “S” is equal to zero and i is even to one.
Step4:Using while loop calculate the sum, display the sum.
Step5:Stop the program.
PROGRAM:
echo " enter n "
read n
i=1
sum=0
while [ $i -le $n ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo " the sum is : $sum "
OUTPUT:
enter n
10
the sum is : 55

RESULT:
Thus a shell script to find the sum of “N” natural numbers was executed successfully.
11

EX.NO: 4.2 FIBONACCI SERIES


DATE:

AIM:
To write a shell script to generate Fibonacci series.
ALGORITHM:
Step1:Start the program.
Step2:Enter the number.
Step3:Initialize “b” and “d” is equal to zero.
Step4:Using while loop, the Fibonacci series is generated.
Step5:Terminate the program.
PROGRAM:
echo " enter the limit "
read n
echo " the fib series is "
b=0
c=1
d=0
i=0
if [ $n -ge 2 ]
then
echo " $b $c "
n=`expr $n - 2`
while [ $i -lt $n ]
do
a=`expr $b + $c`
b=$c
c=$a
echo " $c "
i=`expr $i + 1`
done
else
echo " $b "
fi
Output
Enter the limit
10
the fib series is
0 1
1
2
3
5
8
13
21
RESULT:
Thus a shell script to generate Fibonacci series was executed successfully.
12

EX.NO: 4.3 TO CALCULATE EMPLOYEE’S PAYROLL


DATE :
AIM:
To write the shell script to find net and gross pay.
ALGORITHM:
Step1:Start the program.
Step2:Enter the basic pay.
Step3:Calculate HRA,PF and DA.
Step4:Calculate gross pay, using HRA, PF and DA.
Step5:Calculate the net pay.
Step6:Stop the program.
PROGRAM:
echo " enter basic pay "
read i1
echo " enter da "
read i2
echo " enter hra "
read i3
echo " enter pf "
read i4
s=`expr $i1 + $i2 + $i3 - $i4`
r=`expr $i1 + $i2 + $i3`
echo " netpay: $s "
echo " grosspay : $r "
Output
enter basic pay
10000
enter da
500
enter hra
2000
enter pf
1800
netpay: 10700
grosspay : 12500

RESULT:
Thus a shell script to find net and gross pay was executed successfully.
13

EX.NO:4.4 TO REVERSE THE STRING AND TO CALCULATE THE


LENGTH OF THE STRING.
DATE :
AIM:
To write the shell script to find the length of the string and reverse the string.
ALGORITHM:
Step1:Start the program.
Step2:Enter the string.
Step3:Calculate the length of the string using “wc” command.
Step4:Using while loop, check whether the length of string is greater than zero.
Step5:Using the test command, cut each other character and store it in a temporary
variable.
Step6:Stop the program.
PROGRAM:
echo " enter the string "
read str
len=`echo $str | wc -c`
len=`expr $len - 1`
echo " the length of the string is $len "
while [ $len -gt 0 ]
do
temp=`echo $str | cut -c $len`
rev=`echo $rev$temp`
len=`expr $len - 1`
done
echo " the reversed sytring is $rev "
Output
enter the string
welcome
the length of the string is 7
the reversed sytring is emoclew
RESULT:
Thus the shell script to find the length of the string and reverse the string was
executed successfully.
14

EX.NO:4.5 ARMSTRONG NUMBER


DATE :
AIM:
To write a shell script program to find the given number is Armstrong or not.
ALGORITHM:
Step1:Start the program.
Step2:Read the number.
Step3:Initialize sum=0.
Step4:Using while check num is not equal to 0.
Step5:Using if condition check x is equal to sum.
Step6:Display the result and stop the program.
PROGRAM:
echo " enter a number "
read num
x=$num
sum=0
while [ $num -gt 0 ]
do
y=`expr $num % 10`
z=`expr $y \* $y \* $y`
sum=`expr $sum + $z`
num=`expr $num / 10`
done
if [ $x -eq $sum ]
then
echo " $x is an amstrong number "
else
echo " $x is not an amstrong number "
fi
Output
enter a number
153
153 is an amstrong number
[cselab@svclinux ~]$ sh arm.sh
enter a number
121
121 is not an amstrong number

RESULT:
Thus the shell script program to find the given number is Armstrong or not was
executed successfully.
15

EX.NO:4.6 TO FIND GIVEN STRING IS PALINDROME OR NOT


DATE :
AIM:
To write the shell script to find given string is palindrome or not.
ALGORITHM:
Step1:Start the program.
Step2:Enter the string.
Step3:Calculate the length of the string using “wc” command.
Step4:Using while loop, check whether the length of string is greater than zero.
Step5:Using the test command, cut each other character and store it in a temporary
variable.
Step6:Check reverse of the string and entered string are equal,if it is equal print
string is palindrome else print string is not a palindrome.
Step7:Stop the program.

PROGRAM:
echo " enter the string "
read str
len=`echo $str | wc -c`
len=`expr $len - 1`
echo " the length of the string is $len "
while [ $len -gt 0 ]
do
temp=`echo $str | cut -c $len`
rev=`echo $rev$temp`
len=`expr $len - 1`
done
echo " the reversed sytring is $rev "
if [$rev –eq $str ]
then
echo ”$str is a palindrome”
else
echo ”$str is not a palindrome”
fi

Output
enter the string
madam
the length of the string is 5
the reversed sytring is madam
madam is a palindrome

Result:
Thus the shell script to find the given string is palindrome or not was executed
successfully.
16

CPU SCHEDULING ALGORITHMS


EX.NO: 5 .1 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)
{
17

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;
}
18

Output
[cselab@svclinux ~]$ cc round.c
[cselab@svclinux ~]$ ./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

Average Waiting Time: 3.000000


Avg Turnaround Time: 13.000000
[cselab@svclinux ~]$

RESULT:
Thus the Round Robin CPU scheduling algorithms was executed successfully.
19

EX.NO: 5 .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 job
{
char jn[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);
scanf("%s",j[i].jn);
printf("\nEnter burst time of job %d : ",i);
scanf("%d",&j[i].bt);
}
for(i=1;i<n;i++)
{
20

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);
}
21

Output
[cselab@svclinux ~]$ cc sjf.c
[cselab@svclinux ~]$ ./a.out

Enter Total Number of Processes: 3


Enter Burst Time For Process[1]: 5
Enter Burst Time For Process[2]: 12
Enter Burst Time For Process[3]: 3
Process ID Burst Time Waiting Time Turnaround Time
Process[3] 3 0 3
Process[1] 5 3 8
Process[2] 12 8 20

Average Waiting Time: 3.666667

Average Turnaround Time: 10.333333

RESULT:
Thus the Shortest Job First CPU scheduling algorithms was executed successfully.
22

EX.NO: 5.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");
average_waiting_time = average_waiting_time / count;
average_turnaround_time = average_turnaround_time / count;
23

printf("\nAverage Waiting Time = %f", average_waiting_time);


printf("\nAverage Turnaround Time = %f", average_turnaround_time);
printf("\n");
return 0;
}
Output
[cselab@svclinux ~]$ cc fcfs.c
[cselab@svclinux ~]$ ./a.out
Enter The Number of Processes To Execute: 3

Enter The Burst Time of Processes:

Process [1]:24
Process [2]:3
Process [3]:3

Process Burst Time Waiting Time Turnaround Time

Process [1] 24.00 0.00 24.00


Process [2] 3.00 24.00 27.00
Process [3] 3.00 27.00 30.00

Average Waiting Time = 17.000000


Average Turnaround Time = 27.000000
[cselab@svclinux ~]$

RESULT:
Thus the First in First Out CPU scheduling algorithms was executed successfully.
24

EX.NO: 5 .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])
25

{
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;
}
26

Output
[cselab@svclinux ~]$ cc prior.c
[cselab@svclinux ~]$ ./a.out
Enter Total Number of Processes: 3
Enter Burst Time and Priority For 3 Processes
Process[1]
Process Burst Time: 5
Process Priority: 3
Process[2]
Process Burst Time: 8
Process Priority: 2
Process[3]
Process Burst Time: 7
Process Priority: 1
Process ID Burst Time Waiting Time Turnaround Time

Process[3] 7 0 7

Process[2] 8 7 15

Process[1] 5 15 20

Average Waiting Time: 7.000000


Average Turnaround Time: 14.000000
[cselab@svclinux ~]$

RESULT:
Thus the Priority CPU scheduling algorithms was executed successfully.
27

EX.NO: 6 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;
arg.array[1]=5;
arg.array[2]=0;
semctl(sid,0,SETALL,arg);
28

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;
29

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@svclinux ~]$ cc semaphore.c
[cselab@svclinux ~]$ ./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.
30

EX.NO: 7 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 msgegt() 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;
struct msgbuf msgp;
char tmp_msg[100];
tmp_msg[0]='\0';
31

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;
struct msgbuf
{
long mtype;
32

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;
}
33

Output:
"ex31.c" 44L, 734C written
[cselab@svclinux ~]$ cc ex31.c
[cselab@svclinux ~]$ ./a.out
Enter messsage(max 5lines)
welcome
to SVC
Welcome to OS Lab
Welcome to Second year
Good
[cselab@svclinux ~]$ cc ex32.c
[cselab@svclinux ~]$ ./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@svclinux ~]$

RESULT:
Thus the Inter Process Communication program was executed successfully.
34

ExNo 7.1) To perform matrix addition in child process using shared memory
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++)
{
scanf("%d",(a+i));
}
35

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;
}
}
36

Output
[cselab@svclinux ~]$ cc matadd.c
[cselab@svclinux ~]$ ./a.out
enter the order of matrix3
enter the elements of a1 2 3
456
789
enter the elements of b4 5 6
213
345

[cselab@svclinux ~]$ matrix addition 8 10 12


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.
37

Ex No: 7.2) To generate ODD number using Shared memory.


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 number");
if(b!=0)
{for(i=0;i<n;i++)
{
if((*a+i)%2!=0)
38

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@svclinux os]$ cc odd.c
[cselab@svclinux os]$ ./a.out
enter the no of elements3
enter the elements1
2
3
odd number13[cselab@svclinux os]$

Result
Thus the Program to To generate ODD number using Semaphore was written and
executed successfully.
39

EX.NO: 8 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();
return 0;
}
void input()
40

{
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);
41

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];
}
}
printf("\n");
while(flag)
42

{
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)
{
c1++;
}
else
43

{
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");
}
}
44

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
010
200
302
211
002
Enter the available Resources
332
Process Allocation Max Available
P1 010 753 332
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.
45

EX.NO: 9 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;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Deadlock Detection Algo ************\n");
input();
show();
cal();

return 0;
}
void input()
{
int i,j;
46

printf("Enter the no of Processes\t");


scanf("%d",&n);
printf("Enter the no of resource 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);
for(j=0;j<r;j++)
{
47

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 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++)
48

{
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)
{
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
49

process are\n");
for(i=0;i<n;i++)
{
printf("P%d\t",dead[i]);
}}
else
{
printf("\nNo Deadlock Occur");
}}
Output

********** Deadlock Detection Algo ************


Enter the no of Processes 5
Enter the no of resource instances 3
Enter the Max Matrix
753
322
902
222
433
Enter the Allocation Matrix
010
200
302
211
002
Enter the available Resources
322
Process Allocation Max Available
P1 010 753 322
P2 200 322
P3 302 902
P4 211 222
P5 002 433
P1 P3 P4 P2 P0 No Deadlock Occur
RESULT:
Thus the program for implementing deadlock detection algorithm was executed
successfully
50

EX.NO:10 WRITE C PROGRAM TO IMPLMENT THREADING &


SYNCHRONIZATION APPLICATIONS
DATE:
AIM:
To write a C program to implement thread and synchronization application.
ALGORITHM:
Step1. Start the program
Step2. i is initialized in the beginning of the main function.
Step3. Pthread function used to create thread by calling myThread().The variable is
locked using mutex variable i
Step4. The threads wait for 30ms then the thread is completed one by one in the order
which will completed execution.
Step5. At the end of the main function the mutex is destroyed .
Step6. Stop the program
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);
pthread_exit(NULL);
}
int main()
51

{
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);

}
52

Output

[cselab@svclinux ~]$ cc extrial.c -pthread


cc: extrial.c: No such file or directory
cc: no input files
[cselab@svclinux ~]$ cc ex5trial.c -pthread
[cselab@svclinux ~]$ ./a.out
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@svclinux ~]$

RESULT:
Thus the program for implementing thread and synchronization application was
executed successfully
53

EX.NO: 11 IMPLEMENTATION OF MEMORY MANAGEMENT - PAGING


DATE:
AIM:
To write a C program to implement paging concept for memory management.
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.
Step 4: The corresponding physical address can be calculate by,
PA = [ pageframe* No. of page size ] + Page offset.
Step 5: Print the physical address for the corresponding logical address.
Step 6: Terminate the program.
PROGRAM:
#include<stdio.h>
#define MAX 50
int main()
{
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("\nEnter the page table\n");
printf("(Enter frame no as -1 if that page is not present in any frame)\n\n");
printf("\npageno\tframeno\n-------\t-------");
for(i=0;i<n;i++)
{
printf("\n\n%d\t\t",i);
54

scanf("%d",&page[i]);
}
printf("\n\nEnter the logical address(i.e,page no & offset):");
scanf("%d%d",&pno,&off);
if(page[pno]==-1)
printf("\n\nThe required page is not available in any of frames");
else
printf("\n\nPhysical address(i.e,frame no & offset):%d,%d",page[pno],off);
printf("\nPhysical Address is %d",(page[pno]*ps)+off);
return 1;
}

Output
Enter the no of pages in memory4
Enter page size2
Enter no of frames4
Enter the page table
(Enter frame no as -1 if that page is not present in any frame)
pageno frameno
------- -------

0 3
1 5
2 1
3 7

Enter the logical address(i.e,page no & offset):2 19


Physical address(i.e,frame no & offset):1,19
Physical Address is 21

RESULT:
Thus C program for implementing paging concept for memory management has been
executed successfully.
55

PAGE REPLACEMENT ALGORITHM

EX.NO: 12.1 First In First Out


DATE:

AIM:
To write a C program to implement FIFO 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 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];
for(m = 0; m < frames; m++)
56

{
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;
}
57

Output

[cselab@svclinux ~]$ ./a.out


Enter Total Number of Pages: 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
Total Page Faults: 4

Result
Thus the program to implement FIFO page replacement algorithm was written and
executed successfully.
58

EX.NO: 12.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);
59

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++)
60

{
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;
}
61

Output

[cselab@svclinux ~]$ ./a.out

Enter Total Number of Frames: 3


Enter Total Number of Pages: 7
Enter Values for Reference String:
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
Total Number of Page Faults: 4

Result

Thus the program to implement LRU page replacement algorithm was written and
executed successfully.
62

EX.NO: 12.3 OPTIMAL (LFU) 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++)
{
63

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)
{
64

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]);
}
}
65

printf("\n");
}
printf("\nTotal Number of Page Faults:\t%d\n", page_faults);
return 0;
}
Output

Enter Total Number of Pages: 6


Enter Values of Reference 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
Enter Total Number of Frames: 4
FAULT 3
FAULT 3 2
3 2
FAULT 3 2 5
FAULT 3 2 5 6
FAULT 3 2 5 1
Total Number of Page Faults: 5

Result

Thus the program to implement optimal page replacement algorithm was written and
executed successfully.
66

FILE ALLOCATION STRATEGIES

EX.NO: 13.1 Sequential File Allocation


DATE:

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 filefrom 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:");
67

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.
68

EX.NO:13.2 INDEXED FILE ALLOCATION


DATE:
AIM:
To write a C Program to implement Indexed File Allocation method.

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.
Step 4: print the file name and index block.
Step 5: Stop the program.

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++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
69

}
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@svclinux ~]$ ./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
254672647
Enter starting block and size of file2:3 4
Enter blocks occupied by file2:5
enter blocks of file2:2 3 4 5 6

File index length


1 2 10
2 3 5

Enter file name:1


file name is:1
Index is:2 Block occupied are: 3 2 5 4 6 7 2 6 4 7

Result
Thus the following file allocation technique of Indexed File Allocation was written
and executed successfully.
70

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.
Step 7: Stop the execution.
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);
71

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");
}
}
72

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.

You might also like