Вы находитесь на странице: 1из 123

EX. NO. : 1.

DATE

BASIC COMMANDS IN Linux

AIM: To study the basic commands in Linux.

COMMANDS:

1. TASK
COMMAND
SYNTAX

: To display the system date and time.


: date.
: date.

EXPLANATION: This command displays the current system date and time on the
screen.
OUTPUT

2. TASK

: Tue Jun 19

11:37:17 GMT 2007.

: To display the current month.

COMMAND

: date.

SYNTAX

: date +%m.

EXPLANATION: This command displays the current month on the screen.


OUTPUT

3. TASK
COMMAND
SYNTAX

: 06.

: To display the name of the current month.


: date.
: date +%h.
1

EXPLANATION: This command displays the name of the current month on the
screen.
OUTPUT

4. TASK
COMMAND
SYNTAX

: Jun.

: To display the current system date.


: date.
: date +%d.

EXPLANATION: This command displays the current system date on the screen.
OUTPUT

5. TASK
COMMAND
SYNTAX

: 19.

: To display the current system date (year).


: date.
: date +%y.

EXPLANATION: This command displays the current year on the screen.


OUTPUT

6. TASK
COMMAND
SYNTAX

: 07.

: To display the current system time.


: date.
: date +%H.

EXPLANATION: This command displays the current system time (in hours) on the
screen.
OUTPUT

: 11.

2
Department of Information Technology, RGCE, Sriperumbudur

7. TASK

: To display the current system time.

COMMAND
SYNTAX

: date.
: date +%M.

EXPLANATION: This command displays the current system time (in minutes) on
the
screen.
OUTPUT

: 43.

8. TASK

: To display the current system time.

COMMAND
SYNTAX

: date.
: date +%S.

EXPLANATION: This command displays the current system time (in seconds) on
the
screen.
OUTPUT

: 15.

9. TASK

: To display the calendar of the current month.

COMMAND
SYNTAX

: calendar.
: cal.

EXPLANATION: This command displays the calendar of the current month on the
screen.
OUTPUT

:
S

Jun 07
T

S
3

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

10. TASK
COMMAND
SYNTAX

: To display user-defined message.


: echo.
: echo message.

EXPLANATION: This command displays on the screen the argument of the echo
command.
OUTPUT

: echo OS.
OS

11. TASK
COMMAND
SYNTAX

: To display the details of all users.


: who.
: who.

EXPLANATION : This command lists the information about all the users who
have
logged on to that system.
OUTPUT

12. TASK
COMMAND

: To display the user detail.


: who.

4
Department of Information Technology, RGCE, Sriperumbudur

SYNTAX

: whoami.

EXPLANATION : This command displays information about the current user of


the
system on the screen.
OUTPUT

13. TASK
COMMAND
SYNTAX

: root.

: To create a directory.
: make directory.
: mkdir.

EXPLANATION : This command is used to create a new directory with the


specified
name.
EXAMPLE
OUTPUT

14. TASK
COMMAND
SYNTAX

: mkdir student.
: The directory student is created.

: To change directory.
: change directory.
: cd directory name.

EXPLANATION : This command is used to switch from one directory to another.


EXAMPLE
OUTPUT

15. TASK
COMMAND

: cd staff.
: The directory staf is switched onto.

: To delete a directory.
: remove directory.
5

SYNTAX

: rmdir directory name

EXPLANATION : This command is used to delete the specified directory.


EXAMPLE
OUTPUT

: rmdir student.
: The student directory is deleted.

16. TASK
COMMAND
SYNTAX

: To come out of a sub-directory.


: change directory.
: cd ..

EXPLANATION : This command helps in switching to the main directory.


OUTPUT

17. TASK
COMMAND
SYNTAX

: To list all the files and directories.


: list.
: ls.

EXPLANATION : This command displays all the files and directories of the
system.
OUTPUT

18. TASK
COMMAND
SYNTAX

: To create a file.
: cat.
: cat> file name.

EXPLANATION : This command leads to the creation of a new file with the
specified
file name and contents.
EXAMPLE

: cat> wind.

6
Department of Information Technology, RGCE, Sriperumbudur

OUTPUT

19. TASK
COMMAND
SYNTAX

: A null file called wind is created.

: To view a file.
: cat.
: cat file name.

EXPLANATION : This command displays the contents of the specified file.


EXAMPLE

: cat wind.

OUTPUT
screen.

: Contents of the file called wind will be displayed on the

20. TASK

: To copy a file.

COMMAND
SYNTAX

: copy.
: cp sourcefile destinationfile.

EXPLANATION : This command produces a copy of the source file and is stored
in
the specified destination file by overwriting its previous
contents.
EXAMPLE
OUTPUT

21. TASK
COMMAND
SYNTAX

: cp sun moon.
: The contents of sun file will be copied to the moon file.

: To move a file.
: move.
: mv sourcefile destinationfile.

EXPLANATION : After moving the contents of the source file into destination
file,
the source file is deleted.
EXAMPLE
OUTPUT

: mv sun moon.
: After copying contents from the sun file to moon file,

the
sun file is deleted.

22. TASK
COMMAND
SYNTAX

: To display / cut a column from a file.


: cut.
: cut c no. file name.

EXPLANATION : This command displays the characters of a particular column in


the
specified file.
EXAMPLE
OUTPUT

: cut c3 moon.
: Those characters occurring in the 3rd column of the file called
moon are displayed.

23. TASK
COMMAND

: To delete a file.
: remove.

8
Department of Information Technology, RGCE, Sriperumbudur

SYNTAX

