46
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

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

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 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

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

Page 2: 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

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

Page 3: 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

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

Page 4: 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

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

Page 5: 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

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

Page 6: 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

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

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

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

Page 8: 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

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

Page 9: 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

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

Page 10: 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

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`

Page 11: 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

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

Page 12: 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

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”

Page 13: 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

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

Page 14: 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

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:

Page 15: 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

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

Enter a character\Illegal character

8 Shell script to find factorial of a number

Echo “enter a number”read numfact =1for ((i=1;$i<num; i= ‘expr $i+ 1’)) {fact= `expr$fact \ * $i`}echo $num “!= “ $fact

output:

[mca29@localhost] $sh fac.shenter a number44!=24[mca29@localhost]$ 9 Shell script to print reverse of a number

# reverse of a given number

echo “enter a number”read ns=0while [ $n –gt 0]dor= `expr $n %10`s= `expr $s \*10 + $r`n= `expr $n /10`

Page 16: 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

16

doneecho “ reverse of number is =”$s

Output:

[mca9@localhost]$ sh reventer a number53reverse of number is =35 10 Shell script to find length of a string

echo”enter a string”read strn=`expr $str | wc –c`k=`expr $n – 1`echo $k

output:

[mca3@localhost]$ sh st.shenter a stringunix4

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

Page 17: 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

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

Page 18: 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

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

14 WRITE A PROGRAM TO DEMONSTRATE A PIPE

#include<stdio.h>#include<unistd.h>#include<stdlib.h>main(){int pipeid[2],n;char buff1[100],buff2[100];if(pipe(pipeid) <0){

Page 19: 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

19

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

Page 20: 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

20

exit(1);}if((childid=fork()) > 0){close(pipeid1[0]);close(pipeid2[1]);client(pipeid2[0],pipeid1[1]);close(pipeid1[1]);close(pipeid2[0]);}else{close(pipeid1[1]);close(pipeid2[0]);server(pipeid1[0],pipeid2[1]);close(pipeid1[0]);close(pipeid2[1]);}}client(int readid,int writeid){char buff1[50],buff2[50];int n;n=read(0,buff1,50);write(writeid,buff1,n);read(readid,buff2,n);write(1,buff2,n);}server(int readid,int writeid){char buff3[50];int n;n=read(readid,buff3,50);write(writeid,buff3,n);}

OUTPUT:

[mca47@mcaserver2 ~]$ cc echo.c[mca47@mcaserver2 ~]$ ./a.outhello studentshello students[mca47@mcaserver2 ~]$

16 WRITE A PROGRAM TO SEND A MESSAGE FROM PROCESS A TO PROCESS B USING MESSAGE QUEUES

//file mesg.h

#include<sys/types.h>#include<sys/ipc.h>

Page 21: 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

21

#include<stdio.h>#include<sys/msg.h>#define MKEY1 5543L#define MKEY2 4354L#define PERMS 0666typedef struct{ long mtype; char mdata[100];}mesg;

//file2 sender.c

#include "mesg.h"mesg msg;main(){ int mq_id; int n; if((mq_id==msgget(MKEY1,PERMS|IPC_CREAT))<0) { printf("sender:error creating queue"); //exit(1); } msg.mtype=1111L; n=read(0,msg,50); msg.mdata[n]='\0'; msgsnd(mq_id,&msg,50,0);}

//file3 :receiver.c

#include "mesg.h"mesg msg;main(){ int mq_id; int n; if((mq_id=msgget(MKEY1,PERMS|IPC_CREAT))<0) { printf("receiver:errpr opening queue"); //exit(1); } msgrcv(mq_id,&msg,50,1111L,0); write(1,msg,50);}#include "mesg.h"mesg msg;main(){ int mq_id;

Page 22: 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

22

int n; if((mq_id=msgget(MKEY1,PERMS|IPC_CREAT))<0) { printf("receiver:errpr opening queue"); //exit(1); } msgrcv(mq_id,&msg,50,1111L,0); write(1,msg,50);}

Output:

[mca29@mcaserver2 ~]$cc sender.c[mca29@mcaserver2 ~]$./a.outLoyola academy[mca29@mcaserver2 ~]$cc receiver.c[mca29@mcaserver2 ~]$./a.outLoyola academy[mca29@mcaserver2 ~]$

17 WRITE A PROGRAM TO IMPLEMENT ECHOSERVER USING MESSAGEQUEES

//file1:mesg2.h

#include<sys/types.h>#include<sys/ipc.h>#include<stdio.h>#include<sys/msg.h>#define MKEY1 4164L#define MKEY2 4266L#define PERMS 0666typedef struct{ long mtype;}mesg;

//file2:client.c#include<stdio.h>#include "mesg2.h"mesg msg1,msg2;main(){ int mq_id1,mq_id2; int n; if((mq_id1=msgget(MKEY1,PERMS|IPC_CREAT))<0) {

Page 23: 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

23

printf("client:error creating queue"); //exit(1); } if((mq_id=msgget(MKEY2,PERMS|IPC_CREAT))<0) { printf("client:error creating queue"); //exit(1); } msg1.mtype=10L; n=read(0,msg1,mdata,50); msg1.mdata[n]='\0'; msgsnd(mq_id1,&msg1,50,0); msgrcv(mq_id2,&msg2,50,10L,0); write(1,msg2,50);}

//file3:serv.c#include<stdio.h>#include "mesg2.h"mesg msg;int mq_id1,mq_id2;int n;if((mq_id1=msgget(MKEY1,IPC_CREAT|0666))<0){ printf("server:error opening queue"); //exit(1);}if((mq_id2=msgget(MKEY2,IPC_CREAT|0666))<0){ printf("sender:error creating queue"); //exit(1);}msgsnd(mq_id2,&msg,50,0);

output:[mca29@mcaserver2 ~]$cc serv.c

[mca29@mcaserver2 ~]$ ./a.out

[mca29@mcaserver2 ~]$ cc client.c[mca29@mcaserver2 ~]$./a.outSwethaSwetha[mca29@mcaserver2 ~]$

18 WRITE A PROGRAM TO SEND A MESSAGE FROM PROCESS A TO PROCESS B USING SHAREDMEMORY

#include<sys/types.h>

Page 24: 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

24

#include<sys/ipc.h>#include<stdio.h>#include<sys/shm.h>

#define PERMS 0666#define SHMKEY ((key_t)5444)

//file2 sender.c

#include<stdio.h>#include"shm.h"main(){int shmid,n;char *pshm;if((shmid=shmget(SHMKEY,10,PERMS|IPC_CREAT))<0){printf("\n Sender:cant get shared memory");exit(1);}pshm=shmat(shmid,(char *)0,0);if(pshm < 0){perror("sender:cant attach shared memory");exit(1);}n=read(0,pshm,10);pshm[n]='\0';exit(0);}

//file3 receiver.c

#include<stdio.h>#include"shm.h"main(){int shmid,n;char *pshm;if((shmid=shmget(SHMKEY,10,PERMS|IPC_CREAT))<0){printf("\nReceiver:Error opening shared memory");exit(1);}pshm=shmat(shmid,(char *)0,0);if(pshm<0){printf("\nReceiver:cant attach shared memory");exit(1);}printf("%s",pshm);exit(0);

Page 25: 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

25

}

OUTPUT:

[mca28@mcaserver2 ~]$cc sender.c[mca28@mcaserver2 ~]$./a.outLoyola[mca28@mcaserver2 ~]$cc receiver.c[mca28@mcaserver2 ~]$./a.outLoyola[mca28@mcaserver2 ~]$

19 WRITE A PROGRAM TO IMPLEMENT ECHO SERVER USING SHARED MEMORY

//File1:shm.h

#include<sys/types.h>#include<stdio.h>#define PERMS 0666#define SHMKEY0 ((key_t)5444)#define SHMKEY1 ((key_t)5445)

//file2:smop.h

#include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>#define SEMKEY 3010L#define PERMS 0666static struct sembuf lock0[2]={0,0,0,0,1,0};static struct sembuf unlock0[1]={0.-1,IPC_NOWAIT};static struct sembuf lock1[2]={1,0,0,1,1,0};static struct sembuf unlock1[1]={1,-1,IPC_NOWAIT};static struct sembuf lock2[2]={2,0,0,2,1,0};static struct sembuf unlock2[1]={2,-1,IPC_NOWAIT};int semlock0(int semid){ semop(semid,&lock0[0],2);}int semunlock0(int semid){ semop(semid,&unlock0[0],1);}int semlock1(int semid){ semop(semid,&lock1[0],2);} int semunlock1(int semid){

Page 26: 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

26

semop(semid,&unlock1[0],1);}int semlock2(int semid){ semop(semid,&lock2[0],2);}int semunlock2(int semid){ semop(semid,&unlock2[0],1);}

//file3:sender.c

#include "smop.h"#include "shm.h"#include<stdlib.h>main(){int n;int repeat=1;char *pshm0,*pshm1;int semid;int shmid0,shmid1;semid=semget(SEMKEY,2,IPC_CREAT|PERMS);if((shmid0=shmget(SHMKEY0,10,PERMS|IPC_CREAT))<0){perror("\nclient:can't get shared memory 0");exit(1);}pshm0=shmat(shmid0,(char*)0,0);if((shmid1=shmget(SHMKEY1,10,PERMS|IPC_CREAT))<0){perror("\nclient:can't get shared memory 1");exit(1);}pshm1=shmat(shmid1,(char*)0,0);while(repeat){semlock0(semid);perror("\nenter a message:");n=read(0,pshm0,10);semunlock0(semid);semlock1(semid);write(1,pshm1,n);semunlock1(semid);printf("\n do you eant repeat?(1/0):");scanf("%d",&repeat);}if((shmdt(pshm0))<0){perror("\n client:can't detach shared memory 0");

Page 27: 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

27

exit(1);}if((shmdt(pshm1))<0){perror("\n client:can't detach shared memory 1");exit(1);}exit(0);}

//file4:reciever.c

#include"smop.h"#include"shm.h"#include<stdlib.h>main(){int i,n;char repeat='y';char *pshm0,*pshm1;int semid;int shmid0,shmid1;semid=semget(SEMKEY,2,IPC_CREAT|PERMS);if((shmid0=shmget(SHMKEY0,10,PERMS|IPC_CREAT))<0){perror("\nserver: can't get shared memory 0");exit(1);}pshm0=shmat(shmid0,(char*)0,0);if((shmid1=shmget(SHMKEY1,10,PERMS|IPC_CREAT))<0){perror("\nserver:can't get shared memory 1");exit(1);}pshm1=shmat(shmid1,(char*)0,0);while(1){semlock1(semid);for(i=0;i<10;i++){*(pshm1+i)=*(pshm0+i);}semunlock0(semid);semunlock1(semid);}if((shmdt(pshm0))<0){perror("\nserver:can't detach shared memory");exit(1);}if((shmdt(pshm1))<0)

Page 28: 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

28

{perror("\n server:can't detach shared memory");exit(1);}exit(0);}

Output:

[mca3@mcaserver2 ~]$ cc c1.c[mca3@mcaserver2 ~]$ ./a.out

Enter a message:LoyolaLoyola

Do you want to repeat?(1/0):1

Enter a message:Operating systemOperating system

Do you want to repeat?(1/0):0

[mca3@mcaserver2 ~]$

20 WRITE A PROGRAM TO IMPLEMENT PRODUCER CONSUMER PROBLEM USING SEMAPHORES

//file1:smop.h

#include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>#define SEMKEY 3010L

Page 29: 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

29

#define PERMS 0666static struct sembuf lock0[2]={0,0,0,0,1,0};static struct sembuf unlock0[1]={0.-1,IPC_NOWAIT};static struct sembuf lock1[2]={1,0,0,1,1,0};static struct sembuf unlock1[1]={1,-1,IPC_NOWAIT};static struct sembuf lock2[2]={2,0,0,2,1,0};static struct sembuf unlock2[1]={2,-1,IPC_NOWAIT};int semlock0(int semid){ semop(semid,&lock0[0],2);}int semunlock0(int semid){ semop(semid,&unlock0[0],1);}int semlock1(int semid){ semop(semid,&lock1[0],2);} int semunlock1(int semid){ semop(semid,&unlock1[0],1);}int semlock2(int semid){ semop(semid,&lock2[0],2);}int semunlock2(int semid){ semop(semid,&unlock2[0],1);}//file2:producer.c

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

Page 30: 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

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

Page 31: 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

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

//FILE:SMOP.H//

#include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>#define SEMKEY 3010L#define PERMS 0666static struct sembuf lock0[2]={0,0,0,0,1,0};static struct sembuf unlock0[1]={0,-1,IPC_NOWAIT};static struct sembuf lock1[2]={1,0,0,1,1,0};static struct sembuf unlock1[1]={1,-1,IPC_NOWAIT};static struct sembuf lock2[2]={2,0,0,2,1,0};static struct sembuf unlock2[1]={2,-1,IPC_NOWAIT};int semlock0(int semid){ semop(semid,&lock0[0],2);}int semunlock0(int semid){ semop(semid,&unlock0[0],1);}int semlock1(int semid){ semop(semid,&lock1[0],2);}int semunlock1(int semid){ semop(semid,&unlock1[0],1);}int semlock2(int semid){ semop(semid,&lock2[0],2);}int semunlock2(int semid){ semop(semid,&unlock2[0],1);}

//PROGRAM

#include<stdio.h>

Page 32: 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

32

#include "smop.h"main(){ int pno,semid,tmp; semid=semget(SEMKEY,3,PERMS|IPC_CREAT); printf("\n\t\tEnter philosopher no:(0-2) "); scanf("%d",&pno); switch(pno) { case 0: printf("\n\nPhilosopher %d thinking",pno); printf("\n\tEnter 0 to start eating.."); scanf("%d",&tmp); semlock0(semid); printf("\n\n\tPhilosopher %d eating",pno); printf("\n\tPress 1 to stop eating.."); scanf("%d",&tmp); semunlock0(semid); printf("\n\n\tPhilosopher %d thinking,,",pno); break; case 1: printf("\n\n\tPhilosopher %d thinking..",pno); printf("\n\tEnter Zero to start eating,,"); scanf("%d",&tmp); semlock1(semid); semlock2(semid); printf("\n\n\tPhilosopher %d eating,,",pno); printf("\n\tPress one to start eating.."); scanf("%d",&tmp); semunlock2(semid); printf("\n\n\tPhilosopher %d thinking,,",pno); break; case 2: printf("\n\n\tPhilosopher %d thinking,,",pno); printf("\n\tEnter Zero to start eating,,"); scanf("%d",&tmp); semlock2(semid); semlock0(semid); printf("\n\n\tPhilosopher %d eating,,",pno); printf("\n\tPress One to start eating.."); scanf("%d",&tmp); semunlock2(semid); semunlock0(semid); printf("\n\n\tPhilosopher %d thinking,,",pno); break; }}

*****OUTPUT*****

"dining.c" 48L, 1277C written[mca9@mcaserver2 ~]$ cc dining.c

Page 33: 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

33

[mca9@mcaserver2 ~]$ ./a.out

Enter philosopher no:(0-2) 0

Philosopher 0 thinking Enter 0 to start eating..0

22 WRITE A PROGRAM FOR CREATING A FILE

#include<stdio.h>

#include<sys/types.h>

#include<sys/stat.h>

main()

{

int fid1,fid2,i;

char buff[500];

fid1=open("test",0);

i=read(fid1,buff,100);

creat("sample",S_IRWXU);

fid2=open("sample",2);

write(fid2,buff,i);

close(fid1);

close(fid2);

}

OUTPUT:

[mca9@mcaserver2 ~]$ cc crat.c

[mca9@mcaserver2 ~]$ ./a.out

[mca9@mcaserver2 ~]$ cat sample

Nithya

Divya

Hema

23 WRITE A PROGRAM FOR COPYING A FILE

#include<stdio.h>

#include<sys/types.h>

#include<sys/stat.h>

main(int argc,char *argv[])

Page 34: 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

34

{

int f1,f2,i;

char buff[500];

if(argc!=3)

{printf("error");

exit(1);

}

f1=open(argv[1],0);

i=read(f1,buff,500);

creat(argv[2],S_IRWXU);

f2=open(argv[2],1);

write(f2,buff,i);

close(f1);

close(f2);

}

OUTPUT:

[mca9@mcaserver2 ~]$ cc copy.c

[mca9@mcaserver2 ~]$ ./a.out test sample

[mca9@mcaserver2 ~]$ cat sample

Nithya

Divya

Hema

24 WRITE A PROGRAM FOR FILE FLAGS#include<unistd.h>

#include<fent1.h>

#include<stdio.h>

#include<sys/types.h>

#include<sys/stat.h>

Page 35: 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

35

int main(int argc,char * argv[])

{

if(argc!=2)

{

printf(“wrong no: of arguments”);

exit(1);

}

else

{

struct stat str;

stat(argv[1],&str);

printf(“\n mode is %lu\n”,str.st_mode);

printf(“device type is %lu\n”,str.st_dev);

printf(“mode no: is %lu\n”,str.st_ino);

printf(“no:of links is %lu \n”,str.st_nlink);

printf(“user id is %lu\n”,str.st_uid);

printf((“group id is %lu\n”,str.st_gid);

printf(“device name is %lu\n”,str.st_rdev);

printf((“block size is %lu\n”,str.st_blksize);

printf((“no:of blocks is %lu\n”,str.st_blocks);

}return(0);

}

OUTPUT:

[mca9@[mca9@mcaserver2 ~]$cc fileflag.c

mcaserver2 ~]$ ./a.out test

25 WRITE A PROGRAM FOR FILE LOCKING#include<stdio.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fent1.h>

main()

Page 36: 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

36

{

int a,fd,pid;

fd=open(“aaa”,O_RDWR);

lockf(fd,F_LOCK,0);

printf(“process %d locked file\n”,getpid());

pid=fork();

if(pid==0)

/*parent process should correctly set unmusk(0066 is a good one)*/

fd=open(argv[1],O_WRONGLY|O_CREAT,0660);

if ( fd<0)

{

perror(argv[1]);

exit(1);

}

if(lockf(fd,F_LOCK,0)<0)

{

perror(argv[1]);

exit(1);

}

//n=read(0,bfr,128);

//write(fd,bfr,n)

fprintf(stdout,”locked\n”);

fclose(stdout);

while(fgets(bfr,sizeof(bfr),stdin)){ }

exit(0);}

OUTPUT:[mca9@mcaserver2 ~]$ cc filelock.c[mca9@mcaserver2 ~]$ ./a.outProcess 3034 is locked26 WRITE A PROGRAM FOR FILE TYPES#include<stdio.h>

#include<sys/stat.h>

#include<sys/types.h>

main(int argc,char *argv[])

{

Page 37: 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

37

int i;

struct stat buf;

char *ptr;

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

{

printf("%s",argv[i]);

if(stat(argv[i],&buf)<0)

{

printf("error\n");

continue;

}

}

if(S_ISREG(buf.st_mode))

ptr="regular\n";

else if(S_ISDIR(buf.st_mode))

ptr="directory\n";

else if(S_ISCHR(buf.st_mode))

ptr="character special \n";

else if(S_ISBLK(buf.st_mode))

ptr="block special\n";

else if(S_ISFIFO(buf.st_mode))

ptr="FIFO\n";

else

ptr="unknown";

printf("%s",ptr);

exit(1);

}

OUTPUT:

[mca28@mcaserver2 ~]$ cc test.c

[mca28@mcaserver2 ~]$ ./a.out

regular