oslab manual r2013

76
MARTHANDAM COLLEGE OF ENGINEERING & TECHNOLOGY (MACET) COLLEGE ROAD, KUTTAKUZHI, VEEYANNOOR POST, KANYAKUMARI DISTRICT, TAMIL NADU-629 177 B.E COMPUTER SCIENCE AND ENGINEERING LABORATORY MANUAL CS6413 OPERATING SYSTEMS LABORATORY BY, Mr. P. J. MERBIN JOSE AP / CSE, MACET.

Upload: joni

Post on 11-Apr-2016

27 views

Category:

Documents


7 download

DESCRIPTION

Operating systems lab manual regulation 2013

TRANSCRIPT

Page 1: Oslab Manual r2013

MARTHANDAM COLLEGE OF ENGINEERING & TECHNOLOGY (MACET)

COLLEGE ROAD, KUTTAKUZHI, VEEYANNOOR POST,

KANYAKUMARI DISTRICT, TAMIL NADU-629 177

B.E COMPUTER SCIENCE AND ENGINEERING

LABORATORY MANUAL

CS6413 OPERATING SYSTEMS LABORATORY

BY,

Mr. P. J. MERBIN JOSE

AP / CSE, MACET.

Page 2: Oslab Manual r2013

INDEX

EXP NO LIST OF EXPERIMENT PAGE NO

1(a) GENERAL PURPOSE COMMANDS

1 (b) FILE COMMANDS

1(c) DIRECTORY COMMANDS

2(a) SUM OF THE GIVEN NUMBER

2 (b) REVERSE OF THE GIVEN NUMBER

2(c) ARMSTRONG NUMBER

2(d) FIBONACCI SERIES

2(e) FACTORIAL OF GIVEN NUMBER

3 (a) IMPLEMENTATION OF FCFS SCHEDULING ALGORITHM

3(b) IMPLEMENTATION OF SJF SCHEDULING ALGORITHM

3(c) ROUND ROBIN SCHEDULING ALGORITHM

3(d) IMPLEMENTATION OF PRIORITY SCHEDULING

ALGORITHM

4(a)

FILE ALLOCATION TECHNIQUES- SEQUENTIAL

ALLOCATION

4(b)

FILE ALLOCATION TECHNIQUES- INDEXED

ALLOCATION

4(c) FILE ALLOCATION TECHNIQUES- LINKED LIST

ALLOCATION

5 PRODUCER CONSUMER PROBLEM

6(a) FILE CREATION

6(b) FILE ATTRIBUTES

7 IMPLEMENT BANKERS ALGORITHM FOR DEAD LOCK

AVOIDANCE

8 IMPLEMENT AN ALGORITHM FOR DEAD LOCK

DETECTION

9 FIFO PAGE REPLACEMENT ALGORITHM

10 INTER PROCESS COMMUNICATION – SHARED MEMORY

11(a) BEST FIT MEMORY ALLOCATION METHOD

11(b) FIRST FIT MEMORY ALLOCATION METHOD

Page 3: Oslab Manual r2013

************************************************************************

Exp No.1 (a) GENERAL PURPOSE COMMANDS

************************************************************************

AIM:

To study general purpose commands in Linux platform.

LIST OF THE COMMANDS:

The various general purpose commands in LINUX platform is listed below.

1. Date.

2. Calendar.

3. Echo

4. Banner

5. Lock.

6. TPUT.

7. UNAME.

8. TTY.

9. Manual

10. Clear.

11. BC.

EXPLANATION OF THE COMMANDS:

DATE

Date: The command used to display the data format specifier.

%d: it displays system date.

[student@localhost siva7]$ date

Wed Dec 16 11:03:28 IST 2009

%a: it is used to display the full week.

[student@localhost siva7]$ date „+%a‟

Wed

%A: to display full week.

[student@localhost siva7]$ date „+%A‟

Wednesday

%b: It is used to display the abbreviated month.

[student@localhost siva7]$ date „+%b‟

Dec

%c: to display the current date time.

[student@localhost siva7]$ date „+%c‟

Wed 16 Dec 2009 11:04:03 AM IST

Page 4: Oslab Manual r2013

%B: It is used to display the full month.

[student@localhost siva7]$ date „+%B‟

December

%C: To display the century of the year.

[student@localhost siva7]$ date „+%C‟

20

%d: To display the current date.

[student@localhost siva7]$ date „+%d‟

16

%D: To display the month date and year.

[student@localhost siva7]$ date „+%D‟

12/16/09

%h: To display the abbreviated month.

[student@localhost siva7]$ date „+%h‟

Dec

%H: To display the current Hour.

[student@localhost siva7]$ date „+%H‟

11

%I: To display the hour between 0&12.

[student@localhost siva7]$ date „+%I‟

11

%m: To display the month number.

[student@localhost siva7]$ date „+%m‟

12

%S: To display the seconds between 00 & 59.

[student@localhost siva7]$ date „+%S‟

56

%M: To display the minute between 00 &59.

[student@localhost siva7]$ date „+%M‟

05

%Y: To display the full year.

[student@localhost siva7]$ date „+%Y‟

2009

%T: To display the current time.

[student@localhost siva7]$ date „+%T‟

Page 5: Oslab Manual r2013

11:05:09

%U: To display the week of the year.

[student@localhost siva7]$ date „+%U‟

50

%x: To display the month, date and year.

[student@localhost siva7]$ date „+%x‟

12/16/2009

% X: To display the current date and time.

[student@localhost siva7]$ date „+%X‟

11:05:24 AM

%y: To display the last two digit of the year.

[student@localhost siva7]$ date „+%y‟

09

%d%m%y: To display the month, date and year.

[student@localhost siva7]$ date „+%d%m%y‟

161209

CALENDAR

Calendar: To display the calendar of the year.

[student@localhost siva7]$ cal

December 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

ECHO

Echo: To display the given command.

[student@localhost siva7]$ echo siva

siva

WHO

Who: To display the status of the year current by issues.

[student@localhost siva7]$ who –H

NAME LINE TIME COMMENT

student pts/3 Dec 16 11:02 (192.168.5.71)

student pts/2 Dec 16 11:02 (192.168.5.103)

Page 6: Oslab Manual r2013

student pts/0 Dec 16 11:02 (192.168.5.76)

Who-i: To display the given values.

[student@localhost siva7]$ who –i

who: Warning: -I will be removed in a future release; use –u instead

student pts/3 Dec 16 11:02 . 5810 (192.168.5.71)

student pts/2 Dec 16 11:02 00:01 5809 (192.168.5.103)

student pts/0 Dec 16 11:02 . 5854 (192.168.5.76)

student pts/1 Dec 16 11:02 . 5813 (192.168.5.145)

Who-d: To display the login time of the user.

[student@localhost siva7]$ who –d

Dec 16 11:00 24 id=si term=0 exit=0

Dec 16 11:01 961 id=l5 term=0 exit=0

