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

Operating Systems Lab Sylla

This document provides instructions for various tasks in Linux including shell programming, writing C programs using Unix system calls, networking simulations, and developing client-server applications. It also details commands to view system date and time, manipulate files and directories, edit text using vi editor, and more. The aim is to study basic commands in Linux and a shell program is demonstrated to concatenate two strings.

Uploaded by

harijoy
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Operating Systems Lab Sylla

This document provides instructions for various tasks in Linux including shell programming, writing C programs using Unix system calls, networking simulations, and developing client-server applications. It also details commands to view system date and time, manipulate files and directories, edit text using vi editor, and more. The aim is to study basic commands in Linux and a shell program is demonstrated to concatenate two strings.

Uploaded by

harijoy
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 55

12150L35P OPERATING SYSTEMS and NETWORKING LAB

(Implement the following on LINUX platform. Use C for high level language
implementation)
1. Shell programming
- comman s!nta"
- write simple functions
- #asic tests
$. Shell programming
- loops
- patterns
- e"pansions
- su#stitutions
%. &rite programs using the following s!stem calls of UNIX operating s!stem'
for() e"ec) getpi) e"it) wait) close) stat) openir) reair
*. &rite programs using the I+, s!stem calls of UNIX operating s!stem (open)
rea)
write) etc)
-. &rite C programs to simulate UNIX commans li(e ls) grep) etc.
.. Implement some memor! management schemes
NETWORKING:
1. Simulation of /01 + 0/01.
$. &rite a program that ta(es a #inar! file as input an performs #it stuffing
an C0C Computation.
%. Simulation of Sliing-&inow protocol.
*. 2evelop a Client 3 Server application for chat.
-. 2evelop a Client that contacts a given 2NS Server to resolve a given host
name.
.. &rite a Client to ownloa a file from a 4551 Server.
6".No'1
AIM:
5o Stu! the #asic commans in Linu".
COMMANDS:
1. 5as(' 5o ispla! s!stem ate an time.
S!nta"' 2ate
6"planation' 5his comman ispla!s the current ate an time on the screen.
$. 5as(' 5o ispla! current month.
S!nta"' 2ate 78m
6"planation' 5his comman ispla!s the current month on the screen.
%. 5as( ' 5o ispla! the name of the current month.
S!nta"' 2ate 78h
6"planation' 5his comman ispla!s name of the current month on the screen.
*. 5as(' 5o ispla! current s!stem ate.
S!nta"' 2ate 78
6"planation' 5his comman ispla!s the current s!stem ate on the screen.
-. 5as(' 5o ispla! current !ear.
S!nta"' 2ate 78!
6"planation' 5his comman ispla!s the current !ear on the screen.
.. 5as(' 5o ispla! current s!stem time.
S!nta"' 2ate 784
6"planation' 5his comman ispla!s the current s!stem time on the screen.
9. 5as(' 5o ispla! current s!stem time in minutes.
S!nta"' 2ate 78m
6"planation' 5his comman ispla!s the current s!stem time in minutes on the
screen.
:. 5as(' 5o ispla! current s!stem time in secons.
S!nta"' 2ate 78s
6"planation' 5his comman ispla!s the current s!stem time in secons on the
screen.
;.5as(' 5o ispla! the calenar of current month.
S!nta"' cal
6"planation' 5his comman ispla!s the current month calener on the screen.
1<. 5as(' 5o ispla! user efine message.
S!nta"' echo =message>
6"planation' 5his comman ispla!s the message.
11. 5as(' 5his comman ispla!s the list of files in the current wor(ing irector!.
S!nta"'
?ls 3l Lists the files in the long format
?ls 3t Lists in the orer of last moification time
?ls 3 Lists irector! instea of contents
?ls -u Lists in orer of last access time
1$. Calener Comman'
5his comman is use to ispla! the calenar of the !ear or the particular month of
calenar !ear.
S!nta" '
a.?cal @!earA
#.?cal @monthA @!earA
1%. 1resent &or(ing 2irector! Comman '
5o print the complete path of the current wor(ing irector!.
S!nta" ' ?pw
1*.BC2I0 Comman '
5o create or ma(e a new irector! in a current irector! .
S!nta" ' ?m(ir @irector! nameA
1-.C2 Comman '
5o change or move the irector! to the mentione irector! .
S!nta" ' ?c @irector! name.
1..0B2I0 Comman '
5o remove a irector! in the current irector! D not the current irector!
itself.
S!nta" ' ?rmir @irector! nameA
19.C06/56 / EIL6 '
5o create a new file in the current irector! we use C/5 comman.
S!nta" '
?cat A @filename.
5he A s!m#ol is reirector! we use cat comman.
1:.2IS1L/F / EIL6 '
5o ispla! the content of file mentione we use C/5 comman without GA
operator.
S!nta" '
?cat @filename.
,ptions 3s H to neglect the warning +error message.
1;.C,1FINI C,N56N5S '
5o cop! the content of one file with another. If file oesnot e"ist) a new file
is create an if the file e"ists with some ata then it is overwritten.
S!nta" '
? cat @filename sourceA AA @estination filenameA
? cat @source filenameA AA @estination filenameA it is avoi overwriting.
,ptions ' -
-n content of file with num#ers inclue with #lan( lines.
S!nta" '
?cat 3n @filenameA
$<.S,05INI / EIL6 '
5o sort the contents in alpha#etical orer in reverse orer.
S!nta" '
?sort @filename A
,ption ' ? sort 3r @filenameA
$1 .C,1FINI C,N56N5S E0,B ,N6 EIL6 5, /N,5460 '
5o cop! the contents from source to estination file . so that #oth contents
are same.
S!nta" '
?cp @source filenameA @estination filenameA
?cp @source filename path A @estination filename pathA
$$.B,J6 Comman '
5o completel! move the contents from source file to estination file an to
remove the source file.
S!nta" '
? mv @source filenameA @estination filenameA
$%.06B,J6 Comman '
5o permanentl! remove the file we use this comman .
S!nta" '
?rm @filenameA
,perating S!stem La# Banual CS $$-* Kwww.getitcse.t( 1age :
$*.&,02 Comman '
5o list the content count of no of lines ) wors) characters .
S!nta" '
?wc@filenameA
,ptions '
-c 3 to ispla! no of characters.
-l 3 to ispla! onl! the lines.
-w 3 to ispla! the no of wors.


$-. 1.ESC a Command :
5his comman is use to move the eit moe an start to appen after
the current character.
S!nta" ' @6SCA a
$.6SC / C,BB/N2 '
5his comman is also use to appen the file ) #ut this comman appen at
the en of current line.
S!nta" ' @6SCA /
,perating S!stem La# Banual CS $$-* Kwww.getitcse.t( 1age 11
%.6SC i Comman '
5his comman is use to insert the te"t #efore the current cursor position.
S!nta" ' @6SCA i
*.6SC I Comman '
5his comman is use to insert at the #eginning of the current line.
S!nta" ' @6SCA I
-.6SC o Comman '
5his comman is insert a #lan( line #elow the current line D allow insertion
of contents.
S!nta" ' @6SCA o
..6SC , Comman '
5his comman is use to insert a #lan( line a#ove D allow insertion of
contents.
S!nta" ' @6SCA ,
9.6SC r Comman '
5his comman is to replace the particular character with the given
characters.
S!nta" ' @6SCA r" &here " is the new character.
:.6SC 0 Comman '
5his comman is use to replace the particular te"t with a given te"t.
S!nta" ' @6SCA 0 te"t
;.@6SCA s Comman '
5his comman replaces a single character with a group of character .
S!nta" ' @6SCA s
1<.@6SCA S Comman '
5his comman is use to replace a current line with group of characters.
S!nta" ' @6SCA S
CURSOR MOEMENT IN !" :
1.@6SCA h '
5his comman is use to move to the previous character t!pe. It is use to
move to left of the te"t . It can also use to move character #! character (or) a
num#er of characters.
S!nta" ' @6SCA h 3 to move one character to left.
@6SCA nh 3 tomove Gn character to left.
$.@6SCA l '
5his comman is use to move to the right of the cursor (ie) to the ne"t
character. It can also #e use to move the cursor for a num#er of character.
S!nta" ' @6SCA l 3 single character to right.
@6SCA nl - Gn characters to right.
,perating S!stem La# Banual CS $$-* Kwww.getitcse.t( 1age 1$
%.@6SCA L '
5his comman is use to move own a single line or a num#er of lines.
S!nta" '
@6SCA L 3 single own movement.
@6SCA nL 3 Gn times own movement.
*.@6SCA ( '
5his comman is use to move up a single line or a num#er of lines.
S!nta" '
@6SCA ( 3 single line a#ove.
@6SCA n( 3 Gn lines a#ove.
-.6N560 (,0) N 6N560 '
5his comman will move the cursor to the starting of ne"t lines or a group
of lines mentione.
S!nta" '
@6SCA enter @6SCA n enter.
..@6SCA 7 Comman '
5his comman is use to move to the #eginning of the ne"t line.
S!nta" '
@6SCA 7 @6SCA n7
9.@6SCA - Comman '
5his comman is use to move to the #eginning of the previous line.
S!nta" '
@6SCA - @6SCA n-
:.@6SCA < '
5his comman will #ring the cursor to the #eginning of the same current
line.
S!nta" '
@6SCA <
;.@6SCA ? '
5his comman will #ring the cursor to the en of the current line.
S!nta" '
@6SCA ?
1<.@6SCA M '
5his comman is use to move to first character of first lines.
S!nta" '
@6SCA M
11.@6SCA # Comman '
5his comman is use to move #ac( to the previous wor (or) a num#er of
wors.
S!nta" '
@6SCA # @6SCAn#
,perating S!stem La# Banual CS $$-* Kwww.getitcse.t( 1age 1%
1$.@6SCA e Comman '
5his comman is use to move towars an replace the cursor at last
character of the wor (or) no of wors.
S!nta" '
@6SCA e @6SCAne

1%.@6SCA w Comman '
5his comman is use to move forwar #! a single wor or a group of
wors.
S!nta" '
@6SCA w @6SCA nw
DELETING T#E TE$T %ROM " :
1.@6SCA " Comman '
5o elete a character to right of current cursor positions ) this comman is
use.
S!nta" '
@6SCA " @6SCA n"
$.@6SCA X Comman '
5o elete a character to left of current cursor positions ) this comman is
use.
S!nta" '
@6SCA X @6SCA nX
%.@6SCA w Comman '
5his comman is to elete a single wor or num#er of wors to right of
current cursor position.
S!nta" '
@6SCA w @6SCA nw
*.# Comman '
5his comman is to elete a single wor to the left of the current cursor
position.
S!nta" '
@6SCA # @6SCA n#
-.@6SCA Comman '
5his comman is use to elete the current line (or) a num#er of line #elow
the current line.
S!nta" '
@6SCA @6SCA n
..@6SCA ? Comman '
5his comman is use to elete the te"t from current cursor position to last
character of current line.
S!nta" ' @6SCA ?
SAING AND &UITING %ROM !" :'
1.@6SCA w Comman '
5o save the given te"t present in the file.
S!nta" ' @6SCA ' w
,perating S!stem La# Banual CS $$-* Kwww.getitcse.t( 1age 1*
$.@6SCA NO Comman '
5o Nuit the given te"t without saving.
S!nta" ' @6SCA 'NO
%.@6SCA wN Comman '
5his comman Nuits the vi eitor after saving the te"t in the mentione file.
S!nta" ' @6SCA 'wN
*.@6SCA " Comman '
5his comman is same as GwN comman it saves an Nuit.
S!nta" ' @6SCA '"
-.@6SCA N Comman '
5his comman woul Nuit the winow #ut it woul as( for again to save the
file.
S!nta" ' @6SCA ' N
RESULT:
5hus the #asic comman in Linu" has #een executed successfully.
E() No :2a CONCATENATION O% TWO STRINGS
A"m:
5o write a shell program to concatenate two strings.
A*+o,"-.m:
Step1' 6nter into the vi eitor an go to the insert moe for entering the coe
Step$' 0ea the first string.
Step%' 0ea the secon string
Step*' Concatenate the two strings
Step-' 6nter into the escape moe for the e"ecution of the result an verif! the
output
P,o+,am:
echo Penter the first stringQ
rea str1
echo Penter the secon stringQ
rea str$
echo Pthe concatenate string isQ ?str1?str$
Sam/*0 I1P:
6nter first string' 4ello
6nter first string' &orl
Sam/*0 O1P:
5he concatenate string is 4ello&orl
R023*-:
5hus the shell program to concatenate two strings is e"ecute an output is
verifie successfull!.
E() No) : 24 COMPARISON O% TWO STRINGS
A"m:
5o write a shell program to compare the two strings.
A*+o,"-.m:
Step1' 6nter into the vi eitor an go to the insert moe for entering the coe
Step$' 0ea the first string.
Step%' 0ea the secon string
Step*' Compare the two strings using the if loop
Step-' If the conition satisfies then print that two strings are eNual else print two
strings are not eNual.
Step.' 6nter into the escape moe for the e"ecution of the result an verif! the
output
P,o+,am:
echo Penter the first stringQ
rea str1
echo Penter the secon stringQ
rea str$
if R ?str1 H ?str$ S
then
echo Pstrings are eNualQ
else
echo Pstrings are uneNualQ
fi
Sam/*0 I1P:1
6nter first string' hai
6nter secon string' hai
Sam/*0 O1P:1
5he two strings are eNual
Sam/*0 I1P:2
6nter first string' hai
6nter secon string' cse
Sam/*0 O1P:2
5he two strings are not eNual
R023*-:
5hus the shell program to compare the two strings is e"ecute an output is
verifie successfull!.
E()No:25 MA$IMUM O% T#REE NUMBERS
A"m:
5o write a shell program to fin greatest of three num#ers.
A*+o,"-.m:
Step1' 2eclare the three varia#les.
Step$' Chec( if / is greater than T an C.
Step%' If so print / is greater.
Step*' 6lse chec( if T is greater than C.
Step-' If so print T is greater.
Step.' 6lse print C is greater.
P,o+,am:
echo Uenter /U
rea a
echo Uenter TU
rea #
echo Uenter CU
rea c
if R ?a -gt ?# -a ?a -gt ?c S
then
echo U/ is greaterU
elif R ?# -gt ?a -a ?# -gt ?c S
then
echo UT is greaterU
else
echo UC is greaterU
fi
Sample I+1'
6nter /'$%
6nter T'*-
6nter C'.9
Sample ,+1'
C is greater
R023*-:
5hus the shell program to fin the ma"imum of three num#ers is e"ecute
an output is verifie successfull!.
E()No:2d %IBONACCI SERIES
A"m:
5o write a shell program to generate fi#onacci series.
A*+o,"-.m :
Step 1 ' Initialise a to < an # to 1.
Step $ ' 1rint the values of VaV an V#V.
Step % ' / the values of VaV an V#V. Store the ae value in varia#le VcV.
Step * ' 1rint the value of VcV.
Step - ' Initialise VaV to V#V an V#V to VcV.
Step . ' 0epeat the steps %)*)- till the value of VaV is less than 1<.
P,o+,am :
echo enter the num#er
rea n
aH-1
#H1
iH<
while R ?i 3le ?n S
o
tHWe"pr ?a 7 ?#W
echo ?t
aH?#
#H?t
iHWe"pr ?i 7 1W
one
Sample I+1 '
6nter the no' -
Sample ,+1'
<
1
1
$
%
-
0esult'
5hus the shell program to fin the Ei#onacci series is e"ecute an output is
verifie successfull!.
E()No:20 ARIT#METIC OPERATIONS USING CASE
A"m:
5o write a shell program to perform the arithmetic operations using case
A*+o,"-.m :
Step 1 ' 0ea the input varia#les an assign the value
Step $ ' 1rint the various arithmetic operations which we are going to perform
Step % ' Using the case operator assign the various functions for the arithmetic
operators.
Step * ' Chec( the values for all the corresponing operations.
Step - ' 1rint the result an stop the e"ecution.
.
P,o+,am :
echo 1./ition
echo $.Su#raction
echo %.Bultiplication
echo *.2ivision
echo enter !our choice
rea a
echo enter the value of #
rea #
echo enter the value of c
rea c
echo # is ?# c is ?c
case ?a in
1)HWe"pr ?# 7 ?cW
echo the sum is ?
XX
$)HWe"pr ?# - ?cW
echo the ifference is ?
XX
%)HWe"pr ?# YZ ?cW
echo the prouct is ?
XX
*)HWe"pr ?# + ?cW
echo the Nuotient is ?
XX
esac
Sample I+1 '
1./ition
$.Su#raction
%.Bultiplication
*. 2ivision
6nter !our choice'1
6nter the value of #'%
6nter the value of c'*
# is % c is *
the sum is 9
Sample ,+1'
# is % c is *
the sum is 9
0esult'
5hus the shell program to perform arithmetic operations using case is
e"ecute an output is verifie successfull!.
Ex: No:3a IMPLEMENTATION OF FORK, EXEC, GETPID, EXIT
AND WAIT
SYSTEM CALL
AIM:
To write a C program to implement fork, exec, getpid, exit and wait system calls.
ALGORITHM:
1. Using fork system call, create a new process.
2. Assign the processs id in the variable pid.
3. If pid is equal to 1, then report an error and ter minate using the exit system
call.
4. If pid is equal to 0, it is a child process. Print the processs id using
the getpid system call.
5. Otherwise, it is the parent process. Print the ids of both child and parent process.
6. Using execlp system call, the present working directory is displayed.
7. Status of the processes is displayed using wait system call.
8. Terminate the program using the exit system call.
PROGRAM:
[it08b034@linuxlab ~]$ vi gfsystemcall.c
#include<unistd.h>
#include<stdio.h>
main()
{
int pid,status; pid=fork();
if(pid==-1)
{
perror("Error fork()");
}
else if(pid==0)
{
printf("I am the child process\n");
printf("Process id of the child is=%d\n", getpid());
execlp("pwd","pwd",NULL);
}
else
{
printf("I am the parent process\n");
printf("Process id of parent=%d\n Process id of child =%d\n",getpid(),pid);
wait(&status);
printf("Child Returned %d\n",status);
}
exit(0);
}
OUTPUT:
"gfsystemcall.c" 28L, 438C written [it08b034@linuxlab ~]$ cc gfsystemcall.c
[it08b034@linuxlab ~]$ ./a.out
I am the child process
Process id of the child is=29980 I am the parent process
Process id of parent=29979 Process id of child =29980 /home/it08b034
Child Returned 0
RESULT:
Thus the C program for implementing fork, exec, getpid, exit and wait
system calls was executed successfully.
Ex: No:3b IMPLEMENTATION OF OPENDIR, READDIR AND
CLOSEDIR
SYSTEM CALLS
AIM:
To write a C program using opendir, readdir and closedir system calls.
ALGORITHM:
1.Open the current directory using opendir system call.
2.If the directory is not opened report an error message.
3.Otherwise display the contents of the directory using readdir system call.
4.Close the opened directory using closedir system call.
PROGRAM:
[it08b034@linuxlab ~]$ vi gfsystemcall.c
#include<dirent.h>
#include<stdio.h>
main()
{
DIR *dir;
struct dirent *entry; if((dir=opendir("/"))==NULL)
perror("Error during Opendir() error");
else
{
puts("contents of the current directory"); while((entry=readdir(dir))!=NULL)
printf("%s\n",entry->d_name);closedir(dir);
}
}
OUTPUT:
"gfsystemcall.c" [New] 17L, 281C written [it08b034@linuxlab ~]$ cc
gfsystemcall.c [it08b034@linuxlab ~]$ ./a.out
contents of the current directory root
.bash_history
sys opt misc
..
initrd home dev mnt etc media tmp proc
.
lib sbin
.autofsck srv
bin tftpboot var
usr boot
lost+found selinux
RESULT:
Thus the C program for implementing opendir, readdir and closedir system
calls was executed successfully.
Ex: No:4 IMPLEMENTING I/O SYSTEM CALL
AIM:
To write a C program for copying a file into another using I/O system call.
ALGORITHM:
1.Get the name of the source and destination file from the command line.
2.Open the source file in read-only mode using the open system call. If the file
could not be open, print an error message.
3.Create the destination file using creat system call. If the file could not be open,
print an error message.
4. Read from the source file using the read system call and store the content in the
buffer.
5. From the buffer write to the destination file using the write system call.
PROGRAM:
[it08b034@linuxlab ~]$ vi iosys.c
#include<stdio.h>
int main(int argc,char *argv[])
{
int fd1,fd2,n; char buf[512]; if(argc!=3)
{
printf("Error in the number of argument"); exit(1);
}
if((fd1=open(argv[1],0))<0)
{
printf("Can't open %s",argv[1]); exit(1);
}
if((fd2=creat(argv[2],0))<0)
{
printf("Can't create %s",argv[2]); exit(1);
}
n=read(fd1,buf,512);
write(fd2,buf,n);
close(fd1);
close(fd2);
}
INPUT FILE:
[it08b034@linuxlab ~]$ cat fork.c
#include<unistd.h> #include<stdio.h>
main()
{
int pid,status; pid=fork();if(pid==-1)
{
perror("Error fork()"); exit(1);
}
else if(pid==0)
{
printf("I am the child process\n"); printf("My process id is: %d\n",getpid());
printf("My parent process id is:%d\n",getppid()); execlp("pwd","pwd",NULL);
}
else
{
printf("I am the parent process\n"); printf("My process id is: %d\n",getpid());
printf("My childs process id is: %d\n",pid()); wait(&status);
printf("Child returned %d\n",status);
}
}
OUTPUT:
"iosys.c" 27L, 368C written [it08b034@linuxlab ~]$ cc iosys.c
[it08b034@linuxlab ~]$ ./a.out fork.c dir.c [it08b034@linuxlab ~]$ cat dir.c
#include<unistd.h>
#include<stdio.h>
main()
{
int pid,status; pid=fork();if(pid==-1)
{
perror("Error fork()"); exit(1);
}
else if(pid==0)
{
printf("I am the child process\n"); printf("My process id is: %d\n",getpid());
printf("My parent process id is:%d\n",getppid()); execlp("pwd","pwd",NULL);
}
else
{
printf("I am the parent process\n"); printf("My process id is: %d\n",getpid());
printf("My childs process id is: %d\n",pid()); wait(&status);
printf("Child returned %d\n",status);
}
RESULT:
Thus the C program for copying a file into another file using I/O system call
was executed successfully.
Ex: No: 5a SIMULATING GREP COMMAND
AIM:
To write a C program to simulate the grep command.
ALGORITHM:
6.Pass the search string and the file name as argument to main function through
command line.
7.Print an error message and exit if there is not enough number of arguments.
8.Open the file in the read-only mode using fopen function. If the file could not be
open, print the error message.
9.Repeat the following steps until the end of the file is reached.
a.Read each line from the file into the buffer.
b.Search for the option string in the line if it is print the line.
PROGRAM:
[it08b034@linuxlab ~]$ vi grep.c
#include<stdio.h>
#include<string.h> main(int argc,char *argv[])
{
FILE *fp; char line[100]; if(argc!=3)
{
printf("error");
exit(1);
}
if((fp=fopen(argv[2], "r"))==NULL)
{
printf("command open file\n"); exit(1);
}
while(fgets(line,100,fp)!=NULL)
{
if(strstr(line,argv[1])!=NULL)
printf("%s",line);
}
}
OUTPUT:
"grep.c" 24L, 304C written [it08b034@linuxlab ~]$ cc grep.c
error[it08b034@linuxlab ~]$ ./a.out printf grep.c printf("error");
printf("command open file\n"); printf("%s",line);
RESULT:
Thus the C program to simulate grep command was executed successfully.
EX. NO. 5b SIMULATING LS COMMAND
AIM:
To write a C program to simulate the ls command.
ALGORITHM:
1.Declare a pointer variable.
2.If no files found, display it as a . File.
3.Scan directory and find out the file in an alphabetical order.
4.If no files (n<0), it is an error.
5.Otherwise display the name list, using a pointer.
6.Free the name list from the memory.
7.Display the total number of files.
PROGRAM:
[it08b034@linuxlab ~]$ vi ls.c
#include<dirent.h>
#include<time.h>
int main(int argc,char *argv[])
{
struct dirent **namelist; int i=0,n;
if(argc<=1)
{
n=scandir(".",&namelist,0,alphasort);
}
else
{
n=scandir(argv[1],&namelist,0,alphasort);
}
if(n<0)
{
printf("No file or directory");
}
else
{
while(i<n)
{
printf("%s\n",namelist[i]->d_name);i++;
}
free(namelist); printf("Total File:%d\n",n);
}
}
OUTPUT:
"ls.c" [New] 30L, 405C written [it08b034@linuxlab ~]$ cc ls.c
[it08b034@linuxlab ~]$ ./a.out
.
..
.FCFSANDSJF.C.swp
.aspell.en.prepl
.aspell.en.pws
.bash_history
.bash_logou
RESULT:
Thus the C program to simulate ls command was executed successfully.
E$)NO) 6 MEMORY MANAGEMENT SC#EME' PAGING
AIM:
5o write a C program to implement memor! management using paging
techniNue.
ALGORIT#M:
Step1 ' Start the program.
Step$ ' 0ea the #ase aress) page si[e) num#er of pages an memor! unit.
Step% ' If the memor! limit is less than the #ase aress ispla! the memor! limit
is less than
limit.
Step* ' Create the page ta#le with the num#er of pages an page aress.
Step- ' 0ea the page num#er an isplacement value.
Step. ' If the page num#er an isplacement value is vali) a the isplacement
value with the
aress corresponing to the page num#er an ispla! the result.
Step9 ' 2ispla! the page is not foun or isplacement shoul #e less than page
si[e.
Step: ' Stop the program.
PROGRAM:
\[email protected]
\[email protected]
voi main()
]
int #R$<S)n)i)pa)p)a)X
printf(PYn1rogram for pagingQ)X
scanf(P8Q)Dn)X
printf(PYn6nter the #ase aress'Q)X
for(iH<Xi@nXi77)
]
scanf(P8Q)D#RiS)X
^
printf(PYn6nter the logical aress'Q)X
scanf(P8Q)Dp)X
for(iH<Xi@nXi77)
]
if(iHHp)
]
paH#RiS7X
aH#RiSX
printf(PYnYt1ageNo.Yt Tase/. 1h!sical/. YnYt 8 Yt 8 Yt 8 Yt Q)p)a)pa)X
^
^
printf(PYnInvali pageQ)X ^
Sam/*0 In/3- 1:
1rogram for paging
6nter the num#er of pages'$
6nter the #ase aress'
1<<
1-<
6nter the Logical aress'-<
6nter the page num#er'1
Sam/*0 O3-/3- 1:
1ageNo. Tase/. 1h!sical/.
1 1-< $<<
Sam/*0 In/3- 2:
1rogram for paging
6nter the num#er of pages'1
6nter the #ase aress'
1<<
6nter the Logical aress'$
6nter the page num#er'$
Sam/*0 O3-/3- 2:
Invali page.
06SUL5'
5hus the program for implementing the paging concept was e"ecute
an the output was verifie successfull!.
NETWORKING:
E() NO: 01 S"m3*a-"on o7 ARP1RARP
A"m :
5o &rite a program to simulate /ress 0esource 1rotocol or 0everse
/ress 0esource 1rotocol (/01+0/01).
ALGORIT#M :
S0,!0,:
1. Start
$. Create a share memor! segment an get its i using shmget() X
%. Create a share memor! painter an attach it to the share memor! using
shmat().
*. / ta#le containing machine aress an its eNuivalent I1 aress is create an it
is share in the share memor! segment.
-. 2ispla! contents of share memor! using its pointer.
.. 2etach pointer from the share memor! using shmt().
9. stop.
C*"0n-
1. Start.
$. Create the share memor! pointer which refers to the same memor! segment
that server has create using shmget() .
%. Create options to fin /01) 0/01 an e"it.
*. If /01 is reNueste) then compare the given I1 aress in the share memor!
segment an print the machine aress.
-. If 0/01 is reNueste) then compare the given machine aress in the /01 ta#le
an ispla! the eNuivalent I1 aress.
.. 0epeat step * an - epening upon the userVs choice.
9. 6"it.
A,/20,!0,)5
\[email protected]
\inclue@s!s+t!pes.hA
\inclue@s!s+shm.hA
\[email protected]
main()
]
int shmi) a) iX
char Zptr) ZshmptrX
shmiHshmget(%<<<)1<)I1C_C06/5 ` <...)X
shmptrHshmat(shmi)NULL)<)X
ptrHshmptrX
for(iH<Xi@%Xi77)
]
puts(Uenter the macU)X
scanf(U8sU)ptr)X
aHstrlen(ptr)X
printf(Ustring length'8U)a)X
ptrRaSH V V X
puts(Uenter ipU)X
ptrHptr7a71X
scanf(U8sU)ptr)X
ptrRaSHVYnV X
ptrH ptr7a71X
^
ptrRstrlen(ptr)SH VY<VX
printf(UYn /01 ta#le at serversie isHYn8sU) shmptr)X
shmt(shmptr)X
^
A,/5*"0n-)5
\[email protected]
\[email protected]
\inclue@s!s+t!pes.hA
\inclue@s!s+shm.hA
main()
]
int shmi)aX
char Zptr) ZshmptrX
char ptr$R-1S) ipR1$S) macR$.SX
shmiHshmget(%<<<)1<)<...)X
shmptrHshmat(shmi)NULL)<)X
puts(Uthe arp ta#le isU)X
printf(U8sU)shmptr)X
printf(UYn1./01Yn $.0/01Yn %.6XI5YnU)X
scanf(U8U)Da)X
switch(a)
]
case 1'
puts(Uenter ip aressU)X
scanf(U8sU)ip)X
ptrHstrstr(shmptr) ip)X
ptr-H:X
sscanf(ptr)U8s8ZsU)ptr$)X
printf(Umac ar is 8sU)ptr$)X
#rea(X
case $'
puts(Uenter mac arU)X
scanf(U8sU)mac)X
ptrHstrstr(shmptr) mac)X
sscanf(ptr)U8Zs8sU)ptr$)X
printf(U8sU)ptr$)X
#rea(X
case %'
e"it(1)X
^
^
SAMPLE INPUT OUTPUT:
the arp ta#le is
a.#.c. 1.$.%.*
e.f.g.h -...9.:
i.L.(.l ;.1.$.%
1./01
$.0/01
%.6XI5
enter !our choice' 1
enter ip aress' 1.$.%.*
mac ar is a.#.c.
enter !our choice'$
enter mac aress' e.f.g.h
ip ar is -...9.:
06SUL5'
5hus the given program was successfull! simulate an e"ecute
Using /01+0/01.
E$)NO:028a9 BIT STU%%ING
/IB'
5o write a C program to implement Tit stuffing.
/LI,0I54B'
S-0/ 1: Start the program.
S-0/ 2: Initiali[e the varia#les.
S-0/ 3: 6nter the #it seNuence.
S-0/ :: If the #it seNuence contains five consecutive ones) then uring #it stuffing
a
[ero after five ones.
S-0/ 5: Stop the program.
.
10,I0/B'
\[email protected]
\[email protected]
voi main()
]
int aR-<S)tR-<S)cR-<SX
int )iH<)LH<)(H<)#H<)lH<)mH<)pH<X
clrscr()X
printf(U6N560 TI5 S6aU6NC6YnU)X
o
]
Hgetch()X
if(HH*;)
]
printf(U1U)X
aRiSH1X
p77X
^
else if(HH*:)
]
printf(U<U)X
aRiSH<XpH<X
^
i77X
^ while(OH1%)X
for(LH<XL@H1<XL77)
]
cRLSH<X
^
if(p@H1-)
] for(LH<)(H<XL@iXL77)(77)
]
m77X
if(aRLSHH1)
]
#77X
tR(SH1X
if(#HH-)
]
(77X
m77X
cRlSHmX
l77X
tR(SH<X
#H<X
^
^
else if(aRiSHH<)
]
#H<X
tR(SH<X
^
^
printf(UYn S5UEE62 YnU)X
for(LH<XL@(-1XL77)
]
printf(U8U)tRLS)X
^
printf(UYn UNS5UEE62 YnU)X
for(LH<XL@(-1XL77)
]
if(LOHcR<S-1)
]
printf(U8U)tRLS)X
^
else
]
for(iH<Xi@lXi77)
]
cRiSHcRi71SX
^
^
^
^
else
]
printf(UYn 1L6/S6 S6N2 C,006C5 TI5S YnU)X
^
getch()X
^
^
,U51U5'
6N560 TI5 S6aU6NC6
1111111
S5UEE62
11111<11
UNS5UEE62
1111111
06SUL5'
5hus the C program to implement #it stuffing has #een written an e"ecute
successfull!.
E$)NO:02849 CRC COMPUTATION
/IB'
5o write a C program for ispla!ing the Nuotient) remainer an transfer
message.
/LI,0I54B'
S-0/ 1: Start the program.
S-0/ 2: Inclue the heaer files an eclare the varia#les.
S-0/ 3: 0ea the message.
S-0/ :: 0ea the pattern.
S-0/ 5: Calculate the Nuotient an remainer.
S-0/ 6: 2ispla! the transferre message.
S-0/ ;: Stop the program
10,I0/B'
\[email protected]
\[email protected]
voi main()
]
int aR$-S)#R$-S)cR$-S))e)f)iH<)LH<)(H<X
int lH<)mR$-SX
for(lH<Xl@H$-Xl77)
]
aRlSH<X
cRlSH<X
mRlSH<X
^
clrscr()X
printf(UoutputYnU)X
printf(U6nter the messageU)X
o
]
Hgetch()X
if(HH*;)
]
printf(U1U)X
aRi77SH1X
^
else if(HH*:)
]
printf(U<U)X
aRi77SH<X
^
^
while(OH1%)X
for(lH<Xl@H$-Xl77)
]
mRlSHaRlSX
^
printf(UYn 6nter the patternU)X
o
]
Hgetch()X
if(HH*;)
]
printf(U1U)X
#RL77SH1X
^
else if(HH*:)
]
printf(U<U)X
#RL77SH<X
^
^
while(OH1%)X
HL-1X
o
]
cR(SH1X
for(lH<Xl@LDD(@Hi7X(77)l77)
]
aR(SHaR(SM#RlSX
^
for((H<XaR(SOH1DD(@Hi7X(77)X
^
while((@i)X
printf(UYn Nuotient'U)X
for(lH<Xl@iXl77)
]
printf(U8U)cRlS)X
^
printf(UYn remainer'U)X
for(lH((i7)-L)Xl@i7Xl77)
]
printf(U8U)aRlS)X
^
printf(UYn 5ransfer messageU)X
for(lH<Xl@i7Xl77)
]
aRlSHmRlS7aRlSX
printf(U8U)aRlS)X
^
getch()X
^
,U51U5'
6nter the message'1<<1
6nter the pattern'1<1
Nuotient'1<11
remainer'<11
5ransfer message'1<<111
06SUL5'
5hus the program for ispla!ing the Nuotient) remainer an transfer
message has #een written an successfull! e"ecute.
E$)NO:03 SIMULATION O% SLIDING WINDOW PROTOCOL
/IB'
5o write a C program to perform simulation of sliing winow protocol.
/LI,0I54B'
S-0/ 1: Start the program.
S-0/ 2: Inclue the heaer files.
.
S-0/ 3: If the num#er of frames is eNual to the receive frames then ispla! frame
accepte. ,therwise) ispla! frame is not accepte.
.
S-0/ :: In the IoTac( /0a) sener sens the frame in seNuence.
S-0/ 5: 2ispla! the output.
S-0/ 6: Stop the program.
10,I0/B'
\[email protected]
\[email protected]
int num)sen_frame)i)recvr_frame)fno)opt)choice)LX
char pac(etR$<SX
int recvR$<S)(H1X
voi receive(int fno)
]
if(fnoHHrecvr_frame)
]
printf(UYn E0/B6 8 IS /CC61562U)fno)X
recvr_frameHrecvr_frame71X
returnX
^
else
]
printf(UYn E0/B6 8 IS 2ISC/0262U)fno)X
printf(UYn Sent the E0/B6 8U)recvr_frame)X
returnX
^^
voi main()
]
clrscr()X
printf(UYn ZZZU)X
printf(UYn Yt B6NUU)X
printf(UYn 1.I, T/CC /0aU)X
printf(UYn $.S6L6C5IJ6 /0aU)X
printf(UYn %.6XI5U)X
printf(UYn 6nter ur choiceU)X
scanf(U8U)Dchoice)X
switch(choice)
]
case 1'
]
printf(UYn 6nter the no. of frames'U)X
scanf(U8U)Dnum)X
recvr_frameH1X
for(iH<Xi@numXi77)
]
printf(UYn 6nter the frame no'U)X
scanf(U8U)Dsen_frame)X
printf(UYn 6nter the pac(etU)X
scanf(U8sU)Dpac(et)X
receive(sen_frame)X
^^
#rea(X
case $'
]
printf(UYn 6nter the no. of frame'U)X
scanf(U8U)Dnum)X
for(iH<Xi@numXi77)
]
recvRiSH<X
^
while((@Hnum)
]
printf(UYn 6nter the frame no'U)X
scanf(U8U)Dfno)X
printf(UYn 6nter the pac(et'U)X
scanf(U8sU)Dpac(et)X
if(fno@Hnum)
]
recvRfnoSH1X
printf(UYn E0/B6 8 IS accepteU)fno)X
(H(71X
recvRfnoSH1X
^
if(fnoAnum)
printf(UYn ,ut of rangeU)X
^^
for(LH<XL@numXL77)
]
if(recvRLSHH<)
]
printf(UYn 0esen the pac(et 8)LU)X
scanf(U8sU)Dpac(et)X
printf(UYn Erame 8 is accepteU)X
^^
#rea(X
case %'
returnX
^
getch()X
^
,U51U5'

B6NU
1.I, T/CC /0a
$.S6L6C5IJ6 /0a
%.6XI5
6nter ur choice1
6nter the no. of frames'1
6nter the frame no'1
6nter the pac(et CS6
E0/B6 1 IS /CC61562
ZZZ
B6NU
1.I, T/CC /0a
$.S6L6C5IJ6 /0a
%.6XI5
6nter ur choice'$
6nter the no. of frame'$
6nter the frame no'$
6nter the pac(et'//
E0/B6 $ IS accepte
6nter the frame no'1
6nter the pac(et'SS
E0/B6 1 IS accepte
0esen the pac(et <)L
06SUL5'
5hus the program to perform simulation of sliing winow protocol has
#een written an successfull! e"ecute.
E$)NO:0: CLIENT ' SERER APPLICATION %OR C#AT
/IB'
5o write a Lava program for client server application for chat.
/LI,0I54B'
S-0/ 1: Start the program.
S-0/ 2: 2eclare the soc(et for client server enter the string to #e ispla!e on
client
winow to server winow.
.
S-0/ 3: Similarl! enter the String to #e ispla!e in server winow to ispla!e on
client
winow.
S-0/ :: 0ea the string on #oth the winows.
S-0/ 5: Stop the program.
10,I0/B'
SERER SIDE CODING:
import Lava.io.ZX
import Lava.net.ZX
pu#lic class server
]
pu#lic static voi main(String argsRS)throws I,6"ception
]
tr!
]
ServerSoc(et ssHnew ServerSoc(et(*<<<)X
Soc(et sHss.accept()X
String str1)str$X
InputStream isHs.getInputStream()X
InputStream0eaer isrHnew InputStream0eaer(is)X
Tuffere0eaer #rHnew Tuffere0eaer(isr)X
,utputStream osHs.get,utputStream()X
1rintStream psHnew 1rintStream(os)X
2ataInputStream isHnew 2ataInputStream(S!stem.in)X
ps.println(UhelloU)X
o
]
str1H#r.reaLine()X
S!stem.out.println(UE0,B CLI6N5'U7str1)X
S!stem.out.println(U6nter message'U)X
str$His.reaLine()X
ps.println(str$)X
^
while(O(str1.eNuals(Ue"itU)))X
s.close()X ^
catch(6"ception e)
] S!stem.out.println(e)X
^^^
,U51U5'
b'YALavac server.Lava
b'YALava server
E0,B CLI6N5'#!e
6nter message'
5ata
CLIENTSIDE CODING:
import Lava.io.ZX
import Lava.net.ZX
pu#lic class client
]
pu#lic static voi main(String argsRS)throws I,6"ception
]
tr!
]
Soc(et sHnew Soc(et(UlocalhostU)*<<<)X
2ataInputStream isHnew 2ataInputStream(S!stem.in)X
String str1)str$X
,utputStream osHs.get,utputStream()X
1rintStream psHnew 1rintStream(os)X
InputStream isHs.getInputStream()X
InputStream0eaer isrHnew InputStream0eaer(is)X
Tuffere0eaer #rHnew Tuffere0eaer(isr)X
o
]
str$H#r.reaLine()X
S!stem.out.println(UE0,B S60J60'U7str$)X
S!stem.out.println(Uenter messageU)X
str1His.reaLine()X
ps.println(str1)X
^
while(O(str1.eNuals(Ue"itU)))X
s.close()X
^
catch(6"ception e)
]
S!stem.out.println(e)X
^^^
,U51U5'
b'YALavac client.Lava
b'YALava client
E0,B S60J60' hello
6nter message
#!e
E0,B S60J60' tata
6nter message
5ata
06SUL5'
5hus the program for client server application for chat has #een written an
successfull! e"ecute.
E$)NO:05 DNS SERER TO RESOLE #OSTNAME
/IB'
5o write a C program to resolve hostname in UNIX.
/LI,0I54B'
S-0/ 1: Start the program.
S-0/ 2: Inclue the necessar! heaer files) an eclare the varia#les.
S-0/ 3: If the hostname eNuals negative than print as error in hostname.
.
S-0/ :: Iet the host name.
S-0/ 5: 1rint the I1 aress for given hostname.
S-0/ 6: Stop the program.
10,I0/B'
\inclue@s!s+soc(et.hA
\[email protected]
\inclue@net#.hA
\inclue@arpa+inet.hA
\inclue@netinet+in.hA
int main()
]
struct hostent ZhostX
struct in_ar)h_arX
char hostnameR$<<SX
printf(UYn 6N560 546 4,S5N/B6'U)X
scanf(U8sU)hostname)X
if(hostHgethost#!name(hostname))HH-1)
]
printf(UYn 600,0 8sU)hostname)X
e"it(1)X
^
h_ar)s_arHZ((unisgne longZ)host-Ah_ar_listR<S)X
fprintf(stout)U8s has aress 8sYnU)hostname)inet_ntoa(h_ar))X
e"it(<)X
^
,U51U5'
Rm ?cc ns.c -lsoc(et
?.+a.out
6N560 546 4,S5N/B6' scos!s
Sscos!s has aress 1*<.1-<.1.1.1
06SUL5'
5hus the program for resolving hostname has #een verifie an e"ecute.
E$)NO:06 DOWNLOADING A #OMEPAGE %ROM #TTP SERER
/IB'
5o write a C program for ownloaing the homepage in an! 4551 server.
/LI,0I54B'
S-0/ 1: Start the program.
S-0/ 2: Iet the hostname of server.
S-0/ 3: Create a soc(et connection with server using connection metho an ma(e a
reNuest
to http server.
S-0/ :: 6nter the page is to #e ownloae.
S-0/ 5: 2ispla! the output.
S-0/ 6: Stop the program.
10,I0/B'
\[email protected]
\inclue@s!s+t!pes.hA
\inclue@s!s+soc(et.hA
\inclue@netinet+in.hA
\inclue@net#.hA
\[email protected]
char #ufferR.<;.SX
char #ufR.<;.SX
voi error(charZmsg)
]
perror(msg)X
e"it(<)X
^
int main(int argc)charZ argvRS)
]
int soc(f)portno)nX
struct soc(ar_in serv_arX
struct hostentZserverX
char #ufferR*<;.SX
portnoH:<X
soc(fHsoc(et(/E_IN65)S,CC_S506/B)<)X
if(soc(f@<)
error(Uerror opening soc(etU)X
serverHgethost#!name(Uwww.prist.ac.inU)X
if(serverHHNULL)
]
fprintf(sterr)Uerror)no such hostYnU)X
e"it(<)X
^
#[ero((charZ)Dserv_ar)si[eof(serv_ar))X
serv_ar.sin_famil!H/E_IN65X
#cop!((charZ)server-Ah_ar)(charZ)Dserv_ar)sin_ar.s_ar)server-
Ah_length)X
serv_ar.sin_portHhtons(portno)X
if(connect(soc(f)(struct soc(arZ)Dserv_ar)si[eof(serv_ar))@<)
error(U600,0 connectingU)X
strp!(#uffer)UI65 http'++www.prist.ac.inYrYn 4ost'1*<.1-<.1.1.1U)X
nHsen(soc(f)#uffer)strlen(#uffer).<)X
if(n@<)
error(Uerror writing to soc(etU)X
#[ero(#uf)*<;.)X
while(nHrecv(soc(f)#uf)si[eof(#uf))<))OH<)
]
#ufR.<;.SHVY<VX
printf(Ureceive 8sU)#uf)X
^
close(soc(f)X
return <X
^
,U51U5'
?cc http.c
?.+a.out
receive
@htmlA
@heaA@titleA6rror@+titleA@+heaA
#o!
@heaA@titleA2irector! Listing 2enie@+titleA@+heaA
@#o!A
@h1A2irector! Listing 2enie@+h1A5his virtual irector! oes not allow contents
to #e liste.
@+#o!A
@+#o!A
@+htmlA
06SUL5'
5hus the program for ownloaing the homepage using 4551 server has
#een e"ecute an then verifie.

You might also like