0% found this document useful (0 votes)
9 views35 pages

UNIX File System Calls Overview

The document outlines various UNIX system calls related to file and directory operations, including creating, opening, reading, writing, and closing files. It details specific system calls such as creat(), open(), close(), read(), and write(), along with their prototypes and usage. Additionally, it covers directory management functions like mkdir, rmdir, opendir, readdir, and closedir, explaining their parameters and return values.

Uploaded by

doggapavansekhar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views35 pages

UNIX File System Calls Overview

The document outlines various UNIX system calls related to file and directory operations, including creating, opening, reading, writing, and closing files. It details specific system calls such as creat(), open(), close(), read(), and write(), along with their prototypes and usage. Additionally, it covers directory management functions like mkdir, rmdir, opendir, readdir, and closedir, explaining their parameters and return values.

Uploaded by

doggapavansekhar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIX SYSTEM CALLS

File system calls

Course Instructor: Ram Chatterjee 1


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
control devices.

Course Instructor: Ram Chatterjee 2


FILE STRUCTURE RELATED SYSTEM
CALLS (CONT’D)
 To a process then, all input and output
operations are synchronous and unbuffered.
 All 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/O channel.

Course Instructor: Ram Chatterjee 3


FILE DESCRIPTORS
 Each UNIX process has 20 file descriptors at
it disposal, numbered 0 through 19.
 The first three are already opened when the
process begins
 0: The standard input
 1: The standard output
 2: The standard error output
 When the parent process forks a process, the
child process inherits the file descriptors of
the parent.

Course Instructor: Ram Chatterjee 4


CREAT() SYSTEM CALL

 The prototype for the creat() system call is:


int creat(file_name, mode)
char *file_name;
int mode;

Course Instructor: Ram Chatterjee 5


CREAT() SYSTEM CALL (CONT’D)
 The mode is usually specified as an octal number
such as 0666 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.h" file.

Course Instructor: Ram Chatterjee 6


CREAT() SYSTEM CALL (CONT’D)
 The following is a sample of the manifest constants for
the mode argument as defined in /usr/include/sys/stat.h:

#define S_IRWXU 0000700 /* -rwx------ */


#define S_IREAD 0000400 /* read permission, owner */
#define S_IRUSR S_IREAD
#define S_IWRITE 0000200 /* write permission, owner */
#define S_IWUSR S_IWRITE
#define S_IEXEC 0000100 /* execute/search permission, owner */
#define S_IXUSR S_IEXEC
#define S_IRWXG 0000070 /* ----rwx--- */
#define S_IRGRP 0000040 /* read permission, group */
#define S_IWGRP 0000020 /* write " " */
#define S_IXGRP 0000010 /* execute/search " " */
#define S_IRWXO 0000007 /* -------rwx */
#define S_IROTH 0000004 /* read permission, other */
#define S_IWOTH 0000002 /* write " " */
#define S_IXOTH 0000001 /* execute/search " " */

Course Instructor: Ram Chatterjee 7


OPEN() SYSTEM CALL

 The prototype for the open() system call is:

#include <fcntl.h>
int open(file_name, option_flags [, mode])
char *file_name;
int option_flags, mode;

Course Instructor: Ram Chatterjee 8


OPEN() SYSTEM CALL (CONT’D)

 The allowable option_flags as defined in


"/usr/include/fcntl.h" are:
#define O_RDONLY 0 /* Open the file for reading only */
#define O_WRONLY 1 /* Open the file for writing only */
#define O_RDWR 2 /* Open the file for both reading and writing*/
#define O_NDELAY 04 /* Non-blocking I/O */
#define O_APPEND 010 /* append (writes guaranteed at the end) */
#define O_CREAT 00400 /*open with file create (uses third open arg) */
#define O_TRUNC 01000 /* open with truncation */
#define O_EXCL 02000 /* exclusive open */
 Multiple values are combined using the | operator
(i.e. bitwise OR).
Course Instructor: Ram Chatterjee 9
OPEN() SYSTEM CALL (CONT’D)

Course Instructor: Ram Chatterjee 10


