You are on page 1of 35

UNIX SYSTEMS PROGRAMMING

Subject code: 06CS62  

By
Kala.C.L
Lecturer, Dept of ISE
SJBIT, Bengaluru
UNIT-II
UNIX FILES
 Files are the building blocks of any OS.
 When you execute a command in UNIX, the kernel fetches the
executable file from the file system and also any files required for reading
and writing.
 Files in UNIX and POSIX cover a wide range of file types.
 They also provide a common set of interfaces to files.
File Types
• Regular Files
• Directory File
• Character Device File
• Block Device File
• FIFO file
• link file
Regular File

 It can be a text or binary file


 No distinction is made between these files.
 They can be created, browsed and modified by various
means such as text editors and compilers.
 They can be removed by commands like rm in UNIX.
Directory File
 It is like a file folder
 It provides a means for users to organize their files into some
hierarchical structure based on file relationship or uses.
 Eg: /bin directory contains executable programs such as cat, rm, sort…
etc.
 create a directory file – mkdir command.
 A directory is empty if it contains nothing other than “.” and “..” files.
 Remove a directory – rmdir command
 Display contents – ls command
Device Files

 Block device files represent a physical device that transmits data a


block at a time.
 Eg: hard disk drives, floppy deivce drives.
 Character device files represent a physical device that transmits data in
a character-based manner.
 Eg: line printers, modems and consoles.
 An application can perform read and write on device files like on
regular files and the OS will automatically invoke appropriate device
driver to perform actual data transfer.
 create a device file – mknod command.
mknod /dev/cdsk c 115 5
115 – major device number: An index to a kernel table that contains the
address of device driver functions known to the system.
5 – minor device number: An integer value to be passed as an argument to
a device driver function when it is called. It tells which actual device it is
talking to.
c – Character device file b – Block device file
Device Files – Examples
/dev/dsp
Digital Signal Processor. It forms the interface between software which produces sound
and your soundcard. It is a character device on major node 14 and minor 3.
/dev/fd0
The first floppy drive. It is a character device on major node 2 and minor 0.
/dev/lp0
The first parallel printer device. Subsequent printers are numbered lp1, lp2 etc. They are
character devices on major mode 6 and minor nodes starting at 0 and numbered
sequentially.
/dev/psaux
The PS/2 mouse port. This is a character device on major node 10, minor node 1.
/dev/pcd0
Parallel port CD ROM drives. All are block devices on major node 46. /dev/pcd0 is on
minor node 0 with subsequent drives being on minor nodes 1, 2, 3 etc.
FIFO File

 It is a special pipe device file which provides temporary buffer for two
processes to communication by writing/reading data from the buffer.
 Size of the buffer is fixed to PIPE_BUF.
 create a FIFO file – mkfifo command
mkfifo /usr/prog/fifo_pipe
mknod /usr/prog/fifo_pipe p
 Remove a fifo file – rm command.
Symbolic link file

 Supported by BSD UNIX and UNIX System V.4. No support by


POSIX.1
 It contains the path name which references another file in either
a local or remote file system.
 Create a symbolic link file – ln command

ln –s /usr/jose/original /usr/mary/slink
cat –n /usr/mary/slink
ls –l /usr/mary/slink
sr—r—r-- 1 terry 20 Aug 20, 1994 slink->usr/jose/original
UNIX and POSIX File Systems

 root directory – “/”


 Absolute path name – starts from “/”
 Relative path name – may start with a “.” or “..”
 A file name may not exceed NAME_MAX characters and path name
PATH_MAX characters.
 legal file name characters – A to Z a to z 0 to 9.
 The path name of a file is called the hard link. Can have one or more
hard links.
ln /usr/foo/path1 / usr/prog/new/n1
UNIX and POSIX File Systems
/etc Stores system admin files and programs
/etc/passwd Stores all user information
/etc/shadow Stores user passwords (For UNIX System V only)
/etc/group Stores all group info
/bin Stores all system programs like cat, rm, cp..etc
/dev Stores all character and block device files.
/usr/include Stores standard header files/
/usr/lib Stores standard libraries
/tmp Stores temporary file.
UNIX and POSIX File Attributes
File type
Access permission
Hard link count
UID
GID
File size
Last access time
Last modify time
Last change time
Inode number
File system ID
UNIX and POSIX File Attributes

UNIX System Call Attributes Changed


command
chmod chmod Changes access permission, last change time

chown chown Changes UID, last change time

chgrp chown Changes GID, last change time

touch utime Changes last access time, modification time

ln link Increases hard link count

rm unlink Decreases hard link count, remove the file if count


is zero
vi, emac Changes file size last access time, last
modification time.
Inodes in UNIX
 In UNIX System V, a file system has an inode table which
keeps track of all files.
Each inode table is an inode record contains all attributes of a
file including inode no and physical disk address.
 Each inode no is unique to a file system only.
 An OS does not keep the name of a file in its inode record. The
mapping from file names to inode nos is done through directory
files.
Inodes in UNIX
 To access a file, for example /usr/joe
 The UNIX kernel knows the /directory inode no – it is kepr in process-
U area.
 It will scan the “/” directory file to find the inode no of usr file.
 Once it gets the inode no, it checks if the calling process has
permission to search usr directory.
 It then looks for the joe file.
 Whenever a new file is created in directory, entry is made in inode table
and directory file.
 Inode tables are kept in their file systems on disk, but the UNIX kernel
maintains an in-memory inode table to contain a copy of the recently accessed
inode records.
Application Program Interface to Files
• Files are identified by path names
• Files must be created before they can be used.

File type UNIX Command UNIX and POSIX.1


System call
Regular Files Vi, emacs..etc Open, creat

Directory Files mkdir mkdir, mknod