pts/4 Dec 16 11:04 5812 id=4 term=0 exit=0

Who-login: To display the login data.

[student@localhost siva7]$ who –login

student pts/7 Dec 16 11:03 (192.168.5.105)

Who am i: To display the users.

[student@localhost siva7]$ who am i

student pts/7 Dec 16 11:03 (192.168.5.105)

[student@localhost siva7]$ tput rmso

[student@localhost siva7]$ tput smso

[student@localhost siva7]$ tty

/dev/pts/25

MANUAL COMMAND

Manual command: It will give manual about all commands.

[student@localhost siva7]$ man cat

CAT (1) FSF CAT (1)

NAME

cat – concatenate files and print on the standard output

SYNOPSIS

cat [OPTION] [FILE]...

DESCRIPTION

Concatenate FILE(s), or standard input, to standard output.

-A, --show-all

equivalent to –vET

-b, --number-nonblank

number nonblank output lines

Page 7: Oslab Manual r2013

-e equivalent to –vE

-E, --show-ends

display $ at end of each line

-n, --number

number all output lines

UNAME

Uname-n: It is used to print the kernel.

[student@localhost siva7]$ uname –n

localhost.localdomain

Uname-r: It is used to release the number.

[student@localhost siva7]$ uname –r

2.4.21-4.Elsmp

Uname-m: It is used to print the processor name.

[student@localhost siva7]$ uname –m

i686

Uname-i: It displays the platform in which are working.

[student@localhost siva7]$ uname –i

i386

BC

BC command: to perform the binary calculation.

[student@localhost siva7]$ bc

bc 1.06

243+234+436

913

Page 8: Oslab Manual r2013

Exp No.1 (b) FILE COMMANDS

************************************************************************

AIM:

To execute the file commands using LINUX.

LIST OF COMMANDS:

1. CAT COMMAND

2. SORT COMMAND

3. MV COMMAND

4. CD COMMAND

5. RM COMMAND

6. FILE COMMAND

7. WC COMMAND

8. LP COMMAND

EXPLANATION OF THE COMMANDS:

CAT COMMAND

SYN: $cat>new file.

USAGE: To create file.

[student@localhost siva7]$ cat > file1

Siva

Kodi

Babu

Ajmal

SYN: $cat new file

USAGE: To display the contents of the given file.

[student@localhost siva7]$ cat file1

Siva

Kodi

Babu

Ajmal

SYN: $cat new file new file2> newfile3.

USAGE: To concentrate the contents of the files.

[student@localhost siva7]$ cat file1 file2>file3

[student@localhost siva7]$ cat file3

Siva

Kodi

Babu

Ajmal

Page 9: Oslab Manual r2013

"Student names"

SYN: $cat b- new file

USAGE: To number the lines in the files excluding blank spaces.

[student@localhost siva7]$ cat -b file4

1 Hi

2 welcome.

SYN: $cat n- new file

USAGE: To number the lines in the files including blank spaces.

[student@localhost siva7]$ cat -n file4

1 Hi

2

3 welcome.

SORT COMMAND

SYN: $sort file name

USAGE: to sort the given files in alphabetical order.

[student@localhost siva7]$ sort file3

Ajmal

Babu

Kodi

Siva

"Student names"

SYN: $sort –r file name.

USAGE: to sort the given files in reverse order.

[student@localhost siva7]$ sort -r file3

"Student names"

Siva

Kodi

Babu

Ajmal

SYN: $sort +3 file name

USAGE: to sort the files from specified location.

[student@localhost siva7]$ sort +3 file4

Hi welcome.

MV COMMAND

SYN: $ mv file a file b

USAGE: moves to the entire contents from one file to another.

[student@localhost siva7]$ mv file3 file4

[student@localhost siva7]$ cat file4

Siva

Kodi

Babu

Ajmal

Page 10: Oslab Manual r2013

CP COMMAND

SYN: cp files file a file b

USAGE: copy the entire contents from one file to another file.

[student@localhost siva7]$ cp file4 new

[student@localhost siva7]$ cat new

Siva

Kodi

Babu

Ajmal

"Student names"

RM COMMAND

SYN: $rm file.

USAGE: To remove the files.

[student@localhost siva7]$ rm file1

[student@localhost siva7]$ rm -f file2

[student@localhost siva7]$ rm -r file3

FILE COMMAND

SYN: file file

USAGE: to display the type of the file.

[student@localhost siva7]$ file file4

file4: ASCII text

WC COMMAND

SYN: $wc-l file name

USAGE: To display the number of lines in the files.

[student@localhost siva7]$ wc -l file4

4 file4

[student@localhost siva7]$ wc -c file4

36 file4

[student@localhost siva7]$ wc -w file4

6 file4

LP COMMAND

SYN: $cp file name

USAGE: To display the output connected to the system.

[student@localhost siva7]$ lp file4

lp: error - no default destination available.

Page 11: Oslab Manual r2013

************************************************************************

Exp No.1(c) DIRECTORY COMMANDS:

************************************************************************

AIM:

To study and execute the various directory commands.

LIST OF COMMANDS:

1. Make directory command

2. Change directory command

3. Remove directory command

4. Print working directory commands

EXPLANATION OF THE COMMANDS:

MAKE DIRECTORY COMMAND

USAGE: To create a new directory

SYN: $mkdir<direc name>

[cse27@localhost cse27]$ mkdir siva

CHANGE DIRECTORY COMMAND

USAGE: To create the specified directory

SYN: $cd<dir name>

SYN: $cd is used to exit from current working directory

[cse27@localhost cse27]$ cd siva

[cse27@localhost siva]$

[cse27@localhost siva]$ cd

[cse27@localhost cse27]$

REMOVE DIRECTORY COMMAND

USAGE: To remove the directory if they are empty from disk.

SYN: $rmdir<dir name>

[cse27@localhost cse27]$ rmdir siva

[cse27@localhost cse27]$ cd siva

-bash: cd: siva: No such file or directory

[cse27@localhost cse27]$ rmdir-p siva

USAGE: To remove the directory, then try to remove each directory component of that path name.

SYN: $rmdir-p<dir name>

Page 12: Oslab Manual r2013

LIST DIRECTORY COMMAND

USAGE: To list the directory in the current directory.

SYN: $ls<option>

[cse27@localhost cse27]$ ls

udpc.c udps.c udps.cwq yutham yutham1

USAGE: To display hidden files

SYN: $ls -a

[cse27@localhost cse27]$ ls -a

. .bash_logout .canna .kde udpc.c .viminfo yutham1

.. .bash_profile .emacs udps.c .xemacs .zshrc

.bash_history .bashrc .gtkrc udps.cwq yutham

USAGE: To display the files in column vice

SYN: $ls -c

[cse27@localhost cse27]$ ls -c

yutham yutham1 udps.c udps.cwq udpc.c

USAGE: To display the files in sorted order

SYN: $ls -C

