33
Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) www.cs.huji.ac.il/~os

Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

  • View
    218

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

Operating Systems, Spring 2002

Ittai Abraham,

Zinovi Rabinovich(recitation)

www.cs.huji.ac.il/~os

Page 2: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

Agenda

• Basic OS concepts:– kernel and system calls;– process: fork()/exec()/wait()– signals: signal()

– file system: open()/close()/read()/write()/dup()

Page 3: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

System Calls (I)

• Every syscall is a request for a special privileged service available only from kernel

• System calls are provided in form of the library functions called from the user’s program

• Important differences:– not guaranteed to succeed (e.g., writing a full disk)

– executed in special mode (CPU is physically switched to allow special instructions)

– most system calls are non-interruptible

Page 4: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

System Calls (II)

• Sequence of events:– user code is executed on the user stack– system call occurs– results in a “trap” (interrupt) to the kernel– kernel allocates a frame for the system call function on the kernel stack– the thread of control is transferred to the kernel (we say that the process

executes in “kernel mode”)– system call code is executed (without interruption) – result of the execution is placed back on the user stack and the process

returns to the “user mode”

• Read more: – Richard W. Stevens “Adv. Unix Progr.”, pp.20-22

Page 5: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

Process• Program in execution• Executable file usually consists of at least three parts

(segments):– text– data– stack

• In multitasking systems (e.g., Win NT), and in Time Sharing systems (e.g., Unix) more than one process is needed– How to create and control them? Where OS comes in?

• Read more: – Richard W. Stevens “Adv. Unix Progr.”, pp.187-232– Maurice Bach “Unix”, pp. 24-36, 146-150, 191-225.

Page 6: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

fork()/exec()• How to create a new process?• A process is:

– Program Status Word (PSW)– resources (memory areas: text, data, stack, file etc.)– control structures kept by OS for book keeping (e.g., PID)

• fork():– allows to “clone” a process from an existing one;

• exec():– allows to “teach” a newly cloned process to do something

different from its prototype

Page 7: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

fork()

….int pid; …if ((pid=fork()) > 0) {//father

...}else

if (pid == 0) {// son...

}else

perror(“my message”);

Text

D ata

fork()

Stack

Text

D ata

fork()

Stack

P id: 1224

Pid: 12

Father P rocess

Son P rocess

Page 8: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

What Kernel really does

Kernel S tack

U ser S tack

U ser S tack

Son

Father

...

_fork()

...fork()

fork()

return 0

returnson's P ID

Page 9: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

Exec

….if ((pid=fork()) > 0) {//father

...}else

if (pid == 0) {// sonexec(“kuku”);perror(“failed”);exit(1);

}else

perror(“fork failed”);

Text

D ata

exec()

Stack

P id: 12

Pid: 12

Son P rocess

Son P rocess

Text

D ata

S tack

Page 10: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

UserRun

KernelRun

B locked

Readyto Run

Zom bie

Page 11: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

wait()• wait() is used:

– by a father process to synchronize with the execution of its son(s)– to find out the status and statistics about a son’s execution (e.g.,

how much CPU time was consumed by the son)

• wait comes in a variety of forms:– non-blocking wait()– blocking wait()– waitpid: for specific PID;

• Read more: – Richard W. Stevens “Adv. Unix Progr.”, pp.197-202– Maurice Bach “Unix”, pp. 212-217

Page 12: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

wait()/waitpid()

• wait() blocks the caller• Using wait() one cannot choose a specific process

to be “waited” • waitpid() does not block the caller if WNOHANG

option is used• waitpid() allows to choose a specific target

process (or group of processes)• Both wait() and waitpid() do not allow getting the

resource information about the finished son proc.

Page 13: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

wait3()/wait4()

• wait3() extends wait() by:– Allowing to specify an option for non-blocking

– Allowing to read the resource information

• wait4() extends waitpid() by:– Allowing to read the resource information

• Note:– Both wait3() and wait4() are applicable to your ex1.

– Read the manual pages on wait functions family (man 2 wait)

Page 14: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

Signals

• A means of Inter-Process Communication (IPC)• Are used to convey information about

asynchronous events in the system• “Software Interrupts”• Read more:

– Richard W. Stevens “Adv. Unix Progr.”, pp.263-279

– Maurice Bach “Unix”, pp. 200-211.

Page 15: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

Who sends signals?

• One process to another using syscall kill()

• A process to itself using raise()

• Kernel to its own processes and to the user’s ones:– For instance, when a child process terminates,

the father process is sent SIGCHLD signal by the kernel

Page 16: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

Signal Mask

• Every signal is uniquely identified by its non-negative integer number

• Depending on the version of UNIX there are 20-30 signals

• Signals are defined in /usr/include/signal.h• Every process has a signal mask:

