1 / 55

W4118 Operating Systems

This lecture covers the topics of interrupts in Linux, system calls, parameter passing, and an introduction to processes and process dispatching.

patsysmith
Download Presentation

W4118 Operating Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. W4118 Operating Systems Instructor: Junfeng Yang

  2. Logistics • Teams • Those who haven’t found teammates, please stay after class • Homework 1 due before Thu’s class

  3. Last lecture • Interrupts in Linux (cont.): • Philosophy: do as little as possible in interrupt handler, and defer non-critical work later • Linux interrupt handling code is split into top half and bottom half • Top half: do as little work as possible in top half • Bottom half: handle deferred work • Softirq, tasklet, work queue

  4. Last lecture (cont.) • System calls in Linux • system call wrapper macros _syscallN (), system_call(),sys_call_table[] (arch/i386/kernel/entry.S) • Naming conventions (sys_*, do_*) • system call (strace) and lib call tracing (ltrace) • try “strace ls” • Parameter passing • Up to 6 in registers. If more, wrap parameters in struct, and pass pointer • All system call pointer parameters are untrusted; must access using “paranoid” functions

  5. Why can’t the kernel directly dereference user points? p = NULL; *p; p = NULL; syscall with p user kernel *p; page_fault_handler () { if (fault in user mode) { force_sig (faulting process, SEGFAULT); // kernel ok } else { // fault in kernel  bug! die (); // kernel dies !!! } }

  6. With “paranoid” routines p = NULL; *p; p = NULL; syscall with p p = NULL; syscall with p user x = get_user(p); kernel *p; page_fault_handler () { if (fault in user mode) { force_sig (faulting process, SEGFAULT); // kernel ok } else { // fault in kernel  bug! if (fault EIP in paranoid routines) call fixup code; // kernel ok else die (); // kernel dies } }

  7. Last lecture (cont) Intro to process What is it? Process is an execution stream in the context of a particular process state Process != program Program: static code + data Process: dynamic instance of code and data No 1:1 mapping Program > process: one program can create different processes Process > program: can have multiple processes of the same program

  8. Today Process (cont.) Address space Process dispatching Common process operations Interprocess communication

  9. Address Space (AS) • More details when discussing memory management • AS = All memory a process can address + addresses • Address space: • Really large memory to use • Linear array of bytes: [0, N), N roughly 2^32, 2^64 • Process and address space: 1 : 1 mapping • Key: an AS is a protection domain • One process can’t address another process’s address space (without permission) • E.g. 0x800abcd in process p1 points to different memory than 0x800abcd in process p2 • Thus can’t read/write

  10. Address space examples Process A Process B

  11. Process v.s. Thread Process != Thread (more when discussing threads) Threads: separate streams of executions that share an address space. “light weight process” One process can have many threads process thread main() { f(x); } f(int x) { } main() { f(x); } f(int x) { } stack main foo stack main foo stack main foo regs regs regs IP IP IP heap heap data data code code

  12. Why use processes? General principle of divide and conquer Decompose a large problem into smaller ones  easier to think well contained smaller problems Systems have many concurrent jobs going on E.g. Multiple users running multiple shells, I/O, … OS must manage these concurrent activities Easier to reason about processes than threads Sequential activities with well defined interactions

  13. System categorization • All OS support processes, but different number of processes • process = job • Uniprogramming: only one process at a time • Example: early systems, MSDOS • Good: simple • Bad: poor resource utilization, inconvenient for user • Multiprogramming: multiple processes at a time, when one process waits (e.g. I/O), switch to another • Example: Unix, Linux, Windows NT, solaris, all modern OS • Good: increase utilization, user convenience • Bad: OS complex • NOTE: different from multiprocessing (systems with multiple processors)

  14. Multiprogramming • OS requirements for multiprogramming • Policy: what proc to run? (later) • Mechanism: how to switch process? • Methods to protect process from one another (memory management) • Separation of policy and mechanism • Recurring theme in OS • Policy: decision making with some performance metric and workload • Scheduling (later) • Mechanism: low-level code to implement decisions • Dispatching (today)

  15. Process state diagram • Process state • New: being created • Running: instructions are running on CPU • Waiting: waiting for some event (e.g. IO) • Ready: waiting to be assigned a CPU • Terminated: finished

  16. Process dispatching mechanism • OS dispatching loop: while(1) { run process for a while; save process state; next process = schedule (ready processes); load next process state; } Q1: how to gain control? Q3: where to find processes? Q2: what state must be saved?

  17. Q1: How does Dispatcher gain control? • Must change from user mode to system mode • User process is in the “fetch-execute” cycle • Two ways OS gains control (review) • Hardware interrupts: external events • E.g. network card, disk controller, timer • OS gains control via Interrupt Service Routine (ISR) • Traps or Exceptions: generated by instructions running inside CPU • E.g. System calls, Errors, Page faults • OS gains control via trap/exception handlers

  18. Two approaches to obtaining control • Cooperative multitasking: OS trusts processes to voluntarily yield control back • When? System calls that relinquish CPU • E.g. sleep(), read() when data not ready • Why bad? OS trusts user processes !!! • Malicious process? Bugs?  reboot machine! • True Multitasking: OS preempts processes by periodic alarm clock • Dispatcher gains control on every hardware timer interrupt (CPU or other chip) • Eg. By default programmed to 1 ms in Linux on x86 CPU • Processes are assigned time slices • Varies for different processes (5ms – 800ms) • Dispatcher counts timer interrupts between context switch • Why good? OS trusts no one !

  19. Notes how OS obtains control • Can user processes disable timer interrupts? • No ! • What stack do we use to run interrupt/trap handlers? • Kernel stack • User stack may be too small, thus overflow in OS • User stack can be accessed (r/w) by user processes, thus malicious process can do bad things • Building OS is about being paranoid!

  20. Q2: What state must be saved? • Dispatcher must save state of process when not running, to later restore • How? On every interrupt or trap, save state in Process Control Block (PCB) • What goes into PCB? • Process state (running, ready …) • Program counter • CPU registers • CPU scheduling information • Memory-management information • Accounting information • I/O status information

  21. CPU Switch From Process to Process

  22. Context switch • Implementation: machine dependent • Tricky: OS must save state w/o changing state ! • Need to save all registers to PCB in memory • Run code to save registers, but code changes registers • Solution: hardware support • Performance? • Can take long. A lot of stuff to save and restore. The time needed is hardware dependent • Context switch time is pure overhead: the system does no useful work while switching

  23. Q3: where to find processes? • Data structure: process scheduling queues • Job queue– set of all processes in the system • Ready queue– set of all processes residing in main memory, ready and waiting to execute • Device queues– set of processes waiting for an I/O device • Processes migrate among the various queues when their states change

  24. Ready Queue And Various I/O Device Queues

  25. Representation of Process Dispatching

  26. Today Process (cont.) Address space Process dispatching Common process operations Interprocess communication

  27. Process Creation • Two ways to create a process • Build one from scratch • Clone an existing one • Option 1: From scratch • Load specified code and data into memory; create empty call stack • Create and initialize PCB (make look like context-switch) • Put process on ready list • Option 2: Cloning (e.g. fork()) • Stop current process and save its state • Make copy of PCB (can select what to copy) • Add new process PCB to ready list • Anything else ??? • Must differentiate parent and child

  28. UNIX process creation example: cloning • fork system call creates new process • exec system call overlays a new new program • Split of fork() and exec() were rather coincidental, but it turned out to work very well • Key: flexible, can do stuff between fork() & exec(). Tons of things you want to do in child, yet fork() takes no args! while (1) { write (1, "$ “, 2); parse_cmd (command, args); // parse user input switch(pid = fork ()) { case -1: perror (“fork”); break; case 0: // child execv (command, args, 0); break; default: // parent wait (0); break; // wait for child to terminate } }

  29. Process Creation in Win32: from scratch

  30. Process Termination • Process executes last statement and asks the operating system to delete it (exit(int status)) • Output data from child to parent • via wait(int* stat_loc) • Process’ resources are deallocated by operating system • Parent may terminate execution of children processes • Child has exceeded allocated resources • Task assigned to child is no longer required • If parent is exiting • Some operating system do not allow child to continue if its parent terminates: All children terminated - cascading termination

  31. Note on UNIX Process Termination • What happens if child exits before parent? • Parent must call wait() to “reap” child. OS will notify parent about child’s termination • If not, child process becomes a zombie process • What happens if parent exit before child? • Orphaned processes • Re-parented to process 1, the init process while (1) { write (1, "$ “, 2); parse_cmd (command, args); // parse user input switch(pid = fork ()) { case -1: perror (“fork”); break; case 0: // child execv (command, args, 0); break; default: // parent wait (0); break; // wait for child to terminate } }

  32. Today Process (cont.) Address space Process dispatching Common process operations Interprocess communication

  33. Cooperating Processes • Independent process cannot affect or be affected by the execution of another process. • Cooperating process can affect or be affected by the execution of another process • Advantages of process cooperation • Information sharing • Computation speed-up • Modularity/Convenience

  34. Interprocess Communication Models Message PassingShared Memory

  35. Message Passing v.s. Shared Memory • Message passing • Why good? Simpler. All sharing is explicit • Why bad? Overhead. Data copying, cross protection domains • Shared Memory • Why good? Performance. Set up shared memory once, then access w/o crossing protection domains • Why bad? Complex

  36. IPC Example: Unix signals • Signals • A very short message: just a small integer • A fixed set of available signals. Examples: • 9: kill • 11: segmentation fault • Installing a handler for a signal • sighandler_t signal(int signum, sighandler_t handler); • Send a signal to a process • kill(pid_t pid, int sig) • Can be issued by users, kernel, or processes

  37. IPC Example: Unix pipe • int pipe(int fd[2]); • Returns two file descriptors in fd[0] and fd[1]; • Writes to fd[1] will be read on fd[0] • When last copy of fd[1] closed, fd[0] will return EOF • Return 0 on success, -1 on error • Operations on pipes: • read/write/close --- as with files • When fd[1] closed, read(fd[0]) returns 0 bytes • When fd[0] closed, write(fd[1]): • Kills process with SIGPIPE, or if blocked • Failes with EPIPE

  38. IPC Example: Unix pipe (con.t) int pipefd[2]; pipe(pipefd); switch(pid=fork()) { case -1: perror("fork"); exit(1); case 0: close(pipefd[0]); // write to fd 1 break; default: close(pipefd[1]); // read from fd 0 break; }

  39. IPC Example: Unix Shared Memory • int shmget(key_t key, size_t size, int shmflg); • Create a shared memory segment • key: unique identifier of a shared memory segment, or IPC_PRIVATE • int shmat(int shmid, const void *addr, int flg) • Attach shared memory segment to address space of the calling process • shmid: id returned by shmget() • int shmdt(const void *shmaddr); • Detach from shared memory • Problem: synchronization! (later)

  40. Backup slides

  41. Message Passing • Message system – processes communicate with each other without resorting to shared variables • Message passing facility provides two operations: • send(message) – message size fixed or variable • receive(message) • If P and Q wish to communicate, they need to: • establish a communicationlink between them • exchange messages via send/receive • Implementation of communication link • physical (e.g., shared memory, hardware bus) • logical (e.g., logical properties)

  42. Implementation Questions • How are links established? • Can a link be associated with more than two processes? • How many links can there be between every pair of communicating processes? • What is the capacity of a link? • Is the size of a message that the link can accommodate fixed or variable? • Is a link unidirectional or bi-directional?

  43. Direct Communication • Processes must name each other explicitly: • send (P, message) – send a message to process P • receive(Q, message) – receive a message from process Q • Properties of communication link • Links are established automatically • A link is associated with exactly one pair of communicating processes • Between each pair there exists exactly one link • The link may be unidirectional, but is usually bi-directional

  44. Indirect Communication • Messages are directed and received from mailboxes (also referred to as ports) • Each mailbox has a unique id • Processes can communicate only if they share a mailbox • Properties of communication link • Link established only if processes share a common mailbox • A link may be associated with many processes • Each pair of processes may share several communication links • Link may be unidirectional or bi-directional

  45. Indirect Communication • Operations • create a new mailbox • send and receive messages through mailbox • destroy a mailbox • Primitives are defined as: send(A, message) – send a message to mailbox A receive(A, message) – receive a message from mailbox A

  46. Indirect Communication • Mailbox sharing • P1, P2, and P3 share mailbox A • P1, sends; P2and P3 receive • Who gets the message? • Solutions • Allow a link to be associated with at most two processes • Allow only one process at a time to execute a receive operation • Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.

  47. Synchronization • Message passing may be either blocking or non-blocking • Blocking is considered synchronous • Blocking sendhas the sender block until the message is received • Blocking receivehas the receiver block until a message is available • Non-blocking is considered asynchronous • Non-blocking send has the sender send the message and continue • Non-blocking receive has the receiver receive a valid message or null

  48. Buffering • Queue of messages attached to the link; implemented in one of three ways 1.Zero capacity– 0 messagesSender must wait for receiver (rendezvous) 2.Bounded capacity– finite length of n messagesSender must wait if link full 3. Unbounded capacity– infinite length Sender never waits

  49. Client-Server Communication • Sockets • Remote Procedure Calls • Remote Method Invocation (Java)

  50. Sockets • A socket is defined as an endpoint for communication • Concatenation of IP address and port • The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8 • Communication consists between a pair of sockets

More Related