[cse27@localhost cse27]$ ls -C

udpc.c udps.c udps.cwq yutham yutham1

USAGE: To display all the directories

SYN: $ls -d

[cse27@localhost cse27]$ ls -d

.

USAGE: To display all the files

SYN:$ls-m

[cse27@localhost cse27]$ ls -m

udpc.c, udps.c, udps.cwq, yutham, yutham1

USAGE: To display the files in reverse alphabetical order

SYN:$ls -r

[cse27@localhost cse27]$ ls -r

yutham1 yutham udps.cwq udps.c udpc.c

USAGE: To display the files in that last modified time

SYN:$ls -t

[cse27@localhost cse27]$ ls -t

yutham yutham1 udps.c udps.cwq udpc.c

USAGE: To display the files in numerical listing

SYN:$ls -id

[cse27@localhost cse27]$ ls -id

8683739 .

Page 13: Oslab Manual r2013

USAGE: To display the in last occur time

SYN: $ls -u

[cse27@localhost cse27]$ ls -u

yutham yutham1 udps.c udps.cwq udpc.c

PRINT WORKING DIRECTORY COMMAND

USAGE: To display the path of the present working directory

SYN: $pwd

[cse27@localhost cse27]$ pwd

/home/cse27

.

Page 14: Oslab Manual r2013

******************************************************************

Exp No.2(a) SUM OF THE GIVEN NUMBER:

******************************************************************

AIM:

To write a shell program to find out the sum of the given number.

ALGORITHM:

1. Start the program.

2. Read the input variables i and sum.

3. Initialize the variables to 0.

4. Declare the variable n.

5. Check whether the variable n is greater than I,

a. Calculate r=n%10.

b. Calculate n/10.

c. Calculate n/10 and assign it to n.

6. Print the value of n.

7. Stop the program.

Page 15: Oslab Manual r2013

SUM OF THE GIVEN NUMBER

PROGRAM:

echo "Enter the number:"

read num

while test $num -gt 0

do

digit=`expr $num % 10`

num=`expr $num / 10`

sum=`expr $sum + $digit`

done

echo "The sum of the given number is = $sum ."

OUTPUT:

[cse27@localhost siva7]$ sh ex6.sh

Enter the number:

33

The sum of the given number is = 6 .

Page 16: Oslab Manual r2013

******************************************************************

Exp No.2 (b) REVERSE OF THE GIVEN NUMBER:

******************************************************************

AIM:

To write a shell program to calculate the reverse of a given number.

ALGORITHM:

1. Start the program.

2. Read the input n.

3. Initialize the variables rev & i=0.

4. Check whether n is greater than i.

a. Calculate r=n%10.

b. Calculate rev\*10+r.

c. Calculate n/10.

5. Print the value of rev.

6. Stop the program.

Page 17: Oslab Manual r2013

REVERSE OF THE GIVEN NUMBER

PROGRAM:

echo " Enter the number:"

read n

rev=0;

i=0;

while [ $n -gt $i ]

do

r=`expr $n % 10`

rev=`expr $rev \* 10 + $r`

n=`expr $n / 10`

done

echo "Reverse of the given number is $rev"

OUTPUT:

[cse27@localhost siva7]$ sh ex7.sh

Enter the number:

567

Reverse of the given number is 765

.

Page 18: Oslab Manual r2013

************************************************************************

Exp No. 2(c) ARMSTRONG NUMBER

************************************************************************

AIM:

To write a program for finding the given is whether an Armstrong number.

ALGORITHM:

1. Start the program.

2. Initialize the variables sum, n, num.

3. Assign the values of sum as 0.

4. Check whether num is greater than 0.

a. Calculate a=num%10.

b. Calculate b=a*a*a.

c. Calculate sum=sum+b.

d. Calculate num=num/10.

5. Check whether the sum =n,then print “the given is armstrong”.

6. Otherwise print “not armstrong”

7. Stop the program.

Page 19: Oslab Manual r2013

ARMSTRONG NUMBER

PROGRAM:

echo "Enter the number:"

read num

sum=0

n=$num

while [ $num -gt 0 ]

do

a=`expr $num % 10`

b=`expr $a \* $a \* $a`

sum=`expr $sum + $b`

num=`expr $num / 10`

done

if [ $sum -eq $n ]

then

echo "The number is an Armstrong number."

else

echo "The number is not an Armstrong number."

fi

OUTPUT:

[cse27@localhost siva7]$ sh ex8.sh

Enter the number:

153

The number is an Armstrong number.

[cse27@localhost siva7]$ sh ex8.sh

Enter the number:

297

The number is not an Armstrong number.

Page 20: Oslab Manual r2013

************************************************************************

Exp No. 2(d) FIBONACCI SERIES

************************************************************************

AIM:

To write the program to print the Fibonacci series.

ALGORITHM:

1. Start the program.

2. Get the number using read command.

3. Calculate the Fibonacci numbers using while loop.

4. Print the Fibonacci number.

5. Stop the program.

Page 21: Oslab Manual r2013

FIBONACCI SERIES

PROGRAM:

i=0

fib=0

a=0

b=1

echo " Enter the no: "

read n

echo " Fibbonacci series "

if [ $n -eq 0 ]

then

echo " 0 "

else

while [ $n -gt $i ]

do

fib=`expr $fib + $a`

a=$b

b=$fib

echo " $fib "

i=`expr $i + 1`

done

fi

OUTPUT:

Enter the no:

8

Fibbonacci series

0 1 1 2 3 5 8 13

Page 22: Oslab Manual r2013

************************************************************************

Exp No. 2(e) FACTORIAL OF GIVEN NUMBER

************************************************************************

AIM:

To write a shell program to find factorial of given number.

ALGORITHM:

1. Start the program.

2. Get the number using read command.

3. Calculate the factorial value using recursive function.

4. Print the result.

5. Stop the program.

Page 23: Oslab Manual r2013

FACTORIAL OF GIVEN NUMBER

PROGRAM

fact=1

echo "Enter the no:"

read a

i=1

while [ $i -le $a ]

do

fact=`expr $fact \* $i`

i=`expr $i + 1`

done

echo "Factorial of $a is $fact"

OUTPUT:

Enter the no:

4

Factorial of 4 is 24

Page 24: Oslab Manual r2013

************************************************************************

Exp No.3 (a) IMPLEMENTATION OF FCFS SCHEDULING ALGORITHM

************************************************************************

AIM:

To write a program for verify and execute the first come first serve algorithm

ALGORITHM:

1. Start the program.

2. Declare the required variables.

3. Get the no of process and burst time of each process using for loop.

4. Find the shortest burst time process.

5. Allow that process to run and find the waiting time and

turnaround time of process.

6. Continue the step 4 and 5 until no process remaining.

7. Calculate the total waiting and turnaround time of all process.

8. Calculate average waiting time and turnaround time of process.

9. Print the calculated values.

10. Stop the process.

Page 25: Oslab Manual r2013