FIFO Files mkfifo mkfifo, mknod

Device Files mknod Mknod

Symbolic Files ln –s Symlink


APIs for Files

 Files must be opened before they can be accessed by application programs


 A Process may open at most OPEN_MAX files of any types at any one time.
 read and write system calls
 File attributes can be queried by the stat or fstat system call.
 File attributes can be changed by the chmod, chown, utime and link system
calls.
 File hard links can be removed by the unlink system call.
Defined in <sys/stat.h>
Struct stat
{
dev_t st_dev /* file system ID */
ino_t st_ino /* File inode number */
mode_t st_mode /* Contains file type and access flags */
nlink_t st_nlink /* Hard link count */
uid_t st_uid /* File user Id */
gid_t st_gid /* File group Id */
dev_t st_rdev /* Contains major and minor device numbers */
off_t st_size` /* File size in number of bytes */
time_t st_atime /* Last access time */
time_t st_mtime ` /* Last modification time */
time_t st_ctime /* Last status change time */
}

stat, fstat or lstat System Calls


UNIX Kernel Support for Files
File table – All open files
Inode Table – copy of file inodes most recently accessed
File descriptor Table – All files opened by the Process, OPEN_MAX
OPEN function
1. Search the File descriptor Table for first unused entry.index to the entry is returned
to the process.
2. Scan the File table to find an unused entry. If found,
a) File descriptor Table entry will point to file table entry.
b) File able entry will point to inode table entry.
c) File table entry will contain the current file pointer of the open file.
d) File table entry will contain an open mode. (read-only, write-only, read and
write)
e) The reference count in the file table entry is set to 1.
f) The reference count of the in-memory inode of the file is increased by 1.
InodeTable
File descriptor Table
Data Structure
File Table

Kernel Space

r
rc = 1

rw rc = 1
xyz
rc = 1

w rc = 2 abc
rc = 1

Process Space
Kernel data structures for open files
Two independent processes with the same
file open
Kernel data structures after dup(1)
Read/write function
 The file descriptor is the first argument to read/write system call.
 Kernel uses the file descriptor to index to the file descriptor table to
find the file table entry.
 It checks the file table entry to see if the appropriate mode and
permissions are there.
 Use the file table entry to access the file’s inode record.
 Use the file pointer in the file table entry to determine where
read/write should occur.
 Checks the file type in inode record and invokes the appropriate
driver function.
lseek system call
 Invoked to change the file pointer to a different offset for next
read/write operation.
 The kernel gets access to the file inode record and checks that the
file is not a character device file, a FIFO file or a symbolic link file.
 If file type is compatible, change the file pointer to the value in
lseek.
Close function
1. The kernel sets the FD entry to be unused
2. Decrement the reference count in file table by 1. If still non-zero, go to
6.
3. The file table entry is marked as unused.
4. Decrement the reference count in file inode table by 1, if still non-zero,
go to 6.
5. If hard-link count of inode is non zero, return to caller. Otherwise it
marks the inode table entry as unused and deallocates all the physical
disk storage of the file, as all the file path names have been removed by
some process.
6. It returns to the process with a 0 (success) status.
C Stream Pointers and File Descriptors

C Stream pointers (FILE *) are A file descriptor allocated by an open


allocated via the fopen C function system call
call.

A File descriptor is more efficient for


A stream pointer is more efficient to applications that do frequent random
use for applications doing extensive access of file data. (No I/O buffering)
sequential read from or write to
files. (I/O buffering)

File descriptors are used only in UNIX


Stream pointers are supported on all
and POSIX.1 compliant systems
Operating systems, (VMS, CMS,
DOS, UNIX)
 Each process has a fixed-size stream table with OPEN_MAX entries.
FILE : buffer, file I/O error status, EOF flag…etc
 fopen returns a reference to this.
 Extract the file descriptor associated with a stream pointer.
<stdio.h>
int fileno( FILE* stream_pointer);
 To convert a file descriptor to a stream pointer
FILE* fdopen(int file_descriptor, char * open_mode);

C LIBRARY function UNIX system call used


fopen open
fread, fgetc, fscanf, fgets read
fwrite, fputc, fprintf, fputs write
fseek, ftell, frewind lseek
fclose close
Directory Files
<dirent.h> for UNIX System V and POSIX.1 systems
<sys/dir.h> for BSD UNIX

Directory Function Purpose

Opendir Opens a directory file

Readdir Reads the next record from


the file
Closedir Closes a directory file

Rewinddir Sets file pointer to


beginning of file
Hard Link Vs Symbolic
A Hard link is a UNIX path name for a file
Link
ln /usr/mary/abc /usr/mary/xyz

Symbolic links are created with the –s option


ln –s /usr/mary/abc /usr/mary/xyz

Limitations of hard links:


• Cannot create hard links for directories unless they have super user
privileges
• Users cannot create hard links on a file system that references files
on a different system.
Hard Link Vs Symbolic Link

Hard Link Symbolic Link


Does not create a new inode Create a new inode

Cannot link directories, unless this is Can link directories


done by the root

Cannot link files across file systems Can link files across file systems

Increase the hard link count of the linked Does not change the hard link count of
inode the linked inode
ln Vs cp
ln creates a new directory entry to a referenced file whereas cp creates a duplicated copy of
the file to another file with a different name.

ln /usr/mary/abc /usr/mary/xyz
Inode Filename Inode Filename
number number
115 515

89 989

201 abc 201 Xyz

346 a.out 146 Fun.c

(ln –s)/ cp /usr/mary/abc /usr/mary/xyz


Inode Filename Inode Filename
number number
115 515

89 989

201 abc 345 Xyz

346 a.out 146 Fun.c

You might also like