+ All Categories
Home > Engineering > Unix files

Unix files

Date post: 14-Apr-2017
Category:
Upload: sunil-rm
View: 179 times
Download: 0 times
Share this document with a friend
34
UNIX FILES By : Sunil Kumar R.M 1 Sunil Kumar R.M Assistant Professor RLJIT
Transcript
Page 1: Unix files

UNIX FILES

By : Sunil Kumar R.M

1 Sunil Kumar R.M Assistant Professor RLJIT

Page 2: Unix files

UNIX FILES

• Files are the building blocks of any operating

system.

user executes a command in UNIX

the UNIX kernel fetches the corresponding

executable file and loads its instruction text to

memory

and creates a process to execute the

command on your behalf.

2 Sunil Kumar R.M Assistant Professor RLJIT

Page 3: Unix files

File Types

• A file in a UNIX or POSIX system may be one of

the following types:

– Regular file

– Directory file

-- FIFO file

– Character device file

-- Block device file

3 Sunil Kumar R.M Assistant Professor RLJIT

Page 4: Unix files

Regular File

– Text file or binary file

i.e both can be executable files (.exe) with suitable permissions. Ex :pdf, .txt etc

How to create?

using editors(vi editor)

How to delete?

using rm command.

4 Sunil Kumar R.M Assistant Professor RLJIT

Page 5: Unix files

Directory files

• Folder.

• Tree structure.

• HOW TO CREATE?

– mkdir command » Example mkdir /usr/sunil/file1

HOW TO delete?

-- rmdir /usr/sunil/file1

We can also display the contents of directory using ls command

5 Sunil Kumar R.M Assistant Professor RLJIT

Page 6: Unix files

FIFO files

• Used in Interprocess communication (IPC)

buffer

Process1 ------------------------------------------ Process2

(pipe/named pipe)

CREATE?

• mkfifo /usr/sunil/fifo_pipe

• mknod /usr/sunil/fifo_pipe p

6 Sunil Kumar R.M Assistant Professor RLJIT

Page 7: Unix files

DEVICE FILES

• Block device files.

ex: floppy disk, hard disk.

• Character device files. • Ex: line printer, keyboard, modem etc.

CREATE?

Syntax:

$mknod path c/b major minor

name device # device #

Ex: $ mknod /usr/sunil/file1 C 115 20

7 Sunil Kumar R.M Assistant Professor RLJIT

Page 8: Unix files

Device files 2

• Major device no :

– Index to Kernel table

• Device driver function addresses.

• Minor device no:

– Integer no that is a parameter to the device driver

function

8 Sunil Kumar R.M Assistant Professor RLJIT

Page 9: Unix files

Symbolic link files

• Supported by Unix but not by posix

– It contains a path name that refers to another file

/file system that can be local/remote.

– Creating symbolic link file

• $ ln -s usr/sunil/abc usr/rljit/xyz

9 Sunil Kumar R.M Assistant Professor RLJIT

Page 10: Unix files

The UNIX and POSIX File Systems

• tree-like hierarchical file system.

• Name MAX

• PathMAX

• Posix name and path

• Hard link

10 Sunil Kumar R.M Assistant Professor RLJIT

Page 11: Unix files

11 Sunil Kumar R.M Assistant Professor RLJIT

Page 12: Unix files

The UNIX and POSIX File Attributes

• 1) File type - specifies what type of file it is.

• 2) Access permission - the file access

permission for owner, group and others.

• 3) Hard link count - number of hard link of

the file

• 4) Uid - the file owner user id.

• 5) Gid - the file group id

• . 6) File size - the file size in bytes.

12 Sunil Kumar R.M Assistant Professor RLJIT

Page 13: Unix files

The UNIX and POSIX File Attributes

• 7) Inode no - the system inode no of the file. 8) File system id - the file system id where the file is stored.

• 9) Last access time - the time, the file was last accessed.

• 10) Last modified time - the file, the file was last modified.

• 11) Last change time - the time, the file was last changed.

13 Sunil Kumar R.M Assistant Professor RLJIT

Page 14: Unix files

14 Sunil Kumar R.M Assistant Professor RLJIT

Page 15: Unix files

Inodes in UNIX System V

• inode table, which keeps tracks of all files

– Inode record(attributes,inode no, physical

address)

– It’s a u i ue id. – Why os does ’t sto e file a e

– Directory file content.

– Who gives inode no and when?

15 Sunil Kumar R.M Assistant Professor RLJIT

Page 16: Unix files

• Directory is a record oriented file.

• Record data type is struct dirent in UNIX V and

POSIX.1, and struct dirent in BSD UNIX

Directory files

16 Sunil Kumar R.M Assistant Professor RLJIT

Page 17: Unix files

• A hard link is a UNIX path name for a file

• To create hard link ln command is used

ln /usr/abc/old.c /usr/xyz/new.c

