You are on page 1of 91

BASIC COMMANDS IN Linux

Aim:
To study the basic commands in Linux.
Commands:
1. TASK
COMMAND
SYNTAX
EXPLANATION
OUTPUT

: To display the system date and time.


: date.
: date.
: This command displays the current system date and time on the
Screen.
: Tue Jun 19 11:37:17 GMT 2007.

2. TASK
COMMAND
SYNTAX
EXPLANATION
OUTPUT

: To display the current month.


: date.
: date +%m.
: This command displays the current month on the screen.
: 06.

3. TASK
COMMAND
SYNTAX
EXPLANATION

: To display the name of the current month.


: date.
: date +%h.
: This command displays the name of the current month on the
screen.
: Jun.

OUTPUT
4. TASK
COMMAND
SYNTAX
EXPLANATION
OUTPUT

: To display the current system date.


: date.
: date +%d.
: This command displays the current system date on the screen.
: 19.

5. TASK
COMMAND
SYNTAX
EXPLANATION
OUTPUT

: To display the current system date (year).


: date.
: date +%y.
: This command displays the current year on the screen.
: 07.

6. TASK
COMMAND
SYNTAX
EXPLANATION
OUTPUT
7. TASK
COMMAND
SYNTAX
EXPLANATION
OUTPUT
8. TASK
COMMAND
SYNTAX
EXPLANATION
OUTPUT
9. TASK
COMMAND
SYNTAX
EXPLANATION
OUTPUT

: To display the current system time.


: date.
: date +%H.
: This command displays the current system time (in hours) on the
screen.
: 11.
: To display the current system time.
: date.
: date +%M.
: This command displays the current system time (in minutes) on
the screen.
: 43.
: To display the current system time.
: date.
: date +%S.
: This command displays the current system time (in seconds) on the
screen.
: 15.
: To display the calendar of the current month.
: calendar.
: cal.
: This command displays the calendar of the current month on the
screen.
:
Jun 07
S
M
T
W
T
F
S
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

10.TASK
COMMAND
SYNTAX
EXPLANATION
OUTPUT
11. TASK
COMMAND
SYNTAX
EXPLANATION
OUTPUT
12. TASK
COMMAND
SYNTAX
EXPLANATION
OUTPUT
13. TASK
COMMAND
SYNTAX
EXPLANATION
EXAMPLE
OUTPUT
14. TASK
COMMAND
SYNTAX
EXPLANATION
EXAMPLE
OUTPUT

: To display user-defined message.


: echo.
: echo message.
: This command displays on the screen the argument of the echo
command.
: echo OS.
OS
: To display the details of all users.
: who.
: who.
: This command lists the information about all the users who have
logged on to that system.
:
: To display the user detail.
: who.
: whoami.
: This command displays information about the current user of the
system on the screen.
: root.
: To create a directory.
: make directory.
: mkdir.
: This command is used to create a new directory with the specified
name.
: mkdir student.
: The directory student is created.
: To change directory.
: change directory.
: cd directory name.
: This command is used to switch from one directory to another.
: cd staff.
: The directory staff is switched onto.

15. TASK
COMMAND
SYNTAX
EXPLANATION
EXAMPLE
OUTPUT

: To delete a directory.
: remove directory.
: rmdir directory name
: This command is used to delete the specified directory.
: rmdir student.
: The student directory is deleted.

16. TASK
COMMAND
SYNTAX
EXPLANATION

: To come out of a sub-directory.


: change directory.
: cd ..
: This command helps in switching to the main directory.

17. TASK
COMMAND
SYNTAX
EXPLANATION

: To list all the files and directories.


: list.
: ls.
: This command displays all the files and directories of the system.

18. TASK
COMMAND
SYNTAX
EXPLANATION

: To create a file.
: cat.
: cat> file name.
: This command leads to the creation of a new file with the specifie
file name and contents.
: cat> wind.
: A null file called wind is created.

EXAMPLE
OUTPUT
19. TASK
COMMAND
SYNTAX
EXPLANATION
EXAMPLE
OUTPUT

: To view a file.
: cat.
: cat file name.
: This command displays the contents of the specified file.
: cat wind.
: Contents of the file called wind will be displayed on the screen.

20. TASK
COMMAND
SYNTAX
EXPLANATION

: To copy a file.
: copy.
: cp sourcefile destinationfile.
: This command produces a copy of the source file and is stored in
the specified destination file by overwriting its previous contents.
: cp sun moon.
: The contents of sun file will be copied to the moon file.

