previous
 next 
CS 3733 Operating Systems Notes: USP Chapter 4 UNIX I/O Highlights
Read carefully USP: 4.1 - 4.3, 4.6 - 4.7

UNIX I/O is done with file descriptors.
Normally, when your program starts, there are 3 file descriptors open:
STDIN_FILENO
STDOUT_FILENO
STDERR_FILENO
You must use these symbolic values in your code.

read
   #include <unistd.h>

   ssize_t read(int fildes, void *buf, size_t nbyte);


write
   #include <unistd.h>

   ssize_t write(int fildes, const void *buf, size_t nbyte);


open and close
   #include <fcntl.h>
   #include <sys/stat.h>
 
   int open(const char *path, int  oflag);
   int open(const char *path, int  oflag, mode_t mode);
Important values of the flag include:
O_RDONLY: read only
O_WRONLY: write only
O_RDWR: read and write
O_APPEND: writes always write to end
O_CREAT: create the file if it does not exist
O_EXCL: used with O_CREAT, return an error if file exists
O_NONBLOCK: do not block if not ready to open, also affects reads and writes
O_TRUNC: discard previous contents

You must use the 3-parameter form of open if the O_CREAT flag is used. This specifies permissions.
Permissions are given by an integer (mode_t), traditionally given in octal.
For this course you must use symbolic values for the permissions:
S_IRUSR read permission bit for owner
S_IWUSR write permission bit for owner
S_IXUSR execute permission bit for owner
S_IRWXU read, write, execute for owner
S_IRGRP read permission bit for group
S_IWGRP write permission bit for group
S_IXGRP execute permission bit for group
S_IRWXG read, write, execute for group
S_IROTH read permission bit for others
S_IWOTH write permission bit for others
S_IXOTH execute permission bit for others
S_IRWXO read, write, execute for others
S_ISUID set user ID on execution
S_ISGID set group ID on execution

Table 4.1, Page 105: POSIX symbolic names for file permission.


   #include <unistd.h>

   int close(int fildes);

Open files are closed when your program exits normally.

Continue with main Chapter 4 notes.