IMPLEMENTATION OF FCFS SCHEDULING ALGORITHM

PROGRAM:

#include<stdio.h>

#include<stdlib.h>

int main()

{

int i,j,k,l,n,bt[10],wt[10],tn[10];

float avgwt=0,avgtn=0;

printf(" enter the number of process");

scanf("%d",&n);

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

{

printf(" enter the %d bursttime \n",i);

scanf("%d",&bt[i]);

}

wt[1]=0;

for(j=1;j<=n;j++)

{

wt[j+1]=bt[j]+wt[j];

avgwt=avgwt+wt[j];

}

for(k=1;k<=n;k++)

{

tn[k]=wt[k]+bt[k];

avgtn=avgtn+tn[k];

}

printf(" first come first serve \n");

printf("bt\t wt\t tn\t \n");

for(l=1;l<=n;l++)

{

printf("%d\t %d\t %d\t \n",bt[l],wt[l],tn[l]);

}

avgwt=avgwt/n;

avgtn=avgtn/n;

printf(" the average waiting time is %f \n",avgwt);

printf(" the average turn around time is %f \n",avgtn);

}

Page 26: Oslab Manual r2013

OUTPUT

[cse27@localhost siva7]$ cc ex18.c

[cse27@localhost siva7]$ ./a.out

enter the number of process3

enter the 1 bursttime

4

enter the 2 bursttime

5

enter the 3 bursttime

6

first come first serve

bt wt tn

4 0 4

5 4 9

6 9 15

the average waiting time is 4.333333

the average turnaround time is 9.333333

Page 27: Oslab Manual r2013

************************************************************************

Exp No. 3(b) IMPLEMENTATION OF SJF SCHEDULING ALGORITHM

************************************************************************

AIM:

To write a program for verify and execute the shortest first job algorithm

ALGORITHM:

1. Start the program.

2. Declare the required variables.

3. Get the no of process and burst time of each process using for loop.

4. Find the shortest burst time process.

5. Allow that process to run and find the waiting time and

turnaround time of process.

6. Continue the step 4 and 5 until no process remaining.

7. Calculate the total waiting and turnaround time of all process.

8. Calculate average waiting time and turnaround time of process.

9. Print the calculated values.

10. Stop the process.

Page 28: Oslab Manual r2013

IMPLEMENTATION OF SJF SCHEDULING ALGORITHM

PROGRAM:

#include<stdio.h>

int main()

{

int n,i,j,temp,temp1,pr[10],b[10],t[10],w[10],p[10];

float att=0,awt=0;

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

{

b[i]=0;

w[i]=0;

}

printf("enter the number of processes");

scanf("%d",&n);

printf("enter the burst time");

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

{

scanf("%d",&b[i]);

p[i]=i;

}

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

{

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

{if(b[i]>b[j])

{

temp=b[i];

temp1=p[i];

b[i]=b[j];

p[i]=p[j];

b[j]=temp;

p[j]=temp1;

}

}

}

w[0]=0;

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

w[i+1]=w[i]+b[i];

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

{

t[i]=w[i]+b[i];

awt=awt+w[i];

att=att+t[i];

}

awt=awt/n;

att=att/n;

Page 29: Oslab Manual r2013

printf("\n\t process\t waiting time\t turnaround time\n");

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

printf("\t%d]\t%d\t\t%d\n",p[i],w[i],t[i]);

printf("the average waiting time is %f\n",awt);

printf("the average turnaround time is %f\n",att);

return 1;

}

OUTPUT:

[cse27@localhost siva7]$ cc ex19.c

[cse27@localhost siva7]$ ./a.out

enter the number of processes4

enter the burst time

6

4

5

6

process waiting time turnaround time

1 0 4

2 4 9

0 9 15

3 15 21

the average waiting time is 7.000000

the average turnaround time is 5.250000

Page 30: Oslab Manual r2013

************************************************************************

Exp No.3(c) ROUND ROBIN SCHEDULING ALGORITHM

Date:

************************************************************************

AIM:

To write a program for verify and execute the Round Robin scheduling algorithm

ALGORITHM:

1. Start the program.

2. Declare the required variables.

3. Get the number of process and burst time of each process using for loop.

4. Get the time slice of the process.

5. Take the first process and execute for the given time slice.

6. Take the next process and execute for the given time slice.

7. Continue step 6 until no process is remaining with burst time.

8. Calculate the waiting and turnaround time of each process.

9. Calculate total and average value of waiting and turnaround time.

10. Print the calculated values.

11. Stop the process.

Page 31: Oslab Manual r2013

ROUND ROBIN SCHEDULING ALGORITHM

PROGRAM:

#include<stdio.h>

#include<string.h>

int main()

{

char p[10][5];

int et[10],wt[10],timer,count,pt[10],rt,i,j,totwt=0;

int n,found=0,m,tottt=0,tt[10];

float avgwt,avgtt;

printf(" ENTER THE NO OF PROcESS:");

scanf("%d",&n);

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

{

printf(" ENTER THE PROCESS NAME:");

scanf("%s",&p[i]);

printf(" ENTER THE BURST TIME :");

scanf("%d",&pt[i]);

}

printf(" ENTER THE TIME SLICE:");

scanf("%d",&timer);

m=n;

wt[0]=0;

i=0;

do

{

if(pt[i]>timer)

{

rt=pt[i]-timer;

strcpy(p[n],p[i]);

pt[n]=rt;

et[i]=timer;

n++;

}

else

{

et[i]=pt[i];

}

i++;

wt[i]=wt[i-1]+et[i-1];

}

while(i<n);

count=0;

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

{

for(j=i+1;j<=n;j++)

Page 32: Oslab Manual r2013

{

if(strcmp(p[i],p[j])==0)

{

count++;

found=j;

}

}

if(found!=0)

{

wt[i]=wt[found]-(count*timer);

count=0;

found=0;

}

}

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

{

tt[i]=wt[i]+pt[i];

totwt+=wt[i];

tottt+=tt[i];

}

avgwt=(float)totwt/m;

avgtt=(float)tottt/m;

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

{

printf("\n %s \t %d\t %d\t %d",p[i],pt[i],wt[i],tt[i]);

}

printf("\n TOTAL WAITING TIME %d \n",totwt);

printf("\n Average waiting time %f",avgwt);

printf("\n Total turnaround time %d \n",tottt);

printf("\n Average turnaround time %f",avgtt);

return 0;

}

Page 33: Oslab Manual r2013

OUTPUT

[cse27@localhost siva7]$ cc ex20.c

[cse27@localhost siva7]$ ./a.out

Enter the process name: aa

Enter the burst time: 4

Enter the process name: bb

Enter the burst time: 3

Enter the process name: cc

Enter the burst time: 2

Enter the process name: dd

Enter the burst time: 5

Enter the process name: ff

Enter the burst time: 3

Enter the time slice: 3

Process Bursttime Waiting time Turnaroundtime

