1 CERTIFICATE [OS lab] This is to certify that,this is a Bonafide Record of the work done as a partial fulfillment of the course MCA second year second semester in the college during the year 2011- 2012. NAME : CLASS : ROLL NO : HALL TICKET NO : SIGNATURE OF SIGNATURE OF SIGNATURE OF PRINCIPAL LECTURER INCHARGE EXTERNAL EXAMINER
46
Embed
WAP TO DEMONSTRATE FORK SYSTEMCALLmycnis.weebly.com/uploads/4/1/8/7/4187501/os_record.doc · Web view$ chmod ugo +r Gives read, permissions only to user ,group and others $ chmod
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
CERTIFICATE[OS lab]
This is to certify that,this is a Bonafide Record of the work done as a partial fulfillment of the course MCA second year second semester in the college during the year 2011-2012.
NAME :
CLASS :
ROLL NO :
HALL TICKET NO :
SIGNATURE OF SIGNATURE OF SIGNATURE OFPRINCIPAL LECTURER INCHARGE EXTERNAL EXAMINER
INDEX
2
ALL UNIX COMMANDS
SHELL SCRIPTS
1 shell script to do arithmetic operation on integer
2 shell script to find whether the number is odd or even
3 shell script to find a leap year
4 shell script to find the greatest of three numbers
5 shell script to check whether a file is ordinary directory or special
6 shell script to check whether a file has read,write,execute permissions
7 shell script to check whether an entered character is alphabetic, digit or
special symbol
8 shell script to find the factorial of the given number
9 shell script to the reverse of the given number
10 shell script to find length of the string
11 shell script to find sum of digits of a given number
12 shell script to find files into the current directory
‘C’ PROGRAMS
3
13 write a ‘c’ program to demonstrate fork system calls
14 write a ‘c’ program to demonstrate pipes
15 write a ‘c’ program to implement echo server using pipes
16 write a ‘c’ program to send a message from process a to process b using
message queues
17 write a ‘c’ program to implement echo server using message queues
18 write a ‘c’ program to send a message from process a to process b using
shared memory
19 write a ‘c’ program to implement echo server using shared memory
20 write a ‘c’ program to implement producer consumer problem using
semaphores
21 write a ‘c’ program to implement dinning philosophers problem using
semaphores
22 write a program for creating a file
23 write a program for copying files
24 write a program for displaying file flags
25 write a program for file locking
26 write a program for file types
4
COMMANDS FOR CREATING FILES
command Description
$ touch filename Creates a file of a given file name and the size of the file is zero.
$touch f1 f2 f3 Creates a file of a given file names f1, f2,f3 and the size of the files is
zero.
$ cat > filename Creates a file of a given file name and we can type data into these files.
$cat f1 f2>f3 Copies the contents of the files f1 f2 into f3 and the contents of f3 are
lost.
$cat f1 f2>>f3 Copies the contents of the files f1 f2 into f3and the contents of f3 are not
lost
$cp f1 f2 Copies the contents of the files f1 into f2.
$cp f1 f2 f3 Copies the contents of the file f1 and f2 into f3
$cat filename Displays the contents of the file name.
COMMANDS FOR DELETING FILES
$rm-i filename Remove the files of given file name interactively and asks user
permission before deleting
$rm-i directory name Removes the files of given directory name and asks user permission
before deleting. Before deleting the directory we must delete all the
files in the directory
$rm-r directory name Removes the files of given directory name itself recursively
$ rm filename Removes the files of given directory name and asks user permission
before deleting. Before deleting the directory we must delete all the
files in the directory
COMMANDS FOR RENAMING A FILE
5
$mv f1 f2
Rename the file name from f1 to f2
$mv d1 d2
Rename the directory name from d1 to d2
$mv f1 f2 d Moves the files from old directory to d
COMMANDS FOR LISTING FILES AND DIRECTORIES
$ ls Lists all the files in the directory
$ ls Lists all the files in the directory with permissions
$ ls n* Lists all the files of names that start with letter n.
$ ls ?ain Lists all the files of names of 4 word file names and the first letter
can be any digit or character
$ ls [aeiou]* Lists all the files of names that starts with letter a,e,i,ou
$ ls [!aeiou]* Lists all the files of names that doesn’t starts with letter a,e,i,ou
$ ls[a-m][c-z][4-9]?? List all the files with first letter from a to m, second letter from c to z
,with third letter 4 to 9,and fourth and fifth character any digit or
character
$ ls f1 f2 Links file f1 to file f2
$ ls ?? It will give two lettered file names
COMMANDS FOR FILE PERMISSIONS$ chmod 700 filename Gives read, write and execute permissions only for user
$ chmod 070 filename Gives read, write and execute permissions only for group
$ chmod 007 filename Gives read, write and execute permissions only for others
$ chmod 777 filename Gives read, write and execute permissions only for user,
group, and others.
$ chmod ugo +r Gives read, permissions only to user ,group and others
6
$ chmod ug –r Removes read to user, group.
COMMANDS FOR WORD COUNT$ wc -l filename Displays the number of lines in the given file.
$ wc –w filename Displays the number of words in the given file.
$ wc –c filename Displays the number of characters in the given file.
$ wc –lwc filename Displays the number of lines, words, and characters in the given
file.
COMMANDS FOR DOS$dosdir a: To read the contents of the drive a.
$dosdir a:/proj To see the content of a directory ‘project’
$doscat a: filename To see the content of the dos file
$ doscp trial a: To copy the file trial from UNIX directory to the DOS drive a.
$doscp
$ HOME/proj/*.prg
a:/progs
To copy all the files from the proj directory in the HOME directory to progs
directory in drive a.
$dosls To list the files and directories in the dos
COMMANDS FOR CUT$ cut –f 2,7 f This command display the contents present only in 2nd and 7th field
$ cut –f 2 -7 f This will display the contents from 2nd field to 7th field
$ cut –f 2,7 “:” f Here each field is separated by delineator “:”
$ cut –c l-15 f Display the contents present in first 15 columns
COMMANDS FOR SORTING$ sort filename Sorts the contents of the file and displays the result on the screen.
$ sort f1 f2 f3 Sorts the contents of the files f1 and f2 and displays the result on the
screen
7
$ sort -o result f1 f2 Sorts the contents of the files f1 and f2 and stores the result on the file
result
$ sort -u –o result f1 f2 Sorts the contents of the file f1 and f2 and stores the result on the file
result. Here the repeated lines in different files are displayed for one time
$ sort -m f1 f2 Stores the files f1 and f2 and merges those files.
$ sort -r +l -2 f Suppose file f consists of four fields. The sorting of files starts from 1st
field and ends before 3rd field
$ sort -r f Reverses the Sorting of file f
$ sort -b f Sorts the file f ignoring leading spaces.
$ sort -c f Checks whether the files are sorted or not
$ sort -d f Sorts the files in directory order
$ sort -m f Merges files, if files are already been sorted
$ sort -n f Sorts the file in numeric order.
$ sort -tc f Separates fields with tab.
COMMANDS FOR GREP$ grep hai fi Searches for the word hai and displays the lines having word hai
$ grep [Rr]ita f1 Searches for both Rita and rita in file f1
$ grep b??k f1 The lines containing words that start with b and end with k will be
displayed on the screen for file f1
$ grep -c a* f1 Returns the number of mismatches in the text
$ grep –i a* f1 Ignores case while searching
$ grep –l a* Returns only file names that containing the match
8
$ grep –n a* f1 Returns file name and the text that containing the match
$ grep –s a* f1 Suppresses error in the message
$ grep –v a* f1 Returns the lines that doesn’t match
Commands for positioning cursor in the window
Positioning by characterH Moves the cursor one character to the left
Backspace Moves the cursor one character to the left
l Moves the cursor one character to the right
0 Moves the cursor one character to the right
$ Moves the cursor to the end of the current line
Positioning by line
J Moves the cursor down one line from it’s present position, in the same
column
K Moves the cursor up one from it’s present position ,in the same column
+ Moves the cursor down to the beginning of next line
- Moves the cursor up to beginning of previous line
Enter Moves the cursor down to the beginning of the next line
Positioning by wordW Moves the cursor to the right, to the first character of the next word
B Moves the cursor back to the first character of the previous word
E Moves the cursor to the end of the current word
Positioning in window
H Moves the cursor to the first line on the screen, or “home
M Moves the cursor to the middle line on the screen
9
L Moves the cursor to the last line on the screen
Commands for positioning in the file
Commands for ScrollingCtrl f Scrolls the screen forward a full window, revealing the window of
text below the current window
Ctrl b Scrolls the screen back a full window, revealing the window of text
above the current window
Positioning on a Numbered line
G Moves the cursor to the beginning of last line in the file
nG Moves the cursor to the beginning of nth line in the file
Commands for Inserting TextA Enters the text input mode and appends text after the cursor
I Enters the text input mode and inserts text at the cursor
A Enters the text input mode and appends text at the end of current line
I Enters the text input mode and inserts text at the beginning of the
current line
Commands for deleting text
X Deletes the character at current cursor position
X Deletes the character to the left of the cursor
Dw Deletes a word from the cursor to the next space or to the next punctuation
Dd Deletes current line
nx,ndm,ndd Deletes n characters, n words ,n lines
d0 Deletes the current line from the cursor to the beginning of the line
d$ Deletes the current line from the cursor to the end of the line
Miscellaneous commandsCtrl g Gives the line number of current cursor position in the buffer and
modification status of the file
. Repeats the action performed by the last command
U Undoes the effects of the last command
10
U Restores all changes to actual line since you moved the cursor to this line
J Joins the line immediately below the current line with current line
~ Changes character at current cursor position from upper case to lower case
or from lower case to upper case
:sh Temporarily returns to the shell to perform some shell commands type
exit to return to vi
Clears and redraws the current window
Commands for Quitting vi
ZZ Writes the buffer to the file and quits vi
:wq Writes the buffer to the file and quits vi
:w filename and :q Writes the buffer to the file filename(new) and quits vi
:w! filename and :q Overwrites the existing file filename with the content of the buffer and
quits vi
:q! Quits vi whether or not changes made to the buffer were written to a
file .does not incorporate changes made to the buffer since the last
write (:w) command
1 shell script to do arithmetic operations on integers
echo “enter first number”
read a
echo “enter second number”
read b
c=`expr $a + $b`
echo “a+b=” $c
c=`expr $a - $b`
11
echo “a-b=” $c
c=`expr $a * $b`
echo “a*b=” $c
c=`expr $a / $b`
echo “a/b=”$c
c=`expr $a % $b`
echo “a%b=”$c
output:[mca28@localhost ~ ]$sh arith
Enter first number
30
Enter second number
5
a+b=35
a-b=25
a*b=150
a/b=6
a%b=0
2 Shell script to find that a given number is odd or even
Clearecho “enter a number”read nr=`expr $n%2`if test $r – eq 0thenecho $n “ is a even number”elseecho $n”is a odd number”fi
12
output:
[mca28@localhost ~ ]$sh arith
enter a number 5 5 is a odd number[mca28@localhost ~]$
3 Shell script to find a given year is a leap year or not
Clear
echo “enter a year”
read yr
lpy=`expr $y % 4`
if test $lpy –eq 0
then
echo $yr ”is a leap year”
else
echo $yr “is not a leap year”
fi
output:[mca28@localhost ~ ]$sh leap
enter a year
2012
2012 is a leap year
4 Shell script to find a greatest of 3 numbers
echo “ enter 3 numbers”
read a
read b
read c
if test $a –gt $b –a $a –gt $c
then
echo $a “is greater”
13
else
if test $b –gt $a –a $b –gt $c
then
echo $b “ is greater”
else
echo $c “is greater”
fi
fi
output:[mca28@localhost ~ ]$sh gr
enter 3 numbers
34
56
41
56 is greater
5 Shell script to check whether a file is ‘ordinary’, ‘directory’, or ‘special file’
echo “enter a file name:”read fnif test –f $fnthenecho “it is ordinary file”elseif test –d $fnthenecho “it is a directory”elseecho “it is special file”fi fi
output:[mca28@localhost ~ ]$sh dr
enter a file name:f1it is ordinary file [mca28@localhost ~ ]$
6 Shell script to find if a file has write,read,execute permission or not
14
Clearecho “enter a file name”read fnif test –w $fnthenecho “file has write permission”fiif test –r 4fnthen echo “file has read permission”fiif test –x $fnthenecho “file has execute permission”fi
output:[mca28@localhost ~ ]$sh wr
enter a file namef1file has write permissionfile has read permission
enter a file namexyzfile has write permissionfile has read permissionfile has execute permission
7 Shell script to enter a character and check whether it is a alphabet, digit or a special symbol
echo “enter a character”read ccase $c in [a-z]|[A_Z] echo “alphabet”;;[0-9] echo “digit”;;?)echo “special symbol”;;*)echo “illegal character”;;esac
output:
15
[mca28@localhost ~ ]$sh 9.sh
enter a character5Digit[mca19@localhost]$ sh 9.shEnter a characterAAlphabet[mca19@localhost]$ sh 9.shEnter a character?Special symbol[mca19@localhost]$ sh 9.sh
11 Shell script to find the sum of digits of a number
echo”enter the number”read ns=0while[$n –gt 0]dor= `expr $n %10`s= `expr $s+$r`n= `expr $n / 10`echo “ sum of digits” $s
output:[mca28@localhost ~ ]$sh su
enter the number63Sum of digits 9[mca28@localhost]$
12 Shell script to find the files in current directory
k=0
17
for f in *doecho $fk=`expr $k+1`doneecho $k
output:
"cur" [New] 7L, 53C written[mca9@mcaserver2 ~]$ sh cura.outcopyfile.ccrating.ccreating.ccurd.cdining.cdivyadivya.cechop.cfork.cgreatermesg.hpipes.crec.csamplesend.csmop.hsmop.h.gchsumtesttest.cexpr $k+1
13 WRITE A PROGRAM TO DEMONSTRATE FORK SYSTEMCALL
#include<stdio.h>int main(){int pid;pid=fork();if(pid==0){printf(" i am child,id is: %d\n",getpid());printf("parent id is: %d\n",getppid());
18
//exit(0);}else{printf(" i am parent,id is %d\n",getpid());printf("parent,parent id is %d\n",getppid());//exit(0);}return 0 ;}
OUTPUT:
[mca47@mcaserver2 ~]$ cc fork.c [mca47@mcaserver2 ~]$ ./a.out i am parent,id is :9640 parent,parent id is: 8824 i am child,id is: 9641 parent id is: 9640
printf("unable to create pipe");//exit(1)}else{printf("enter some data into pipe");n=read(0,buff1,100);write(pipeid[1],buff1,n);printf("the data from pipe is");read(pipeid[0],buff2,n);write(1,buff2,n);}}
OUTPUT:
[mca47@mcaserver2 ~]$ cc pipe.c[mca47@mcaserver2 ~]$ ./a.outloyola academyloyola academyenter some data into pipethe data from pipe is[mca47@mcaserver2 ~]$
15 WAP TO DEMONSTRATE ECHO SERVER USING PIPES:
#include<stdio.h>#include<unistd.h>#include<stdlib.h>main(){int pipeid1[2],pipeid2[2],childid;if((pipe(pipeid1)<0)||pipe(pipeid2)<0){printf("error in creating pipes");
#include<stdio.h>#include"smop.h"main(){int semid=semget(SEMKEY,3,PERMS|IPC_CREAT);int tmp,flag=1;semlock1(semid);while(flag!=2){printf("\n\n\t\t enter 0 to start producer:");scanf("%d",&tmp);semlock0(semid);semlock2(semid);printf("\n\t\t producer producing....");printf("\n\t\t enter 1 to stop:");scanf("%d",&tmp);semunlock2(semid);semunlock1(semid);printf("\n\n\t\t producer stop producing");
30
printf("\n\n\t\t enter 2 to exit.....\n any other to continue");scanf("%d",&flag);}}
//file3:consumer.c
#include<stdio.h>#include"smop.h"main(){int semid=semget(SEMKEY,3,PERMS|IPC_CREAT);int tmp,flag=1;while(flag!=2){printf("\n\n\t\t enter 0 to start consume");scanf("%d",&tmp);semlock1(semid);semlock2(semid);printf("\n\n\t\t consumer consuming.....");printf("\n\t enter 1 to stop:");scanf("%d",&tmp);semunlock2(semid);semunlock0(semid);printf("\n\n\t\t consumer stop consuming");printf("\n\n\t\t enter 2 to exit.....\n any other to continue");scanf("%d",&flag);}}
Output:
[mca29@mcaserver2 ~]$cc producer.c[mca29@mcaserver2 ~]$./a.outenter 0 to produce:0
producer producing…enter 1 to stop : 1Producer stop producingenter 2 to exitany other key to continue:2[mca29@mcaserver2 ~]$
[mca29@mcaserver2 ~]$cc consumer.c[mca29@mcaserver2 ~]$./a.outenter 0 to consume:0
consumer consuming…enter 1 to stop:1
31
consumer stop consumingenter 2 to exitany other key to continue:2[mca29@mcaserver2 ~]$
21 WRITE A PROGRAM TO IMPLEMENT DINNING PHILOSOPHERS PROBLEM USING SEMAPHORES