Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation) www.cs.huji.ac.il/~os."— Presentation transcript:

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

2 Agenda Basic OS concepts: –kernel and system calls; –process: fork()/exec()/wait() –signals: signal() –file system: open()/close()/read()/write()/dup()

3 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

4 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

5 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.

6 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

7 fork() …. int pid; … if ((pid=fork()) > 0) {//father... } else if (pid == 0) {// son... } else perror(“my message”);

8 What Kernel really does

9 Exec …. if ((pid=fork()) > 0) {//father... } else if (pid == 0) {// son exec(“kuku”); perror(“failed”); exit(1); } else perror(“fork failed”);

10

11 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

12 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.

13 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)

14 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.

15 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

16 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)

17 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

18 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.

19 When SIGNALS are received?

20 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

21 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); }}

22 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

23 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.

24 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

25 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.

26 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()

27 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

28 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

29 File System

30 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

31 dup() and dup2() include 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

32 After dup()

33 Example int fd; … fd = open(“foo”, O_WRONLY); dup2(fd, 1); close(fd); … What will happen?


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

Similar presentations


Ads by Google