aa 4 11 15

bb 3 3 6

cc 2 6 8

dd 5 12 17

ff 3 11 14

Total waiting time: 43

Average waiting time: 8.600000

Total turnaround time: 60

Average turnaround time: 12.000000

Page 34: Oslab Manual r2013

************************************************************************

Exp No. 3(d) IMPLEMENTATION OF PRIORITY SCHEDULING ALGORITHM

Date:

************************************************************************

AIM:

To write a program for verify and execute the priority scheduling algorithm

ALGORITHM:

1. Start the program.

2. Declare the required variables.

3. Get the no of process and burst time of each process using for loop.

4. Get the priority of each process.

5. Find the smallest priority number process.

6. Allow that process to execute and calculate the waiting and turnaround time of

process.

7. Continue step 5 and 6 until no process is remaining.

8. Calculate the total waiting and turnaround time of all process.

9. Calculate average waiting time and turnaround time of process.

10. Print the calculated values.

11. Stop the process.

Page 35: Oslab Manual r2013

IMPLEMENTATION OF PRIORITY SCHEDULING ALGORITHM

PROGRAM:

#include<stdio.h>

#include<stdlib.h>

int main()

{

int i,j,k,l,m,p,n,bt[10],wt[10],tn[10],temp,pr[10];

float avgwt=0,avgtn=0;

printf("enter the number of processor");

scanf("%d",&n);

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

{

printf("enter the %d burst time",i);

scanf("%d",&bt[i]);

printf("enter the %d priority",i);

scanf("%d",&pr[i]);

}

for(m=1;m<=n;m++)

{

for(p=m;p<=n;p++)

{

if(pr[m]>pr[p])

{

temp=pr[m];

pr[m]=pr[p];

pr[p]=temp;

temp=bt[m];

bt[m]=bt[p];

bt[p]=temp;

}

}

}

wt[1]=0;

for(j=2;j<=n;j++)

{

wt[j]=wt[j-1]+bt[j-1];

avgwt=avgwt+wt[j];

}

for(k=1;k<=n;k++)

{

tn[k]=wt[k]+bt[k];

avgtn=avgtn+tn[k];

}

printf("PRIORITY SCHEDULING \n");

printf("pr \t bt \t wt \t tn \t \n");

Page 36: Oslab Manual r2013

for(l=1;l<=n;l++)

{

printf("%d \t %d \t %d \t %d \t \n",pr[l],bt[l],wt[l],tn[l]);

}

avgwt=avgwt /n;

avgtn=avgtn /n;

printf("the average waiting time is %f\n",avgwt);

printf("the average turn around time is %f\n",avgtn);

printf("enter the %d burst time",i);

}

OUTPUT

[cse27@localhost siva7]$ cc ex21.c

[cse27@localhost siva7]$ ./a.out

enter the number of processor2

enter the 1 burst time2

enter the 1 priority2

enter the 2 burst time3

enter the 2 priority4

PRIORITY SCHEDULING

pr bt wt tn

2 2 0 2

4 3 2 5

the average waiting time is 1.000000

the average turn around time is 3.500000

Page 37: Oslab Manual r2013

************************************************************************

Exp No. 4(a) FILE ALLOCATION TECHNIQUES- SEQUENTIAL ALLOCATION

************************************************************************

AIM:

To write a program for verify and execute the priority scheduling algorithm

ALGORITHM:

1. Start the program.

2. Declare the required variables.

3. Get the no of blocks and starting block of file using for loop.

4. Enter the file name.

5. Print the file name and number of blocks occupied.

6. Stop the process.

Page 38: Oslab Manual r2013

FILE ALLOCATION TECHNIQUES- SEQUENTIAL ALLOCATION

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

{

int n,i,j,b[20],sb[20],t[20],x,c[20][20];

clrscr();

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

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

}

Page 39: Oslab Manual r2013

OUTPUT:

Enter no.of files: 2

Enter no. of blocks occupied by file1 4

Enter the starting block of file1 2

Enter no. of blocks occupied by file2 10

Enter the starting block of file2 5

Filename Start block length

1 2 4

2 5 10

Enter file name: rajesh

File name is:12803 length is:0blocks occupied

Page 40: Oslab Manual r2013

************************************************************************

Exp No. 4(b) FILE ALLOCATION TECHNIQUES- INDEXED ALLOCATION

************************************************************************

AIM:

To write the c program for implement the linked list file allocation technique.

ALGORITHM:

1. Start the program.

2. Declare the structure file.

3. Get the file name and file size.

4. Get the segment size of the disk.

5. Allocate the required no of free segment to that file.

6. Select one segment as an index for the given file and store all the allocated blocks to

that segment.

7. Print the allocated segment along with its index.

8. Stop the process.

Page 41: Oslab Manual r2013

FILE ALLOCATION TECHNIQUES- INDEXED ALLOCATION

PROGRAM:

#include<stdio.h>

struct file

{

char name[20];

int size;

}file;

int mem[200];

void indexed()

{

int loc[30],count=0,i,j,n,first=-1;

printf("\nEnter memory segment size: ");

scanf("%d",&n);

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

{

if(mem[i]==-1)

{first=i;

mem[first]=1;

break;

}

}

for(i=0,j=0;i<200;i++)

{

if((mem[i]==-1)&&(file.size >0))

{

loc[j]=i;

count++;

j++;

file.size-=n;

}

else if(file.size<=0)

{

loc[j]=-1;

j++;

break;

}

}

printf("%s :- ",file.name);

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

{

printf("\n %d -> %d",first,loc[i]);

}

printf("\n");

}

int main()

Page 42: Oslab Manual r2013

{

int i;

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

{

mem[i]=1;

}

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

{

if((i%2!=0)&&(i%3!=0))

{

mem[i]=-1;

}

}

printf("\nEnter file name: ");

scanf("%s",file.name);

printf("\nEnter file size: ");

scanf("%d",&file.size);

indexed();

return 0;

}

Page 43: Oslab Manual r2013

OUTPUT:

[cs28103@localhost cs28103]$ cc indexed.c

[cs28103@localhost cs28103]$ ./a.out

Enter file name: file1

Enter file size: 23

Enter memory segment size: 3

pmr :-

1 -> 5

1 -> 7

1 -> 11

1 -> 13

1 -> 17

1 -> 19

1 -> 23

1 -> 25

1 -> -1

Page 44: Oslab Manual r2013

************************************************************************

Exp No. 4(c) FILE ALLOCATION TECHNIQUES- LINKED LIST ALLOCATION

************************************************************************

AIM:

To write the c program for implement the linked list file allocation technique.

ALGORITHM:

1. Start the program.

2. Declare the two structure file and link.

3. Get the file name and file size.

4. Get the segment size of the disk.

5. Allocate the required no of free segment to that file using linked list.

6. Print the allocated block with its number.

7. Stop the process.

Page 45: Oslab Manual r2013