: rm file name.

EXPLANATION : This command deletes the specified file from the directory.
EXAMPLE
OUTPUT

24. TASK
COMMAND
SYNTAX

: rm sun.
: The file called sun will be deleted.

: To retrieve a part of a file.


: head.
: head

-no. of rows file name.

EXPLANATION : This command displays the specified no. of rows form the top
of the specified file.
EXAMPLE
OUTPUT

25. TASK
COMMAND
SYNTAX
EXPLANATION
bottom

: head -1 sun.
: The first row of the file called sun is displayed.

: To retrieve a file.
: tail.
: tail -no. of rows file name.
: This command displays the specified no. of rows form the
of the specified file.

EXAMPLE
OUTPUT

26. TASK

: tail -1 moon.
: The last row of the file called moon is displayed.

: To sort the contents of a file.


9

COMMAND
SYNTAX

: sort.
: sort

file name.

EXPLANATION : This command helps in sorting the contents of a file in


ascending
order.
EXAMPLE
OUTPUT

: sort win.
: The contents of the file win are displayed on the screen in

a
sorted order.
27. TASK
COMMAND
SYNTAX

: To display the no. of characters in a file.


: word count.
: wc

file name.

EXPLANATION : This command displays on the screen the no. of rows, words,
and
the sum of no. of characters and words.
EXAMPLE
OUTPUT

: wc ball.
: The no. of rows, words, and no. of characters present in the

file
ball are displayed.

28. TASK
COMMAND
SYNTAX

: To display the calendar of a year.


: cal.
: cal year.

EXPLANATION : This command displays on the screen the calendar of the


specified
year.
EXAMPLE

: cal 2007.

10
Department of Information Technology, RGCE, Sriperumbudur

OUTPUT

: The calendar of the year 2007 will be displayed.

EX. NO. :2
DATE

SHELL PROGRAMMING

11

A Linux shell is a command language interpreter, the primary purpose of which is to


translate the command lines typed at the terminal into system actions. The shell
itself is a program, through which other programs are invoked

What is a shell script ?

A shell script is a file containing a list of commands to be executed by the


Linux shell. shell script provides the ability to create your own customized
Linux commands
Linux shell have sophisticated programming capabilities which makes shell
script powerful Linux tools

How to work with shell ?

Step1:
In the dollar prompt type
$ vi < file name>
Where vi is the editor ,it will open a new window in which you can type the program
you want

Step2:
After typing the program press ESC and : together then at the bottom of the vi
screen you can see i.e. prompt .In that type as wq which means write and quit i.e.
the content what is typed will be written and saved into that file that has been
created

Step3:
Once wq is typed at the : prompt ,the prompt would change to $ symbol in which
you have to do the following
12
Department of Information Technology, RGCE, Sriperumbudur

$ sh < file name >

Sh

command is used to run the shell program

<file name> - is the name of the file for which the output is to be got

Basically to print a text in the your shell programs echo command is used

13

SYNTAX FOR LOOPING STATEMENTS

IF THEN-ELSE CONSTRUCT

if [ condition]

then

<action>

else
statements
fi (end of if)

WHILE
while <condition>
do
<statements>
Done

CASE

Case $<option> in
1) <statements>;;
2) <statements>;;
3) ..
14
Department of Information Technology, RGCE, Sriperumbudur

4)
.
.
.
*) <error statement>;;
esac

For loop

For(( intitialization;condition;incremetation/decrementation))

15

PROGRAM:
echo Enter the number:
read number
i= 1
fact= 1
while [$i le $number]
do
fact= `expr $fact \* $i `
i= `expr $i +1`
done
echo The factorial of $number is $fact.

SAMPLE OUTPUT:
Enter the number:
3
The factorial of 3 is 6.
16
Department of Information Technology, RGCE, Sriperumbudur

17

PROGRAM:

echo Enter two numbers:


read a
read b
c=`expr $a + $b `
echo The sum is $c.

SAMPLE OUTPUT:

Enter two numbers:


3
6
The sum is 9.

18
Department of Information Technology, RGCE, Sriperumbudur

PROGRAM:
echo Enter the number:
read n
x= $n
sum= 0
while [ $n gt 0]
do
y= `expr $n % 10`
z= `expr $y \* $y \* $y`
sum= `expr $sum + $z`
n= `expr $n / 10`
done
if [ $x eq $sum]
then
echo $x is an Armstrong number.
else
echo $x is not an Armstrong number.
fi

SAMPLE OUTPUT 1:
Enter a number:
153
153 is an Armstrong number.

SAMPLE OUTPUT 2:
19

Enter a number:
33
33 is not an Armstrong number.

20
Department of Information Technology, RGCE, Sriperumbudur

PROGRAM:
echo Enter the first number:
read a
echo Enter the second number:
read b
echo Enter the third number:
read c
if [$a gt $b a $a gt $c]
then
echo $a is greater.
elif [$b gt $c]
then
echo $b is greater.
else
echo $c is greater.
fi

SAMPLE OUTPUT:

Enter the first number:


3
21

Enter the second number:


6
Enter the third number:
9
9 is greater.

22
Department of Information Technology, RGCE, Sriperumbudur

PROGRAM:

echo Enter any two numbers:


read a
read b
echo The numbers are $a and $b.
c= $a
a= $b
b= $c
echo The swapped numbers are $a and $b.

SAMPLE OUTPUT:

Enter any two numbers:


3
9
The numbers are 3 and 9.
The swapped numbers are 9 and 3.

23