EXAMPLE
OUTPUT

21. TASK
COMMAND
SYNTAX
EXPLANATION
EXAMPLE
OUTPUT
22. TASK
COMMAND
SYNTAX
EXPLANATION
EXAMPLE
OUTPUT

: To move a file.
: move.
: mv sourcefile destinationfile.
: After moving the contents of the source file into destination
file the source file is deleted.
: mv sun moon.
: After copying contents from the sun file to moon file, the
sun file is deleted.
: To display / cut a column from a file.
: cut.
: cut c no. file name.
: This command displays the characters of a particular column in the
specified file.
: cut c3 moon.
: Those characters occurring in the 3rd column of the file called
moon are displayed.

23. TASK
COMMAND
SYNTAX
EXPLANATION
EXAMPLE
OUTPUT

: To delete a file.
: remove.
: rm file name.
: This command deletes the specified file from the directory.
: rm sun.
: The file called sun will be deleted.

24. TASK
COMMAND
SYNTAX
EXPLANATION
EXAMPLE
OUTPUT

: To retrieve a part of a file.


: head.
: head -no. of rows file name.
: This command displays the specified no. of rows form the top
: head -1 sun.
: The first row of the file called sun is displayed.

25. TASK
COMMAND
SYNTAX
EXPLANATION

: To retrieve a file.
: tail.
: tail -no. of rows file name.
: This command displays the specified no. of rows form the bottom
of the specified file.
: tail -1 moon.
: The last row of the file called moon is displayed.

EXAMPLE
OUTPUT

26. TASK
COMMAND
SYNTAX
EXPLANATION
EXAMPLE
OUTPUT
27. TASK
COMMAND
SYNTAX
EXPLANATIO
EXAMPLE
OUTPUT
28. TASK
COMMAND
SYNTAX
EXPLANATION
EXAMPLE
OUTPUT

: To sort the contents of a file.


: sort.
: sort file name.
: This command helps in sorting the contents of a file in
ascending order.
: sort win.
: The contents of the file win are displayed on the screen in a
sorted order.
: To display the no. of characters in a file.
: word count.
: wc file name.
: This command displays on the screen the no. of rows, words, and
the sum of no. of characters and words.
: wc ball.
: The no. of rows, words, and no. of characters present in the file
ball are displayed.
: To display the calendar of a year.
: cal.
: cal year.
: This command displays on the screen the calendar of the specified
year.
: cal 2007.
: The calendar of the year 2007 will be displayed.

SHELL PROGRAMMING
Aim:
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
$ 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

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)..
4)
.
.
.
*) <error statement>;;
esac
FOR LOOP
For(( intitialization;condition;incremetation/decrementation))

SHELL SCRIPT PROGRAMS

SUM AND AVERAGE OF N NUMBERS

Program:
echo Enter the value of n:
read n
i=1
sum=0
while [$i le $n]
do
sum=expr $sum + $i
i=expr $i + i
done
avg=expr $sum / $n
echo Sum=$sum
echp Average=$avg

Output:
Enter the value of n: 4
Sum=10
Average=2

FACTORIAL OF A NUMBER
Program:
echo Enter value to find the factorial

