0% found this document useful (0 votes)
18 views32 pages

Linux System Calls for File Management

The document outlines a series of experiments aimed at familiarizing users with Linux system calls related to file and process management, including operations like open, close, read, write, and directory handling. It details the usage of file descriptors, standard I/O streams, and various system calls such as creat(), open(), read(), write(), and stat(). Additionally, it includes a set of viva questions and video references for further learning.

Uploaded by

koro sensei
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)
18 views32 pages

Linux System Calls for File Management

The document outlines a series of experiments aimed at familiarizing users with Linux system calls related to file and process management, including operations like open, close, read, write, and directory handling. It details the usage of file descriptors, standard I/O streams, and various system calls such as creat(), open(), read(), write(), and stat(). Additionally, it includes a set of viva questions and video references for further learning.

Uploaded by

koro sensei
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

WEEK-1

Aim:
1. Familiarity and usage of system calls of Linux on
a) File management (open, close, read, write, opendir, readdir, stat etc)
List of Experiments

1. Familiarity and usage of system calls of Linux on


a) File management (open,close, read, write, open dir, readdir, stat etc)
b) Process management (fork, exec ,getpid, wait exit etc)
2. Implement a program to get and set the environment variables using system calls.
3. Implementation of Echo server using pipes.
4. Implementation of Echo server using shared memory.
5. Implementation of Echo server using messages.
6. Implementation of Producer Consumer Problem using semaphores.
7. Implementation of Producer Consumer Problem using message passing.
8. Implementation of Reader-writer problem using semaphores.
9. Implementation of Dining philosophers' problem using semaphores.
10. Creating threads and manipulating under Linux platform
FILES
• We all know what is a file , it is nothing but a collection of similar records and here each and every file
is associated with its file descriptors(fd) and using that file descriptor we can read or write in to that file
and initially to get the fd of a file ,we have to make call to open() by sending path of the file as an
argument (either absolute or relative path) and then we can read or write in to that file using the fd of
that file.

• File descriptor 0 is for standard input (key board) , if we mention fd in read() function as 0 means to
read from key board it self.

• File descriptor 1 is for standard output (monitor/screen) , if we mention fd in write() function as 1


means to write on the screen (monitor) it self.

• File descriptor 2 for standard error


File and Directory Structure Related system
calls
• creat()
• open()
• close()
• read()
• write()
• opendir()
• readdir()
• stat()
Standard I/O streams
POSIX(Portable Operating System Interface) defines the following three
standard I/O streams.
Standard Default
Purpose
stream endpoint

stdin Standard input is the stream of data data (often text) going terminal
into a program.

stdout Standard output is the stream where a program terminal


writes its output data.
Standard error is another output stream typically
stderr terminal
used by programs to output error messages or
diagnostics.
File descriptor s
In Unix and related computer operating systems, a file
descriptor is an abstract indicator used to access a file or
other input/output resources, such as a pipe or network
connection.
★ File descriptors form part of the POSIX application
programming interface.

★ A file descriptor is a non-negative integer, represented


in C programming language as the type int.
Standard I/O streams and file descriptors
POSIX defines file descriptor values for the three
standard I/O streams.

File
descriptor Standard
value stream Mode Endpoint
0 stdin read terminal
1 stdout write terminal
2 stderr write terminal
File descriptor Table

The kernel keeps a table with information about a process’s open 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.
A Process By default, a process got
three open descriptors:
Space

★ stdin
User

★ stdout
★ stderr

File descriptor table


Standard
Index Mode Endpoint
Kernel

stream
Space

0 read stdin terminal


1 write stdout terminal
2 write stderr terminal
File Structure Related System Calls

• 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.
creat() system call
• The prototype for the creat() system call is:
int creat(file_name, mode)
char *file_name;
int mode;
creat() system call (Cont’d)
• The mode is usually specified as an octal number such as 0777 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.
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 " " */
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;
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).
open()
Open is a system call that is used to open a new file and obtain its file descriptor.

i n t open(const char *path, i n t oflags );


C h a r * path
The relative or absolute path to the file that is to be opened.

int oflags
A bitwise 'or' separated list of values that determine the method in
which the file is to be opened (whether it should be read only,
read/write, whether it should be cleared when opened, etc).
return value
The file descriptor for the opened file. The file descriptor returned
is always the smallest integer greater than zero that is still
available. If a negative value is returned, then there was an error
opening the file.
Open a file for reading using the open() system call

A Process
1
int fd; When opening a file for reading,
Space

a descriptor number is
User