PROGRAM:
a= -1
b= 1
c= 0
echo Enter the limit:
read n
echo FIBONACCI SERIES
for (i =1; i <=n; i++)
do
c= `expr $a + $b`
echo $c
a=$b
b=$c
done

SAMPLE OUTPUT:
Enter the limit:
5
FIBONACCI SERIES
0
1
1
2
24
Department of Information Technology, RGCE, Sriperumbudur

25

PROGRAM:
echo enter the day of the week (1-7)
read n
case $n in
1)echo 1 is Sunday;;
2)echo 2 is Monday;;
3)echo 3.is Tuesday;;
4)echo 2 is Wednesday;;
5)echo 3.is thursday;;
6)echo 2 is Friday;;
7)echo 3.is Saturday;;
*)echo Invalid option;;

SAMPLE OUTPUT 1:
Enter the day of the week(1-7)
1
Sunday

SAMPLE OUTPUT 2:
26
Department of Information Technology, RGCE, Sriperumbudur

Enter the day of the week(1-7)


9
Invalid option

27

PROGRAM:
echo enter string 1
read s1
echo enter string2
read s2
if [ $s1 == $s2]
then
echo the given string are equal
else
echo The given strings are not equal
fi

SAMPLE OUTPUT 1:

enter string 1
ram
enter string 2
sam
The given strings are not equal
28
Department of Information Technology, RGCE, Sriperumbudur

SAMPLE OUTPUT 2
enter string 1
sam
enter string 2
sam
The given strings are qual

IMPLEMENTATION OF FIRST COME FIRST SERVE


SCHEDULING ALGORITHM

29

Aim

To write a program to implement the FCFS scheduling algorithm

Algorithm

1. Start the process

2. Declare the array size

3. Get the number of processes to be inserted

4. Get the value

5. Start with the first process from its initial position let other process to be in
queue

6. Calculate the total number of burst time

7. Display the values

8. Stop the process


30
Department of Information Technology, RGCE, Sriperumbudur

Result

Thus the program for FCFS is implemented and verified.

31

Program

#include<stdio.h>
main()
{
int n,a[10],b[10],t[10],w[10],g[10],i,m;
float att=0,awt=0;
for(i=0;i<10;i++)
{
a[i]=0; b[i]=0; w[i]=0; g[i]=0;
}
printf("enter the number of process");
scanf("%d",&n);
printf("enter the burst times");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
printf("\nenter the arrival times");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
g[0]=0;
for(i=0;i<10;i++)
32
Department of Information Technology, RGCE, Sriperumbudur

g[i+1]=g[i]+b[i];
for(i=0;i<n;i++)
{
w[i]=g[i]-a[i];
t[i]=g[i+1]-a[i];
awt=awt+w[i];
att=att+t[i];
}

awt =awt/n;
att=att/n;
printf("\n\tprocess\twaiting time\tturn arround time\n");
for(i=0;i<n;i++)
{
printf("\tp%d\t\t%d\t\t%d\n",i,w[i],t[i]);
}
printf("the average waiting time is %f\n",awt);
printf("the average turn around time is %f\n",att);
}

33

34
Department of Information Technology, RGCE, Sriperumbudur

Output

enter the number of process 4


enter the burst times
4 9 8 3
enter the arrival times
0 2 4 3
process

waiting time

p0

turn arround time

p1

11

p2

17

p3

18

21

the average waiting time is 7.250000


the average turn around time is 13.250000

35

IMPLEMENTATION OF ROUND ROBIN SCHEDULING


ALGORITHM

Aim

To write a program to implement the round robin scheduling algorithm

Algorithm

1. Start the process

2. Declare the array size

3. Get the number of elements to be inserted

4. Get the value

5. Set the time sharing system with preemption

6. Define quantum is defined from 10 to 100ms

7. Declare the queue as a circular

8. Make the CPU scheduler goes around the ready queue allocating CPU to each
process
36
Department of Information Technology, RGCE, Sriperumbudur

for the time interval specified

9. Make the CPU scheduler picks the first process and sets time to interrupt after
quantum
expired dispatches the process

9. If the process have burst less than the time quantum than the process release
the CPU

10. If the process have bust greater then time quantum then time will go off and
cause
interrupt to OS and the process put into the tail of ready queue and the
schedule select
next process

11. Display the results

12. Stop the process

Result

The program for round robin scheduling was implemented and hence verified

37

Program
#include<stdio.h>
int ttime,i,j,temp;
main()
{
int pname[10],btime[10],pname2[10],btime2[10];
int n,x,z;
printf("Enter the no. of process:");
scanf("%d",&n);
printf("Enter the process name and burst time for the process\n");
for(i=0;i<n;i++)
{
printf("Enter the process name:");
scanf("%d",&pname2[i]);
printf("Enter burst time for the process %d:",pname2[i]);
scanf("%d",&btime2[i]);
}
printf("PROCESS NAME \t\t BURST TIME\n");
for(i=0;i<n;i++)
printf("%d\t\t\t %d\n",pname2[i],btime2[i]);
z=1;
38
Department of Information Technology, RGCE, Sriperumbudur

while(z==1)
{
ttime=0;
for(i=0;i<n;i++)
{
pname[i]=pname2[i];
btime[i]=btime2[i];
}

printf ("PRESS 1.ROUND ROBIN 2.EXIT\n");


scanf("%d",&x);
switch(x)
{
case 1:
rrobin(pname,btime,n);
break;
case 2:
exit(0);
break;
default:
39

printf("Invalid option");
break;
}
printf("\n\n If you want to continue press 1:");
scanf("%d",&z);
}
}

rrobin(int pname[],int btime[],int n)


