Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 57

PE-1 (Unix Programming)

Unit – IV

Sandeep Singh Rawat

Department of Computer Science & Engineering


Anurag University, Hyderabad, India

https://sites.google.com/view/sandeeprawat
Three Parts

07/21/2021
File System
• Before the advent of computers the term file system was used to describe
a method of storing and retrieving paper documents.
From Wikipedia,
• In computing, a file system or filesystem controls how data is stored and
retrieved.
• Without a file system, information placed in a storage medium would be one
large body of data with no way to tell where one piece of information stops and
the next begins.
• File systems can be used on numerous different types of storage devices like
hard disk drive, flash memory, magnetic tapes, and optical discs.
From techopedia
• A file system is a process that manages how and where data on a storage disk,
typically a hard disk drive (HDD), is stored, accessed and managed.
File Concept
• A File is a named collection of related information that is recorded
on secondary storage. Files represents programs and data
Data files may be
•numeric
•character
•Binary
•Alphabetic
•Alphanumeric
– Program
Many different types of information may be stored in a file.
Source Program, Object Program, Executable programs, numeric
data, data, graphics, images, sound recording and so on.
File Attributes
A File is named, for the convenience of its human users, and is
referred to by its name. Name is usually a string of characters, such
as example.c.
•Name – only information kept in human-readable form
•Identifier – unique tag (number) identifies file within file system
•Type – needed for systems that support different types
•Location – pointer to file location on device
•Size – current file size
•Protection – controls who can do reading, writing, executing
•Time, date, and user identification – data for protection, security,
and usage monitoring
•Information about files are kept in the directory structure, which is
maintained on the disk
Example in Unix/Linux
File Types in Linux/Unix

- : regular file


d : directory
c : character device file
b : block device file
s : local socket file
p : named pipe
l : symbolic link
File Operations
• A file is an abstract data type. The operating system can provide
system calls to create, write, read, reposition, delete and truncate
files. Basic file operations:
• Creating a file
• Writing a file
• Reading a file
• Reposition within a file
• Deleting a file
• Truncating a file
System Calls
• To use the services in the OS Unix offers some special functions known as system
calls.
• System calls provide an interface between user programs and operating system.
• It is a programmatic way in which a computer program requests a service from
the kernel of the operating system.
• Typically written in a high-level language (C or C++)
• Mostly accessed by programs via a high-level Application Program Interface
(API)rather than direct system call use
• Three most common APIs are Win32 API for Windows, POSIX API (Portable
Operating System Interface) for POSIX-based systems (including virtually all
versions of UNIX, Linux, and Mac OS X), and Java API for the Java virtual machine
(JVM)
System Call Interface

App Software
User-running
API
syscall - trap
Sys Software
“You are here” exit()
Sys Call (OS) Interface
Kernel-running Zombie
read(), write(),
OS wait(), sleep()

Sw-Hw I-face (drivers) Blocked Ready


Hardware
Types of System Calls
• Process control • File management • Information maintenance
• end, abort • create file, delete file • get time or date, set time or
date
• load, execute • open, close file • get system data, set system
• create process, terminate • read, write, reposition data
process • get and set file attributes • get and set process, file, or
• get process attributes, set device attributes
process attributes • Device management • Communications
• wait for time • request device, release device • create, delete communication
• read, write, reposition connection
• wait event, signal event
• get device attributes, set device • send, receive messages
• allocate and free memory attributes • transfer status information
• logically attach or detach devices • attach and detach remote
devices
File management

open function:
A file is opened or created by calling the open function.
#include <fcntl.h>
int open(const char *pathname, int oflag, .../ mode_t
mode */ );
Returns: file descriptor if OK, -1 on error.
The pathname is the name of the file to open or create.
This function has a multitude of options, which are
specified by the oflag argument.

07/21/2021
• This argument is formed by ORing together one or more of
the following constants from the <fcntl.h> header:
O_RDONLY open for reading only.
O_WRONLY open for writing only.
O_RDWR open for reading and writing.
• Most implementations define O_RDONLY as 0, O_WRONLY as
1, and O_RDWR as 2, for compatibility with older programs.

07/21/2021
• The following constants are optional:
O_APPEND Append to the end of file on
each write.
O_CREAT Create the file if it doesn't exist. This option requires a
third argument to the open function, the mode, which specifies
the access permission bits of the new file.

O_EXCL Generate an error if O_CREAT is also specified and the


file already exists
O_TRUNC If the file exists and if it is successfully opened for
either write-only or readwrite, truncate its length to 0.

07/21/2021
O_NOCTTY If the pathname refers to a terminal device, do not
allocate the device as the controlling terminal for this process.
O_NONBLOCK If the pathname refers to a FIFO, a block special file, or a
character special file, this option sets the nonblocking mode for both
the opening of the file and subsequent I/O.