• Symbolic link is also a means of referencing a

file

• To create symbolic link ln command is used

with option –s

ln –s /usr/abc/old.c /usr/xyz/new.c

Hard and symbolic links

17 Sunil Kumar R.M Assistant Professor RLJIT

Page 18: Unix files

• Cp command creates a duplicated copy of file

to another file with a different path name

• Where as ln command saves space by not

duplicating the copy here the new file will

have same inode number as original file

Difference : cp and ln command

18 Sunil Kumar R.M Assistant Professor RLJIT

Page 19: Unix files

Difference : hard link and symbolic link

Hard link Symbolic link

Does not create a new

inode

Create a new inode

Cannot link directories

unless it is done by root

Can link directories

Cannot link files across

file systems

Can link files across file

system

Increase hard link count

of the linked inode

Does not change hard

link count of the linked

inode

19 Sunil Kumar R.M Assistant Professor RLJIT

Page 20: Unix files

• Files are indentified by path names

• Files must be created before they can be used.

• open system call returns a file descriptor

APPLICATION PROGRAM INTERFACE

TO FILES(API)

20 Sunil Kumar R.M Assistant Professor RLJIT

Page 21: Unix files

• OPEN_MAX

• READ and WRITE

• Stat and fstat file attributes

• File attributes are changed using chmod,

chown, utime and link system calls

• Hard links are removed by unlink system call

21 Sunil Kumar R.M Assistant Professor RLJIT

Page 22: Unix files

22 Sunil Kumar R.M Assistant Professor RLJIT

Page 23: Unix files

• user executes a command -> process(kernel)

command execution

• process data structures: file descriptor table is

one among them

• File descriptor table OPEN_MAX entries,

and it records all files opened by the process

UNIX KERNEL SUPPORT FOR FILES

23 Sunil Kumar R.M Assistant Professor RLJIT

Page 24: Unix files

• Whenever an open function is called the

kernel will resolve the pathname to file inode

• Open call fails and returns -1 (file inode not exist

/the process lacks appropriate permissions)

Kernel support to open system call

24 Sunil Kumar R.M Assistant Professor RLJIT

Page 25: Unix files

rc=1

rc=2

r

rc=1

rw

rc=1

w

rc=1

File descriptor table File table Inode table

25 Sunil Kumar R.M Assistant Professor RLJIT

Page 26: Unix files

• Else a series of steps follow

• The kernel will search the file descriptor

table and look for first unused entry, which is

returned as file descriptor of the opened file

• The kernel will scan the file table, which is

used to reference the file If an unused entry

is found then

26 Sunil Kumar R.M Assistant Professor RLJIT

Page 27: Unix files

1. The p o ess’s file des ipto ta le e t y will e set to point to file table entry

2. The file table entry will be set to point to inode

table entry where the inode record of file is present

3. The file table entry will contain the current file

pointer of the open file

4. The file table entry will contain an open mode

which specifies the file is opened for read- only

,write-only etc.

5. Reference count of file table is set to 1.

6. The reference count of the in-memory inode of file

is increased by 1.

27 Sunil Kumar R.M Assistant Professor RLJIT

Page 28: Unix files

• The kernel will use the file descriptor to index the p o ess’s file des ipto ta le to fi d file table entry to opened file

• It checks the file table entry to make sure that the file is opened with appropriate mode

• The kernel will use the file pointer used in file table entry to determine where the read operation should occur in file

Kernel support : read system call

28 Sunil Kumar R.M Assistant Professor RLJIT

Page 29: Unix files

• The kernel will check the type of file in the

inode record and invokes an appropriate

driver function to initiate the actual data

transfer with a physical file

• If the process calls lseek function then the

changes are made provided the file is not a

character device file, a FIFO file, or a symbolic

link file as they follow only sequential read

and write operations

29 Sunil Kumar R.M Assistant Professor RLJIT

Page 30: Unix files

1. The kernel will set the corresponding

descriptor table entry to unused

2. It decrements the reference count in file

table entry by 1.if reference count !=0 then

go to 6

3. File table entry is marked unused

4. The reference count in file inode table entry

by 1.if reference count !=0 then go to 6

Kernel support : close system call

30 Sunil Kumar R.M Assistant Professor RLJIT

Page 31: Unix files

5. If hard link count is non zero, it returns a

success status, otherwise marks the inode

table entry as unused and and deallocates all

the physical disk storage

6. It returns the process with a 0 (success )

status

31 Sunil Kumar R.M Assistant Professor RLJIT

Page 32: Unix files

32 Sunil Kumar R.M Assistant Professor RLJIT

Page 33: Unix files

33 Sunil Kumar R.M Assistant Professor RLJIT

Page 34: Unix files

rc=1

rc=2

r

rc=1

rw

rc=1

w

rc=1

File descriptor table File table Inode table

34 Sunil Kumar R.M Assistant Professor RLJIT


Recommended