OPEN() SYSTEM CALL (CONT’D)

Course Instructor: Ram Chatterjee 11


OPEN() SYSTEM CALL (CONT’D)

Course Instructor: Ram Chatterjee 12


CLOSE() SYSTEM CALL

 To close a channel, use the close() system call.


The prototype for the close() system call is:

int close(file_descriptor)
int file_descriptor;

Course Instructor: Ram Chatterjee 13


OPEN()
SYSTEM
CALL
(CONT’D)

Course Instructor: Ram Chatterjee 14


READ() & WRITE() SYSTEM CALLS
 Theread() system call does all input and the
write() system call does all output.

int read(file_descriptor, buffer_pointer, transfer_size)


int file_descriptor;
char *buffer_pointer;
unsigned transfer_size;

int write(file_descriptor, buffer_pointer, transfer_size)


int file_descriptor;
char *buffer_pointer;
unsigned transfer_size;

Course Instructor: Ram Chatterjee 15


READ() & WRITE() SYSTEM CALLS

Course Instructor: Ram Chatterjee 16


READ() & WRITE() SYSTEM CALLS

Course Instructor: Ram Chatterjee 17


READ() & WRITE() SYSTEM CALLS

Course Instructor: Ram Chatterjee 18


WHAT IS IN AN INODE?

Course Instructor: Ram Chatterjee 19


WHAT IS IN AN INODE?

Course Instructor: Ram Chatterjee 20


STAT() SYSTEM CALLS

Course Instructor: Ram Chatterjee 21


STAT() SYSTEM CALLS

Course Instructor: Ram Chatterjee 22


STAT() SYSTEM CALLS

Course Instructor: Ram Chatterjee 23


STAT() SYSTEM CALLS

Course Instructor: Ram Chatterjee 24


STAT() SYSTEM CALLS

Course Instructor: Ram Chatterjee 25


UNIX SYSTEM CALLS
Directory system calls

Course Instructor: Ram Chatterjee 26


DIRECTORIES: MKDIR 1/2
#include <sys/stat.h>
#include <sys/types.h>

int mkdir(const char *pathname, mode_t


mode);

Attempts to create a directory.


To create a directory

 Specify directory name.


 Specify permission. E.g. 0740;

Course Instructor: Ram Chatterjee 27


DIRECTORIES: MKDIR 2/2
 mode_t perms = 0740; // Who can access?
 if (mkdir (“test_dir”, perms)) == -1) {

 perror(“failed to create directory”);


 return -1;
}

 What does return value “0” mean?

Course Instructor: Ram Chatterjee 28


DIRECTORIES: RMDIR
#include <unistd.h>

int rmdir(const char *pathname);

Deletes a directory which must be empty.


What errors can happen?

You must have a permission to delete a directory.

Course Instructor: Ram Chatterjee 29


DIRECTORIES: OPENDIR 1/2
#include <sys/types.h>
#include <dirent.h>

DIR * opendir(const char * name);

Opens a directory stream corresponding to the


directory name.
What errors can happen?

Course Instructor: Ram Chatterjee 30


DIRECTORIES: OPENDIR 2/2
 DIR * dir; // So where is the DIR defined?
 dir = opendir (“test_dir”);

 if(dir == NULL) {
 perror (“Failed to open directory”);
}

Course Instructor: Ram Chatterjee 31


DIRECTORIES: READDIR 1/2
#include <dirent.h>

struct dirent * readdir(DIR * dirp);

Returns a pointer to directory structure representing


the net directory entry.
What is struct dirent?

Course Instructor: Ram Chatterjee 32


DIRECTORIES: READDIR 2/2
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file; not supported
by all file system types */
char d_name[256]; /* filename */
};

Course Instructor: Ram Chatterjee 33


DIRECTORIES: CLOSEDIR
#include <sys/types.h>
#include <dirent.h>

int closedir(DIR * dirp);

Closes the directory stream associated with dirp.


What errors can happen?

Course Instructor: Ram Chatterjee 34


Course Instructor: Ram Chatterjee 35

You might also like