CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY...

40
10 CASE STUDY 1: UNIX AND LINUX 10.1 HISTORY OF UNIX 10.2 OVERVIEW OF UNIX 10.3 PROCESSES IN UNIX 10.4 MEMORY MANAGEMENT IN UNIX 10.5 INPUT/OUTPUT IN UNIX 10.6 THE UNIX FILE SYSTEM 10.7 SECURITY IN UNIX 10.8 SUMMARY

Transcript of CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY...

Page 1: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

10CASE STUDY 1: UNIX AND LINUX

10.1 HISTORY OF UNIX

10.2 OVERVIEW OF UNIX

10.3 PROCESSES IN UNIX

10.4 MEMORY MANAGEMENT IN UNIX

10.5 INPUT/OUTPUT IN UNIX

10.6 THE UNIX FILE SYSTEM

10.7 SECURITY IN UNIX

10.8 SUMMARY

Page 2: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

Users

Standards utility programs(shell, editors, compliers etc)

Standard library(open, close, read, write, fork, etc)

UNIX operating system(process management, memory management,

the file system, I/O, etc)

Hardware(CPU, memory, disks, terminals, etc)

Userinterface

Libraryinterface

Systemcall

interface

Usermode

Kernel mode

Fig. 10-1. The layers in a UNIX system.

Page 3: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

2222222222222222222222222222222222222222222222222222222Program Typical use2222222222222222222222222222222222222222222222222222222cat Concatenate multiple files to standard output2222222222222222222222222222222222222222222222222222222chmod Change file protection mode2222222222222222222222222222222222222222222222222222222cp Copy one or more files2222222222222222222222222222222222222222222222222222222cut Cut columns of text from a file2222222222222222222222222222222222222222222222222222222grep Search a file for some pattern2222222222222222222222222222222222222222222222222222222head Extract the first lines of a file2222222222222222222222222222222222222222222222222222222ls List directory2222222222222222222222222222222222222222222222222222222make Compile files to build a binary2222222222222222222222222222222222222222222222222222222mkdir Make a directory2222222222222222222222222222222222222222222222222222222od Octal dump a file2222222222222222222222222222222222222222222222222222222paste Paste columns of text into a file2222222222222222222222222222222222222222222222222222222pr Format a file for printing2222222222222222222222222222222222222222222222222222222rm Remove one or more files2222222222222222222222222222222222222222222222222222222rmdir Remove a directory2222222222222222222222222222222222222222222222222222222sort Sort a file of lines alphabetically2222222222222222222222222222222222222222222222222222222tail Extract the last lines of a file2222222222222222222222222222222222222222222222222222222tr Translate between character sets2222222222222222222222222222222222222222222222222222222111111111111111111111111

111111111111111111111111

111111111111111111111111

Fig. 10-2. A few of the common UNIX utility programs requiredby POSIX.

Page 4: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

System calls Interrupts and traps

Terminal handing Sockets

Network protocols

Routing

Filenaming

Map-ping

Pagefaults Signal

handling

Processcreation andtermination

Rawtty

Cooked tty

Linedisciplines

Characterdevices

Networkdevice drivers

Hardware

Diskdevice drivers

Processdispatching

Processscheduling

Pagecache

Buffercache

Filesystems

Virtualmemory

Fig. 10-3. Structure of the 4.4BSD kernel.

Page 5: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

pid = fork( ); /* if the fork succeeds, pid > 0 in the parent */if (pid < 0) {

handle3error( ); /* fork failed (e.g., memory or some table is full) */} else if (pid > 0) {

/* parent code goes here. /*/} else {

/* child code goes here. /*/}

Fig. 10-4. Process creation in UNIX.

Page 6: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

222222222222222222222222222222222222222222222222222222222222222222Signal Cause222222222222222222222222222222222222222222222222222222222222222222