FILE ALLOCATION TECHNIQUES- LINKED LIST ALLOCATION

PROGRAM:

#include<stdio.h>

struct file

{

char name[20];

int size;

}file;

struct link

{

int present,next;

}ln[100];

int mem[200];

void linked()

{

int first=-1,count=0,i=0,j=0,n;

printf("\nEnter memory segment size: ");

scanf("%d",&n);

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

{

if(mem[i]==-1)

{first=i;

break;

}

}

ln[0].present=first;

for(i=1,j=1;i<200;i++)

{

if(mem[i]==-1)

{

file.size-=n;

ln[j].present=i;

ln[j-1].next=ln[j].present;

count++;

j++;

}

if(file.size<=0)

{

ln[j-1].next=-1;

break;

}

}

printf("%s : %d ",file.name,first);

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

{

printf(" -> %d ",ln[i].next);

Page 46: Oslab Manual r2013

}

}

int main()

{

int i;

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

{

mem[i]=1;

}

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

{

if((i%2!=0)&&(i%3!=0))

{

mem[i]=-1;

}

}

printf("\nEnter file name: ");

scanf("%s",file.name);

printf("\nEnter file size: ");

scanf("%d",&file.size);

linked();

return 0;

}

Page 47: Oslab Manual r2013

OUTPUT:

[cs28105@localhost cs28105]$ cc linked.c

[cs28105@localhost cs28105]$ ./a.out

Enter file name: pmr

Enter file size: 25

Enter memory segment size: 5

pmr : 1 -> 5 -> 7 -> 11 -> 13 -> -1

[cs28105@localhost cs28105]$

Page 48: Oslab Manual r2013

************************************************************************

Exp No. 5 PRODUCER CONSUMER PROBLEM

************************************************************************

AIM:

To write a program for verify and execute the consumer problem using semaphores.

ALGORITHM:

1. Start the program.

2. To call main function.

3. Get the choice and print it.

4. Declare i value and check whether item->buffer.

5. To call producer function and print now many no of item available.

6. Print the result.

7. Stop the program.

Page 49: Oslab Manual r2013

PRODUCER CONSUMER PROBLEM

PROGRAM:

#include<stdio.h>

void producer();

void consumer();

int i,item=0,buff=4,a[4],ch;

main()

{

printf(" Producer Consumer Problem.");

do

{

printf("\n Size of the buffer is %d.\n",buff);

printf(" Number of items in buffer %d.\n",item);

printf(" 1.Producer\n 2.Consumer\n 3.Exit\n Enter your choice: ");

scanf("%d",&ch);

if(ch==1)

producer();

else if(ch==2)

consumer();

}while(ch!=3);

}

void producer()

{

if(i==0)

{

printf("\n Enter how much item to be produced: ");

scanf("%d",&item);

if(item>buff)

{

printf("\n The buffer will overflow!");

printf("\n Re-enter below buffer size.\n");

producer();

}

else

{

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

{

printf("\n Enter the %d element: ",i);

scanf("%d",&a[i]);

}

i=i-1;

}

}

Page 50: Oslab Manual r2013

else if(i!=0)

printf("\n Producer is waiting...");

}

void consumer()

{

if(i>0)

{

printf("\n Consumer consumes %d items.\n",a[i]);

i--;

item--;

}

else

printf("\n No item are available for producer...\n");

}

OUTPUT:

[cse27@localhost siva7]$ cc ex22.c

[cse27@localhost siva7]$ ./a.out

Producer Consumer Problem.

Size of the buffer is 4.

Number of items in buffer 0.

1.Producer

2.Consumer

3.Exit

Enter your choice: 1

Enter how much item to be produced: 3

Enter the 1 element: 2

Enter the 2 element: 4

Enter the 3 element: 7

Size of the buffer is 4.

Number of items in buffer 3.

1.Producer

2.Consumer

3.Exit

Enter your choice: 2

Consumer consumes 7 items.

Size of the buffer is 4.

Number of items in buffer 2.

Page 51: Oslab Manual r2013

1.Producer

2.Consumer

3.Exit

Enter your choice: 2

Consumer consumes 4 items.

Size of the buffer is 4.

Number of items in buffer 1.

1.Producer

2.Consumer

3.Exit

Enter your choice: 2

Consumer consumes 2 items.

Size of the buffer is 4.

Number of items in buffer 0.

1.Producer

2.Consumer

3.Exit

Enter your choice: 2

No item are available for producer...

Size of the buffer is 4.

Number of items in buffer 0.

1.Producer

2.Consumer

3.Exit

Enter your choice: 3

Page 52: Oslab Manual r2013

************************************************************************

Exp No. 6(a) FILE CREATION

************************************************************************

AIM:

To write a c program to create a file using create system call.

ALGORITHM:

1. Start the program.

2. Import the required header files.

3. Enter the file name which is to be created.

4. Create the given file using create system call with read and write mode.

5. Write the set of string to the file using write system call.

6. Read the file using read system call.

7. Close the file.

8. Stop the program.

Page 53: Oslab Manual r2013

FILE CREATION

PROGRAM:

#include<stdio.h>

#include<unistd.h>

#include<sys/stat.h>

#include<sys/types.h>

#include<fcntl.h>

main()

{

int fd;

char buf1[100],buf2[]="hello everybody",fname[30];

printf("enter filename\n");

scanf("%s",fname);

fd = creat(fname,S_IRWXU);

if(fd < 0)

{

printf("error in file creation");

}

else

{

write(fd,buf2,30);

printf("contents written successfully\n");

fd=open(fname,O_RDONLY);

read(fd,buf1,30);

printf("contents is:\n%s\n",buf1);

close(fd);

}

}

Page 54: Oslab Manual r2013

OUTPUT:

ams@ams-desktop:~/uma$ cc systemcall3.c

ams@ams-desktop:~/uma$ ./a.out

enter filename

myfile

contents written successfully

contents is:

hello everybody

ams@ams-desktop:~/uma$ cat myfile

hello everybody

Page 55: Oslab Manual r2013

************************************************************************

Exp No. 6(b) FILE ATTRIBUTES

************************************************************************

AIM:

To write the c program to get the attributes of given file.

ALGORITHM:

1. Start the program.

2. Import the required header files.

3. Copy the given file attributes using stat system call into stat

Structure.

4. Print the copied attributes .

5. Stop the program.

Page 56: Oslab Manual r2013

FILE ATTRIBUTES

PROGRAM:

#include<errno.h>

#include<stdio.h>

#include<sys/stat.h>

#include<unistd.h>

int main()

{

struct stat statv;

if(stat("/home/staff/rajkumar/ww",&statv)==-1)

perror("stat");

else

{

printf("\n the file user id is: %d",statv.st_uid);

printf("\n the file group id is: %d",statv.st_gid);

printf("\n the file size is :%d",statv.st_size);

printf("\n the file inode number is :%d",statv.st_ino);

}

return (0);

}

OUTPUT:

the file user id is: 920

