(a) What is the output generated by this program? In fact the output is not uniquely defined, i.e., it is not always the same. So please give three examples.

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

Chapter 3 Process Description and Control
CSCC69: Operating Systems
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
CSE 451: Operating Systems Winter 2006 Module 4 Processes Ed Lazowska Allen Center 570.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Processes CSCI 444/544 Operating Systems Fall 2008.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
Introduction to Kernel
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
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-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
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.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
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.
What is the output generated by this program? Please assume that each executed print statement completes, e.g., assume that each print is followed by an.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
What is the output generated by this program? (Assume that there are no errors or failures.) [30 pts] CPS 310 final exam, 12/12/2014 Your name please:
Protection and the Kernel: Mode, Space, and Context.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
(a) What is the output generated by this program? In fact the output is not uniquely defined, i.e., it is not necessarily the same in each execution. What.
Introduction to Processes CS Intoduction to Operating Systems.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Processes: program + execution state
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
1 CSE 451 Section 2: Interrupts, Syscalls, Virtual Machines, and Project 1.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
(a) What is the output generated by this program? In fact the output is not uniquely defined, i.e., it is not always the same. So please give three examples.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Operating Systems Process Creation
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
(a) What is the output generated by this program? In fact the output is not uniquely defined, i.e., it is not necessarily the same in each execution. What.
Processes and Virtual Memory
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
Process Management Azzam Mourad COEN 346.
Consider the Java code snippet below. Is it a legal use of Java synchronization? What happens if two threads A and B call get() on an object supporting.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Lecture 5 Page 1 CS 111 Online Process Creation Processes get created (and destroyed) all the time in a typical computer Some by explicit user command.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 1.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Threads prepared and instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University 1July 2016Processes.
Introduction to Kernel
/50 /60 /40 /30 A Tale of Two Clients
Protection of System Resources
Unix Process Management
Process Creation Processes get created (and destroyed) all the time in a typical computer Some by explicit user command Some by invocation from other running.
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
System Structure and Process Model
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Process Description and Control
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Processes in Unix, Linux, and Windows
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
Process Description and Control in Unix
CSE 153 Design of Operating Systems Winter 2019
Process Description and Control in Unix
Presentation transcript:

(a) What is the output generated by this program? In fact the output is not uniquely defined, i.e., it is not always the same. So please give three examples of possible outputs. CPS 310 first midterm exam, 10/9/2013 Your name please: Part 1. Fun with forks (b) Briefly justify/explain your answer for (a). Try to characterize the set of all possible outputs. int i = 0; main() { printf(“%d\n”, i); /* print i on a line */ fork(); i = i + 1; printf(“%d\n”, i); fork(); i = i + 1; printf(“%d\n”, i); } (In columns, of course.) The program executes in a process. The first fork creates a child process. After the first fork, both parent and child execute the remainder of the program. Both execute the second fork, creating two more processes. Refer to the figure on the following page. The processes may execute in various orders/schedules, including the schedules that produce the outputs listed for part (a) above. In particular, each legal schedule corresponds to a traversal of the tree in which each node of the tree is visited before any of its children: a process can run only after the fork that creates it. Part (a) is worth 30 points: 15 for one correct output, 25 for two demonstrating an awareness of concurrency, and 30 for three. Any clear explanation gets the rest of the points. Some answers gave alternative outputs presuming that one or both of the forks failed. These got some partial credit.

Partial order / dependency graph int i = 0; main() { printf(“%d\n”, i); fork(); i = i + 1; printf(“%d\n”, i); fork(); i = i + 1; printf(“%d\n”, i); } 0 fork

