Documents

Basic_System_calls.pdf

Description
System Calls Related to File Manipulation Objective: Practicing simple programs using some simple file structure related system calls to get an overview of system calls. File Structure Related System Calls: The file structure related system calls available in the UNIX system let you create, open, and close files, read and write files, randomly access files, alias and remove files, get information about files, check the accessibility of files, change protections, owner, and group of files, and
Categories
Published
of 6
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Transcript
  System Calls Related to File Manipulation Objective: Practicing simple programs using some simple fle structure related system calls to get an overview o system calls. File Structure Related System Calls: The file structure related system calls available in the UNIX system let you create, open, andclose files, read and write files, randomly access files, alias and remove files, get informationabout files, check the accessibility of files, change protections, owner, and group of files, andcontrol devices. These operations either use a character string that defines the absolute or relative path name of a file, or a small integer called a file descriptor that identifies the I/ channel. !channel is a connection between a process and a file that appears to the process as anunformatted stream of bytes. The kernel presents and accepts data from the channel as a processreads and writes that channel. To a process then, all input and output operations are synchronousand unbuffered. hen doing I/, a process specifies the file descriptor for an I/ channel, a buffer to be filled or emptied, and the ma#imum si$e of data to be transferred. !n I/ channel may allow input,output, or both. %urthermore, each channel has a read/write pointer. &ach I/ operation startswhere the last operation finished and advances the pointer by the number of bytes transferred. ! process can access a channel's data randomly by changing the read/write pointer.!ll input and output operations start by opening a file using either the ( creat( ) ( or ( open( ) (system calls. These calls return a file descriptor that identifies the I/ channel. )ecall that filedescriptors *, +, and  refer to standard input, standard output, and standard error filesrespectively, and that file descriptor * is a channel to your terminal's keyboard and filedescriptors + and  are channels to your terminal's display screen. creat( ) The prototype for the creat( )  system call is- int creat(file_name, mode)car !file_name int mode where file name is pointer to a null terminated character string that names the file and modedefines the file's access permissions. The mode is usually specified as an octal number such as* that would mean read/write permission for owner, group, and others or the mode may also be entered using manifest constants defined in the ( #usr#include#sys#stat$ ( file. If the file  named by filename does not e#ist, the UNIX system creates it with the specified mode permissions. 0owever, if the file does e#ist, its contents are discarded and the mode value isignored. The permissions of the e#isting file are retained. %ollowing is an e#ample of how to use creat( ) - /* This program explains use of creat( )  system call*/  #include <stdio.h>#include <sys/types.h> /* defines types */#include <sys/stat.h> /* defines S_IREAD & S_IWRIE */int !ain $int fd%fd  creat 'datafile.dat'( S_IREAD | S_IWRITE %if fd  )p+intf 'E++o+ in openin, datafile.dat-n'%else$p+intf 'datafile.dat opened fo+ +ead/+iteaccess-n'%p+intf 'datafile.dat is cu++ently e!pty-n'% close fd%e0it 1%  open( ) The file management system calls allow you to manipulate the full collection of regular,directory, and special files. In most cases, open  1 2 is used to initially access or create a file. If thefile system call succeeds, it returns a small integer called a file descriptor that is used insubse3uent I/ operations on that file. If open  1 2 fails, it returns  %& . hen a process no longer needs to access an open file, it should close it using the close  1 2 system call. !ll of a process4sopen files are automatically closed when the process terminates. !lthough this means that youmay often omit an e#plicit call to close  1 2, it4s better programming practice to e#plicitly closeyour files. File descriptors are numbered se'uentially, startin from ero . &very process may have up   to *+  1the usual system limit2 open files at any one time, with the file descriptor values ranging between * and +5. 6y convention the first three file descriptor values have special meaning- alue Meanin * 7tandard Input 17creen2+ 7tandard utput 17creen2 7tandard &rror %or e#ample, the printf ( )  library function always sends its output using file descriptor & , and  scanf ( ) always reads its input using file descriptor   + which is the display screen. hen areference to a file is closed, the file descriptor is freed and may be reassigned by a subse3uent open 1 2. - sinle file may be opened several times and tus ave several file descriptors associated .it it$ &ach file descriptor has its own private set of properties that have nothing to do with the file. /en a file descriptor is created, its file pointer is positioned at offset + in te file (te firstcaracter) by default$ -s te process reads and#or .rites, te file pointer is updatedaccordinly$ %or e#ample, if a process opened a file and then read  &+   bytes from the file, thefile    pointer would end up positioned at offset &+ . If the process then wrote *+  bytes, the bytes atoffset &+$$*0  in the file would be overwritten and the file pointer would end up positioned atoffset 1+ . int open (car !  filename , int mode  2, int  permissions 3) open ( ) allows you to open or create a file for reading and/or writing.    filename   is an absoluteor    relative pathname and mode  is a )4ing of a read/write flag together with $ero or moremiscellaneous flags.  permissions  is a number that encodes the value of the file4s permissionflags, and sould only be supplied .en a file is bein created . The values of the predefinedread/write and miscellaneous flags are stored in 8usr/include/sys/file.h9. The read/write flagsare as follows- alueMeanin ):N;<pen for reading only )N;<pen for writing only): )pen for reading and writingN:&;!<Used wih communications devices .NN6;=>  Used with communication devioces!??&N:pen at end of file for writing7<N=If set this results in write()  calls not returning until data has actually beenwritten to dics.=)&!T=reate the file if it doesn't already e#ist&X=;If set and =)&!T set will cause open12 to fail if the file already e#istsT)UN=Truncate file si$e to $ero if it already e#istsIf successful   open  1 2 returns a non4neative  file descriptor, otherwise it returns –1 . #include <fcntl.h> int open(file_name option_fla!   mode$% char &file_name'int option_fla!  mode'  where file_name  is a pointer to the character string that names the file, option_flas  representthe type of channel, and mode  defines the file's access permissions  if the file is being created.@ultiple values are combined using the A operator 1i.e. bitwise )2. Note- some combinations aremutually e#clusive such as- O_R5O678 9 O_/RO678  and will cause open( )  to fail. If the O_CR-;  flag is used, then a mode argument is re'uired . The mode argument may bespecified in the same manner as in the creat( )  system call. close( ) int close (int  fd  ) close ( ) frees the file descriptor     fd  .   If     fd    is the last file descriptor associated with a particular open file, the resources associated with that file are deallocated .If successful   close  1 2 returns ero , otherwise it returns  %& .To close a channel, use the close( )  system call. The prototype for the close( )  system call is- int close(file_descriptor) int file_descriptor where file_descriptor  identifies a currently open channel. close( )  fails if file_descriptor  does not identify a currently open channel. read( ) int read (int  fd  , car ! buf  , int count  ) read ( ) copies   count     bytes from the file referenced by the file descriptor     fd    into the buffer 1character array2 buf  . ;e bytes are read from te current file position, .ic is tenupdated accordinly .  read ( ) copies as many bytes from te file as it can, up to te numberspecified by count  , and returns te number of bytes actually copied$ If a  read ( )  is attemptedafter the last byte as already been read , it returns + , which indicates end4of4file .If successful , read ( )  returns the number of bytes tat it read B otherwise, it returns  %& . .rite( ) int .rite (int  fd  , car ! buf  , int count  ) .rite ( ) copies   count     bytes from a buffer 1character array2   buf    to the file referenced by the filedescriptor  fd  . ;e bytes are .ritten at te current file position, .ic is ten updatedaccordinly . If the  O_-<<65 flag was set for     fd  , the file position is set to the  end of te file  before each write. .rite ( )   copies as many bytes from te buffer as it can, up to te numberspecified by count   , and returns te number of bytes actually copied . If the returned value isnot count  , then the disk probably filled up and no space was left.
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks