Sei sulla pagina 1di 46

RAJA COLLEGE OF ENGINEERING & TECHNOLOGY

Department of Computer Science & Engineering

OS LA !AN"AL

LIST OF E#ERCISES E$erci%e &'( Study of UNIX Commands E$erci%e &)( Shell Script Programs E$erci%e &*( FCFS Scheduling and SJF Scheduling E$erci%e &+( Priority Scheduling and Round Robin Scheduling E$erci%e &,( Producer Consumer Problem E$erci%e &-( Inter process communication using shared memory E$erci%e &.( Memory Management echni!ue I " First fit# $est fit# %orst fit& E$erci%e &/( Memory Management echni!ue II " FIF' # (RU page replacement& E$erci%e &0( Contiguous Memory )llocation& E$erci%e '&( Study of Uni* $asic and I+' System Calls

Prepared by S&J&Subashini , -&.&/esa0an

CODE(CS &+.

O1ERATING SYSTE!S LA !AN"AL

CSE & IT

(Implement the following on LINUX or other Unix like platform. Use C for high level language Implementation) 1& %rite programs using the follo2ing system calls of UNIX operating system3 for4# e*ec# getpid# e*it# 2ait# close# stat# opendir# readdir 5& %rite programs using the I+' System calls of UNIX operating system&6open# read# 2rite# etc7 8& %rite C programs to simulate UNIX commands li4e ls# grep# etc& 9& -i0en the list of processes# their CPU burst times and arri0al times& .isplay+print the -antt chart for FCFS and SJF& For each of the scheduling policies# compute and print the a0erage 2aiting time and a0erage turnaround time& :& -i0en the list of processes# their CPU burst times and arri0al times& .isplay+print the -antt chart for Priority and Round robin& For each of the scheduling policies# compute and print the a0erage 2aiting time and a0erage turnaround time& ;& .e0elop )pplication using Inter"Process"Communication 6Using shared memory# pipes or message !ueues7& <& Implement the Producer"Consumer problem using semaphores6Using UNIX system calls7 =& Implement some Memory management schemes li4e Paging and Segmentation& >& Implement some Memory management schemes li4e FIRS %'RS FI & FI # $?S FI ,

1@& Implement any file allocation techni!ues6Contiguous# (in4ed or Inde*ed7 ?*ample for e*ercises = , > Free space is maintained as a lin4ed list of nodes 2ith each node ha0ing the starting byte address and the ending byte address of a free bloc4& ?ach memory re!uest consists of the process"id and the amount of storage space re!uired in bytes& )llocated memory space is again maintained as a lin4ed list of nodes 2ith each node ha0ing the process"id# starting byte address and the ending byte address of the allocated space& %hen a process finishes 6ta4en as input7 the appropriate node from the allocated list should be deleted and this free space should be added to the free space list 6care should be ta4en to merge contiguous free bloc4s into one single bloc4& his results in deleting more than one node from the from the free space list and changing the start and end address in the appropriate node7& For allocation use first fit# 2orst fit and best fit& otal3 9: Ars Prepared by S&J&Subashini , -&.&/esa0an

E$erci%e '( Stu23 of "NI# Comman2% Aim( o study and e*ecute the basic commands in Uni* 'S installed along 2ith (inu*& A4gorit5m( a7 he Uni* system is logged in through the IP address 1>5&1;=&@&1@@ from Start"BRun& b7 he user enters the user login name as CstudentD and Pass2ord as CstudentD& c7 he system displays the current directory as student6Common .irectory for all user logging in as student7 d7 ?ach command is entered as mentioned in synta* and output for each command is noted do2n& e7 )ny number of files or directories are created during command e*ecution& f7 he session is ended using e*it command Ce*itD& Comman2%( '6 TAS7
CO!!AND SYNTA# E#1LANATION O"T1"T

( o display the system date and time& ( date& ( date& ( his command displays the current system date and time on the screen& ( ue Jun 1> 1138<31< -M 5@@<& ( o display the current month& ( date& ( date EFm& ( his command displays the current month on the screen& ( @;&

)6 TAS7
CO!!AND SYNTA# E#1LANATION O"T1"T

*6 TAS7

( o display the name of the current month& CO!!AND ( date& SYNTA# ( date EFh& E#1LANATION ( his command displays the name of the current month on the screen& O"T1"T ( Jun& ( o display the current system date& ( date& ( date EFd& ( his command displays the current system date on the screen& ( 1>& ( o display the current system date 6year7& ( date& ( date EFy& Prepared by S&J&Subashini , -&.&/esa0an

+6 TAS7
CO!!AND SYNTA# E#1LANATION O"T1"T

,6 TAS7
CO!!AND SYNTA#

E#1LANATION O"T1"T

( his command displays the current year on the screen& ( @<& ( o display the current system time& ( date& ( date EFA& ( his command displays the current system time 6in hours7 on the screen& ( 11& ( o display the current system time& ( date& ( date EFM& ( his command displays the current system time 6in minutes7 on the screen& ( 98& ( o display the current system time& ( date& ( date EFS& ( his command displays the current system time 6in seconds7 on the screen& ( 1:&

-6 TAS7
CO!!AND SYNTA# E#1LANATION O"T1"T

.6 TAS7
CO!!AND SYNTA# E#1LANATION O"T1"T

/6 TAS7
CO!!AND SYNTA# E#1LANATION O"T1"T

06 TAS7

( o display the calendar of the current month& CO!!AND ( calendar& SYNTA# ( cal& E#1LANATION ( his command displays the calendar of the current month on the screen& O"T1"T ( Jun @< S M % F S 1 5 8 9 : ; < = > 1@ 11 15 18 19 1: 1; 1< 1= 1> 5@ 51 55 58 59 5: 5; 5< 5= 5> 8@ ( o display user"defined message& on the screen the argument of the echo
CO!!AND ( echo& SYNTA# ( echo GmessageH& E#1LANATION ( his command displays O"T1"T

'&6 TAS7

command& ( echo G'SH& 'S

Prepared by S&J&Subashini , -&.&/esa0an

''6 TAS7

( o display the details of all users& CO!!AND ( 2ho& SYNTA# ( 2ho& E#1LANATION ( his command lists the information about all the users 2ho ha0e logged on to that system& O"T1"T ( displays all logged in user& ( o display the user detail& CO!!AND ( 2ho& SYNTA# ( 2hoami& E#1LANATION ( his command displays information about the current user of the system on the screen& O"T1"T ( root&
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

')6 TAS7

'*6 TAS7

( o create a directory& ( ma4e directory& ( m4dir& ( his command is used to create a ne2 directory 2ith the specified name& ( m4dir student& ( he directory GstudentH is created&

'+6 TAS7

( o change directory& CO!!AND ( change directory& SYNTA# ( cd directory name& E#1LANATION ( his command is used to s2itch from one directory to another& E#A!1LE ( cd staff& O"T1"T ( he directory GstaffH is s2itched onto& ( o delete a directory& CO!!AND ( remo0e directory& SYNTA# ( rmdir directory name E#1LANATION ( his command is used to delete the specified directory& E#A!1LE ( rmdir student& O"T1"T ( he GstudentH directory is deleted&
CO!!AND SYNTA# E#1LANATION O"T1"T

',6 TAS7

'-6 TAS7

( o come out of a sub"directory& ( change directory& ( cd && ( his command helps in s2itching to the main directory& ( he user comes out of the directory

'.6 TAS7

( o list all the files and directories& CO!!AND ( list& SYNTA# ( ls& E#1LANATION ( his command displays all the files and directories of the system& Prepared by S&J&Subashini , -&.&/esa0an

O"T1"T

( (ist all the directory contents

'/6 TAS7

( o create a file& CO!!AND ( cat& SYNTA# ( catB file name& E#1LANATION ( his command leads to the creation of a ne2 file 2ith the specified file name and contents& E#A!1LE ( catB 2ind& O"T1"T ( ) null file called G2indH is created&
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

'06 TAS7

( o 0ie2 a file& ( cat& ( cat file name& ( his command displays the contents of the specified file& ( cat 2ind& ( Contents of the file called G2indH 2ill be displayed on the screen& ( o copy a file& ( copy& ( cp sourcefile destinationfile& ( his command produces a copy of the source file and is stored in the specified destination file by o0er2riting its pre0ious contents& ( cp sun moon& ( he contents of GsunH file 2ill be copied to the GmoonH file& ( o mo0e a file& ( mo0e& ( m0 sourcefile destinationfile& ( )fter mo0ing the contents of the source file into destination file# the source file is deleted& ( m0 sun moon& ( )fter copying contents from the GsunH file to GmoonH file# the GsunH file is deleted&

)&6 TAS7
CO!!AND SYNTA# E#1LANATION

E#A!1LE O"T1"T

)'6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

))6 TAS7

( o display + cut a column from a file& CO!!AND ( cut& SYNTA# 3 cut Ic no& file name& E#1LANATION ( his command displays the characters of a particular column in the specified file& E#A!1LE ( cut Ic8 moon& O"T1"T ( hose characters occurring in the 8rd column of the file called GmoonH are displayed&
CO!!AND

)*6 TAS7

( o delete a file& ( remo0e& Prepared by S&J&Subashini , -&.&/esa0an

SYNTA# E#1LANATION

E#A!1LE
O"T1"T

( rm file name& ( his command deletes the specified file from the directory& ( rm sun& ( he file called GsunH 2ill be deleted& ( o retrie0e a part of a file& ( head& ( head "no& of ro2s file name& ( his command displays the specified no& of ro2s form the top of the specified file& ( head "1 sun& ( he first ro2 of the file called GsunH is displayed& ( o retrie0e a file& ( tail& ( tail "no& of ro2s file name& ( his command displays the specified no& of ro2s form the bottom of the specified file& ( tail "1 moon& ( he last ro2 of the file called GmoonH is displayed& ( o sort the contents of a file& ( sort& ( sort file name& ( his command helps in sorting the contents of a file in ascending order& ( sort 2in& ( he contents of the file G2inH are displayed on the screen in a sorted order& ( o display the no& of characters in a file& ( 2ord count& ( 2c file name& ( his command displays on the screen the no& of ro2s# 2ords# and the sum of no6 of characters and 2ords& ( 2c ball& ( he no& of ro2s# 2ords# and no& of characters present in the file GballH are displayed& ( o display the calendar of a year& ( cal& ( cal year& ( his command displays on the screen the calendar of the specified year& ( cal 5@@<& ( he calendar of the year 5@@< 2ill be displayed& Prepared by S&J&Subashini , -&.&/esa0an

)+6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

),6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

)-6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

).6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

)/6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

)06 TAS7

( o find the specified file& CO!!AND ( find SYNTA# ( find filename& E#1LANATION ( his command is used to find the specified file& E#A!1LE ( find rose& O"T1"T (& he file is found message 2ill be displayed& ( o display the gi0en message as banner& ( banner ( banner format ( he command is used to display the message in banner format& ( banner %elcome to CS? (& he message %elcome to CS? 2ill be displayed in banner format& ( o )ppend t2o files ( cat ( cat oldfilename BB ne2filename ( he command is used to append one file to another& ( cat pin4 BB orange (& he contents pin46source7 is appended to orange6destination7 ( o find current 2or4ing directory& ( p2d ( p2d ( he command is used to display the full pathname of current directory& ( p2d (&home+student+4esa0 ( o flush the screen ( clear ( clear ( he command is used to clear the screen& ( clear (&screen gets emptied& ( Paste the contents from one file to another& ( paste ( paste filename1 filename5 ( he command is used to paste the contents of a file 0ertically& ( paste rose lilly (&Contents of rose6source7 is pasted into lilly6destination7 ( o find occurance of a string in a file ( grep ( grep string filename Prepared by S&J&Subashini , -&.&/esa0an

*&6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

*'6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

*)6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

**6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

*+6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

*,6 TAS7
CO!!AND SYNTA#

E#1LANATION E#A!1LE O"T1"T

( he command is used to find the specified string 2ithin the file& ( grep CS? 4esa0 (& he string CS? is matched from the file 4esa0& ( o find occurance of a string in current lines in a file ( grep ( grepJc string filename ( he command is used to find current lines that matches string pattern specified& ( grepJc Indian 4esa0 (& he string Indian is matched from the file 4esa0& ( Inputting 0alues from user ( read ( read 0ariablename ( he command is used to get input to a 0ariable ( read a (&-ets the user supplied 0alue and stores in specified 0ariable& ( o Koin t2o common fields of files ( Koin ( Koin filename1 filename5 ( he command is used to e*tract common fields bet2een files and Koin them& ( Koin 4esa0 4annan (&the common fields from 4esa0 and 4annan files are Koined& ( o compare lines in a file ( uni! ( uni!Lc+d+uM filename ( he command is used to compare adKacent lines in a file ( uni! ** (&Sun arises in the east is the uni!ue line in the file ( o match string 2ith a postfi* ( grep ( grep patternN filename ( he command is used to ( grep appleN fruits (&pineapple ( o match string 2ith a prefi* ( grep ( grep O parrern filename ( he command is used to display the line that matches pattern in prefi*& Prepared by S&J&Subashini , -&.&/esa0an

*-6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

*.6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

*/6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

*06 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

+&6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

+'6 TAS7
CO!!AND SYNTA# E#1LANATION

E#A!1LE O"T1"T

( grep Opome fruits (&pomegranate ( o compare t2o files to find differences ( cmp ( cmp filename1 filename5 ( he command is used to compare t2o files& ( cmp ** yy (& generates the first difference bet2een the t2o files# ** and yy& ( o configure protection ( chmod ( chmod ( he command is used to alter the access permissions& ( chmod (&)ccess permission gets altered& ( o display month from calender ( cal ( cal m LyearM ( he command is used to display calendar of specified month ( cal ; 5@@< (& Jun @< M % F S 1 5 9 : ; < = > 11 15 18 19 1: 1; 1= 1> 5@ 51 55 58 5: 5; 5< 5= 5> 8@ ( o display contents of directory ( ll ( ll ( he command is used to list all the files in long list format ( ll (& he contents of the directory is displayed

+)6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

+*6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

++6 TAS7
CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

S 8 1@ 1< 59 +,6 TAS7


CO!!AND SYNTA# E#1LANATION E#A!1LE O"T1"T

Re%u4t( he $asic Uni* commands is e*ecuted in (inu*& 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 8

Prepared by S&J&Subashini , -&.&/esa0an

E$erci%e )( S5e44 Script 1rogram% a9 Sum of N num:er% u%ing ;5i4e 4oop Aim( o 2rite a shell script program for finding the sum of n numbers using 2hile loop& A4gorit5m( a7 'pen the editor in Insert mode using esc E i and start the program& b7 Read the range of numbers to be added from user& c7 Find the sum using 2hile iterati0e structure and display the result& d7 erminate the program& 1rogram( echo G?nter nH read n sum P@ iP@ 2hile L Ni Ile Nn M do sumPQe*pr Nsum E NiQ iPQe*pr Ni E 1Q done echo GSum of Nn number is NsumH Re%u4t( hus sum of n numbers is computed using 2hile iteration& :9 Sum of N num:er% u%ing for 4oop Aim( o 2rite a shell script program for finding the sum of n numbers using for loop& A4gorit5m( a7 'pen the editor in Insert mode using esc E i and start the program& b7 Read the range of numbers to be added from user& c7 Find the sum using for iterati0e structure and display the result& d7 erminate the program& 1rogram( echo G?nter nH read n sum P@ iP@ for i in Qse! 1 NnQ do Prepared by S&J&Subashini , -&.&/esa0an

sumPQe*pr Nsum E NiQ iPQe*pr Ni E 1Q done echo GSum of Nn number is NsumH Re%u4t( hus sum of n numbers is computed using for iteration& c9 Sum of N num:er% u%ing unti4 4oop Aim( o 2rite a shell script program for finding the sum of n numbers using until loop& A4gorit5m( a7 'pen the editor in Insert mode using esc E i and start the program& b7 Read the range of numbers to be added from user& c7 Find the sum using until iterati0e structure and display the result& d7 erminate the program& 1rogram( echo G?nter nH read n sum P@ iP@ until L Nn Ile Ni M do sumPQe*pr Nsum E NiQ iPQe*pr Ni E 1Q done echo GSum of Nn number is NsumH Re%u4t( hus sum of n numbers is computed using until iteration& 29 Factoria4 of N num:er% Aim( o 2rite a shell script program for finding the factorial of n numbers& A4gorit5m( a7 'pen the editor in Insert mode using esc E i and start the program& b7 Read the 0alue of n from user till 2hich factorial is computed& c7 Find the factorial using for loop structure and display the result& d7 erminate the program& 1rogram( fact P1 KP1 Prepared by S&J&Subashini , -&.&/esa0an

echoH?nter the numberH read n 2hile L NK Ile Nn M do factPQe*pr Ni RS NfactQ KPQe*pr NK E 1Q done echo G he factorial of Nn is NfactH Re%u4t( hus factorial for n numbers is computed using shell script& e9 Fi:onacii Se<uence Aim( o 2rite a shell script program for print the fibonacii Se!uence& A4gorit5m( a7 'pen the editor in Insert mode using esc E i and start the program& b7 Read the number# n# from user# that number of se!uence generated& c7 .isplay the se!uence using 2hile iteration& d7 erminate the program& 1rogram( echo G?nter the limitH read n cP@ aP@ bP1 echo G he Fibonacii Series isH echo GNaH echo GNbH iP8 2hile L Ni Ile Nn M do cPQe*pr Na E NbQ aPNb bPNc echo GNcH iPQe*pr Ni E 1Q done Re%u4t( hus fibonacii se!uence is printed using shell script& f9 Stu2ent !ar= 4i%t Aim( o 2rite a shell script program for computing Student Mar4 list&

Prepared by S&J&Subashini , -&.&/esa0an

A4gorit5m( a7 'pen the editor in Insert mode using esc E i and start the program& b7 Read the 0alues for 0ariables student# name# m1# m5 and m8 from user& c7 Calculate the total and a0erage and display the result& d7 erminate the program& 1rogram( echo G ?nter number of the StudentH read n iP1 tP1 2hile L Ni Ilt Nn M do echo G?nter a Student nameH read name echo G?nter the Student register numberH read rollno KP1 totP@ 2hile L NK Ile 5 M do echo G?nter the mar4 NKH read s tot PQe*pr Ntot E NsQ KPQe*pr NK E 1Q done echo GName3 NnameH echo GRegister Number3 NrollnoH echo G otal3 NtotH a0gPQe*pr Ntot+5Q echo G)0erage3 Na0gH iPQe*pr Ni E 1Q done Re%u4t( hus student mar4 list is computed using shell script& g9 !enu 2ri>en program Aim( o 2rite a shell script program for implementing Menu dri0en options& A4gorit5m( a7 'pen the editor in Insert mode using esc E i and start the program& b7 .isplay the choices to the user& c7 Input the choice through 0ariable n from user& d7 Use Case conditional branching instruction# to branch to the e*act choice& Prepared by S&J&Subashini , -&.&/esa0an

e7 .isplay the result respecti0e to the choice chosen& f7 erminate the program& 1rogram( echo G?nter your ChoiceH echo G1&.ate 5&%or4ing .irectory 8&(ogged Users 9&Current UserH echo G:&Copy File ;&Mo0e File <&Aead , ail =&Pattern MatchingH read n case Nn in 17dateTT 57p2dTT 872hoTT 972ho am iTT :7 echo G?nter the Source File NameH read a echo G?nter the .estination File NameH read b cp Na Nb echo GNa File is copied to Nb FileH TT ;7 echo G?nter the Source File NameH read a echo G?nter the .estination File NameH read b m0 Na Nb echo GNa File is copied to Nb FileH TT <7 echo G?nter the File NameH read a echo GAeadH head "1 Na echo G ailH tail "5 Na TT =7 echo G?nter the File NameH read a echo G?nter the Matching StringH read b echo G he Matched String isH grep GNbH Na TT esac Re%u4t( hus menu dri0en option is implemented using shell script& 59 Arm%trong num:er% Prepared by S&J&Subashini , -&.&/esa0an

Aim( o 2rite a shell script program for chec4ing for )rmstrong numbers& A4gorit5m( a7 'pen the editor in Insert mode using esc E i and start the program& b7 Read the 0alue of n from user# 2hich must be 0alidated for )rmstrong& c7 Sum the segregated cube of indi0idual digits of n from least significant digit using 2hile iterati0e structure& d7 Chec4 the sum 2ith user entered number& e7 If same display C)rmstrongD else display CNot )rmstrongD& f7 erminate the program& 1rogram( echo G?nter then numberH read n bPNn sP@ 2hile L Nn Igt @ M do aPQe*pr Nn F 1@Q sPQe*pr Ns E Na RS Na RS NaQ nPQe*pr Nn + 1@Q done if L Ns Ie! Nb M then echo G he Nb is )rmstrong NumberH else echo G he Nb is not )rmstrong NumberH fiT Re%u4t( hus armstrong number chec4ing is implemented using shell script& i9 1rime num:er% Aim( o 2rite a shell script program for chec4ing for prime numbers& A4gorit5m( a7 'pen the editor in Insert mode using esc E i and start the program& b7 Input range from user# till 2hich count of prime numbers displayed& c7 Chec4 for .i0isibility 2ithin the range& d7 .i0isibility is determined if remainder is Uero& e7 If not di0isible then count is incremented& f7 .isplay the count to the user& g7 erminate the program& 1rogram( Prepared by S&J&Subashini , -&.&/esa0an

echo G?nter the 0alue for n H read n iP5 flagP@ *PQe*pr Nn+5Q 2hile L Ni Ile N* M do if L Qe*pr Nn F NiQ "e! @ M then flagP1 fi iPQe*pr Ni E 1Q done if L Nflag Ie! @ M then echo G he Nn number is prime NumberH else echo G he Nn number is not a prime NumberH fiT Re%u4t( hus prime number chec4ing is implemented using shell script& ?9 Re>er%e t5e %tring Aim( o 2rite a shell script program for re0ersing a gi0en string& A4gorit5m( a7 'pen the editor in Insert mode using esc E i and start the program& b7 Input the string from user& c7 Find the length of the string by tra0ersing the string till last character& d7 Segregate indi0idual characters# using cut function# starting from end of the string& e7 )dd the splitted characters one by one to an empty string& f7 .isplay the re0ersed string to user& g7 erminate the program& 1rogram( echo G?nter the StringH read s lPQe*pr Ns R2c IcQ echo GNlH 2hile L Nl Ine @ M do ch PQe*pr Ns Rcut Ic NlQ echo GNchH re0PQe*pr Nre0 NchQ Prepared by S&J&Subashini , -&.&/esa0an

lPQe*pr Nl I 1Q done echo G he Re0ersed String is3 Nre0H Re%u4t( hus re0ersing the string is implemented using shell script& 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 8 E$erci%e *( FCFS Sc5e2u4ing an2 SJF Sc5e2u4ing a9 Fir%t Come Fir%t Ser>e2 Sc5e2u4ing Aim( o 2rite a program for First Come First Ser0ed CPU Scheduling algorithm& A4gorit5m( a7 Create array structures to store process name# 2aiting time# turn around time and burst time& b7 -et the number of Kobs# process name and burst time from user& c7 InitialiUe time factors as Uero& d7 Calculate 2aiting time# turn around time for each process& e7 )0erage 2aiting time# )0erage turn around time is also computed and display to the user& 1rogram( VincludeWstdio&hB VincludeWconio&hB 0oid main67 X float a2#atT int bL1@M#I#2L1@M#tL1@M#2t#tt#nT printf6G?nter the number of process 2ithin 1@3H7T scanf6GFdH#,n7T for6iP1TiWPnTiEE7 X printf6G?nter the burst time for the process FdH#i7T scanf6FdH#,bLiM7T Y 2tP@T ttP@T tL1@MP@T printf6GRn Process no Rt $urst time Rt %aiting time Rt urn around timeH7T for6iP1TiWPnTiEE7 X Prepared by S&J&Subashini , -&.&/esa0an

2LiM PtLi"1MT tLiMP2LiMEbLiMT 2tP2tE2LiMT ttPttEtLiMT printf6GRnFdRtRtFdRtRtFdRtRtFdRnH#i#bLiM#2LiM#tLiM7T Y a2P2t+nT atPtt+nT printf6G)0erage %aiting ime PFf Rn )0erage urn )round ime PFfH#a2#at7T getch67T Y Re%u4t( hus the inputted process are scheduled using FCFS algorithm& :9 S5orte%t Jo: Fir%t Sc5e2u4ing Aim( o 2rite a program for Shortest Job First CPU Scheduling algorithm& A4gorit5m( a7 Create array structures to store process name# 2aiting time# turn around time and burst time& b7 -et the number of Kobs# process name and burst time from user& c7 InitialiUe time factors as Uero& d7 Sort the burst time in ascending order& e7 Calculate 2aiting time# turn around time for each process& f7 )0erage 2aiting time# )0erage turn around time is also computed and display to the user& 1rogram( VincludeWstdio&hB VincludeWconio&hB 0oid main67 X float a2#atT int bL1@M#i#2L1@M#tL1@M#2t#tt#n#tp#tb#aL1@M#KT printf6G?nter the number of process 2ithin 1@3H7T scanf6GFdH#,n7T for6iP1TiWPnTiEE7 X aLiMPiT printf6G?nter the burst time for the process FdH#i7T scanf6FdH#,bLiM7T Y 2tP@T Prepared by S&J&Subashini , -&.&/esa0an

ttP@T tL1@MP@T for6iP1TiWPnTiEE7 X for6KPiE1TKWPnTKEE7 X if6bLiMBbLKM7 X tbPbLiMT bLiMPbLKMT bLKMPtbT tpPaLiMT aLiMPaLKMT aLKMPtpT Y Y Y printf6GRn Process no Rt $urst time Rt %aiting time Rt urn around timeH7T for6iP1TiWPnTiEE7 X 2LiM PtLi"1MT tLiMP2LiMEbLiMT 2tP2tE2LiMT ttPttEtLiMT printf6GRnFdRtRtFdRtRtFdRtRtFdRnH#i#bLiM#2LiM#tLiM7T Y a2P2t+nT atPtt+nT printf6G)0erage %aiting ime PFf Rn )0erage urn )round ime PFfH#a2#at7T getch67T Y Re%u4t( hus the inputted process are scheduled using SJF algorithm& 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 8 E$erci%e +( 1riorit3 Sc5e2u4ing an2 Roun2 Ro:in Sc5e2u4ing a9 1riorit3 Sc5e2u4ing Aim( o 2rite a program for Priority CPU Scheduling algorithm& A4gorit5m( a7 Create array structures to store process name# 2aiting time# turn around time and burst time& Prepared by S&J&Subashini , -&.&/esa0an

b7 -et the number of Kobs# process name and burst time from user& c7 InitialiUe time factors as Uero& d7 Sort the burst time in ascending order based on priority number& e7 Calculate 2aiting time# turn around time for each process& f7 )0erage 2aiting time# )0erage turn around time is also computed and display to the user& 1rogram( VincludeWstdio&hB VincludeWconio&hB 0oid main67 X float a2#atT int bL1@M#i#2L1@M#tL1@M#2t#tt#n#tp#tb#aL1@M#K#pL1@MT printf6G?nter the number of process 2ithin 1@3H7T scanf6GFdH#,n7T for6iP1TiWPnTiEE7 X aLiMPiT printf6G?nter the $urst time for the process FdH#i7T scanf6FdH#,bLiM7T printf6G?nter the Priority for the process FdH#i7T scanf6FdH#,pLiM7T Y 2tP@T ttP@T tL1@MP@T for6iP1TiWPnTiEE7 X for6KPiE1TKWPnTKEE7 X if6pLiMBpLKM7 X tbPbLiMT bLiMPbLKMT bLKMPtbT tpPaLiMT aLiMPaLKMT aLKMPtpT Y Y Y printf6GRn Process no Rt $urst time Rt %aiting time Rt urn around timeH7T for6iP1TiWPnTiEE7 X Prepared by S&J&Subashini , -&.&/esa0an

2LiM PtLi"1MT tLiMP2LiMEbLiMT 2tP2tE2LiMT ttPttEtLiMT printf6GRnFdRtRtFdRtRtFdRtRtFdRnH#i#bLiM#2LiM#tLiM7T Y a2P2t+nT atPtt+nT printf6G)0erage %aiting ime PFf Rn )0erage urn )round ime PFfH#a2#at7T getch67T Y Re%u4t( hus the inputted process are scheduled using Priority algorithm& :9 Roun2 Ro:in Sc5e2u4ing Aim( o 2rite a program for Round Robin CPU Scheduling algorithm& A4gorit5m( a7 Create a structure to store process name# 2aiting time# turn around time and burst time of indi0idual process& b7 -et the number of Kobs# time slice# process name and burst time from user& c7 If the process has burst time less than time !uantum then schedule the process and mo0e to ne*t process& d7 If the process has burst time greater than time !uantum then schedule the process till time !uantum and mo0e to ne*t process& e7 InitialiUe time factors as Uero& f7 Calculate 2aiting time# turn around time for each process& g7 )0erage 2aiting time# )0erage turn around time is also computed and display to the user& 1rogram( VincludeWstdio&hB VincludeWconio&hB struct rr X char pnameL8@MT int 2t#bt#tt#rbt#lt#flagT YrrL;@MT float at#a2#s1P@#s5P@T int i#n1P@#nP@#*P@#t!T 0oid get67 X Prepared by S&J&Subashini , -&.&/esa0an

printf6GRn?nter the total number of KobsH7T scanf6GFdH#,n7T printf6GRn?nter the time Zuantum for Round Robin SchedulingH7T scanf6GFdH#,t!7T for6iP1TiWPnTiEE7 X printf6HRn ?nter the process name3H7T scanf6GFdH#,rrLiM&pname7T printf6GRn ?nter the burst time3H7T scanf6GFdH#,rrLiM&bt7T rrLiM&rbtPrrLiM&btT rrLiM&flagPrrLiM&ltP@T Y Y 0oid process67 X 2hile6n1[Pn7 X for6iP1TiWPnTiEE7 X if6rrLiM&flag[P17 X if6rrLiM&rbtBPt!7 X rrLiM&rbtPrrLiM&rbt"t!T *P*Et!T Y else X *P*ErrLiM&rbtT rrLiM&rbtPrrLiM&rbt"t!T Y if6rrLiM&rbtW17 X rrLiM&flagP1T n1EET Y rrLiM&ttP*T Y Y Y for6iP1TiWPnTiEE7 X rrLiM&2tPrrLiM&tt"rrLiM&btT rrLiM&ttPrrLiM&2tErrLiM&btT Prepared by S&J&Subashini , -&.&/esa0an

s1Ps1ErrLiM&2tT s5Ps5ErrLiM&ttT Y a2Ps1+nT atPs5+nT Y 0oid display67 X for6iP1TiWPnTiEE7 X printf6GFsRtFdRtRtFdRtRtFdH#rrLiM&pname#rrLiM&bt#rrLiM&2t#rrLiM&tt7T printf6GRn )0erage %aiting ime PFfH#a27T printf6GRn )0erage urn )round ime PFfH#at7T Y Y 0oid main67 X clrscr67T get67T process67T disp67T getch67T Y Re%u4t( hus the inputted process are scheduled using Round Robin algorithm& 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 8 E$erci%e ,( 1ro2ucer Con%umer 1ro:4em Aim( o implement the Producer"Consumer Problem using Semaphores& A4gorit5m( a7 ypedef a 0ariable GsumH as integer and implement up and do2n function 2hich simulate signal and 2ait procedure& b7 ) buffer 0ariable for producer and consumer are 2ritten& c7 In producer# buffer full condition is chec4ed and if it is full then it is displayed& d7 In consumer# buffer empty condition is chec4ed and a message is displayed& e7 $oth the producer67 and consumer67 program produces are randomly from main67& f7 he produce67 and Consumer67 produces messages are displayed till Prepared by S&J&Subashini , -&.&/esa0an

the user !uits the program& 1rogram( VincludeWstdio&hB VincludeWconio&hB VincludeWtime&hB Vdefine M)X 8 typedef int semT sem bufferP@T int numP1T int up6semS s7 X if6SsBPM)X7 return "1T SsPSsE1T return @T Y int do2n6semS s7 X if6SsWP@7 return "1T SsPSs"1T return @T Y 0oid producer67 X if6up6,buffer7PP"17 X printf6GRn$uffer Full& Producer %aiting[[[H7T returnT Y printf6GRnProduced FdH#numEE7T returnT Y 0oid consumer67 X if6do2n6,buffer7PP"17 X printf6GRn$uffer ?mpty& Consumer %aiting[[[H7T returnT Prepared by S&J&Subashini , -&.&/esa0an

Y printf6GRnConsumedFdH#Num"6bufferE177T return Y int main67 X int i#aP@#KT srand66unsigned7time6@77T 2hile617 X if6rand67F1@BP:7 producer67T else consumer67T for6iP1TiWP>>@@@@TiEE7 X aP@T aEET a""T aPi+1:@T Y for6KP1TKWP1@@TKEE7 X for6iP1TiWP>>@@@@TiEE7 X aP@T aEET a""T aPi+1:@T Y Y Y return @T Y Re%u4t( hus Producer"Consumer Problem is implemented using Semaphores& 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 8 E$erci%e -( Inter 1roce%% Communication "%ing S5are2 !emor3 Aim( o 2rite a program to implement a shared memory A4gorit5m( a7 Start the process b7 .eclare the siUe and data 0ariables Prepared by S&J&Subashini , -&.&/esa0an

c7 d7 e7 f7 g7 h7 i7 K7 47 l7

-et the number of processes to be inserted -et the 0alue Start and identify the process 2ith process id Ma4e a common function to communicate 2ith each process running in shared memory Ma4e the 0ariables and arguments to be a global 2hile in communication %rite a separate routine for creating memory delete memory in separate region -i0e permission that a process can 4ill shared memory .isplay the 0alues Stop the process

1rogram( VincludeWsys+types&hB VincludeWsys+shm&hB VincludeWsys+ipc&hB main67 X int shmidT 4eyJt 4eyP@*1@T shmidPshmget64ey#1@@#IPCJCR?) \@;;;7T if6 shmid W @ 7 printf6]RnFirst SAMI. failedRn]7T else printf6]RnFirst SAMI. Succeded idPFd Rn]#shmid7T shmidPshmget64ey#1@1#IPCJCR?) \@;;;7T if6shmidW@7 printf6]RnSecond SAMI. failedRn]7T else printf6]RnSecond SAMI. Succeded idPFd Rn]#shmid7T shmidPshmget64ey#>@#IPCJCR?) \@;;;7T if6shmidW@7 printf6]Rn hird SAMI. failedRn]7T else printf6]Rn hird SAMI. Succeded idPFd Rn]#shmid7T Y Re%u4t( he program for interprocess communication using shared memory 2as implemented and hence 0erified& 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 8 E$erci%e .( !emor3 !anagement Tec5ni<ue I u%ing Fir%t@ e%t@ Aor%t fit6 a9 Fir%t fit A4gorit5m Aim( o 2rite a program to implement First fit page replacement algorithm Prepared by S&J&Subashini , -&.&/esa0an

A4gorit5m( a7 Start the process b7 .eclare the siUe c7 -et the number of processes to be inserted d7 )llocate the first hole that is big enough searching e7 Start at the beginning of the set of holes f7 If not start at the hole that is sharing the per0ious first fit search end g7 Compare the hole h7 if large enough then stop searching in the procedure i7 .isplay the 0alues K7 Stop the process 1rogram( VincludeWstdio&hB VincludeWprocess&hB 0oid main67 X int aL5@M#pL5@M#i#K#n#mT printf6]?nter no of $loc4s&Rn]7T scanf6]Fd]#,n7T for6iP@TiWnTiEE7 X printf6]?nter the Fdst $loc4 siUe3]#i7T scanf6]Fd]#,aLiM7T Y printf6]?nter no of Process&Rn]7T scanf6]Fd]#,m7T for6iP@TiWmTiEE7 X printf6]?nter the siUe of Fdst Process3]#i7T scanf6]Fd]#,pLiM7T Y for6iP@TiWnTiEE7 X for6KP@TKWmTKEE7 X if6pLKMWPaLiM7 X printf6] he Process Fd allocated to FdRn]#K#aLiM7T pLKMP1@@@@T brea4T Y Y Y Prepared by S&J&Subashini , -&.&/esa0an

for6KP@TKWmTKEE7 X if6pLKM[P1@@@@7 X printf6] he Process Fd is not allocatedRn]#K7T Y Y Y Re%u4t( he program for First fit 2as implemented and hence 0erified&

:9

e%t fit A4gorit5m Aim( o 2rite a program to implement $est fit page replacement algorithm A4gorit5m( a7 Start the process b7 .eclare the siUe c7 -et the number of processes to be inserted d7 )llocate the best hole that is small enough searching e7 Start at the best of the set of holes f7 If not start at the hole that is sharing the per0ious best fit search end g7 Compare the hole h7 If small enough then stop searching in the procedure i7 .isplay the 0alues K7 Stop the process 1rogram( VincludeWstdio&hB VincludeWprocess&hB 0oid main67 X int aL5@M#pL5@M#i#K#n#m#temp#bL5@M#temp1#temp5#cL5@MT printf6]?nter no of $loc4s&Rn]7T scanf6]Fd]#,n7T for6iP@TiWnTiEE7 X printf6]?nter the Fdst $loc4 siUe3]#i7T scanf6]Fd]#,aLiM7T bLiMPiT Y printf6]?nter no of Process&Rn]7T scanf6]Fd]#,m7T for6iP@TiWmTiEE7 Prepared by S&J&Subashini , -&.&/esa0an

X printf6]?nter the siUe of Fdst Process3]#i7T scanf6]Fd]#,pLiM7T cLiMPiT Y for6iP@TiWnTiEE7 X for6KP@TKWmTKEE7 X if6aLiMWaLKM7 X tempPaLiMT temp1PbLiMT aLiMPaLKMT bLiMPbLKMT aLKMPtempT bLKMPtemp1T Y if6pLiMWpLKM7 X tempPpLiMT temp5PcLiMT pLiMPpLKMT cLiMPcLKMT pLKMPtempT cLKMPtemp5T Y Y Y for6iP@TiWnTiEE7 X for6KP@TKWmTKEE7 X if6pLKMWPaLiM7 X printf6] he Process Fd allocated to $loc4 FdRn]#cLKM#bLiM7T pLKMP1@@@@T brea4T Y Y Y for6KP@TKWmTKEE7 X if6pLKM[P1@@@@7 X Prepared by S&J&Subashini , -&.&/esa0an

printf6] he Process Fd is not allocatedRn]#K7T Y Y Y Re%u4t( he program for $est fit 2as implemented and hence 0erified& c9 Aor%t fit A4gorit5m Aim( o 2rite a program to implement %orst fit page replacement algorithm A4gorit5m( a7 Start the process b7 .eclare the siUe c7 -et the number of processes to be inserted d7 )llocate the best hole that is small enough searching e7 Start at the best of the set of holes f7 If not start at the hole that is sharing the per0ious best fit search end g7 Compare the hole h7 If small enough then stop searching in the procedure i7 .isplay the 0alues K7 Stop the process 1rogram( VincludeWstdio&hB VincludeWprocess&hB 0oid main67 X int aL5@M#pL5@M#i#K#n#m#temp#bL5@M#temp1#temp5#cL5@MT printf6]?nter no of $loc4s&Rn]7T scanf6]Fd]#,n7T for6iP@TiWnTiEE7 X printf6]?nter the Fdst $loc4 siUe3]#i7T scanf6]Fd]#,aLiM7T bLiMPiT Y printf6]?nter no of Process&Rn]7T scanf6]Fd]#,m7T for6iP@TiWmTiEE7 X printf6]?nter the siUe of Fdst Process3]#i7T scanf6]Fd]#,pLiM7T cLiMPiT Y for6iP@TiWnTiEE7 Prepared by S&J&Subashini , -&.&/esa0an

X for6KP@TKWmTKEE7 X if6aLiMWaLKM7 X tempPaLiMT temp1PbLiMT aLiMPaLKMT bLiMPbLKMT aLKMPtempT bLKMPtemp1T Y if6pLiMWpLKM7 X tempPpLiMT temp5PcLiMT pLiMPpLKMT cLiMPcLKMT pLKMPtempT cLKMPtemp5T Y Y Y for6iP@TiWnTiEE7 X for6KP@TKWmTKEE7 X if6pLKMWPaLiM7 X printf6] he Process Fd allocated to $loc4 FdRn]#cLKM#bLiM7T pLKMP1@@@@T brea4T Y Y Y for6KP@TKWmTKEE7 X if6pLKM[P1@@@@7 X printf6] he Process Fd is not allocatedRn]#K7T Y Y Y Re%u4t( he program for %orst fit 2as implemented and hence 0erified& Prepared by S&J&Subashini , -&.&/esa0an

88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 8 E$erci%e /( FIFO & LR" 1age Rep4acement A4gorit5m a9 Imp4ementation of FIFO 1age Rep4acement A4gorit5m Aim( o 2rite a program to implement FIF' page replacement algorithm

A4gorit5m( a7 Start the process b7 .eclare the siUe 2ith respect to page length c7 Chec4 the need of replacement from the page to memory d7 Chec4 the need of replacement from old page to ne2 page in memory e7 Forma !ueue to hold all pages f7Insert the page re!uire memory into the !ueue g7 Chec4 for bad replacement and page fault h7 -et the number of processes to be inserted i7 .isplay the 0alues K7 Stop the process 1rogram( VincludeWstdio&hB int main67 X int i#K#n#aL:@M#frameL1@M#no#4#a0ail#countP@T printf6]Rn ?N ?R A? NUM$?R 'F P)-?S3Rn]7T scanf6]Fd]#,n7T printf6]Rn ?N ?R A? P)-? NUM$?R 3Rn]7T for6iP1TiWPnTiEE7 scanf6]Fd]#,aLiM7T printf6]Rn ?N ?R A? NUM$?R 'F FR)M?S 3]7T scanf6]Fd]#,no7T for6iP@TiWnoTiEE7 frameLiMP "1T KP@T printf6]Rtref stringRt page framesRn]7T for6iP1TiWPnTiEE7 X printf6]FdRtRt]#aLiM7T a0ailP@T for64P@T4WnoT4EE7 if6frameL4MPPaLiM7 a0ailP1T if 6a0ailPP@7 X frameLKMPaLiMT Prepared by S&J&Subashini , -&.&/esa0an

KP6KE17FnoT countEET for64P@T4WnoT4EE7 printf6]FdRt]#frameL4M7T Y printf6]Rn]7T Y printf6]Page Fault Is Fd]#count7T return @T Y Re%u4t( he program for FIF' page replacement 2as implemented and hence 0erified& 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 8 :9Imp4ementation of LR" 1age Rep4acement A4gorit5m Aim( o 2rite a program a program to implement (RU page replacement algorithm A4gorit5m( a7 Start the process b7 .eclare the siUe c7 -et the number of pages to be inserted d7 -et the 0alue e7 .eclare counter and stac4 f7 Select the least recently used page by counter 0alue g7 Stac4 them according the selection& h7 .isplay the 0alues i7 Stop the process 1rogram( VincludeWstdio&hB main67 X int !L5@M#pL:@M#cP@#c1#d#f#i#K#4P@#n#r#t#bL5@M#c5L5@MT printf6]?nter no of pages3]7T scanf6]Fd]#,n7T printf6]?nter the reference string3]7T for6iP@TiWnTiEE7 scanf6]Fd]#,pLiM7T printf6]?nter no of frames3]7T scanf6]Fd]#,f7T !L4MPpL4MT printf6]RnRtFdRn]#!L4M7T cEET Prepared by S&J&Subashini , -&.&/esa0an

4EET for6iP1TiWnTiEE7 X c1P@T for6KP@TKWfTKEE7 X if6pLiM[P!LKM7 c1EET Y if6c1PPf7 X cEET if64Wf7 X !L4MPpLiMT 4EET for6KP@TKW4TKEE7 printf6]RtFd]#!LKM7T printf6]Rn]7T Y else X for6rP@TrWfTrEE7 X c5LrMP@T for6KPi"1TKWnTK""7 X if6!LrM[PpLKM7 c5LrMEET else brea4T Y Y for6rP@TrWfTrEE7 bLrMPc5LrMT for6rP@TrWfTrEE7 X for6KPrTKWfTKEE7 X if6bLrMWbLKM7 X tPbLrMT bLrMPbLKMT bLKMPtT Y Y Prepared by S&J&Subashini , -&.&/esa0an

Y for6rP@TrWfTrEE7 X if6c5LrMPPbL@M7 !LrMPpLiMT printf6]RtFd]#!LrM7T Y printf6]Rn]7T Y Y Y printf6]Rn he no of page faults is Fd]#c7T Y Re%u4t( he program for (RU page replacement 2as implemented and 0erified 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 8 E$erci%e &0( Contiguou% !emor3 A44ocation6 Aim( o 2rite a program a program to implement contiguous memory allocation& A4gorit5m( a7 Start the program and input the siUe of hole# process etc from user& b7 )llocate the process if less than hole siUe and add the process to running array& c7 %ait the process if siUe more than hole and add the process to 2aiting array& d7 %hen running process completes# resume the allocated space& e7 Mo0e the 2aiting array to process array and try to allocate the process& f7 )llocation is made continuous here& g7 erminate the program& 1rogram( VincludeWstdio&hB VincludeWprocess&hB 0oid main67 X int aL5@M#pL5@M#i#K#n#m#yP@#UP@#runningL5@M#2aitingL5@M#bL5@MT printf6]?nter siUe of hole&Rn]7T scanf6]Fd]#,n7T printf6]?nter no of Process&Rn]7T scanf6]Fd]#,m7T for6iP@TiWmTiEE7 X aLiMPiT printf6]?nter the siUe of Fdst Process3]#i7T scanf6]Fd]#,pLiM7T Prepared by S&J&Subashini , -&.&/esa0an

Y 2hile6m7 X for6iP@TiWmTiEE7 X if6pLKMWPn7 X printf6] he Process Fd is allocated Rn]#aLiM7T nPn"pLiMT runningLyMPaLiMT yEET Y else X printf6G he Process Fd is 2aitingRnH#aLiM7T 2aitingLUMPaLiMT bLUMPpLiMT UEET Y Y for6iP@TiWyTiEE7 X printf6] he Process Fd erminatedRn]#runningLiM7T nPnEpLiMT m""T Y for6iP@TiWmTiEE7 pLiMP@T for6iP@TiWmTiEE7 pLiMPbLiMT UP@T yP@T Y Y Re%u4t( he program for Contiguous Memory allocation 2as implemented and hence 0erified& 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 8 E$erci%e '&( Stu23 of "ni$ a%ic & IBO S3%tem Ca44%

Prepared by S&J&Subashini , -&.&/esa0an

T5e for=C9 & getpi2C9 S3%tem Ca44 System call for=C9 is used to create processes& It ta4es no arguments and returns a process I.& he purpose of for=C9 is to create a ne; process# 2hich becomes the child process of the caller& )fter a ne2 child process is created# :ot5 processes 2ill e*ecute the ne*t instruction follo2ing the for=C9 system call& herefore# 2e ha0e to distinguish the parent from the child& his can be done by testing the returned 0alue of for=C93

If for=C9 returns a negati0e 0alue# the creation of a child process 2as unsuccessful& for=C9 returns a Uero to the ne2ly created child process& for=C9 returns a positi0e 0alue# the proce%% ID of the child process# to the parent& he returned process I. is of type pi2Dt defined in %3%Bt3pe%65& Normally# the process I. is an integer& Moreo0er# a process can use function getpi2C9 to retrie0e the process I. assigned to this process&

herefore# after the system call to for=C9# a simple test can tell 2hich process is the child& 14ea%e note t5at "ni$ ;i44 ma=e an e$act cop3 of t5e parentE% a22re%% %pace an2 gi>e it to t5e c5i426 T5erefore@ t5e parent an2 c5i42 proce%%e% 5a>e %eparate a22re%% %pace%& ^oid main67 X PidJt pidT for467T pidPgetpid67T if6pid PP "17 printf6GRn ?rror in creating process G7T else if6pid PP @7 printf6]Rn?*ecuting in child process# pidPFd and its parent pid P Fd ]# getpid67#getppid677T else printf6]Rn?*ecuting in parent process#pidPFd Rn]#getppid677T Y T5e e$ecC9 S3%tem Ca44 he e$ec functions of Uni*"li4e operating systems are a collection of functions that causes the running process to be completely replaced by the program passed as argument to the function& )s a ne2 process is not created# the process I. 6PI.7 does not change across an e*ecute# but the data# heap and stac4 of the calling process are replaced by those of the ne2 process& int main6int argc# char Sarg0LM7 X e*ec0p6arg0L1M# ,arg0L1M7T Prepared by S&J&Subashini , -&.&/esa0an

perror6]e*ec failure]7T e*it617T Y T5e ;aitC9 S3%tem Ca44

) parent process usually needs to synchroniUe its actions by 2aiting until the child process has either stopped or terminated its actions& he 2ait67 system call allo2s the parent process to suspend its acti0ities until one of these actions has occurred& he 2ait67 system call accepts a single argument# 2hich is a pointer to an integer and returns a 0alue defined as type pidJt& If the calling process does not ha0e any child associated 2ith it# 2ait 2ill return immediately 2ith a 0alue of "1& If any child processes are still acti0e# the calling process 2ill suspend its acti0ity until a child process terminates&

^oid main67 X int statusT pidJt pidT pid P for467T if6pid PP "17 printf6GRn?RR'R child not created G7T else if 6pid PP @7 +S child process S+ X printf6]Rn I_m the child[]7T e*it6@7T Y else +S parent process S+ X 2ait6,status7T printf6]Rn I_m the parent[]7 printf6]Rn Child returned3 FdRn]# status7 Y Y T5e %tatC9 S3%tem Ca44

Prepared by S&J&Subashini , -&.&/esa0an

here are a number of system calls that a process can use to obtain file information& he most useful one is ]stat] system call& he stat67 system call is used to obtain file information& Its prototype is li4e this3 int %tatCcon%t c5ar Ffi4eDname@ %truct %tat F:uf9 he %tat structure is a pre"defined structure# 2hich contains the follo2ing fields3 %truct %tat G de0Jt stJde0T +S de0ice S+ inoJt stJinoT +S inode S+ modeJt stJmodeT +S protection S+ nlin4Jt stJnlin4T +S number of hard lin4s S+ uidJt stJuidT +S user I. of o2ner S+ gidJt stJgidT +S group I. of o2ner S+ de0Jt stJrde0T +S de0ice type 6if inode de0ice7 S+ offJt stJsiUeT +S total siUe# in bytes S+ bl4siUeJt stJbl4siUeT +S bloc4siUe for filesystem I+' S+ bl4cntJt stJbloc4sT +S number of bloc4s allocated S+ timeJt stJatimeT +S time of last access S+ timeJt stJmtimeT +S time of last modification S+ timeJt stJctimeT +S time of last change S+ HI const int NJ$I S P 8T int main6int argc# char Sarg0L M7 X unsigned int mas4 P @<@@T struct stat buffT static char SpermLM P X]"""]# ]""*]# ]"2"]# ]"2*]# ]r""]# ]r"*]# ]r2"]# ]r2*]YT if 6argc B 17 X if 66stat6arg0L1M# ,buff7 [P "177 X cout WW ]Permissions for ] WW arg0L1M WW ] ]T for 6int iP8T iT ""i7 X cout WW permL6buff&stJmode , mas47 BB 6i"17SNJ$I SMT mas4 BBP NJ$I ST Y cout WW endlT Y else X perror6arg0L1M7T return 1T Prepared by S&J&Subashini , -&.&/esa0an

Y Y else X cerr WW ]Usage3 ] WW arg0L@M WW ] fileJnameRn]T return 5T Y return @T Y T5e c4o%eC9 S3%tem Ca44 he close67 system call is used to close a file descriptor& Vinclude Wunistd&hB int close6int fd7T main6int argc#char Sarg0LM7 X int fd#iT char chL1MT if 6argcW57 X printf6]Usage3 mycat filenameRn]7T e*it6@7T Y fdPopen6arg0L1M#'JR.'N(`7T if6fdPP"17 printf6]Fs is not e*ist]#arg0L1M7T else X printf6]Contents of the file Fs is 3 Rn]#arg0L1M7T 2hile6read6fd#ch#17B@7 printf6]Fc]#chL@M7T close6fd7T Y T5e rea22irC9 S3%tem Ca44 he readdir67 function returns a pointer to a dirent structure repre"enting the ne*t directory entry in the directory stream pointed to by dir& It returns NU(( on reaching the end"of"file or if an error occurred& main67 X if6readdir67 [P null7 X printf6].irectory contents a0ailableRn]7T Prepared by S&J&Subashini , -&.&/esa0an

e*it6@7T Y ?lse X printf6].irectory contents is in end of fileRn]7T e*it6@7T Y Y T5e open2irC9 S3%tem Ca44 he opendir67 function opens a directory stream corresponding to the directory name# and returns a pointer to the directory stream& he stream is positioned at the first entry in the directory&

main67 X if6opendir67 [P null7 X printf6].irectory openedRn]7T e*it6@7T Y ?lse X printf6]?rrorRn]7T e*it6@7T Y Y T5e e$itC9 S3%tem Ca44 his system call is used to terminate6normal+abnormal7 the current running program& main6int argc#char Sarg0LM7 X int fd#I#KP@T char chL1M#ne2L1@MT if 6argcW57 X printf6]Usage3 mycat filenameRn]7T e*it6@7T Y fdPopen6arg0L1M#'JR.'N(`7T if6fdPP"17 printf6]Fs is not e*ist]#arg0L1M7T Prepared by S&J&Subashini , -&.&/esa0an

else X Ne2L@MPDaDT printf6]Contents of the file Fs is 3 Rn]#arg0L1M7T 2hile6read6fd#ch#17B@7 printf6]Fc]#chL@M7T 2hile 6K WP1@7X X 2rite6fd#ne2#1@7 KEET ne2LKMPne2LK"1ME1T Y close6fd7T e*it6@7T Y T5e openC9 S3tem Ca44 he ]open67] system call opens an e*isting file for reading or 2riting& It has the synta*3 Wfile descriptor 0ariableB P open6 WfilenameB# Waccess modeB 7T he ]open67] call is similar to the ]creat67] call in that it returns a file descriptor for the gi0en file# and returns a file descriptor of "1 if it encounters an error& Ao2e0er# the second parameter is an ]access mode]# not a permission code& here are three modes 6defined in the ]fcntl&h] header file73 'JR.'N(` 'pen for reading only& 'J%R'N(` 'pen for 2riting only& 'JR.%R 'pen for reading and 2riting&

main6int argc#char Sarg0LM7 X int fd#iT char chL1MT if 6argcW57 X printf6]Usage3 mycat filenameRn]7T e*it6@7T Y fdPopen6arg0L1M#'JR.'N(`7T if6fdPP"17 printf6]Fs is not e*ist]#arg0L1M7T else Prepared by S&J&Subashini , -&.&/esa0an

X printf6]Contents of the file Fs is 3 Rn]#arg0L1M7T 2hile6read6fd#ch#17B@7 printf6]Fc]#chL@M7T close6fd7T Y T5e ;riteC9 S3tem Ca44 he ]2rite67] system call 2rites data to an open file& It has the synta*3 2rite6 Wfile descriptorB# WbufferB# Wbuffer lengthB 7T he file descriptor is returned by a ]creat67] or ]open67] system call& he ]buffer] is a pointer to a 0ariable or an array that contains the dataT and the ]buffer length] gi0es the number of bytes to be 2ritten into the file& %hile different data types may ha0e different byte lengths on different systems# the ]siUeof67] statement can be used to pro0ide the proper buffer length in bytes& ) ]2rite67] call could be specified as follo2s3 float arrayL1@MT 2rite6 fd# array# siUeof6 array 7 7T he ]2rite67] function returns the number of bytes it actually 2rites& It 2ill return "1 on an error& main6int argc#char Sarg0LM7 X int fd#i#KP@T char chL1M# ne2L1@MT if 6argcW57 X printf6]Usage3 mycat filenameRn]7T e*it6@7T Y fdPopen6arg0L1M#'JR.'N(`7T if6fdPP"17 printf6]Fs is not e*ist]#arg0L1M7T else X Ne2L@MPDaD printf6]Contents of the file Fs is 3 Rn]#arg0L1M7T 2hile6read6fd#ch#17B@7 printf6]Fc]#chL@M7T 2hile 6K WP1@7X Prepared by S&J&Subashini , -&.&/esa0an

X 2rite6fd#ne2#1@7 KEET ne2LKMPne2LK"1ME1T Y Y close6fd7T Y T5e rea2C9 S3tem Ca44 he ]read67] system call reads data from a open file& Its synta* is e*actly the same as that of the ]2rite67] call3 rea2C Jfi4e 2e%criptorK@ J:ufferK@ J:uffer 4engt5K 9I he ]read67] function returns the number of bytes it actually returns& )t the end of file it returns @# or returns "1 on error main6int argc#char Sarg0LM7 X int fd#I#KP@T char chL1M#ne2L1@MT if 6argcW57 X printf6]Usage3 mycat filenameRn]7T e*it6@7T Y fdPopen6arg0L1M#'JR.'N(`7T if6fdPP"17 printf6]Fs is not e*ist]#arg0L1M7T else X Ne2L@MPDaDT printf6]Contents of the file Fs is 3 Rn]#arg0L1M7T 2hile6read6fd#ch#17B@7 printf6]Fc]#chL@M7T 2hile 6K WP1@7X X 2rite6fd#ne2#1@7 KEET ne2LKMPne2LK"1ME1T Y close6fd7T Y

Prepared by S&J&Subashini , -&.&/esa0an

88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 8

Prepared by S&J&Subashini , -&.&/esa0an

Potrebbero piacerti anche