CPS 310 first midterm exam, 10/9/2013, page 2 of 7 Part 2. Inside the shell A typical Unix shell executes many of its commands by invoking (launching) a named program in a child process. In some respects that is similar to calling a local subprogram (e.g., a procedure in a library). In particular, the caller can pass arguments into the (sub)program, the program runs until it completes, and then the program returns a result to the caller. (a) How does your Lab #2 shell pass arguments into a program running in a child process? Feel free to illustrate with drawings or pseudocode. How is this different from passing arguments into a local subprogram? (b) How does your Lab #2 shell obtain a result from a program running in a child process? Feel free to illustrate with drawings or pseudocode. How is this different from obtaining a result from a local subprogram? The shell uses exec* syscall (execvp, execve) to “launch a named program in a child process”. Exec* allows the caller to pass (argc, argv[]) as arguments. The kernel installs the arguments in the program’s virtual address space and passes them to the program’s “main”. Other useful detail: the arguments are limited to strings. The argv is a variable- sized array of variable-length strings. These are copied through the kernel, but the kernel does not ever interpret them. Also, an array of environment variables (strings of the form “name=value”) are preserved in the address space across exec*, and are also passed into the program’s main(). The kernel does not interpret environment variables either. Arguments are passed to a local subprogram in registers or on the stack, with no kernel involvement. They may include data of any type, including pointers to data of any type anywhere in the address space. 2a is worth 10 points. I gave at least 7 for any answer that mentioned exec syscall and argv. Some answers focused on stdin/stdout and did not mention exec syscall or argv. I gave those 3/10 in general. A child process may pass a result (status) to the exit() syscall when the program completes. The parent obtains the result via the wait*() syscall, e.g., waitpid. Other useful supporting detail: the exit status is limited to a single integer. It is copied through the kernel. A local subprogram may return one or more results in registers or on the stack, or by writing any data anywhere in the shared address space, with no kernel involvement. The results may include data of any type. 2b is worth 10 points. I gave at least 7 for any answer that mentioned exit/wait status. Some answers focused on stdin/stdout. I gave those 3/10 in general.