07/21/2021
creat Function
A new file can also be created by calling the creat function.
Syntax:
#include <fcntl.h>
int creat( const char *pathname, mode_t mode);
Returns: file descriptor opened for write-only if OK, -1 on error.
This function is equivalent to
open (pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);

07/21/2021
close Function:
• An open file is closed by calling the close function.
#include <unistd.h>
int close(int filedes);
Returns: 0 if OK,-1 on error.

07/21/2021
read :
• Data is read from an open file with the read function.
#include <unistd.h>
ssize_t read( int filedes, void *buf, size_t nbytes);
Returns: number of bytes read, 0 if end of file, -1 on error
• read attempts to read nbyte characters from the file descriptor filedes into the
buffer buf.buf represents the pointer to the generic buffer. nbyte is the size of
the buffer.

07/21/2021
write function:
• Data is written to an open file with the write function.
#include <unistd.h>
ssize_t write(int filedes, const void *buf, size_t nbytes);
Returns: number of bytes written if OK,-1 on error
• write writes nbyte no. of bytes from the generic buffer buf to the file
descriptor filedes.

07/21/2021
lseek function:
An open file's offset can be set explicitly by calling
lseek.
#include <unistd.h>
off_t lseek(int filedes, off_t offset, int whence);
Returns: new file offset if OK, -1 on error.

07/21/2021
The interpretation of the offset depends on the value of
the whence argument.
If whence is:
• SEEK_SET--the file's offset is set to offset bytes from
the beginning of the file.
• SEEK_CUR--the file's offset is set to its current value
plus the offset. The offset can be positive or negative.
• SEEK_END--the file's offset is set to the size of the file
plus the offset. The offset can be positive or negative.

07/21/2021
Eg:
lseek(fd, 10, SEEK_CUR)
It moves the pointer forwarded by 10 characters from
its current position.
Eg:
lseek(fd, -10, SEEK_END)
It sets pointer 10 characters before EOF.
lseek returns position of pointer in bytes from the
beginning of the file.The return value can be used to
determine the file size.
Size= lseek(fd, 0,SEEK_END)

07/21/2021
stat, fstat, and lstat Functions

07/21/2021
stat, fstat, and lstat Functions:
#include <sys/stat.h>
int stat(const char * pathname, struct stat * buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char * pathname, struct stat * buf);
All three return: 0 if OK, -1 on error.
Given a pathname, the stat function returns a structure of
information about the named file. The fstat function
obtains information about the file that is already open on
the descriptor filedes.

07/21/2021
The definition of the structure can be:
struct stat {
mode_t st_mode; /*file type & mode*/
ino_t st_ino; /* i-node number */
dev_t st_dev; /* device number */
nlink_t st_nlink; /* number of links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
off_t st_size; /* size in bytes, for regular files */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last file status change */
07/21/2021
blksize_t st_blksize; blkcnt_t st_blocks; };
File Types:
The types of files are:
1.Regular file
2.Directory file
3.Block special file
4.Character special file
5.FIFO
6.Socket
7.Symbolic link
• The type of a file is encoded in the st_mode member of
the stat structure.

07/21/2021
Macro Type of file
S_ISREG() regular file
S_ISDIR() directory file
S_ISCHR() character special file
S_ISBLK() block special file
S_ISFIFO() pipe or FIFO
S_ISLNK() symbolic link
S_ISSOCK() socket
• File type macros are defined in <sys/stat.h>.

07/21/2021
File Access Permissions:
• The st_mode value also encodes the access permission
bits for the file.
S_IRUSR user-read
S_IWUSR user-write
S_IXUSR user-execute
S_IRGRP group-read
S_IWGRP group-write
S_IXGRP group-execute
S_IROTH other-read
S_IWOTH other-write
S_IXOTH other-execute

07/21/2021
Program to print type of file.
#include<sys/ types.h> #include<sys/stat.h>
int main(int argc, char *argv[])
{ int i;
Struct stat buf;
Char *ptr;
for(i=1;i< argc;i++)
{
printf(“%s”,argv[i]);
if( lstat(argv[i],&buf)<0)
{
printf(“lstat error”);
07/21/2021 Continue;}
If(S_ISREG( buf.st_mode))
ptr=“regular”;
elseif(S_ISDIR( buf.st_mode))
ptr=“directory”;
elseif(S_ISCHR( buf.st_mode))
ptr=“character special”;
elseif(S_ISBLK( buf.st_mode))
ptr=“block special”;
elseif(S_ISFIFO(buf.st_mode))
ptr=“fifo”;
else ptr=“unknown”;
printf(“%s”, ptr); }
07/21/2021 exit(0); }
access function:
#include <unistd.h>
int access(const char *pathname, int mode);
Returns: 0 if OK,-1 on error.
The mode is the bitwise OR of any of the constants shown
below.
Mode description
R_OK test for read permission
W_OK test for write permission
X_OK test for execute permission
F_OK test for existence of a file

07/21/2021
ioctl Function:
It performs a variety of control functions on devices.
#include <unistd.h> /* System V */
#include <sys/ioctl.h>/* BSD and Linux*/ #include
<stropts.h> /* XSI STREAMS */ int ioctl(int filedes, int
request, ...);
Returns: -1 on error, something else if OK.

07/21/2021
umask Function:
• The umask function sets the file mode creation mask for
the process and returns the previous value.
#include <sys/stat.h>
mode_t umask(mode_t cmask);
Returns: previous file mode creation mask.
• The cmask argument is formed as the bitwise OR of any
of the nine constants like S_IRUSR, S_IWUSR, and so on.

07/21/2021
File Sharing:
• The UNIX System supports the sharing of open files
among different processes.
• The kernel uses three data structures to represent an
open file:
1.Process table: Every process has an entry in the process
table. Within each process table entry is a table of open
file descriptors, with one entry per descriptor.
Associated with each file descriptor are
a)The file descriptor flags
b)A pointer to a file table entry

07/21/2021
2.File table: The kernel maintains a file table for all open
files. Each file table entry contains
a)The file status flags for the file, such as read, write,
append, sync, and non blocking
b)The current file offset
c) A pointer to the v-node table entry for the file
3.v-node table: Each open file (or device) has a v-node
structure that contains information about the type of file
and pointers to functions that operate on the file.

07/21/2021
How UNIX System supports the sharing of open files among different processes?

07/21/2021
dup and dup2 Functions:
• An existing file descriptor is duplicated by either of
the following functions.
#include <unistd.h>
int dup(int filedes);
int dup2(int filedes, int filedes2);
Both return: new file descriptor if OK,-1 on error.
• The new file descriptor returned by dup is guaranteed
to be the lowest-numbered available file descriptor.

07/21/2021
• With dup2, we specify the value of the new descriptor
with the filedes2 argument.
• If filedes2 is already open, it is first closed.
• If filedes equals filedes2, then dup2 returns filedes2 without
closing it.
• dup(filedes); is equivalent to
fcntl(filedes, F_DUPFD, 0);
• dup2(filedes, filedes2); is equivalent to
close(filedes2);
fcntl(filedes, F_DUPFD, filedes2);

07/21/2021
Standard I/O

07/21/2021
Standard I/O
• All the I/O routines centered around file descriptors.
When a file is opened, a file descriptor is returned, and
that descriptor is then used for all subsequent I/O
operations. With the standard I/O library, the discussion
centers around streams.
• When we open a stream, the standard I/O function fopen
returns a pointer to a FILE object. This object is normally a
structure that contains all the information required by the
standard I/O library to manage the stream.

07/21/2021
Standard Input, Standard Output, and Standard Error:
• Three streams are predefined and automatically available
to a process: standard input, standard output, and
standard error. These streams refer to the same files as
the file descriptors STDIN_FILENO, STDOUT_FILENO, and
STDERR_FILENO.
• These three standard I/O streams are referenced through
the predefined file pointers stdin, stdout, and stderr. The
file pointers are defined in the <stdio.h> header.

07/21/2021
Opening a Stream: fopen()
• The following three functions open a standard I/O stream.
#include < stdio.h>
FILE *fopen(const char *restrict pathname, const char *restrict type);
FILE *freopen(const char *restrict pathname, const char *restrict type,FILE *restrict fp);
FILE *fdopen(int filedes,const char *type);
All three return: file pointer if OK, NULL on error.

• The fopen function opens a specified file.


• The freopen function opens a specified file on a specified stream, closing the stream first if it is already
open. If the stream previously had an orientation, freopen clears it.
• The fdopen function takes an existing file descriptor, which we could obtain from the open, dup, dup2,
fcntl, pipe, socket, socketpair, or accept functions, and associates a standard I/O stream with the
descriptor.

07/21/2021
The fopen() method in C is a library function that is used to open a file
to perform various operations which include reading, writing etc.
along with various modes. If the file exists then the particular file is
opened else a new file is created.
Syntax:
FILE *fopen(const char *file_name, const char *mode_of_operation);

07/21/2021
Parameters: The method accepts two parameters of character type:
file_name: This is of C string type and accepts the name of the file that is needed to be opened.
mode_of_operation: This is also of C string type and refers to the mode of the file access. Below are the
file access modes for C:
“r” – Opens the file for reading only.
“w” – If the file exists already, its contents are overwritten. If the file doesn’t exist, a new file is created.
Returns NULL, if unable to open the file. It creates a new file for writing only(no reading).
“a” – If the file is opened successfully fopen() loads it into memory and sets up a pointer that points to the
last character in it.
“r+” – Opens the file for both reading and writing.
“w+” – If the file exists, its contents are overwritten. If the file doesn’t exist, a new file is created. Returns
NULL, if unable to open the file. The difference between w and w+ is that we can also read the file
created using w+.
“a+” –The file is opened for reading and appending(writing at end of file).
Return Value: The function is used to return a pointer to FILE if the execution succeeds else NULL is
returned.
07/21/2021
Type Description
• r / rb  open for reading
• W / wb truncate to 0 length or create for
writing
• a / ab  append; open for writing at end of
file, or create for writing
• r+ / r+b / rb+ open for reading and writing
• w+ / w+b / wb+  truncate to 0 length or
create for reading and writing
• a+/ a+b / ab+  open or create for reading
and writing at end of file
07/21/2021
fclose:
• An open stream is closed by calling fclose.
#include <stdio.h>
int fclose(FILE *fp);
Returns: 0 if OK, EOF on error.
• Any buffered output data is flushed before the file is closed. Any input data that
may be buffered is discarded.
• If the standard I/O library had automatically allocated a buffer for the stream, that
buffer is released.

07/21/2021
Reading and Writing a Stream:
• Once we open a stream, we can choose from among three
types of unformatted I/O:
1.Character-at-a-time I/O. We can read or write one
character at a time.
2.Line-at-a-time I/O. If we want to read or write a line at a
time, we use fgets and fputs.
3.Direct I/O. This type of I/O is supported by the fread and
fwrite functions

07/21/2021
Input Functions:
• Three functions allow us to read one character at a time.
#include <stdio.h>
int getc (FILE *fp);
int fgetc (FILE *fp);
int getchar (void);
All three return: next character if OK, EOF on end of file or
error.
The function getchar is equivalent to getc(stdin). getc can be
implemented as a macro, whereas fgetc cannot be
implemented as a macro.

07/21/2021
Output Functions
an output function that corresponds to each of the input
functions are:
#include <stdio.h>
int putc (int c, FILE *fp);
int fputc (int c, FILE *fp);
int putchar (int c);
All three return: c if OK, EOF on error.
• Like the input functions, putchar(c) is equivalent to putc(c,
stdout), and putc can be implemented as a macro,
whereas fputc cannot be implemented as a macro.

07/21/2021
Line-at-a-Time I/O:
#include <stdio.h>
char *fgets (char *restrict buf, int n, FILE *restrict fp);
char *gets(char *buf);
Both return: buf if OK, NULL on end of file or error.
• Both specify the address of the buffer to read the line
into.
• The gets function reads from standard input, whereas
fgets reads from the specified stream.

07/21/2021
• With fgets, we have to specify the size of the buffer, n.
This function reads up through and including the next
newline, but no more than n1 characters, into the buffer.
The buffer is terminated with a null byte.
• The gets function should never be used. The problem is
that it doesn't allow the caller to specify the buffer size.
This allows the buffer to overflow, if the line is longer than
the buffer, writing over whatever happens to follow the
buffer in memory.

07/21/2021
• Line-at-a-time output is provided by fputs& fputs.
#include <stdio.h>
int fputs(const char *restrict str, FILE *restrict fp);
int puts(const char *str);
Both return: non-negative value if OK, EOF on error.
• The function fputs writes the null-terminated string to the
specified stream. The null byte at the end is not written.

07/21/2021
Formatted I/O

07/21/2021
Formatted I/O
Formatted Output:
Formatted output is handled by the four printf functions.
#include <stdio.h>
int printf(const char *restrict format, ...);
int fprintf(FILE *restrict fp, const char *restrict format, ...);
Both return: number of characters output if OK, negative
value if output error.

07/21/2021
int sprintf (char *restrict buf, const char *restrict format, ...);
int snprintf (char *restrict buf, size_t n, const char *restrict
format, ...);
Both return: number of characters stored in array if OK, negative
value if encoding error.
• The printf function writes to the standard o/p, fprintf writes to
the specified stream& sprintf places the formatted characters
in the array buf.
• The sprintf function automatically appends a null byte at the
end of the array, but this null byte is not included in the return
value.

07/21/2021
Formatted Input:
Formatted input is handled by the three scanf functions.
#include <stdio.h>
int scanf (const char *restrict format, ...);
int fscanf (FILE *restrict fp, const char *restrict format, ...);
int sscanf (const char *restrict buf, const char *restrict
format, ...);
All three return: number of input items assigned,
EOF if input error or end of file before any conversion.

07/21/2021
• The scanf family is used to parse an input string and
convert character sequences into variables of specified
types.
chmod and fchmod Functions:
• These two functions allow us to change the file access
permissions for an existing file.
#include <sys / stat.h>
int chmod( const char *pathname, mode_t mode);
int fchmod (int filedes, mode_t mode);
Both return: 0 if OK, -1 on error.

07/21/2021

You might also like