– bit vector in which every entry corresponds to a specific signal (e.g., entry #9 corresponds to the signal #9, SIGKILL)

Page 17: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

Signal Mask

• When an entry in the signal mask equals “1” then the process wishes to receive the corresponding signal, otherwise it ignores the signal

• Some signals cannot be ignored, e.g., SIGSTOP, SIGKILL

• Process can define what action it wants to perform upon signal reception:– signal handler

Page 18: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

How SIGNALS are sent and received?

• int kill(int pid, int signum);• Mechanism:

– kernel accesses the entry #pid in the process table and sets “1” to the entry in a “vector of received signals”

– Then AND is performed on the mask and this vector

– handlers corresponding to the “1” entries of the resulting vector are called sequentially.

Page 19: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

When SIGNALS are received?

UserRun

KernelRun

B locked

Readyto Run

Zom biene

wly

fork

ed

Page 20: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

signal() syscall void (*signal(int signum, void (*handler)(int)))(int)

• simplified interface to sigaction()• return value: -1 in case of failure and previous

handler in case of success• process receiving the signal is interrupted and the

specified handler is called• future occurrences of the signals are blocked

(sequential processing)• SIG_IGN/SIG_DFL

Page 21: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

signal() example• In Linux the handler should be re-installed at the end

of signal handler (if future signals are to be caught)…if (signal(SIGCHLD, fire_man) == SIG_ERR) {

perror(“failed handler”);exit(1);

}…void fire_man(int signum) {

printf(“Some of your sons is(are) dead :(“);if (signal(SIGCHLD, fire_man) == SIG_ERR) {

perror(“failed handler”);exit(1);

}}

Page 22: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

Signals Limitations

• Signals can be used only on the same machine• Parameters (data) other than the signal number

cannot be passed to handlers• Therefore used only in order to communicate

various conditions asynchronously• Process can process signals only when it runs

Page 23: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

Peculiarities of Signals (I)• Many asynchronous events that cause signals may

happen very close in time• When a signal is caught it means only that at least

one instance of event has occurred• Example: a father spawns many children and

continues with other stuff. When a child finishes and enters the Zombie state, SIGCHLD signal is sent to father by the kernel. More than one child may finish, but the father receives only one signal.

Page 24: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

Peculiarities of Signals (II)

• Signals are asynchronous and thus may interfere with the system calls

• Most of the system calls are non-interruptible.• Some system calls, long calls, are interruptible:

– read

– write

– wait and some other

Page 25: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

Peculiarities of Signals (III)

• Example: signals and read() syscall– if signal is received before any data from the

device started to arrive, the call is interrupted and the handler is called. Afterwards the call is restarted.

– otherwise, the return value of read will not match the number of bytes requested. Errno will indicate EINTR condition.

Page 26: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

Signals and fork()/exec()

• Signal mask is inherited by a child as part of PSW• Handlers are also available since the text segment

is logically cloned.• When exec() succeeds, the handlers corresponding

to the signal mask of a process are cleared (why?)• The ignored signals remain ignored after exec()

Page 27: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

I/O syscalls• File is a collection of data stored on a long-term

media that can be collectively referred and manipulated. Goes beyond the lifetime of individual processes.

• Basic tools (syscalls) include:– open()/close()– read()/write()– unlink()/creat()

• Read more: – Richard W. Stevens “Adv. Unix Progr.”, pp.47-55, 61-

63.– Maurice Bach “Unix”, pp. 22-24, 91-108, 117-119

Page 28: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

File Descriptor

• Programs refer to files using non-negative integer values termed “file descriptors”.

• Every process is allocated a table (maintained by the kernel), known as “file descriptors table”

• Individual file descriptors are simply indices into this table

• File descriptors have no meaning across different processes

Page 29: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

File System

...

...

...

...

...

...

i-node tab le

FD table

FD table

F ile table

...

Page 30: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

Open Files and exec()

• Some information about the process that calls exec() is retained by the process after successful exec() system call

• Example: PID, sigmask, pending signals, resource limits

• Open files may be left open after exec(), this depends on whether FD_CLOEXEC flag is set for the descriptor

• By default FD_CLOEXEC is not set

Page 31: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

dup() and dup2()

include <unistd.h>

int dup(int fd)• Duplicates the specified fd into the first lowest

available file descriptor number. Returns fd of the copy.

int dup2(int fd1, int fd2)• Duplicates fd1 into fd2. Closes fd2 first.• Read more:

– Richard W. Stevens “Adv. Unix Progr.”, pp.61-63 – Maurice Bach “Unix”, pp. 22-24, 91-108, 117-119

Page 32: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

After dup()

...

...

...

...

...

i-node tab le

FD tab le

F ile tab le

......

Page 33: Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) os

Example

int fd;

fd = open(“foo”, O_WRONLY);

dup2(fd, 1);

close(fd);

What will happen?