SIGABRT Sent to abort a process and force a core dump222222222222222222222222222222222222222222222222222222222222222222SIGALRM The alarm clock has gone off222222222222222222222222222222222222222222222222222222222222222222SIGFPE A floating-point error has occurred (e.g., division by 0)222222222222222222222222222222222222222222222222222222222222222222SIGHUP The phone line the process was using has been hung up222222222222222222222222222222222222222222222222222222222222222222SIGILL The user has hit the DEL key to interrupt the process222222222222222222222222222222222222222222222222222222222222222222SIGQUIT The user has hit the key requesting a core dump222222222222222222222222222222222222222222222222222222222222222222SIGKILL Sent to kill a process (cannot be caught or ignored)222222222222222222222222222222222222222222222222222222222222222222SIGPIPE The process has written to a pipe which has no readers222222222222222222222222222222222222222222222222222222222222222222SIGSEGV The process has referenced an invalid memory address222222222222222222222222222222222222222222222222222222222222222222SIGTERM Used to request that a process terminate gracefully222222222222222222222222222222222222222222222222222222222222222222SIGUSR1 Available for application-defined purposes222222222222222222222222222222222222222222222222222222222222222222SIGUSR2 Available for application-defined purposes2222222222222222222222222222222222222222222222222222222222222222221

1111111111111111

11111111111111111

11111111111111111

Fig. 10-5. The signals required by POSIX.

Page 7: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

22222222222222222222222222222222222222222222222222222222222222222222222222222222System call Description22222222222222222222222222222222222222222222222222222222222222222222222222222222

pid = fork( ) Create a child process identical to the parent22222222222222222222222222222222222222222222222222222222222222222222222222222222pid = waitpid(pid, &statloc, opts) Wait for a child to terminate22222222222222222222222222222222222222222222222222222222222222222222222222222222s = execve(name, argv, envp) Replace a process’ core image22222222222222222222222222222222222222222222222222222222222222222222222222222222exit(status) Terminate process execution and return status22222222222222222222222222222222222222222222222222222222222222222222222222222222s = sigaction(sig, &act, &oldact) Define action to take on signals22222222222222222222222222222222222222222222222222222222222222222222222222222222s = sigreturn(&context) Return from a signal22222222222222222222222222222222222222222222222222222222222222222222222222222222s = sigprocmask(how, &set, &old) Examine or change the signal mask22222222222222222222222222222222222222222222222222222222222222222222222222222222s = sigpending(set) Get the set of blocked signals22222222222222222222222222222222222222222222222222222222222222222222222222222222s = sigsuspend(sigmask) Replace the signal mask and suspend the process22222222222222222222222222222222222222222222222222222222222222222222222222222222s = kill(pid, sig) Send a signal to a process22222222222222222222222222222222222222222222222222222222222222222222222222222222residual = alarm(seconds) Set the alarm clock22222222222222222222222222222222222222222222222222222222222222222222222222222222s = pause( ) Suspend the caller until the next signal22222222222222222222222222222222222222222222222222222222222222222222222222222222111111111111111111

111111111111111111

111111111111111111

Fig. 10-6. Some system calls relating to processes. The return codes is −1 if an error has occurred, pid is a process ID, and residual isthe remaining time in the previous alarm. The parameters are whatthe name suggests.

Page 8: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

