asynchronous notifications
play

Asynchronous notifications Everything must change, so that - PowerPoint PPT Presentation

The Skinny System calls Web Servers Compilers Syscall interface allows Programming interface to the Word Processing User Program Databases separation of concern services provided by the OS Web Browsers Email open() Application can think


  1. The Skinny System calls Web Servers Compilers Syscall interface allows Programming interface to the Word Processing User Program Databases separation of concern services provided by the OS Web Browsers Email open() Application can think of OS as providing a Innovation library of services Portable OS Library system call interface Narrow much care spent in keeping interface System call interface secure interface open() simple implementation of e.g., parameters are copied to kernel open() system call i Portable OS Kernel . powerful space before they are checked . . Mostly accessed through an API highly portable return x86 ARM PowerPC (Application Programming robust 10Mbps/100Mbps/1Gbps Ethernet Interface) 1802.11 a/b/g/n SCSI Win32, POSIX, Java API Graphics accellerators LCD Screens Asynchronous notifications “Everything must change, so that everything can stay the same” in user space “The Leopard”, by T. di Lampedusa Interrupts inform kernel of asynchronous events — what Interrupts/Exceptions Signals/Upcalls about processes? Hardware-defined Kernel-defined Signals (UNIX); Asynchronous events (Windows) Why? Interrupt vector for Handlers (user) handlers (kernel) pre-empting user level threads Signal stack or process asynchronous I/O Interrupt stack (kernel) stack (user) suspending/resuming a process (e.g., for debugging) adapting to changing HW resources provided by OS (e.g., memory) Signal masking (user) Interrupt masking (kernel) Upon receipt Processor State (user) Ignore Processor state (kernel) Terminate process Catch through handler

  2. “Everything must change, so that Booting an OS Kernel everything can stay the same” “The Leopard”, by T. di Lampedusa Bootloader Bootloader Interrupts/Exceptions Signals/Upcalls OS Kernel Login app Hardware-defined Kernel-defined Interrupt vector for Handlers (user) handlers (kernel) BIOS Signal stack or process Interrupt stack (kernel) stack (user) Basic Input/Output System Signal masking (user) Interrupt masking (kernel) In ROM; includes the first instructions fetched and executed Processor State (user) Processor state (kernel) BIOS copies Bootloader, checking its cryptographic hash 1 to make sure it has not been tampered with Booting an OS Kernel Booting an OS Kernel Bootloader Bootloader OS Kernel OS Kernel OS Kernel Login app Login app BIOS BIOS OS Kernel Bootloader Bootloader Bootloader copies OS Kernel, Bootloader copies OS Kernel, 2 2 checking its cryptographic hash checking its cryptographic hash

  3. Booting an OS Kernel Booting an OS Kernel Bootloader Bootloader OS Kernel OS Kernel Login app Login app Login app BIOS BIOS OS Kernel OS Kernel Bootloader Bootloader Kernel initializes its data structures 3 4 Kernel: Copies first process from disk (devices, interrupt vector table, etc) Booting an OS Kernel Shall we dance? All processes are progeny of that first process Bootloader OS Kernel Created with a little help from its friend… Login app BIOS OS Kernel Login app Bootloader CreateProcess (Windows) 4 Kernel: Copies first process from disk …via system calls! Changes PC and sets mode bit to 1 fork + exec (UNIX) And the dance begins!

  4. Starting a new process: Which API? the recipe Windows: CreateProcess System Call (simplified) 1. Allocate & initialize PCB if (!CreateProcess( NULL, / / No module name (use command line) 2. Create and initialize a new address space argv[1], / / Command line 3. Load program intro address space NULL, / / Process handle not inheritable NULL, / / Thread handle not inheritable 4. Allocate user-level and kernel-level stacks. FALSE, / / Set handle inheritance to FALSE 5. Initialize HW context to begin execution at start 0, / / No creation flags NULL, / / Use parent's environment block 6. Copy arguments (if any) to the base of the user- NULL, / / Use parent's starting directory &si, / / Pointer to STARTUPINFO structure level stack &pi ) / / Ptr to PROCESS_INFORMATION structure 7. Inform scheduler that a new process is ready ) 8.Transfer control to user mode Which API? In action Unix: fork() and exec() #include <stdio.h> #include <unistd.h> int main() { fork() int child_pid = fork(); int pid = fork() if (child_pid == 0) { / / child process printf("I am process #%d\n", getpid()); Creates a complete copy (child) of return 0; the invoking process (parent) } else { / / parent process printf("I am the parent of process #%d\n", child_pid); Returns twice (!), to both the parent return 0; and the child process, setting pid to } } different values for the child: pid := 0; Possible outputs? for the parent: pid := child’ s process id

  5. Which API? wait() and exit() Unix: fork() and exec() wait() causes parent to wait until child terminates parent gets return value from child fork() exec() if no children alive, wait() returns immediately Loads executable in memory & int pid = fork() starts executing it exit() is called after program terminates Creates a complete copy (child) of code, stack, heap are the invoking process (parent) overwritten closes open files the process is now running a Returns twice (!), to both the parent deallocates memory and the child process, setting pid to different program! different values deallocates most OS structures for the child: pid := 0; checks if parent is alive. If so… for the parent: pid := child’ s process id Creating and managing In action processes Process 13 Process 13 Program A Program A pid = fork(); pid = fork(); PC PC Syscall Description if (pid==0) if (pid==0) exec(B); exec(B); pid pid Create a child process as a clone of the current process. Return to both else else fork() parent and child. Return child’ s pid to parent process; return 0 to child ? 14 wait(&status); wait(&status); Run the application prog in the current context with the exec Process 14 Process 14 specified args (proc, args) Program A Program B wait pid = fork(); Pause until some child process has exited PC PC main() { (&status) if (pid==0) … exec(B); Tell kernel current process is complete and its data structures exit(3); exit pid pid else (stack, heap, code) should be garbage collected. May keep PCB. } (status) 0 0 wait(&status); Send a signal of a specified type to a process kill (a bit of an overdramatic misnomer…) (pid, type)

  6. In action What is a shell? Process 13 Process 13 Program A Program A Job control system pid = fork(); pid = fork(); Status PC PC if (pid==0) if (pid==0) 3 Runs programs on behalf of the user exec(B); exec(B); pid pid else else ? 14 wait(&status); wait(&status); Allows programmer to create/manage set of programs sh Original Unix shell (Bourne, 1977) Process 14 Program B csh BSD Unix C shell (tcsh enhances it) PC main() { bash “Bourne again” shell … exit(3); Every command typed in the shell starts a child process } of the shell Runs at user-level. Uses syscalls: fork, exec, etc. The Unix shell (simplified) More on signals while(! EOF) Default read input ID Name Corresponding Event Action handle regular expressions Interrupt 2 SIGINT Terminate int pid = fork() / / create child (e.g., CTRL-C from keyboard) if (pid == 0) { / / child here Kill program 9 SIGKILL Terminate exec(“program”, argc, argv0,...); (cannot override or ignore) } 14 SIGALRM Terminate Timer signal else { / / parent here ... 17 SIGCHLD Ignore Child stopped or terminated } Stop until Stop signal from terminal 20 SIGSTP SIGCONT (e.g., CTRL-Z from keyboard)

  7. void int_handler(int sig) { printf("Process %d received signal %d\n", getpid(), sig); exit(0); int main() { } pid_t pid[N]; int main() { int i, child_status; Signal Handler pid_t pid[N]; int i, child_status; for (i = 0; i < N; i++) / / N forks signal(SIGINT, int_handler) / / register handler for SIGINT if ((pid[i] = fork()) == 0) { for (i = 0; i < N; i++) / / N forks while(1); / / child infinite loop Example Example if ((pid[i] = fork()) == 0) { } while(1); / / child infinite loop /* Parent terminates the child processes */ } for (i = 0; i < N; i++) { / / parent continues executing /* Parent terminates the child processes */ printf("Killing proc. %d\n", pid[i]); for (i = 0; i < N; i++) { / / parent continues executing kill(pid[i], SIGINT); printf("Killing proc. %d\n", pid[i]); } kill(pid[i], SIGINT); /* Parent reaps terminated children */ } for (i = 0; i < N; i++) { /* Parent reaps terminated children */ pid_t wpid = wait(&child_status); for (i = 0; i < N; i++) { if (WIFEXITED(child_status)) / / parent checks for each child’ s exit pid_t wpid = wait(&child_status); printf("Child %d terminated w/ exit status %d\n", wpid, if (WIFEXITED(child_status)) / / parent checks for each child’ s exit WEXITSTATUS(child_status)); printf("Child %d terminated w/ exit status %d\n", wpid, else WEXITSTATUS(child_status)); printf("Child %d terminated abnormally\n", wpid); else } printf("Child %d terminated abnormally\n", wpid); exit(0); } } exit(0); } Kernel Operation (conceptual, simplified) Initialize devices Initialize “first process” while (TRUE) { while device interrupts pending - handle device interrupts CPU Scheduling while system calls pending - handle system calls if run queue is non-empty - select a runnable process and switch to it otherwise - wait for device interrupt }

Recommend


More recommend