(c) The shell interprets any file names (pathnames) relative to the shell’s current directory. The cd command changes the current directory. But cd is a builtin command: the shell does not fork a child process to execute a cd. Why? How does cd work? CPS 310 first midterm exam, 10/9/2013, page 3 of 7 (d) Children of the shell also interpret file names relative to the shell’s current directory. How do they “know” what directory to use? What happens if the shell performs a cd while a child is running? The kernel tracks the current directory of each process. A process may change its current directory using the chdir() syscall. The shell implements cd by invoking chdir directly: if the shell forked a child, the child could use chdir to change its own current directory, but cannot affect the current directory of the parent. So cd would not work. Other useful detail: Any syscall that takes a pathname argument (open, mkdir, rmdir, link, unlink, symlink…) interprets the pathname relative to the current directory of the requesting process, unless the pathname begins with a “/”, in which case the kernel interprets the pathname relative to the root directory. Shells and other user [programs may also keep track of the pathname of the current directory, e.g., in an environment variable. However, this optional choice does not involve the kernel and does not affect the kernel’s handling of pathname arguments passed to system calls. 2c was worth 10 points. I gave 5 points for anything reasonable, but you had to mention chdir syscall to get all 10. Like other kernel state for a process, the process current directory is inherited across fork(). As with other process state, the parent and child may diverge after the fork. In particular, any change to the current directory of one process (via chdir) does not affect the other. 2d was worth 10 points. I gave 5 points for anything reasonable.

Part 3. Fixing the plumbing For some command lines the shell (dsh) spawns two child processes connected by a pipe. (a) The following pseudocode proposes sequences of system calls for the parent shell and for each child to set up the pipe. Naturally, the sequences are incorrect. Point out three key errors in the sequences. For each error, write a sentence or two explaining why it is wrong and how to fix it. Extra credit: estimate how many hours of sleep each error will cost you. Justify your answer without reference to “tcsetpgrp” or “setpgid”. CPS 310 first midterm exam, 10/9/2013, page 4 of 7 parent fork(); wait*(…); fork(); wait*(…); left child pipe(); close(…); dup2(…); exec*(…); right child close(…); dup2(…); exec*(…); (1)Do the pipe() syscall in the parent, and not in a child. The parent must do it so that each child may inherit the file descriptors for both ends of the pipe. A pipe created in a child cannot enable communication with a sibling. (2)Fork both children before waiting for either to exit. As structured, the first child must complete before the second child can start. It is then impossible for them to run concurrently as intended. Worse, in a typical shell the left child leads the process group for the job, and the process group is destroyed when the leader exits, causing errors in any attempt to bind a new process to the same group after the second fork. (3)The parent should close both ends of the pipe. The kernel reference-counts the pipe descriptors: if the parent holds the write end open, then the kernel presumes the parent may intend to write to the pipe, and it does not terminate the reader (right child) properly when the writer (left child) exits. (4)Each child should close whichever end of the pipe it is not using (e.g., the close before the dup2), and then close the original descriptor for the other end after the dup2. 3a was worth 20 points. 7 points each for any clear identification of the first two major bugs, and six points for a third. Partial credit as warranted. Some students hypothesized errors not shown in the pseudocode, like passing the wrong arguments or not checking the error returns. But the question asked for bugs in the sequences. I didn’t give much if any credit for those answers.

(b) Once the pipe is set up correctly the children may pass any amount of data through it. Suppose the total amount of data passed through the pipe is larger than the memory of the machine. Show how Unix uses context switches to avoid having to hold all of the data in memory at once. Feel free to draw a picture. CPS 310 first midterm exam, 10/9/2013, page 5 of 7 Once the pipe is established, the left child issues a series of write system calls to write bytes into the pipe, while the right child issues a series of read system calls to read bytes from the pipe. Like all system calls, read and write trap to the kernel and execute in kernel mode in the kernel space. Write copies bytes from the writer process (user space) into a kernel memory buffer for the pipe. Read copies bytes from the kernel memory buffer into the reader process (user space). If the buffer is full, then write blocks, putting the writer process to sleep until there is space in the buffer again. If the buffer is empty, then read blocks, putting the reader process to sleep until there are bytes in the buffer again. If either process blocks, the kernel context switches to the other process. If the pipe buffer is full, then the writer blocks and the reader runs to drain it. If the buffer is empty, then the reader blocks and the writer runs to fill it. The kernel may context switch at other times as it chooses. But in general it will alternate between the reader and writer to pump the bytes through the pipe. I gave 20 points for any reasonable answer, with partial credit as warranted. I wanted you to say that read/write are blocking system calls, but did not require it. I took points off for semantically garbled explanations, but allowed for vagueness. “Producer/consumer bounded buffer” is a full-credit answer.

Part 4. Piling on the heap Misuse of heap-managed storage (malloc/free) can cause a C program to crash. In fact, some kinds of program errors can cause the process to crash in the heap manager, even if the heap manager code itself is correct. Give three examples with reference to your solution for Lab #1. Feel free to illustrate with pseudocode or drawings. CPS 310 first midterm exam, 10/9/2013, page 6 of 7 The key point here is that the program and the heap manager reside in the same address space and are not protected from one another, at least not for program written in a type-unsafe language like C. We discussed a number of common errors for C programs using heap memory: dangling references (using a block after a call to free), memory leaks (failing to call free), buffer overflows (writing memory past the end of an allocated block), double frees (freeing the same block twice), and pointer arithmetic errors that confuse the start of a block, e.g., to pass free a pointer that is not the start of a currently allocated block. I gave all 40 points to any two good errors with a good explanation. To get the points you had to say something about how these errors could cause a failure in the heap manager, e.g., by corrupting data structures in the heap manager, such as the freelist or block headers. Some students sketched out scenarios involving failures outside the heap manager, or bugs or limitations in the heap manager, such fragmentation. Those got a little bit of partial credit because I am a softie, and not for merit. Note also that heap manager operations do not affect the validity of any part of the address space (except for heap region allocations, e.g., using sbrk).

Part 5. Crossing the border (a) How does the classical Unix kernel choose the user ID for a process launched with fork/exec*? (b) What causes the machine to enter privileged (kernel) mode? Upon entering kernel mode, what instruction does the machine execute next, i.e., how is the value in the Program Counter (or Instruction Pointer) register determined? (c) What causes the machine to enter unprivileged (user) mode? Upon entering user mode, what instruction does the machine execute next, i.e., how is the value in the Program Counter (or Instruction Pointer) register determined? (d) What is a kernel stack? Where do kernel stacks come from? Why do they exist? (e) What is a stub? Where do stubs come from? Why do they exist? CPS 310 first midterm exam, 10/9/2013, page 7 of 7 Inherited across fork. If the program is a setuid program, then exec changes it to the owner of the program. Trap, fault, interrupt. Begins executing in a handler routine chosen by the kernel. Kernel can do this any time, and can control how the PC is set, along with the rest of the register context. Common case is return from trap/fault/interrupt to the next instruction. However, after a fault the kernel might set the PC to reexecute the faulting instruction. The kernel can trigger a transition to user mode at any PC of its choice: e.g., to start in main on exec, or to upcall a signal handler. A region of kernel-space memory used to store the runtime stack of a thread executing in the kernel, i.e., in a trap or fault. They are created on thread creation. They maintain a thread’s call chain and local variables when the thread is not running, e.g., to allow a thread to block in the kernel and then wake up and resume whatever it was doing. A short routine that is called to initiate a protected procedure call, such as a system call or an RPC call. Stubs for system calls are in the standard runtime library. Stubs for RPC interfaces are generated during the build process by a stub generator, based on an specification of the RPC interface in an Interface Description Language.

Midterm 13F1 P1 int i = 0; main() { printf(“%d\n”, i); fork(); i = i + 1; printf(“%d\n”, i); fork(); i = i + 1; printf(“%d\n”, i); }

Partial order / dependency graph int i = 0; main() { printf(“%d\n”, i); fork(); i = i + 1; printf(“%d\n”, i); fork(); i = i + 1; printf(“%d\n”, i); } 0 fork