{
int tslice;
j=0;
printf("\n\t ROUND ROBIN SCHEDULING \n\n");
printf("Enter the time slice:\n");
scanf("%d",&tslice);
printf("PROCESS NAME \t REMAINING TIME\t TOTAL TIME");

while(j<n)
{
for(i=0;i<n;i++)
40
Department of Information Technology, RGCE, Sriperumbudur

{
if(btime[i]>0)
{
if(btime[i]>=tslice)
{
ttime+=tslice;
btime[i]=btime[i]-tslice;
printf("\n%d\t\t %d \t\t %d",pname[i],btime[i],ttime);
if(btime[i]==0)
j++;
}
else
{
ttime+=btime[i];
btime[i]=0;
printf("\n%d\t\t %d \t\t %d",pname[i],btime[i],ttime);
}
}
}
}
}

41

Output

Enter the no. of process: 4


Enter the process name and burst time for the process
Enteer the process name: 1
Enter burst time for the process 1: 8
Enteer the process name: 2
Enter burst time for the process 2: 3
42
Department of Information Technology, RGCE, Sriperumbudur

Enteer the process name: 3


Enter burst time for the process 3: 6
Enteer the process name: 4
Enter burst time for the process 4: 1

PROCESS NAME

BURST TIME

PRESS 1.ROUND ROBIN 2.EXIT


1
ROUND ROBIN SCHEDULING

Enter the time slice:


2
PROCESS NAME
1

REMAINING TIME TOTAL TIME


6

7
43

10

12

14

16

44
Department of Information Technology, RGCE, Sriperumbudur

IMPLEMENTATION OF SHORTEST JOB FIST SCHEDULING


ALGORITHM

Aim

To implement the shortest job first scheduling algorithm

Algorithm

1. Start the process

2. Declare the array size

3. Get the number of elements to be inserted

4. Select the process which have shortest burst will execute first

5. If two process have same burst length then FCFS scheduling algorithm used

6. Make the average waiting the length of next process


45

7. Start with the first process from its selection as above and let other process to
be in
queue

6. Calculate the total number of burst time

7. Display the values

8. Stop the process

Result

The program for SJF was implemented and hence verified

46
Department of Information Technology, RGCE, Sriperumbudur

Program

#include<stdio.h>
int main()
{

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

float att=0,awt=0;

for(i=0;i<10;i++)
{
b[i]=0;w[i]=0;
}

printf("enter the number of process");


scanf("%d",&n);
printf("enter the burst times");

for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
p[i]=i;
}
47

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];
48
Department of Information Technology, RGCE, Sriperumbudur

awt=awt+w[i];
att=att+t[i];
}
awt=awt/n;
att=att/n;
printf("\n\t process \t waiting time \t turn around time \n");
for(i=0;i<n;i++)
printf("\t p[%d] \t %d \t\t %d \n",p[i],w[i],t[i]);
printf("the average waitingtimeis %f\n",awt);
printf("the average turn around time is %f\n",att);
return 1;
}

49

Output

enter the number of process 5


enter the burst times
2 4 5 6 8

process

waiting time

turn around time

p[0]

p[1]

p[2]

11

p[3]

11

17

p[4]

17

25

the average waitingtime is 7.200000


the average turn around time is 12.200000

50
Department of Information Technology, RGCE, Sriperumbudur

IMPLEMENTATION OF BEST FIT ALGORITHM

Aim

To write a program to implement best fit algorithm for memory management.

Algorithm

1. Start the process

2. Declare the size

3. Get the number of processes to be inserted

4. Allocate the best hole that is small enough searching

5. Start at the best of the set of holes

51

6. If not start at the hole that is sharing the pervious best fit search end

7. Compare the hole

8. If small enough then stop searching in the procedure

9. Display the values

10. Stop the process

Result

The program for best fit was implemented and hence verified

52
Department of Information Technology, RGCE, Sriperumbudur

Program
#include<stdio.h>
#include<process.h>
void main()
{
int a[20],p[20],i,j,n,m,temp,b[20],temp1,temp2,c[20];
printf("Enter no of Blocks.\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %dst Block size:",i);
scanf("%d",&a[i]);
b[i]=i;
}
printf("Enter no of Process.\n");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the size of %dst Process:",i);
scanf("%d",&p[i]);
c[i]=i;
}
for(i=0;i<n;i++)
{
53

for(j=0;j<m;j++)
{
if(a[i]<a[j])
{
temp=a[i];
temp1=b[i];
a[i]=a[j];
b[i]=b[j];
a[j]=temp;
b[j]=temp1;
}
if(p[i]<p[j])
{
temp=p[i];
temp2=c[i];
p[i]=p[j];
c[i]=c[j];
p[j]=temp;
c[j]=temp2;
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
54
Department of Information Technology, RGCE, Sriperumbudur

{
if(p[j]<=a[i])
{
printf("The Process %d allocated to Block %d\n",c[j],b[i]);
p[j]=10000;
break;
}
}
}
for(j=0;j<m;j++)
{
if(p[j]!=10000)
{
printf("The Process %d is not allocated\n",j);
}
}
}

55

\Output

Enter no of Blocks.
3
Enter the 0st Block size:300
Enter the 1st Block size:200
Enter the 2st Block size:100
Enter no of Process.
3
56
Department of Information Technology, RGCE, Sriperumbudur

Enter the size of 0st Process:100


Enter the size of 1st Process:300
Enter the size of 2st Process:200

The Process 0 allocated to Block 2


The Process 2 allocated to Block 1
The Process 1 allocated to Block 0

57

IMPLEMENTATION OF FIRST FIT ALGORITHM

Aim

To write a program to implement best fit algorithm for memory management.

Algorithm

1. Start the process

2. Declare the size

3. Get the number of processes to be inserted

4. Allocate the first hole that is big enough searching

5. Start at the beginning of the set of holes

58
Department of Information Technology, RGCE, Sriperumbudur

6. If not start at the hole that is sharing the pervious first fit search end

7. Compare the hole

8. if large enough then stop searching in the procedure

9. Display the values

10. Stop the process

Result

The program for first fit was implemented and hence verified

Program

59

#include<stdio.h>
#include<process.h>
void main()
{
int a[20],p[20],i,j,n,m;
printf("Enter no of Blocks.\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %dst Block size:",i);
scanf("%d",&a[i]);
}
printf("Enter no of Process.\n");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the size of %dst Process:",i);
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(p[j]<=a[i])
{
60
Department of Information Technology, RGCE, Sriperumbudur

printf("The Process %d allocated to %d\n",j,a[i]);


p[j]=10000;
break;
}
}
}
for(j=0;j<m;j++)
{
if(p[j]!=10000)
{
printf("The Process %d is not allocated\n",j);
}
}
}