char buff[BUFF_SIZE];
fd = open("[Link]", O_RDONLY); 1 returned. In this example, a
descriptor with value is 3
returned.

File descriptor table


Index Mode Endpoint
0 read terminal
1 write terminal
2 write terminal
3 read [Link] File [Link]
Kernel
Space
read()
Read is a system call used to read data from a file into a buffer.
size_t read(int f i l d e s , void * b u f , size_t nbytes);
int fildes The file descriptor of where to read the input. You can either use a file descriptor
obtained from the open system call, or you can use 0, 1, or 2, to refer to standard
input, standard output, or standard error, respectively.

void *buf A character array (buffer) where the read content will be stored.

int nbytes The number of bytes to read before truncating the data. If the data to be read is
smaller than nbytes, all data is saved in the buffer.

return value On success, the number of bytes read is returned (zero indicates end of file). If
value is negative, then the system call returned an error.
Reading data from a file using the read() system call

A Process
2
int fd; Reading data from the file is
Space

done by using the descriptor


User

char buff[BUFF_SIZE];
(index) as a reference.
fd = open("[Link]", O_RDONLY);
read(fd, b u f f , BUFF_SIZE); 2

File descriptor table


Index Mode Endpoint
0 read terminal
1 write terminal
2 write terminal
3 read [Link] File [Link]
Kernel
Space
write()
write is a system call used to write data from a buffer into a file.
size_t write(int f i l d e s , void * b u f , size_t nbytes);
int fildes The file descriptor of where to read the input. You can either use a file descriptor
obtained from the open system call, or you can use 0, 1, or 2, to refer to standard
input, standard output, or standard error, respectively.

void *buf A character array (buffer) where the read content will be stored.

int nbytes The number of bytes to read before truncating the data. If the data to be read is
smaller than nbytes, all data is saved in the buffer.

return value On success, the number of bytes read is returned (zero indicates end of file). If
value is negative, then the system call returned an error.
close()
Close is a system call that is used to close an open file descriptor.
i n t close(int f i l d e s ) ;
int fildes The file descriptor to be closed.

return value Retuns a 0 upon success, and a -1 upon failure. It is important to check the return
value, because some network errors are not returned until the file is closed.
Directory Related System Calls
opendir()
The opendir() system call opens a directory stream corresponding to
the directory name, and returns a pointer to the directory stream.
The stream is positioned at the first entry in the directory.
DIR *opendir(const char *name);
Char *name The name of the directory

return value On success it return a pointer to the directory stream. On error,


NULL is returned .
readdir()
The readdir() function returns a pointer to a dirent structure
representing the next directory entry in the directory stream
pointed to by dirp.
Struct dirent *readdir(DIR *dirp);
DIR *dirp The directory stream returned by opendir system call

return value On success it return a pointer to the dirent structure. It returns NULL
on reaching the end of the directory stream or -1 if an error
occurred.
The dirent structure is defined as follows:
• Struct dirent
•{
• ino_t d_ino; /* Inode number */
• off_t d_off; /* Not an offset; see below */
• unsigned short d_reclen; /* Length of this record */
• unsigned char d_type; /* Type of file; not supportedby all
filesystem types */
• Char d_name[256]; /* Null-terminated filename */
• };
Display Attributes Of A File Using Stat() System call
int stat(const char *file_name, struct stat *buf);

• This function retrieves the status of the file pointed to by file_name and fills in buf.
• It returns a stat structure, which contains the following fields:
• struct stat {
• dev_t st_dev; /* device */
• ino_t st_ino; /* inode */
• mode_t st_mode; /* protection */
• nlink_t st_nlink; /* number of hard links */
• uid_t st_uid; /* user ID of owner */
• gid_t st_gid; /* group ID of owner */
• dev_t st_rdev; /* device type (if inode device) */
• off_t st_size; /* total size, in bytes */
• unsigned long st_blksize; /* blocksize for filesystem I/O */
• unsigned long st_blocks; /* number of blocks allocated */
• time_t st_atime; /* time of last access */
• time_t st_mtime; /* time of last modification */
• time_t st_ctime; /* time of last change */
• };
Viva Questions:
1. What are the services provided by the operating system
2. Define system call
3. Distinguish creat and open system call
4. Write the syntax of read and write system calls.
5. Describe directory structure member variables.
6. List the operating system components
7. Distinguish multi-programmed operating system and time sharing operating system
8. Under what circumstances would a user be better off using a time sharing system than a PC or a
single-user workstation?
9. Describe three general methods for passing parameters to the operating system
10. What are the five major activities of an operating system with regard to file management?
Video Link References
• [Link]
• [Link]

You might also like