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

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
CSCC69: Operating Systems
Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
6/9/2015B.Ramamurthy1 Process Description and Control B.Ramamurthy.
Process Description and Control
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes Professor Jennifer Rexford
Architectural Support for OS March 29, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
Page 1 Processes and Threads Chapter 2. Page 2 Processes The Process Model Multiprogramming of four programs Conceptual model of 4 independent, sequential.
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Introduction to Kernel
CSCE 351: Operating System Kernels
OS Spring’03 Introduction Operating Systems Spring 2003.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
CSSE Operating Systems
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Process Description and Control A process is sometimes called a task, it is a program in execution.
OS Spring’04 Introduction Operating Systems Spring 2004.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Processes CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Creating and Executing Processes
LINUX System : Lecture 7 Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System.
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
System calls for Process management
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
Silberschatz, Galvin and Gagne  Operating System Concepts Process Concept An operating system executes a variety of programs:  Batch system.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Lecture Topics: 10/29 Architectural support for operating systems –timers –kernel mode –system calls –protected instructions.
Operating Systems Process Creation
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
4300 Lines Added 1800 Lines Removed 1500 Lines Modified PER DAY DURING SUSE Lab.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
Processes and Threads MICROSOFT.  Process  Process Model  Process Creation  Process Termination  Process States  Implementation of Processes  Thread.
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
System calls for Process management Process creation, termination, waiting.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 1.
Exceptional Control Flow
Introduction to Kernel
Protection of System Resources
Tarek Abdelzaher Vikram Adve Marco Caccamo
Exceptional Control Flow: System Calls, Page Faults etc.
Structure of Processes
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Process Description and Control
Architectural Support for OS
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Process Description and Control
LINUX System : Lecture 7 Lecture notes acknowledgement : The design of UNIX Operating System.
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
CS510 Operating System Foundations
Architectural Support for OS
Process Description and Control in Unix
Process Description and Control in Unix
Presentation transcript:

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

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

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

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

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 –Maurice Bach “Unix”, pp , ,

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

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

What Kernel really does

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

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 –Maurice Bach “Unix”, pp

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.

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)

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 –Maurice Bach “Unix”, pp

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

Signal Mask Every signal is uniquely identified by its non- negative integer number Depending on the version of UNIX there are 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)

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

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.

When SIGNALS are received?

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

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

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

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.

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

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.

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

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, –Maurice Bach “Unix”, pp , ,

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

File System

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

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 –Maurice Bach “Unix”, pp , ,

After dup()

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