read n
i=1
fact=1
while [$i le $n]
do
fact=expr $fact /* $i
i=expr $i + 1
done
echo Factorial=$fact

Output:
Enter value to find the factorial 5
Factorial=120

FIBONNACI SERIES
Program:
echo Enter the n value
read n
i=2
a=0
b=1
echo $a
echo $b
while [$i le $n]
echo $c

a=$b
b=$c
i=expr $i + 1
done

Output:
Enter the n value :2
0
1
1

EVEN AND ODD NUMBERS


Program:
echo enter the value
read n
check=expr $n %2
if [$check = 0]
then
echo the given number is even
else
echo the given number is odd
fi

Output:
Enter the value: 45
The given number is odd

Enter the value: 24


The given number is even

STRING COMPARISON
Program:
echo Enter the Strings
read str1
read str2
if [ $str1 = $str2 ]
then
echo Strings are Equal
else
echo Strings are Not Equal
fi

Output:
Enter the strings:Read In
Read Out
Strings are not equal
Enter the strings: Salai
Salai
Strings are equal

THE LENGTH OF THE STRING


Program:
echo Enter the string
read str
len=echo $str | wc-c
len=expr $len -1
echo Length of the string is =$len

Output:
Enter the string: baba
Length of the string is=4

ARMSTRONG NUMBER

Program:
Echo enter the 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 $num ]
then
echo$x is an Armstrong number
else
echo$x is not an Armstrong number
fi

Output:
Enter the number : 979
979 is an Armstrong number

CALCULATE THE VALUE OF X^N


Program:

echo enter the value of x:


read x
echo enter the value of n:
read n
sum=1
i=1
While [$i -le $n ]
do
sum=`expr $sum \*$x`
i=expr $i+1
done
echo the value of $x power $n is $sum

Output:
Enter the value of x: 4
Enter the value of n: 2
The value 4 power 2 is 16

REVERSE STRING

Program:

echo Enter a String


read str
len=echo $str | wc c
while [ $len gt 0 ]
do
temp =echo $str | cut c $len
rev=echo $rev $temp
len=expr $len -1
done
echo Reversed String=$rev

Output:
Enter a string: Data
Reversed string=atad

REPLACING A CHARACTER IN THE STRING


Program:
Echo enter the string:
Read str
Echo enter the character to be changed:
Read c
Echo enter the character to be replaced:

Read r
Len=`echo $str | wc c`
Len=expr $len -1`
Count=0
I=1
While [ $i le $len ]
Do
Temp=`echo $str | cut c $i`
If [ $temp = $c ]
Then
New =`echo $new$r`
Else
New=`echo $new$temp`
Fi
I=`expr $i + 1`
Done
Echo the old string is $str
Echothe new string is $new

Output:
Enter the string :nut
Enter the character to be changed : n
Enter the character to be replaced : b
The old string is nut
The new string is but

C PROGRAM

CPU SCHEDULING

IMPLEMENTATION OF FIRST COME FIRST SERVE


SCHEDULING ALGORITHM

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

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int nop,i,bt[10],wt[10],tat[10],ttat,twt;
float awt,atat;
clrscr();
awt=0.0;
atat=0.0;
printf("\n\t FCFS sceduling\n");
printf("\n\t Enter the no.of process:");
scanf("%d",&nop);
printf("\n\t Enter the cpu burst time");
for(i=0;i<nop;i++)
scanf("%d",&bt[i]);

wt[0]=0;
tat[0]=bt[0];
twt=wt[0];
ttat=tat[0];
for(i=1;i<nop;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
twt=twt+wt[i];
ttat=ttat+tat[i];
}
awt=twt/nop;
atat=ttat/nop;
printf("\n\t process id \t burst time \t waiting time \t turn around time \n");
for(i=0;i<nop;i++)
{
printf("\n\t%d\t\t%d\t\t%d\t\t%d\n",i,bt[i],wt[i],tat[i]);
}
printf("\n the avg wt :%f",awt);
printf("\n theatat:%f",atat);
getch();
}

Output:
FCFS sceduling
Enter the no.of process:3
Enter the cpu burst time10
20
30

process id

burst time

waiting time

turn around time

10

10

20

10

30

30

30

60

the avg wt :13.000000


theatat:33.000000

Result:
Thus the program for FCFS is implemented and verified.

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
7. Start with the first process from its selection as above and let other process to be
in queue
8. Calculate the total number of burst time
9. Display the values
10. Stop the process

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

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

Result :
The program for SJF was implemented and hence verified

IMPLEMENTATION OF PRIORITY SCHEDULING


Aim:
Write a C program to implement Priority Scheduling.

Algorithm :
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU
burst time
Step 4: Sort the ready queue according to the priority number.
Step 5: Set the waiting of the first process as 0 and its burst time as its turn around

time
Step 6: For each process in the Ready Q calculate
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of
process(n-1)
(b) Turn around time for Process(n)= waiting time of Process(n)+ Burst time
for process(n)
Step 7: Calculate
(a) Average waiting time = Total waiting Time / Number of process
(b) Average Turnaround time = Total Turnaround Time / Number of process
Step 8: Stop the process

Program:
#include<stdio.h>
main()
{
int i,j,n,tp,t,p[20],pr[20],tot=0;
printf("Enter the no of process:");
scanf("%d",&n);
printf("\nEnter the %d Process:");
for(i=0;i<=n;i++)
scanf("%d",&p[i]);
printf("\nEnter %d Priority from %d:");
for(i=0;i<n;i++)
scanf("%d",&pr[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(pr[i]>pr[j])
{
tp=pr[i];

t=p[i];
pr[i]=pr[j];
p[i]=p[j];
pr[j]=tp;
p[j]=t;
}
c[0]=0;
for(i=0;i<n-1;i++)
c[i+1]=c[i]+p[i];
printf("\nSNO\t
PROCESS\tWAITING TIME\tPRIORITY\n");
for(i=0;i<n;i++)
{
printf(""\n\t%d\t%d\t%d",i+1,p[i],c[i],pr[i]);
tot=tot+c[i];
}
printf("\n Average Waiting time is:%f",((float)tot/n));
}

Output:
ENTER THE NO OF PROCESS : 4
ENTER THE 1 PROCESS :2
ENTER THE 2 PROCESS :1
ENTER THE 3 PROCESS :3
ENTER THE 4 PROCESS :1
ENTER 4 PRIORITY FROM:
6
7
8
7
SNO
1
2
3
4

PROCESS
3
1
1
2

WAITING TIME
0
3
4
5

AVERAGE WAITING TIME IS :3.000000

PRIORITY
8
7
7
6

Result:
The program is hence verified

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 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
10. If the process have burst less than the time quantum than the process release the
CPU
11. 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
12. Display the results
13. Stop the process

Program:
#include<stdio.h>
main()
{
int p[10][12];
int i,j,a,w=0,n,tq,count=0;
printf("how many process:\t");
scanf("%d",&n);
printf("\n\n process \t burst time\n");

for(i=1;i<=n;i++)
{
p[i][1]=i;
printf("\n \t p%d",p[i][1]);
printf("\t\t");
scanf("%d",&p[i][2]);
}
printf("\n");
printf("\n enter the time quantum:");
scanf("%d",&tq);
printf("\n");
printf("..........\t");
printf("\n\t process \t burst time \t waiting time \n");
printf(".......\t");
while(count<n)
{
for(i=1;i<=n;i++)
{
if(p[i][2]<tq)
a=p[i][2];
else
a=tq;
p[i][2]=-a;
printf("\n\t\t %d\t\t %d\t\t%d\n",p[i][1],a,w);
w=w+a;
count ++;
}
}
}

Output:
how many process:
process

3
burst time

p1

p2

p3

enter the time quantum:2


..........
process
.......
1

burst time

waiting time

Result:
The program for round robin scheduling was implemented and hence verified

MEMORY MANAGEMENT
SCHEME-I

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

Program:
#include<stdio.h>
int main()
{
int a,b[50],i,j;
int temp,s;
printf("\n========BEST FIT==========");
printf("\nenter the number of unallocated spaces:");
scanf("%d",&a);
printf("\n enter the unallocated space in KB:");
for(i=1;i<=a;i++)
for(i=j+1;j<=a;j++)
if(b[i]>b[j])
{
temp=b[j];
b[j]=b[i];
b[i]=temp;
}
printf("\n enter the space for required process:");
scanf("%d",&s);
for(i=1;i<=a;i++)
`
{
if(s<=b[i])
{
printf("\n the best fit for the required space is :%d\n",b[i]);
break;
}
}
}

Output:
===================BEST FIT======================
ENTER THE NO OF UNALLOCATED SPACES: 3
ENTER THE UNALLOCATED SPACE IN KB:
10
15
20
ENTER THE SPACE FOR REQUIRED PROCESS: 8
THE BEST FIT FOR THE REQUIRED SPACE IS: 10

Result:
The program for best fit was implemented and hence verified

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

Program:
#include<stdio.h>
main()
{
int i,j,n,ns,jb,c,ss[20];
printf("\n==========first fit==========");
printf("\nenter the number of slots:");
scanf("%d",&ns);
printf("\n enter %d slots:");
for(i=0;i<ns;i++)
scanf("%d",&jb);
do
{
printf("\n Enter the job size:");
scanf("%d",&jb);
for(i=0;i<ns;i++)
{
if(jb<=ss[i])
{
printf("\n %d is allocated to slot %d of size %d",jb,i+1,ss[i]);
ss[i]=ss[i]-jb;
printf("\n Remaining space of slot %d is= %d",i+1,ss[i]);
n=1;
break;
}
else
continue;
}
if(n!=1)
printf("\n %d is not allocated to any slot");
n=0;
printf("\n enter your choice:");
scanf("%d",&c);
}while(c==1);
}

Output:
==================FIRST FIT===================
ENTER THE NUMBER OF SLOTS: 4
ENTER 4 SLOTS: 12 23 34 45
ENTER THE JOB SIZE: 35
35 IS SLLOCATED TO SLOT 3 OF SIZE 34
REMAINING SPACE OF SLOT 3 IS=9
ENTER YOUR CHOICE: 1
ENTER JOB SIZE: 13
13 IS ALLOCATED TO SLOT 2 OF SIZE 23
REMAINING SPACE OF SLOT 2 IS =10
ENTER YOUR CHOICE: 2

Result:
The program for first fit was implemented and hence verified

IMPLEMENTATION OF WORST FIT ALGORITHM

Aim:

To write a program to implement worst 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
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

Program:
#include<stdio.h>
int main()
{
int a[50],n,i,j,p,t;
printf("\n=============WORST FIT============");

printf(:\nenter the number of unallocated memories:");


scanf("%d",&n);
printf("\n enter the memory space:");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
printf("\n enter the memory required for the process:");
scanf("%d",&p);
}
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
printf("\n the worst fit of the process:%d",a[i]);
}

Output:
====================WORST FIT=====================
ENTER THE NUMBER OF UNALLOCATED MEMORIES: 4
ENTER THE MEMORY SPACE:
12

23
34
45
ENTER THE MEMORY REQUIRED FOR THE PROCESS: 25
THE WORST FIT OF THE PROCESS: 45

Result:
Thus the program is verified.

PROCESS
SYNCHRONIZATION

READER AND WRITER PROBLEM


Program:
#include<stdio.h>
#include<cono.h>
Int main()
{
Typedef int semaphore;

Semaphore sread=0,swrite=0;
Int ch,r=0;
Clrscr();
Printf(readers writers problem);
Do
{
Printf(\n MENU);
Printf(\n \t 1.read from the file);
Printf(\n\t 2.write to the file);
Printf(\n\t 3.exit a reader);
Printf(\n\t 4.exit a writer);
Printf(\n\t 5. status of the file);
Printf(\n\t 6.exit);
Printf(\n enter your choice);
Scanf(%d,&ch);
Switch(ch)
{
Case 1:
If(swrite==0)
{
Sread=1;
R=r+1;
Printf(\n reader %d reads the file\n,r);
}
Else
Printf(\n cannot read while writer writes to the file\n);
Break;
Case 2:
If(sread==0&&swrite==0)
{
Swrite=1;
Printf(\n writer %d writes the file\n);
}
Else
If(sread==1)
Printf(\n cannot write while readers read the file\n);

Else if(swrite==1)
Printf(\n already one writer is writing to the filw\n);
Break;
Case 3:
If(r!=0)
{
Printf(\n the reader %d closes the file\n);
R=r-1;
If(r==0)
{
Printf(\n currently no reader are using the file\n);
Sread=0;
}
Else if(r==1)
Printf(\n currently only one reader is reading the file\n);
Else
Printf(\n currently %d readers are reading the file\n);
}
Break;
Case 4:
If(swrite==1)
{
Printf(\n writer closes the file);
Swrite=0;
}
Else
Printe(\n there is no writer writing to the file);
Break;
Case 5:
If(sread==1)
{
If(r==1)
Printf(\n readers are reading the file,r);
}
Else if(swrite==1)
Printf(\n writer is writing the file);

Else
Printf(\n the file is not in use);
Break;
Case 6:
Break;
}
}
While(ch!=6);
Return 0;
}

Output:
==========READER WRITER PROBLEM===========
MAIN MENU
R--------------->READER
W--------------->WRITER
N--------------->NONE
ENTER THE CHOICE.....R
R--------->READ DATABASE
X--------->EXIT DATABASE

ENTER THE CODE......R


==============READING PROCESS==============
READER 1 EXIT FROM THE DATABASE
MAIN MENU
R--------------->READER
W--------------->WRITER
N--------------->NONE
ENTER THE CHOICE.....R
R--------->READ DATABASE
X--------->EXIT DATABASE
ENTER THE CODE......R
===============READING PROCESS=============
READER 2 EXIT FROM THE DATABASE
MAIN MENU
R--------------->READER
W--------------->WRITER
N--------------->NONE
ENTER THE CHOICE.....R
R--------->READ DATABASE
X--------->EXIT DATABASE
ENTER THE CODE......X
READER 2 EXIT FROM THE DATABASE
MAIN MENU
R--------------->READER
W--------------->WRITER
N--------------->NONE
ENTER THE CHOICE.....R
R--------->READ DATABASE
X--------->EXIT DATABASE
ENTER THE CODE......X
READER 1 EXIT FROM THE DATABASE

MAIN MENU
R--------------->READER
W--------------->WRITER
N--------------->NONE
ENTER THE CHOICE.....R
R--------->READ DATABASE
X--------->EXIT DATABASE
ENTER THE CODE......X
THERE IS NO READER
MAIN MENU
R--------------->READER
W--------------->WRITER
N--------------->NONE
ENTER THE CHOICE.....W
W------->WRITE INTO DATABASE
X------->EXIT DATABASE
ENTER THE CODE.....W

=======UPDATING THE DATABASE========


WRITER WRITES INTO THE DATABASE
MAIN MENU
R--------------->READER
W--------------->WRITER
N--------------->NONE
ENTER THE CHOICE.....W
W------->WRITE INTO DATABASE
X------->EXIT DATABASE
ENTER THE CODE.....W
ONLY ONE WRITE ALLOWED AT A TIME

MAIN MENU
R--------------->READER
W--------------->WRITER
N--------------->NONE
ENTER THE CHOICE.....W
W------->WRITE INTO DATABASE
X------->EXIT DATABASE
ENTER THE CODE.....X
WRITER EXIT THE DATABASE
MAIN MENU
R--------------->READER
W--------------->WRITER
N--------------->NONE
ENTER THE CHOICE.....N

PRODUCER CONSUMER PROBLEM USING


SEMAPHORE
Aim:
To write a program to implement producer consumer problem.

Algorithm:
Step 1: Start.

Step 2: Let n be the size of the buffer


Step 3: check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer
Step 6: If the buffer is full the producer has to wait
Step 7: Check there is any cosumer.If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer
Step 9: If the buffer is empty, the consumer has to wait.
Step 10:Repeat checking for the producer and consumer till required
Step 11:Terminate the process.

Program:
#include<stdio.h>
main()
{
int in=0,out=0,c,b[20],bs,i,count=0;
printf("\n enter buffer size");
scanf("%d",&bs);
for(i=1;i<bs;i++)

b[1]=0;
do
{
printf("\n 1. producer\n 2.consumer\n 3.buffer\n4.exit");
scanf("%d",&c);
switch(c)
{
case 1:
if(count==bs)
printf("\n buffer is full\n");
else
{
printf("\n enter the item produced\t");
scanf("%d",&i);
b[in]=i;
in=(in+1)%bs;
count++;
}
break;

case 2:
if(count==0)
printf("\n buffer is empty\n");
else
{
i=b[out];
b[out]=0;
out=(out+1)%bs;
count--;
printf("\n consumed item is%d\n",i);

}
break;
case 3:
for(i=0;i<bs;i++)
printf("%d",b[i]);
printf("\n");
break;

}
}
while(c<=4);
}

Output:
enter buffer size4
1. producer
2.consumer
3.buffer
4.exit1
enter the item produced

1. producer
2.consumer
3.buffer
4.exit1
enter the item produced

1. producer
2.consumer
3.buffer
4.exit1
enter the item produced

1. producer
2.consumer
3.buffer
4.exit1
enter the item produced
1. producer
2.consumer
3.buffer
4.exit2
consumed item is8
1. producer
2.consumer
3.buffer
4.exit2
consumed item is7
1. producer
2.consumer

3.buffer
4.exit2
consumed item is9
1. producer
2.consumer
3.buffer
4.exit2
consumed item is6
1. producer
2.consumer
3.buffer
4.exit3
0000
1. producer
2.consumer
3.buffer
4.exit

Result:
Thus the program is verified.

MEMORY MANAGEMENT
SCHEME-II

PAGING
Aim :
To implement the C program using Paging Technique.

Algorithm :
Step 1 : Start the program.
Step 2 : Declare the structure namely p stable and p count.
Step 3 : Intialise the required variable.
Step 4 : Get the memory information such as no of pages and page size.
Step 5 : Get the starting address for each page and the datas for each page.
Step 6 : Print the page datas and page tables details namely page no and starting
address that was ready .
Step 7 : Else print the data not found.
Step 8 : Stop the program.

Program :
#include<stdio.h>
#include<conio.h>
struct ptable
{
int pno,staddr;
}p[50];
struct pcount
{
int pno;
char data[50];
}pg[24][24];
void main()
{
int n,size,i,j,k,st,pagenum,disp,phyaddr,laddr,found=0;
clrscr();
printf("\n\t\tMemory information\n");
printf("\t*********************************\n");
printf("Enter the no pages:\n");
printf("Enter the page size:\n");
scanf("%d",&size);
for(i=1;i<=n;i++)
{
printf("Enter the starting address for the page:[%d]\n",i);
scanf("%d",&st);
for(j=1;j<=size;j++)
{
printf("Page[%d]\tdata:",st);

scanf("%s",pg[i][j].data);
pg[i][j].pno=st;
st=st+1;
}
}
printf("\n\t\tMemory\n");
printf("\t*********************\n");

for(i=1;i<=n;i++)
{
for(j=1;j<=size;j++)
{
printf("\npage[%d]\tdata:%s\n",pg[i][j].pno,pg[i][j].data);
}
}
printf("\n\t Page details\n");
printf("\t***********************\n");
printf("\nEnter the starting address for the pages");
for(k=1;k<=n;k++)
{
printf("\npage[%d]\tstarting address:\n",k);
p[k].pno=k;
scanf("%d",&p[k].staddr);
}
printf("\npageno\tstarting 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 address:\n");


scanf("%d",&laddr);
pagenum=laddr/100;
disp=laddr%100;
printf("\nPageno:%d\ndisp:%d\n",pagenum,disp);
for(k=1;k<=n;k++)
if(p[k].pno==pagenum)
{
phyaddr=p[k].staddr+disp;
found=1;
break;
}
if(found==1)
{
printf("\nPhysical address:%d",phyaddr);

for(j=1;j<=size;j++)
{
if(pg[pagenum][j].pno==phyaddr)
printf("\nData:%s\n",pg[pagenum][j].data);
}
}
else
printf("\nNot found\n");
}

Output:
MEMORY INFORMATION
*********************************
Enter the no pages:
2
Enter the page size:
2
Enter the starting address for the page:[1]
100
Page[100]
data: SENTHIL
Page[101]
data: SURIYA
Enter the starting address for the page:[2]
200
Page[200]
data: TAMIZH
Page[201]
data: VENU
Memory
****************************
page[100]

data: SENTHIL

page[101]

data: SURIYA

page[200]

data: TAMIZH

page[201]

data: VENU

Page details
********** **********************************
Enter the starting address for the pages
page[1] starting address:
100
page[2] starting address:
200
pageno starting address
1

100

200

Enter the logical address:


0100
Pageno: 1
disp: 0
Physical address: 100
Data: SENTHIL

SEGMENTATION
Aim :
To write a C program for explaining the concept of Segmentation.

Algorithm :
Step 1 : Start the program.
Step 2 : Declare the structure table and its variables.
Step 3 : Read the no of arguments.
Step 4 : Get the starting address and limit of the segment.
Step 5 : Read the data for the corresponding segments.
Step 6 : Display the segment table details.
Step 7 : Stop the program.

Program :
#include<stdio.h>
#include<conio.h>
struct stable
{
int sno;
int staddr;
int limit;
}
s[50];
struct segcount
{
int snum;
char data[25];
}
seg[25][25];
void main()
{
int n,m,i,j,k=1,st,end,laddr,segnum,disp,phyaddr,found=0,limit;
clrscr();
printf("\n\t\tMEMORY INFORMATION");
printf("\n\t************************");
printf("\nEnter the no of segment :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter the starting address and the limit of the segment[%d]:\n",i);
scanf("%d%d",&st,&limit);
end=st+limit;

s[k].sno=i;
s[k].staddr=st;
s[k].limit=limit;
k++;

for(j=st;j<end;j++)
{
printf("\nSegment[%d]\tdata : ",st);
scanf("%s",seg[i][j].data);
seg[i][j].snum=st;
st=st+1;
}
}
printf("Memory address\tdata : \n");
for(i=1;i<=n;i++)
for(j=s[i].staddr;j<s[i].staddr+s[i].limit;j++)
printf("%d\t\t%s\n",seg[i][j].snum,seg[i][j].data);
printf("\n\t\tSegment table details \n");
printf("\n\t**************************");
printf("\nSegment\t\tstarting address\t\tlength\n");
for(m=1;m<=n;m++)
{
printf("%d\t\t%d\t\t%d\n",m,s[m].staddr,s[m].limit);
}
printf("\nEnter the logical address :");
scanf("%d",&laddr);
segnum=laddr/100;
disp=laddr%100;
printf("\nSegment no : %d\ndisp : %d\n",segnum,disp);
for(k=1;k<=n;k++)
{

if(s[k].sno==segnum)
{
phyaddr=s[k].staddr+disp;
found=1;
break;
}
if(found==1)
{
printf("\nPhysical address : %d\n",phyaddr);
for(j=1;j<=n;j++)
{
if(seg[segnum][j].snum==phyaddr)
printf("\nData : %s\n",seg[segnum][j].data);
}
}
else
{
printf("\nNot Found");
}
}
}

Output:
MEMORY INFORMATION
********************* ************************
Enter the no of segment :
2
Enter the starting address and the limit of the segment[1]:
300
3
Segment[300]

data : WIND

Segment[301]

data : AIR

Segment[302]

data : WATER

Enter the starting address and the limit of the segment[2]:


20
2
Segment[20]

data : ICE

Segment[21]

data : FIRE

Memory address data


300
WIND
301
AIR
302
WATER
20
ICE
21
FIRE

Segment table details


**************************************************
Segment

starting address

300

20

Enter the logical address : 0301


Segment no : 3
disp : 1
Not Found

length

SYSTEM CALL

PROCESS CREATION
Aim:
To write a c program for implementing the process creation

Algorithm:
Step1:Start the program
Step2:Include the header files
Step3:Call the fork function and equate this value of pid
Step4:Check whether the value of pid is 0
Step5:If the value is 0 means open the files and type message
Step6:Other wise read the file and print the message
Step7: Stop the program

Program:
#include<stdio.h>
void par_proc();
void chi_proc();
void slee_proc();
int main();
{
int n;
char c;
loop:
printf("=========CREATION OF PROCESS===========");
printf("\n1.parent process\n2. child process\n3.sleep process\n);
printf("ENTER YOUR CHOICE FROM 1 TO 3\n");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\n Parent process");
par_proc();
break;
case 2:
printf("\n Child Process");
chi_proc();
break;
case 3:
printf("\n Sleep process");
slee_proc();
break;
default:
printf("\n Try Again ENter correct choice");
}
printf("\n If U Want to continue(Y/N)...\n");
scanf(""%s",&c);
if(c==||'y'||c=='Y')

goto loop;
else
exit(1);
}
void par_proc()
{
int pid;
pid=getpid();
fflush(stdin);
printf("\n Process ID=%d\n",pid);
printf("\n");
}
void chi_proc()
{
int pid;
pid=fork();
if(pid==0)
{
fflush(stdin);
printf("\n Child ID=%d\n",getpid());
printf("\n Child's Parent ID=%d\n",getpid()); }
else
{
printf("\n Parent ID=%d\n",getpid());
printf("\n Parent's parent if=%d\n",getpid());
}}
void slee_proc()
{
int pid;
pid=getpid();
sleep(5);
printf("\n Process ID=%d\n",getpid());
printf("\n");
}

Output:
===============CREATION OF PROCESS================
1. PARENT PROCESS
2. CHILD PROCESS
3. SLEEP PROCESS
ENTER YOUR CHOICE FROM 1 TO 3
1
PARENT PROCESS
PROCESS ID=15918
IF YOU WANT TO CONTINUE(Y/N)........Y
===============CREATION OF PROCESS================
1. PARENT PROCESS
2. CHILD PROCESS
3. SLEEP PROCESS
ENTER YOUR CHOICE FROM 1 TO 3
2
CHILD PROCESS
CHILD ID=15919
CHILD'S PARENT ID=15919
IF YOU WANT TO CONTINUE(Y/N)....Y
===============CREATION OF PROCESS================
1. PARENT PROCESS
2. CHILD PROCESS
3. SLEEP PROCESS
ENTER YOUR CHOICE FROM 1 TO 3
3
SLEEP PROCESS
PROCESS ID=15919
IF YOU WANT TO CONTINUE(Y/N)....N

Result:
Thus the program is verified.

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
9. Give permission that a process can kill shared memory
10. Display the values
11. Stop the process

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

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

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

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

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

120 Sep 7 23:58 sample.java

-rw-rw-r--

918 Sep 8 23:22 sjf.c

-rw-rw-r--

34 Sep 21 00:04 test.sh

enter the file name to be locked


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

Result:
The program for the file locking was implemented and hence verified

You might also like