while (TRUE) { /* repeat forever /*/type3prompt( ); /* display prompt on the screen */read3command(command, params); /* read input line from keyboard */

pid = fork( ); /* fork off a child process */if (pid < 0) {

printf("Unable to fork0); /* error condition */continue; /* repeat the loop */

}

if (pid != 0) {waitpid (−1, &status, 0); /* parent waits for child */

} else {execve(command, params, 0); /* child does the work */

}}

Fig. 10-7. A highly simplified shell.

Page 9: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

22222222222222222222222222222222222222222222222222222222222222222222222222222222Thread call Description22222222222222222222222222222222222222222222222222222222222222222222222222222222

pthread3create Create a new thread in the caller’s address space22222222222222222222222222222222222222222222222222222222222222222222222222222222pthread3exit Terminate the calling thread22222222222222222222222222222222222222222222222222222222222222222222222222222222pthread3 join Wait for a thread to terminate22222222222222222222222222222222222222222222222222222222222222222222222222222222pthread3mutex3 init Create a new mutex22222222222222222222222222222222222222222222222222222222222222222222222222222222pthread3mutex3destroy Destroy a mutex22222222222222222222222222222222222222222222222222222222222222222222222222222222pthread3mutex3 lock Lock a mutex22222222222222222222222222222222222222222222222222222222222222222222222222222222pthread3mutex3unlock Unlock a mutex22222222222222222222222222222222222222222222222222222222222222222222222222222222pthread3cond3 init Create a condition variable22222222222222222222222222222222222222222222222222222222222222222222222222222222pthread3cond3destroy Destroy a condition variable22222222222222222222222222222222222222222222222222222222222222222222222222222222pthread3cond3wait Wait on a condition variable22222222222222222222222222222222222222222222222222222222222222222222222222222222pthread3cond3signal Release one thread waiting on a condition variable2222222222222222222222222222222222222222222222222222222222222222222222222222222211111111111111111

11111111111111111

11111111111111111

Fig. 10-8. The principal POSIX thread calls.

Page 10: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

sh sh ls

Fork code Exec code

New process Same process

1. Fork call 3. exec call

4. sh overlaidwith ls

2. new shcreated

PID = 501 PID = 748 PID = 748

Allocate child's process table entryFill child's entry from parentAllocate child's stack and user areaFill child's user area from parentAllocate PID for childSet up child to share parent's textCopy page tables for data and stackSet up sharing of open filesCopy parent's registers to child

Find the executable programVerify the execute permissionRead and verify the headerCopy arguments, environ to kernelFree the old address spaceAllocate new address spaceCopy arguments, environ to stackReset signalsInitialize registers

Fig. 10-9. The steps in executing the command ls typed to theshell.

Page 11: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

222222222222222222222222222222222222222222222222222222222222222222222222222222222222Flag Meaning when set Meaning when cleared222222222222222222222222222222222222222222222222222222222222222222222222222222222222

CLONE3VM Create a new thread Create a new process222222222222222222222222222222222222222222222222222222222222222222222222222222222222CLONE3FS Share umask, root, and working dirs Do not share them222222222222222222222222222222222222222222222222222222222222222222222222222222222222CLONE3FILES Share the file descriptors Copy the file descriptors222222222222222222222222222222222222222222222222222222222222222222222222222222222222CLONE3SIGHAND Share the signal handler table Copy the table222222222222222222222222222222222222222222222222222222222222222222222222222222222222CLONE3PID New thread gets old PID New thread gets own PID222222222222222222222222222222222222222222222222222222222222222222222222222222222222111111111

111111111

111111111

111111111

Fig. 10-10. Bits in the sharing3flags bitmap.

Page 12: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

Highestpriority

Lowestpriority

-4

-3

-2

-1

0

0

1

2

3

Waiting for disk I/O

Waiting for disk buffer

Waiting for terminal input

Waiting for terminal output

Waiting for child to exist

User priority 0

User priority 1

User priority 2

User priority 3

Process queuedon priority level 3

Process waitingin user mode

Process waitingin kernel mode

Fig. 10-11. The UNIX scheduler is based on a multilevel queuestructure.

Page 13: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

Process 0

Process 1 Process 2Pagedaemon

Terminal 0 Terminal 1 Terminal 2

Login: Password: % cp f1 f2login sh

cp

getty

init

Fig. 10-12. The sequence of processes used to boot some UNIXsystems.

Page 14: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

Text ��

��������������������

BSSData

Stack pointer Stack pointer

20K

8K

0

Unusedmemory

24K

8K

0K

BSS

TextData

(a) (b) (c)

OS

Physical memoryProcess A Process B

Fig. 10-13. (a) Process A’s virtual address space. (b) Physicalmemory. (c) Process B’s virtual address space.

Page 15: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

Text�

����

��� ������������

BSSData

Text

Stack pointer Stack pointer

20K

8K

0K

24K

0K(a) (b) (c)

OS

Physical memory

Mapped file

Mapped file

Process A Process B

BSS

Data 8K

Unusedmemory

Fig. 10-14. Two processes can share a mapped file.

Page 16: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

222222222222222222222222222222222222222222222222222222222222222222System call Description222222222222222222222222222222222222222222222222222222222222222222

s = brk(addr) Change data segment size222222222222222222222222222222222222222222222222222222222222222222a = mmap(addr, len, prot, flags, fd, offset) Map a file in222222222222222222222222222222222222222222222222222222222222222222s = unmap(addr, len) Unmap a file222222222222222222222222222222222222222222222222222222222222222222111111

111111

111111

Fig. 10-15. Some system calls relating to memory management.The return code s is −1 if an error has occurred; a and addr arememory addresses, len is a length, prot controls protection, flagsare miscellaneous bits, fd is a file descriptor, and offset is a fileoffset.

Page 17: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

Page frame3

Page frame2

Page frame1

Page frame0

4.3 BSDkernel

Two-handedclock scanscore map

Main memory Core map entry

Core mapentries, one

per page frame

Index of next entry

Index of previous entry

Disk block number

Disk device number

Block hash code

Index into proc table

Text/data/stack

Offset within segment

Used whenpage frame ison the free list

Locked inmemory bit

WantedIn transitFree

Fig. 10-16. The core map in 4BSD.

Page 18: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

Globaldirectory

Directory

Pagemiddle

directoryPagetable

Page

Word selected

Virtual addressOffsetPageMiddle

Fig. 10-17. Linux uses three-level page tables.

Page 19: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

(a)

64

(b)

32

32

(d)

32

8

8

16

(e)

16

8

8

32

(f)

8

8

8

32

8

(g)

84

8

8

32

(h)

48

8

8

32

(i)

16

448

32

(c)

16

16

32

44

Fig. 10-18. Operation of the buddy algorithm.

Page 20: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

User space

Kernel space

Receiving processSending process

Socket

Connection

Network

Fig. 10-19. The uses of sockets for networking.

Page 21: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

222222222222222222222222222222222222222222222222222222222Function call Description222222222222222222222222222222222222222222222222222222222

s = cfsetospeed(&termios, speed) Set the output speed222222222222222222222222222222222222222222222222222222222s = cfsetispeed(&termios, speed) Set the input speed222222222222222222222222222222222222222222222222222222222s = cfgetospeed(&termios, speed) Get the output speed222222222222222222222222222222222222222222222222222222222s = cfgtetispeed(&termios, speed) Get the input speed222222222222222222222222222222222222222222222222222222222s = tcsetattr(fd, opt, &termios) Set the attributes222222222222222222222222222222222222222222222222222222222s = tcgetattr(fd, &termios) Get the attributes22222222222222222222222222222222222222222222222222222222211

11111111

1111111111

1111111111

Fig. 10-20. The main POSIX calls for managing the terminal.

Page 22: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

2222222222222222222222222222222222222222222222222222222222222222222222222Device Open Close Read Write Ioctl Other2222222222222222222222222222222222222222222222222222222222222222222222222Null null null null null null ...2222222222222222222222222222222222222222222222222222222222222222222222222Memory null null mem3read mem3write null ...2222222222222222222222222222222222222222222222222222222222222222222222222Keyboard k3open k3close k3read error k3 ioctl ...2222222222222222222222222222222222222222222222222222222222222222222222222Tty tty3open tty3close tty3read tty3write tty3 ioctl ...2222222222222222222222222222222222222222222222222222222222222222222222222Printer lp3open lp3close error lp3write lp3 ioctl ...2222222222222222222222222222222222222222222222222222222222222222222222222111111111

111111111

111111111

111111111

111111111

111111111

111111111

111111111

Fig. 10-21. Some of the fields of a typical cdevsw table.

Page 23: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

File system

Buffer cache

Disk drivers Terminal drivers

Line disciplines

Userspace

Kernel

Disk

Reading/writing filesCooked

interface to/dev/tty

Raw interfaceto/dev/tty

Fig. 10-22. The UNIX I/O system in BSD.

Page 24: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

A

Stream head

TCP

IP

Ethernet driver

Ethernet controller

B

Stream head

TCP

Token ring driver

Token ring controller

Process

Ethernet

Token Ring

Computer

User mode

Kernel mode

Fig. 10-23. An example of streams in System V.

Page 25: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

2222222222222222222222222222222222222222222Directory Contents2222222222222222222222222222222222222222222bin Binary (executable) programs2222222222222222222222222222222222222222222dev Special files for I/O devices2222222222222222222222222222222222222222222etc Miscellaneous system files2222222222222222222222222222222222222222222lib Libraries2222222222222222222222222222222222222222222usr User directories222222222222222222222222222222222222222222211111111

11111111

11111111

Fig. 10-24. Some important directories found in mostUNIX systems.

Page 26: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

bindevetclib

tmpusr

xyz

abc

fred lisa

/

(a)

bindevetclib

tmpusr

xyz

abcx

fred lisa

/

(b)

Link

Fig. 10-25. (a) Before linking. (b) After linking.

Page 27: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

/

a b a

c

p q r q q r

d

/

c d

b

Diskette

/

Hard diskHard disk

x y z

x y z

Fig. 10-26. (a) Separate file systems. (b) After mounting.

Page 28: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

0(a) 1 2 3 8 9 10 11 12 13 14 15

0 1 2 3 10 11 12 13 14 15

0 1 12 13 14 15

(b)

(c)

Process A'sshared

lock

A's shared lock

B's shared lock

C's shared lock

A B

4 5 6 7

4 5 6 7 8 9

2 3 4 5 8 9 10 1176

Fig. 10-27. (a) A file with one lock. (b) Addition of a second lock.(c) A third lock.

Page 29: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

222222222222222222222222222222222222222222222222222222222222222222222222System call Description222222222222222222222222222222222222222222222222222222222222222222222222

fd = creat(name, mode) One way to create a new file222222222222222222222222222222222222222222222222222222222222222222222222fd = open(file, how, ...) Open a file for reading, writing or both222222222222222222222222222222222222222222222222222222222222222222222222s = close(fd) Close an open file222222222222222222222222222222222222222222222222222222222222222222222222n = read(fd, buffer, nbytes) Read data from a file into a buffer222222222222222222222222222222222222222222222222222222222222222222222222n = write(fd, buffer, nbytes) Write data from a buffer into a file222222222222222222222222222222222222222222222222222222222222222222222222position = lseek(fd, offset, whence) Move the file pointer222222222222222222222222222222222222222222222222222222222222222222222222s = stat(name, &buf) Get a file’s status information222222222222222222222222222222222222222222222222222222222222222222222222s = fstat(fd, &buf) Get a file’s status information222222222222222222222222222222222222222222222222222222222222222222222222s = pipe(&fd[0]) Create a pipe222222222222222222222222222222222222222222222222222222222222222222222222s = fcntl(fd, cmd, ...) File locking and other operations222222222222222222222222222222222222222222222222222222222222222222222222111111111111111

111111111111111

111111111111111

Fig. 10-28. Some system calls relating to files. The return code s is−1 if an error has occurred; fd is a file descriptor, and position is afile offset. The parameters should be self explanatory.

Page 30: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

22222222222222222222222222222222222222222Device the file is on22222222222222222222222222222222222222222I-node number (which file on the device)22222222222222222222222222222222222222222File mode (includes protection information)22222222222222222222222222222222222222222Number of links to the file22222222222222222222222222222222222222222Identity of the file’s owner22222222222222222222222222222222222222222Group the file belongs to22222222222222222222222222222222222222222File size (in bytes)22222222222222222222222222222222222222222Creation time22222222222222222222222222222222222222222Time of last access22222222222222222222222222222222222222222Time of last modification2222222222222222222222222222222222222222211111111111111

11111111111111

Fig. 10-29. The fields returned by the stat system call.

Page 31: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

2222222222222222222222222222222222222222222222222222222222222222System call Description2222222222222222222222222222222222222222222222222222222222222222

s = mkdir(path, mode) Create a new directory2222222222222222222222222222222222222222222222222222222222222222s = rmdir(path) Remove a directory2222222222222222222222222222222222222222222222222222222222222222s = link(oldpath, newpath) Create a link to an existing file2222222222222222222222222222222222222222222222222222222222222222s = unlink(path) Unlink a file2222222222222222222222222222222222222222222222222222222222222222s = chdir(path) Change the working directory2222222222222222222222222222222222222222222222222222222222222222dir = opendir(path) Open a directory for reading2222222222222222222222222222222222222222222222222222222222222222s = closedir(dir) Close a directory2222222222222222222222222222222222222222222222222222222222222222dirent = readdir(dir) Read one directory entry2222222222222222222222222222222222222222222222222222222222222222rewinddir(dir) Rewind a directory so it can be reread222222222222222222222222222222222222222222222222222222222222222211111111111111

11111111111111

11111111111111

Fig. 10-30. Some system calls relating to directories. The returncode s is −1 if an error has occurred; dir identifies a directorystream and dirent is a directory entry. The parameters should beself explanatory.

Page 32: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

Bootblock

Superblock

I nodes Data blocks

Fig. 10-31. Disk layout in classical UNIX systems.

Page 33: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

222222222222222222222222222222222222222222222222222222222222222222222222222Field Bytes Description222222222222222222222222222222222222222222222222222222222222222222222222222Mode 2 File type, protection bits, setuid, setgid bits222222222222222222222222222222222222222222222222222222222222222222222222222Nlinks 2 Number of directory entries pointing to this i-node222222222222222222222222222222222222222222222222222222222222222222222222222Uid 2 UID of the file owner222222222222222222222222222222222222222222222222222222222222222222222222222Gid 2 GID of the file owner222222222222222222222222222222222222222222222222222222222222222222222222222Size 4 File size in bytes222222222222222222222222222222222222222222222222222222222222222222222222222Addr 39 Address of first 10 disk blocks, then 3 indirect blocks222222222222222222222222222222222222222222222222222222222222222222222222222Gen 1 Generation number (incremented every time i-node is reused)222222222222222222222222222222222222222222222222222222222222222222222222222Atime 4 Time the file was last accessed222222222222222222222222222222222222222222222222222222222222222222222222222Mtime 4 Time the file was last modified222222222222222222222222222222222222222222222222222222222222222222222222222Ctime 4 Time the i-node was last changed (except the other times)222222222222222222222222222222222222222222222222222222222222222222222222222111111111111111

111111111111111

111111111111111

111111111111111

Fig. 10-32. Structure of the i-node in System V.

Page 34: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

Mode

i-node

Link count

Uid

Gid

File size

Times

Addresses offirst 10

disk blocks

Single indirect

Double indirect

Triple indirect

Parent’sfile

descriptortable

Child’sfile

descriptortable

Unrelatedprocess

filedescriptor

table

Open filedescription

File positionR/W

Pointer to i-node

File positionR/W

Pointer to i-node

Pointers todisk blocks

Tripleindirectblock Double

indirectblock Single

indirectblock

Fig. 10-33. The relation between the file descriptor table, the openfile description table, and the i-node table.

Page 35: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

19(a) 19F 8 F 10 88 D 6 bigdircolossal voluminous ��Unused

19(b) F 8 88 D 6 bigdircolossal ��Unused��Unused

I-node numberEntry sizeType

File name length

Fig. 10-34. (a) A BSD directory with three files. (b) The samedirectory after the file voluminous has been removed.

Page 36: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

Boot Block group 0

Super– Groupblock descriptor

Block group 1

Blockbitmap

Datablocks

I–nodebitmap I–nodes

Block group 2 Block group 3 Block group 4 ...

Fig. 10-35. Layout of the Linux Ext2 file system.

Page 37: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

Client 1 Client 2

Server 1 Server 2

/

/usr

/usr/ast

/usr/ast/work

/bin

/bin

cat cp Is mv sha b c d e

/proj2/proj1

/projects

/mnt/bin

Mount

/

Fig. 10-36. Examples of remote mounted file systems. Directoriesare shown as squares and files are shown as circles.

Page 38: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

Client kernel Server kernel

System call layer

Buffer cache Buffer cache

Virtual file system layer Virtual file system layer

LocalFS 1

LocalFS 1

LocalFS 2

LocalFS 2

NFSclient

NFSserver

Driver Driver Driver Driver

Messageto server

Messagefrom client

Local disks Local disks

V- node

Fig. 10-37. The NFS layer structure.

Page 39: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

22222222222222222222222222222222222222222222222222222222222222222222222222222Binary Symbolic Allowed file accesses22222222222222222222222222222222222222222222222222222222222222222222222222222

111000000 rwx– – – – – – Owner can read, write, and execute22222222222222222222222222222222222222222222222222222222222222222222222222222111111000 rwxrwx– – – Owner and group can read, write, and execute22222222222222222222222222222222222222222222222222222222222222222222222222222110100000 rw–r– – – – – Owner can read and write; group can read22222222222222222222222222222222222222222222222222222222222222222222222222222110100100 rw–r– –r– – Owner can read and write; all others can read22222222222222222222222222222222222222222222222222222222222222222222222222222111101101 rwxr–xr–x Owner can do everything, rest can read and execute22222222222222222222222222222222222222222222222222222222222222222222222222222000000000 – – – – – – – – – Nobody has any access22222222222222222222222222222222222222222222222222222222222222222222222222222000000111 – – – – – –rwx Only outsiders have access (strange, but legal)222222222222222222222222222222222222222222222222222222222222222222222222222221

1111111111

11111111111

11111111111

11111111111

Fig. 10-38. Some example file protection modes.

Page 40: CASE STUDY 1: UNIX AND LINUX - Instituto de Computaçãocelio/mc514/docs/figs-10.pdf · CASE STUDY 1: UNIX AND LINUX ... UNIX operating system (process management, ... The sequence

22222222222222222222222222222222222222222222222222222222222222222222222System call Description22222222222222222222222222222222222222222222222222222222222222222222222

s = chmod(path, mode) Change a file’s protection mode22222222222222222222222222222222222222222222222222222222222222222222222s = access(path, mode) Check access using the real UID and GID22222222222222222222222222222222222222222222222222222222222222222222222uid = getuid( ) Get the real UID22222222222222222222222222222222222222222222222222222222222222222222222uid = geteuid( ) Get the effective UID22222222222222222222222222222222222222222222222222222222222222222222222gid = getgid( ) Get the real GID22222222222222222222222222222222222222222222222222222222222222222222222gid = getegid( ) Get the effective GID22222222222222222222222222222222222222222222222222222222222222222222222s = chown(path, owner, group) Change owner and group22222222222222222222222222222222222222222222222222222222222222222222222s = setuid(uid) Set the UID22222222222222222222222222222222222222222222222222222222222222222222222s = setgid(gid) Set the GID2222222222222222222222222222222222222222222222222222222222222222222222211

111111111111

11111111111111

11111111111111

Fig. 10-39. Some system calls relating to security. The return codes is −1 if an error has occurred; uid and gid are the UID and GID,respectively. The parameters should be self explanatory.