the file group id is: 921

the file size is :33

the file inode number is :8230320

Page 57: Oslab Manual r2013

************************************************************************

Exp No. 7 IMPLEMENT BANKERS ALGORITHM FOR DEAD LOCK AVOIDANCE

************************************************************************

AIM:

To write a C program to implement a bankers algorithm for Dead Lock Avoidance.

ALGORITHM:

1. Start the program.

2. Declare the required variables.

3. Get the no resources and allocation resources using for loop.

4. Enter the allocation of resources.

5. Print the total available resources max resources for every process.

6. Stop the process.

Page 58: Oslab Manual r2013

IMPLEMENT BANKERS ALGORITHM FOR DEAD LOCK AVOIDANCE

PROGRAM:

#include<stdio.h>

#include<conio.h>

main()

{

int

i,j,a=0,b=0,c=0,f[10],t[10][10],al[10][10],ta[10][10];

int a1[10][10], max[10][10], n[10][10], n1,p,k=0;

printf(“\n enter no.of resources”);

scanf(“%d”,n1);

printf(“\nenter the max no .of resources for each type”);

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

scanf(“%d”,&t[b][i]);

printf(“\nenter no .of process”);

scanf(“%d”,&p);

printf(“\nenter allocation resources”);

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

{

f[i]=0;

for(j=0;j<n1;j++)

scanf(“%d”,&a1[i][j]);

}

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

for(j=0;j<n1;j++)

{

if(a1[i][j]<=t[b][j])

{

t[b][j]+=a1[i][j];

continue;

}

else

printf(“\n wrong resourcesallocation”);

printf(“\n chance of deadlock occurrence after

allocation”);

for(j=0;j<n1;j++)

printf(“%d”,a1[b][j]);

printf(“\n enter the max resources for every process”);

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

for(j=0;j<n1;j++);

{

scanf(“%d”,&max[i][j]);

n[i][j]=max[i][j]-a1[i][j];

}

j=0;

printf(“\n needed resources for every process to start

Page 59: Oslab Manual r2013

execution”);

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

printf(“\n%d %d%d”,n[i][j],n[i][j+1],n[i][j+2]);

printf(“\n safe sequence the sequence of process to

compute

their execution”);

for(a=0;a<(p-c);)

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

{

j=0;

b=0;

if(f[i]==0)

{

if(n[i][j]<=a1[b][j]&&n[i][j+1]<=a1[b][j+1]&&

n[i][j+2]<=a1[b][j+2])

{

printf(“\n process %d execution started and

completed”,i+1);

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

a1[b][k]+=a1[i][k];

f[i]=1;

c++;

}

else

f[i]=0;

}

}

getch();

}

Page 60: Oslab Manual r2013

INPUT:

enter no.of resources

3

enter the max no .of resources for each type

10 5 7

enter the no .of process

5

enter allocation of resources

0 1 0 2 0 0 3 0 2 2 1 1 0 0 2

OUTPUT:

total available resources after allocation

3 3 2

enter the max resources for every process

7 5 3 3 2 2 9 0 2 2 2 2 4 3 3

needed resources for every process to start execution

7 4 3

1 2 2

6 0 0

0 1 1

4 3 1

Safe sequence ,the sequence of process to complete their

execution

Procee 2 execution started & completed

Procee 4 execution started & completed

Procee 5 execution started & completed

Procee 1 execution started & completed

Procee 3 execution started & completed

Page 61: Oslab Manual r2013

************************************************************************

IMPLEMENT AN ALGORITHM FOR DEAD LOCK DETECTION

Date:

************************************************************************

AIM:

To write a C program to implement a Dead Lock Detection.

ALGORITHM:

1. Start the program.

2. Declare the required variables.

3. Get the total no of resources and processes.

4. Enter the maximum need, allocation, resource vector.

5. Print the deadlock causing process.

6. Stop the process.

Page 62: Oslab Manual r2013

IMPLEMENT AN ALGORITHM FOR DEAD LOCK DETECTION

PROGRAM:

#include<stdio.h>

#include<conio.h>

voidmain()

{

intfound,flag,l,p[4][5],tp,tr,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;

clrscr();

printf("Enter total no of processes");

scanf("%d",&tp);

printf("Enter total no of resources");

scanf("%d",&tr);

printf("Enter claim (Max. Need) matrix\n");

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

{

printf("process %d:\n",i);

for(j=1;j<=tr;j++)

scanf("%d",&c[i][j]);

}

printf("Enter allocation matrix\n");

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

{

printf("process %d:\n",i);

for(j=1;j<=tr;j++)

scanf("%d",&p[i][j]);

}

printf("Enter resource vector (Total resources):\n");

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

{

scanf("%d",&r[i]);

}

printf("Enter availability vector (available resources):\n");

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

{

scanf("%d",&a[i]);

temp[i]=a[i];

}

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

{

sum=0;

for(j=1;j<=tr;j++)

{

sum+=p[i][j];

Page 63: Oslab Manual r2013

}

if(sum==0)

{

m[k]=i;

k++;

}

}

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

{

for(l=1;l<k;l++)

if(i!=m[l])

{

flag=1;

for(j=1;j<=tr;j++)

if(c[i][j]<temp[j])

{

flag=0;

break;

}

}

if(flag==1)

{

m[k]=i;

k++;

for(j=1;j<=tr;j++)

temp[j]+=p[i][j];

}

}

printf("deadlock causing processes are:");

for(j=1;j<=tp;j++)

{

found=0;

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

{

if(j==m[i])

found=1;

}

if(found==0)

printf("%d\t",j);

}

getch();

}

Page 64: Oslab Manual r2013

OUTPUT:

Enter total no. of processes : 4

Enter total no. of resources : 5

Enter claim (Max. Need) matrix :

0 1 0 0 1

0 0 1 0 1

0 0 0 0 1

1 0 1 0 1

Enter allocation matrix :

1 0 1 1 0

1 1 0 0 0

0 0 0 1 0

0 0 0 0 0

Enter resource vector (Total resources) :

2 1 1 2 1

Enter availability vector (available resources) :

0 0 0 0 1

deadlock causing processes are : 2 3

Page 65: Oslab Manual r2013

************************************************************************

Exp No. 9 FIFO PAGE REPLACEMENT ALGORITHM

************************************************************************

AIM

To write a c program to implement FIFO page replacement algorithm

ALGORITHM

1. Start the process

2. Declare the size with respect to page length

3. Check the need of replacement from the page to memory

4. Check the need of replacement from old page to new page in memory

5. Form a queue to hold all pages

6. Insert the page require memory into the queue

7. Check for bad replacement and page fault

8. Get the number of processes to be inserted

9. Display the values

10. Stop the process

Page 66: Oslab Manual r2013

FIFO PAGE REPLACEMENT ALGORITHM

PROGRAM:

#include<stdio.h>

int main()

{

int i,j,n,a[50],frame[10],no,k,avail,count=0;

printf("\n ENTER THE NUMBER OF PAGES:\n");

scanf("%d",&n);

printf("\n ENTER THE PAGE NUMBER :\n");

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

scanf("%d",&a[i]);

printf("\n ENTER THE NUMBER OF FRAMES :");

scanf("%d",&no);

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

frame[i]= -1;

j=0;

printf("\tref string\t page frames\n");

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

{

printf("%d\t\t",a[i]);

avail=0;

for(k=0;k<no;k++)

if(frame[k]==a[i])

avail=1;

if (avail==0)

{

frame[j]=a[i];

j=(j+1)%no;

count++;

for(k=0;k<no;k++)

printf("%d\t",frame[k]);

}

printf("\n");

}

printf("Page Fault Is %d",count);

return 0;

}

Page 67: Oslab Manual r2013

OUTPUT:

ENTER THE NUMBER OF PAGES : 20

ENTER THE PAGE NUMBER : 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1

ENTER THE NUMBER OF FRAMES : 3

ref string page frames

7 7 -1 -1

0 7 0 -1

1 7 0 1

2 2 0 1

0

3 2 3 1

0 2 3 0

4 4 3 0

2 4 2 0

3 4 2 3

0 0 2 3

3

2

1 0 1 3

2 0 1 2

0

1

7 7 1 2

0 7 0 2

1 7 0 1

No. of Page Fault Is: 15

Page 68: Oslab Manual r2013

************************************************************************

Exp No. 10 INTER PROCESS COMMUNICATION – SHARED MEMORY

************************************************************************

AIM:

To write a c program for inter process communication.

ALGORITHM:

1. Start the program

2. Import the required header file.

3. Create the key and shared memory

a. Create the shared memory and process using key.

b. Get the message of share memory using shmget() system call.

4. Stop the program.

Page 69: Oslab Manual r2013

INTER PROCESS COMMUNICATION – SHARED MEMORY

PROGRAM:

#include<sys/types.h>

#include<sys/shm.h>

#include<sys/ipc.h>

main()

{

int shmid;

key_t key=0x10;

shmid=shmget(key,100,IPC_CREAT|0666);

if( shmid < 0 )

printf("\nFirst SHMID failed\n");

else

printf("\nFirst SHMID Succeded id=%d \n",shmid);

shmid=shmget(key,101,IPC_CREAT|0666);

if(shmid<0)

printf("\nSecond SHMID failed\n");

else

printf("\nSecond SHMID Succeded id=%d \n",shmid);

shmid=shmget(key,90,IPC_CREAT|0666);

if(shmid<0)

printf("\nThird SHMID failed\n");

else

printf("\n Third SHMID Succeded id=%d \n",shmid);

}

OUTPUT:

First SHMID Succeded id=589836

Second SHMID failed

Third SHMID Succeded id=589836

Page 70: Oslab Manual r2013

************************************************************************

Exp No. 11(a) BEST FIT MEMORY ALLOCATION METHOD

************************************************************************

AIM:

To write a c program to implement the best fit memory allocation scheme.

ALGORITHM:

1. Start the program.

2. Get the available no of holes along with its size.

3. Get the no of process along with its size.

4. Take the first process and scan the all holes and find the hole which is smallest enough to fit.

5. If it finds allocate the process otherwise print as no enough space.

6. Continue the processes until all process are allocated.

7. Stop the process.

Page 71: Oslab Manual r2013

BEST FIT MEMORY ALLOCATION METHOD

PROGRAM :

#include<stdio.h>

main()

{

int i,n,m,ps[100],bs[150],j,pi[50],bi[50],t;

printf("enter the no of blocks\n");

scanf("%d",&n);

printf("enter the block size\n");

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

{

printf("%d\n",i);

scanf("%d",&bs[i]);

bi[i]=i;

}

printf("enter no of process\n");

scanf("%d",&m);

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

{

printf("enter process %d size:\n",i);

scanf("%d",&ps[i]);

pi[i]=i;

}

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

{

for(j=1;j<=m;j++)

{

if(bs[i]<bs[j])

{

t=bs[i];

bs[i]=bs[j];

bs[j]=t;

t=bi[i];

bi[i]=bi[j];

bi[j]=t;

}

if(ps[i]<ps[j])

{

t=ps[i];

ps[i]=ps[j];

ps[j]=t;

t=pi[i];

pi[i]=pi[j];

pi[j]=t;

}

Page 72: Oslab Manual r2013

}

}

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

{

for(j=1;j<=m;j++)

{

if(ps[j]<=bs[i])

{

printf("process %d is allocated to block of size %d\n",pi[j],bs[i]);

ps[j]=10000;

break;

}

}

}

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

{

if(ps[i]!=10000)

{

printf("\nprocess %d is not allowed",i);

}

}

}

Page 73: Oslab Manual r2013

OUTPUT:

enter the no of blocks

4

enter the block size

1

200

2

250

3

300

4

400

enter no of process

3

enter process 1 size:

225

enter process 2 size:

275

enter process 3 size:

325

process 0 is allocated to block of size 200

process 1 is allocated to block of size 250

process 2 is allocated to block of size 300

Page 74: Oslab Manual r2013

************************************************************************

Exp No. 11(b) FIRST FIT MEMORY ALLOCATION METHOD

************************************************************************

AIM:

To write a c program to implement the first fit memory allocation scheme.

ALGORITHM:

1. Start the program.

2. Get the available no of holes along with its size.

3. Get the no of process along with its size.

4. Take the first process and find the hole which is enough to fit for that process.

5. Take the remaining process and allocate the as previous step.

6. If there is no enough space then print as no enough space.

7. Stop the program.

Page 75: Oslab Manual r2013

FIRST FIT MEMORY ALLOCATION METHOD

PROGRAM :

#include<stdio.h>

main()

{

int i,n,m,ps[100],bs[150],j;

printf("enter the no of blocks\n");

scanf("%d",&n);

printf("enter the block size\n");

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

{

printf("%d\n",i);

scanf("%d",&bs[i]);

}

printf("enter no of process\n");

scanf("%d",&m);

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

{

printf("enter process %d size:\n",i);

scanf("%d",&ps[i]);

}

//memory allocation

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

{

for(j=1;j<=m;j++)

{

if(ps[j]<=bs[i])

{

printf("process %d is allocated to block of size %d\n",j,bs[i]);

ps[j]=10000;

break;

}

}

}

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

{

if(ps[i]!=10000)

{

printf("\nprocess %d is not allowed",i);

}

}

}

Page 76: Oslab Manual r2013

OUTPUT:

enter the no of blocks

4

enter the block size

1

400

2

500

3

300

4

600

enter no of process

4

enter process 1 size:

200

enter process 2 size:

250

enter process 3 size:

350

enter process 4 size:

550

process 1 is allocated to block of size 400

process 2 is allocated to block of size 500

process 3 is allocated to block of size 600

process 4 is not allowed