Output
Enter no of Blocks.
5
Enter the 0st Block size:500
Enter the 1st Block size:400
61

Enter the 2st Block size:300


Enter the 3st Block size:200
Enter the 4st Block size:100
Enter no of Process.
5
Enter the size of 0st Process:100
Enter the size of 1st Process:350
Enter the size of 2st Process:400
Enter the size of 3st Process:150
Enter the size of 4st Process:200
The Process 0 allocated to 500
The Process 1 allocated to 400
The Process 3 allocated to 200
The Process 2 is not allocated
The Process 4 is not allocated

62
Department of Information Technology, RGCE, Sriperumbudur

IMPLEMENTATION OF FIFO PAGE REPLACEMENT


ALGORITHM

Aim

To write a 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. Forma queue to hold all pages

6. Insert the page require memory into the queue

7. Check for bad replacement and page fault


63

8. Get the number of processes to be inserted

9. Display the values

10. Stop the process

Result

The program for FIFO page replacement was implemented and hence
verified.

64
Department of Information Technology, RGCE, Sriperumbudur

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)
65

{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}

Output

ENTER THE NUMBER OF PAGES: 20


66
Department of Information Technology, RGCE, Sriperumbudur

ENTER THE PAGE NUMBER :

70120304230321201701

ENTER THE NUMBER OF FRAMES :3


ref string

page frames

-1

-1

-1

3
2

0
1

Page Fault Is 15

67

68
Department of Information Technology, RGCE, Sriperumbudur

IMPLEMENTATION OF LRU PAGE REPLACEMENT ALGORITHM

Aim

To write a program a program to implement LRU page replacement algorithm

Algorithm

1. Start the process

2. Declare the size

3. Get the number of pages to be inserted

4. Get the value

5. Declare counter and stack

6. Select the least recently used page by counter value

7. Stack them according the selection.

8. Display the values

69

9. Stop the process

Result

The program for LRU page replacement was implanted and hence verified

70
Department of Information Technology, RGCE, Sriperumbudur

Program
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
71

}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
72
Department of Information Technology, RGCE, Sriperumbudur

for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}
73

Output

Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
7

74
Department of Information Technology, RGCE, Sriperumbudur

The no of page faults is 10

75

CREATION OF SHARED MEMORY

Aim

To write a program to implement a shared memory

Algorithm

1. Start the process

2. Declare the size and data variables

3. Get the number of processes to be inserted

4. Get the value

5. Start and identify the process with process id

6. Make a common function to communicate with each process running in


shared memory

7. Make the variables and arguments to be a global while in communication

8. Write a separate routine for creating memory delete memory in separate region
76
Department of Information Technology, RGCE, Sriperumbudur

9. Give permission that a process can kill shared memory

10. Display the values

11. Stop the process

Result

The program for creation of shared memory was implemented and hence
verified.

77

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");
78
Department of Information Technology, RGCE, Sriperumbudur

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

Output

First SHMID Succeded id=589836


Second SHMID failed
Third SHMID Succeded id=589836

79

80
Department of Information Technology, RGCE, Sriperumbudur

IMPLEMENTATION OF FILE LOCKING

Aim

To write a program to implement file locking concept using shell


programming

Algorithm

1. Start

2. Read the input as one of its filename

3. Check the permission

4. Using change mode command we can change the permission of file

5. Display the changes and verify it by opeing the file and modify the contents

6. Repeat the process changing the permission

7. Stop the process

81

Result

The program for the file locking was implemented and hence verified

82
Department of Information Technology, RGCE, Sriperumbudur

Program

ls -l
echo "enter the file name to be locked";
read f;
echo $f;
ls -l $f
echo "1.read only \n";
echo "2.read write \n";
read c;
if [ $c == 1 ]
then
chmod 444 $f
ls -l $f
elif [ $c == 2 ]
then
chmod 666 $f
ls -l $f
else
echo "wrong choice";
fi

83

Output
$ sh lock.sh
-rw-rw-rw-

-rw-rw-r--

-rw-rw-r--

120 Sep 7 23:58 sample.java


918 Sep 8 23:22 sjf.c
34 Sep 21 00:04 test.sh

enter the file name to be locked


84
Department of Information Technology, RGCE, Sriperumbudur

rrb.c
-rw-rw-r--

1355 Sep 9 00:07 rrb.c

1.read only \n
2.read write \n
1
-r--r--r--

1355 Sep 9 00:07 rrb.c

85

IMPLEMENTATION OF BANKERS ALGORITHM

Aim

To write a program to implement the Bankers algorithm

Algorithm

1. Start the process

2. Declare the processes

3. Get the number of processes to be inserted

4. Get the value

5. Let N be the number of processes in the system and M be the number of


resource types

6. Vector of length of M = Number of available resources in each type


86
Department of Information Technology, RGCE, Sriperumbudur

7. If available[j]=k then there are k instances of resorce of type Rj

8. Maximum demand of each process = M x N

9. If max[i,j]=k then process Pi may request at most k instances of resource type Rj

10. If allocation[i,j]=k, then process Pi is currently allocate k instances of resource


type Rj

11. If need[i,j]=k then process Pi may need k more instances of resource type
Rj to complete its task

12. Need[i,j]= Max[i,j]- Allocation[i,j]

13. Safety Algorithm

i) Let work and finish be vectos of lengh m and n, respectively.


87

a) Initialise work = available and Finish[i] = 1,2,..,n

ii) Find an I such that both

a) Finish[i] = false

b) Need i <= work

if no such I exists, go to step 4

iii)

work = work + allocation

finish[i] = true

goto step (ii)

iv)

if finish[i] = true for all I, then the system is in a safe state

14. Resource-Request algorithm

i)

available = available-request

allocation = allocation + request

88
Department of Information Technology, RGCE, Sriperumbudur

need = need-request

15. Display the values

16. Stop the process

Result

Thus the program for FCFS is implemented and verified.

Program

/* Bankers Algorithm */
#include<iostream.h>
#include<conio.h>
89

class bank
{
int **p,**max,**need,*avail,prno,rno;
int check(int);
public:
bank();
void input();
void process();
~bank();
};
bank::bank()
{
clrscr();
cout<<"\nEnter the number of Process:\t";
cin>>prno;
cout<<"\n Enter the number of Resources:\t";
cin>>rno;
avail=new int[rno];
p=new int*[prno];
max=new int*[prno];
need=new int*[prno];
for(int i=0;i<prno;i++)
{
p[i]=new int[rno];
max[i]=new int[rno];
90
Department of Information Technology, RGCE, Sriperumbudur

need[i]=new int[rno];
}
}
void bank::input()
{
int a,b;
cout<<"\n Enter maximum Need:\n";
for(a=0;a<prno;a++)
for(b=0;b<rno;b++)
cin>>max[a][b];
cout<<"\nEnter the Current state:\n";
for(a=0;a<prno;a++)
for(b=0;b<rno;b++)
{
cin>>p[a][b];
need[a][b]=max[a][b]-p[a][b];
}
cout<<"\n Enter Available Resources:\n";
for(b=0;b<rno;b++)
cin>>avail[b];
}
int bank::check(int c)
{
int d;
for(d=0;(avail[d]>=need[c][d])&&(d<rno)&&(need[c][d]!=-1);d++);
return d;
91

}
void bank::process()
{
int e=0,f=0;
do
{
int g=check(f);
if(g==rno)
{
for(int h=0;h<rno;h++)
{
avail[h]+=p[f][h];
need[f][h]=-1;
}
e++;
cout<<"\n Process["<<f+1<<"] is completed \n";
f=0;
}
else
{
f++;
if(f==prno)
{
cout<<"\n Deadlock\n";
break;
92
Department of Information Technology, RGCE, Sriperumbudur

}
}
}
while(e<prno);
}
bank::~bank()
{
delete *p,*max,*need;
delete p,max,need,avail;
}
void main()
{
clrscr();
bank msat;
char op;
do
{
msat.input();
msat.process();
cout<<"\n Do you want to continue(y/n):\t";
cin>>op;
getch();
}
while(op=='y');
cout<<"\n Press Any Key To Exit";
getch();
93

94
Department of Information Technology, RGCE, Sriperumbudur

PRODUCER CONSUMER PROBLEM

#include<stdio.h>
int mutex=1,full=0,empty=3,x=0;
main()
{
int n;
void producer();
void consumer();
int wait(int);
95

int signal(int);
printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");
while(1)
{
printf("\nENTER YOUR CHOICE\n");
scanf("%d",&n);
switch(n)
{
case 1:
if((mutex==1)&&(empty!=0))
producer();
else
printf("BUFFER IS FULL");
break;
case 2:
if((mutex==1)&&(full!=0))
consumer();
else
printf("BUFFER IS EMPTY");
break;
case 3:
exit(0);
break;
}
}
96
Department of Information Technology, RGCE, Sriperumbudur

}
int wait(int s)
{
return(--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nproducer produces the item%d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\n consumer consumes item%d",x);
x--;
mutex=signal(mutex);
97

98
Department of Information Technology, RGCE, Sriperumbudur

OUTPUT:1.PRODUCER
2.CONSUMER
3.EXIT

ENTER YOUR CHOICE


1

producer produces the item1


ENTER YOUR CHOICE
1

producer produces the item2


ENTER YOUR CHOICE
99

producer produces the item3


ENTER YOUR CHOICE
1
BUFFER IS FULL
ENTER YOUR CHOICE
2

consumer consumes item3


ENTER YOUR CHOICE
2

consumer consumes item2


ENTER YOUR CHOICE
2

consumer consumes item1


ENTER YOUR CHOICE
2
BUFFER IS EMPTY
100
Department of Information Technology, RGCE, Sriperumbudur

ENTER YOUR CHOICE


3

IMPLEMENTATION OF MEMORY ALLOCATION USING


PAGES
Program:

#include<stdio.h>
struct ptable
{
int pno, staddr;
}p[50];
struct pcount
{
int pnum;
char data[50];
}pg[50][50];
main()
{
int n,psize, i,j,k,st,pagenum,disp,phyaddr,laddr,found=0;
printf("\n MEMORYIFORMATION\n");
101

printf("***************\n");
printf("\n Enter the number of pages:\n");
scanf("%d",&n);
printf("Enter the page size:\n");
scanf("%d", &psize);
for(i=1;i<=n;i++)
{
printf("\n Enter the starting addresss for page[%d]:\n",i);
scanf("%d",&st);
for(j=1;j<=psize;j++)
{
printf("page[%d]\tData:",st);
scanf("%s", pg[i][j].data);
pg[i][j].pnum=st;
st=st+1;
}
}
printf("\n MEMORY \n");
printf("**********\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=psize;j++)
{
printf("\n page[%d]\t Data:%s\n", pg[i][j].pnum, pg[i][j].data);
}
102
Department of Information Technology, RGCE, Sriperumbudur

}
printf("\n page table details\n");
printf("***********************");
printf("\n Enter the starting address for the pages:",i);
for(k=1;k<=n;k++)
{
printf("page [%d] \t starting address:\n",k);
p[k].pno=k;
scanf("%d", &p[k].staddr);
}
printf("\n pageno\t starting address\n");
for(k=1;k<=n;k++)
{
printf("\n %d\t\t%d\n", p[k].pno, p[k].staddr);
}
printf("\nEnter the logical addressL:\n");
scanf("%d", &laddr);
pagenum=laddr/100;
disp=laddr%100;
printf("\n pageno%d\tdisp%d\n", pagenum, disp);
for(k=1;k<=n;k++)
if(p[k].pno==pagenum)
{
phyaddr=p[k].staddr+disp;
found=1;
break;
103

}
if(found==1)
{
printf("\n physical address%d\n", phyaddr);
for(j=1;j<=psize;j++)
{
if(pg[pagenum][j].pnum==phyaddr)
printf("\nData%s\n",pg[pagenum][j].data);
}
}
else
printf("\n not found\n");
}

104
Department of Information Technology, RGCE, Sriperumbudur

OUTPUT:-

MEMORYIFNORMATION
***************

Enter the number of pages:


2
Enter the page size:
2

Enter the starting addresss for page[1]:


100
page[100]

Data:blue

page[101]

Data:green

Enter the starting addresss for page[2]:


200
page[200]

Data:red

page[201]

Data:yellow
105

MEMORY
**********

page[100]

Data:blue

page[101]

Data:green

page[200]

Data:red

page[201]

Data:yellow

page table details


***********************
Enter the starting address for the pages:page [1]
address:

starting

100
page [2]

starting address:

200

106
Department of Information Technology, RGCE, Sriperumbudur

pageno starting address

100

200

Enter the logical addressL:


0101

pageno1

disp1

physical address101

Datagreen

Ex:no:2
Date:

107

108
Department of Information Technology, RGCE, Sriperumbudur

DEKKERS ALGORITHM
AIM:
To solve mutual exclusion problem by using DEKKERS algorithm.

Algorithm:

Step 1: start the program.

Step 2: The mutual exclusion requirement is assured.

Step 3: Every process checks the other flag after setting its own.If both are set, the
turn
variable is used to allow only one process to proceed.

Step 4: The trun variable is only considered when both processes are using,or
trying to
use, the resource.

Step 5: The process with the turn will(eventually)discover the other flag free and
will
proceed.

Step 6: Stop the program.

109

PROGRAM 2:

# include<iostream.h>
class dekker
{
public:
int c1,c2;
dekker()
{
c1=c2=0;
}
void dek()
{
do
{
cout<<\n The both process doesnt have the same value;
c1 = 1;
if(c1==1 && c2==0)
{
cout<<Setting value for process C2;
c1 = c2 =2;
}
if(c1 ==2 && c2 == 2)
{
cout<<\n the process C2 is entered;
110
Department of Information Technology, RGCE, Sriperumbudur

break;
}
}while(c1!=c2);
}
};
int main()
{
dekker d;
d.dek();
return 0;
}

111

OUTPUT:

The both process doesn't have the same value

Setting value for process C2

The process c1 is entered

Setting value for process c1

The process c2 is entered

112
Department of Information Technology, RGCE, Sriperumbudur

BANKERS ALGORITHM
AIM:
Program to implement Safe state of a system and to avoid deadlock situation
Using Bankers Algorithm in C language.

Algorithm:
113

Step 1: start the program.

Step 2: Get the number of process, resource, allocation matrix, Maximum matrix
and
Available matix.

Step 3: Calcualte need matrix using Maximum matrix and allocation matrix i.e.,
NEED[i,j]=MAX[i,j]-ALLOC[i,j]. Also initialize work matrix i.e.,
WORK[j]=AVAIL[j].

Step 4:Now find safety sequence of the system using work matrix,need matrix.

Step 5:Now compare NEED and WORK matrix, if NEED[i,j]<=WORK[j] then


Execute the corresponding process also calculate new work matrix i.e.,
WORK[j]=WORK[j]+ALLOC[i,j].

Step 6: If condition doesnt satisfies, move to next process and repeat Step 5,
until completion of all processes and print the safety sequence.

Step 7: If any process request for resource, get the process id and request matrix
from
The user.
Step 8: Check the following conditions for the respective process using Request
matrix,Need matrix and Available matrix i.e., REQ[j]<=NEED[k,j] and
REQ[j]<=AVAIL[j].
114
Department of Information Technology, RGCE, Sriperumbudur

Step 9: If the above condition satisfies, request can be granted and proceed next
step
otherwise display error as Request cannot be granted.

Step10: Now calculate new Available matrix,Allocation matrix,Need matrix and


Work matrix i.e.,AVAIL[j]=AVAIL[j]-REQ[j],
WORK[j]=AVAIL[j],ALLOC[k,j]=ALLOC[k,j]+REQ[j],
NEED[k,j]=NEED[k,j]-REQ[j].

Step 11: To find safety sequence of the system, repeat Step 5 and Step 6 for all
processes.

Step 12: Then print the safety sequence of execution of processes.

Step 13 :Stop the program .

115

PROGRAM 10:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
int alloc[20][20],max[20][20],avail[20],need[20][20],work[20]={0};
int newavail[20],req[20]={0},check=0,check2=0,cond,p;
int i=0,j=0,m=0,n=0,t=0,x=0,c[20]={0},k=0,count,count2,a[20],b;
int x2=0,c2[20];
printf("Enter the no. of resources\n");
scanf("%d",&m);
printf("Enter the no. of process");
scanf("%d",&n);
printf("\n Enter the resources for available\n");
for(j=1;j<=m;j++)
{
printf("Enter the %d resources of avail",j);
scanf("%d", &avail[j]);
work[j]=avail[j];
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
116
Department of Information Technology, RGCE, Sriperumbudur

printf("Enter the %d resources of %d alloc",j,i);


scanf("%d", &alloc[i][j]);
}
for(j=1;j<=m;j++)
{
printf("\n Enter the %d resource of %d max",j,i);
scanf("%d",&max[i][j]);
need[i][j]=max[i][j]-alloc[i][j];
}
}
printf("\n Allocation

max

need\n");

for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
printf("%2d",alloc[i][j]);
printf("\t");
for(j=1;j<=m;j++)
printf("%2d",max[i][j]);
printf("\t");
for(j=1;j<=m;j++)
printf("%2d",need[i][j]);
printf("\n");
}
printf("\n Process executes in this order\n");
do{
for(i=1;i<=n;i++)
117

{
count=0;
if(c[i]!=i+1)
{
for(j=1;j<=m;j++)
{
if(need[i][j]<=work[j])
count = count+1;
}
if(count == m)
{
printf("p%d\t",i);
c[i]=i+1;
x = x+1;
for(j=1;j<=n;j++)
work[j]=work[j]+alloc[i][j];
}
}
}
check = check +1;
}
while(x<n && check <=n);
if(x==n)
printf("\n system is in saftey\n");
else
118
Department of Information Technology, RGCE, Sriperumbudur

printf("\n System is not in saftey");


printf("\n Checking the bankers algorithm after the request");
printf("\n Enter the request process number");
scanf("%d",&p);
printf("\n Enter the values");
for(j=1;j<=m;j++)
{
scanf("%d",&req[j]);
}
for(j=1;j<=m;j++)
{
if(req[j]<=avail[j]&& req[j]<=need[p][j])
cond=cond+1;
}
if(cond == m)
{
for(j=1;j<=m;j++)
{
alloc[p][j]=alloc[p][j]+req[j];
avail[j]=avail[j]-req[j];
need[p][j]=need[p][j]-req[j];
}
}
else
{
printf("req is not satisfied");
119

exit(0);
}
printf("\n Execution of process after request");
do
{
for(i=1;i<=n;i++)
{
count2 = 0;
if(c2[i]!=i+1)
{
for(j=1;j<=m;j++)
{
if(need[i][j]<=avail[j])
count2 = count2+1;
}
if(count2 == m)
{
printf("p%d\t",i);
c2[i]=i+1;
x2=x2+1;
for(j=1;j<=n;j++)
{
avail[j]=avail[j]+alloc[i][j];
}
}
120
Department of Information Technology, RGCE, Sriperumbudur

}
}
check2 = check2 +1;
}while((x2<n)&&(check2<=n));

if(x2==n)
printf("\n System is in safe state we can grant the request");
else
printf("\n System is in unsafe state we cannot grant the request");

OUTPUT 10:
Enter the no. of resources
3
Enter the no. of process3

Enter the resources for available


121

Enter the 1 resources of avail1


Enter the 2 resources of avail2
Enter the 3 resources of avail3
Enter the 1 resources of 1 alloc3
Enter the 2 resources of 1 alloc2
Enter the 3 resources of 1 alloc1

Enter the 1 resource of 1 max1

Enter the 2 resource of 1 max2

Enter the 3 resource of 1 max3


Enter the 1 resources of 2 alloc2
Enter the 2 resources of 2 alloc2
Enter the 3 resources of 2 alloc2

Enter the 1 resource of 2 max3

Enter the 2 resource of 2 max3

Enter the 3 resource of 2 max3


Enter the 1 resources of 3 alloc2
Enter the 2 resources of 3 alloc2
Enter the 3 resources of 3 alloc2

122
Department of Information Technology, RGCE, Sriperumbudur

Enter the 1 resource of 3 max1

Enter the 2 resource of 3 max2

Enter the 3 resource of 3 max3


Allocation

max

need

3 2 1 1 2 3 -2 0 2
222 333 111
2 2 2 1 2 3 -1 0 1
Process executes in this order
p1

p2

p3

system is in saftey
Checking the bankers algorithm after the request
Enter the request process number2
Enter the values1
1
1
req is not satisfied

123

Вам также может понравиться