Lecture notes on introduction to Operating systems

Operating Systems Course Notes and advanced operating systems lecture notes | pdf free download
DavidCooper Profile Pic
DavidCooper,Singapore,Researcher
Published Date:11-07-2017
Your Website URL(Optional)
Comment
1 CS 350 Operating Systems Course Notes Fall 2007 David R. Cheriton School of Computer Science University of Waterloo2 Intro 1 What is an Operating System? • Three views of an operating system Application View: what services does it provide? System View: what problems does it solve? Implementation View: how is it built? An operating system is part cop, part facilitator. CS350 Operating Systems Fall 2007 Intro 2 Application View of an Operating System • The OS provides an execution environment for running programs. – The execution environment provides a program with the processor time and memory space that it needs to run. – The execution environment provides interfaces through which a program can use networks, storage, I/O devices, and other system hardware components. ∗ Interfaces provide a simplified, abstract view of hardware to application programs. – The execution environment isolates running programs from one another and prevents undesirable interactions among them. CS350 Operating Systems Fall 20073 Intro 3 Other Views of an Operating System System View: The OS manages the hardware resources of a computer system. • Resources include processors, memory, disks and other storage devices, network interfaces, I/O devices such as keyboards, mice and monitors, and so on. • The operating system allocates resources among running programs. It controls the sharing of resources among programs. • The OS itself also uses resources, which it must share with application programs. Implementation View: The OS is a concurrent, real-time program. • Concurrency arises naturally in an OS when it supports concurrent applications, and because it must interact directly with the hardware. • Hardware interactions also impose timing constraints. CS350 Operating Systems Fall 2007 Intro 4 Schematic View of an Operating System User Programs system call system calls results user−space system call interface kernel Operating System commands data and interrupts and data Resources CS350 Operating Systems Fall 20074 Intro 5 Operating System Abstractions • The execution environment provided by the OS includes a variety of abstract entities that can be manipulated by a running program. Examples: files and file systems: abstract view of secondary storage address spaces: abstract view of primary memory processes, threads: abstract view of program execution sockets, pipes: abstract view of network or other message channels • This course will cover – why these abstractions are designed the way they are – how these abstractions are manipulated by application programs – how these abstractions are implemented by the OS CS350 Operating Systems Fall 2007 Intro 6 Course Outline • Introduction • Simple C Primer • Processes and Threads • Concurrency and Synchronization • The Kernel and System Calls • Address Spaces and Virtual Memory • Scheduling • Devices and Device Management • File Systems • Interprocess Communication and Networking • Security CS350 Operating Systems Fall 20075 C-Primer 1 Using Memory include stdio.h include stdlib.h struct foo_struct int x; char a; char b; char c; char d; ; int main() int i; char a40; int iptr = (int ) a; struct foo_struct sptr = (struct foo_struct ) a; CS350 Operating Systems Fall 2007 C-Primer 2 Using Memory for (i=0; i40; i++) ai = (char) i; for (i=0; i10; i++) printf("%2d = 0x%08x\n", i, iptri); printf("x = 0x%08x a = %d b = %d c = %d d = %d\n", sptr0.x, (int) sptr0.a, (int) sptr0.b, (int) sptr0.c, (int) sptr0.d); exit(0); CS350 Operating Systems Fall 20076 C-Primer 3 Using Memory: Example Output (OS/161) 0 = 0x00010203 1 = 0x04050607 2 = 0x08090a0b 3 = 0x0c0d0e0f 4 = 0x10111213 5 = 0x14151617 6 = 0x18191a1b 7 = 0x1c1d1e1f 8 = 0x20212223 9 = 0x24252627 x = 0x00010203 a = 4 b = 5 c = 6 d = 7 CS350 Operating Systems Fall 2007 C-Primer 4 Arrays and Addresses include stdio.h include stdlib.h static char alpha = "abcdefghijklmnopqrstuvwxyz"; int main() char array12; char value = 0; int i; CS350 Operating Systems Fall 20077 C-Primer 5 Arrays and Addresses for (i=0; i12; i++) arrayi = alphai; printf("addr of array = %p\n", &array); printf("addr of array0 = %p\n", &array0); printf(" array = %c\n", array); printf("addr of value = %p\n", &value); printf("addr of value0 = %p\n", &value0); printf("value = %p\n", value); printf("\n"); CS350 Operating Systems Fall 2007 C-Primer 6 Arrays and Addresses value = array; printf("addr of value = %p\n", &value); printf("addr of value0 = %p\n", &value0); printf("value = %p\n", value); printf(" value = %c\n", value); printf("\n"); value = &array4; printf("addr of value = %p\n", &value); printf("addr of value0 = %p\n", &value0); printf("value = %p\n", value); printf(" value = %c\n", value); printf("\n"); exit(0); CS350 Operating Systems Fall 20078 C-Primer 7 Arrays and Addresses: Example Output (OS/161) addr of array = 0x7fffffd0 addr of array0 = 0x7fffffd0 array = a addr of value = 0x7fffffe0 addr of value0 = 0x0 value = 0x0 addr of value = 0x7fffffe0 addr of value0 = 0x7fffffd0 value = 0x7fffffd0 value = a addr of value = 0x7fffffe0 addr of value0 = 0x7fffffd4 value = 0x7fffffd4 value = e CS350 Operating Systems Fall 2007 C-Primer 8 Writing to a File include stdio.h include stdlib.h include assert.h include sys/types.h include sys/stat.h include fcntl.h int main() int i, rc, fd; int array40; CS350 Operating Systems Fall 20079 C-Primer 9 Writing to a File for (i=0; i40; i++) arrayi = i; fd = open("test-output", O_WRONLY O_CREAT); if (fd 0) exit(1); CS350 Operating Systems Fall 2007 C-Primer 10 Writing to a File rc = write(fd, array, sizeof(array)); if (rc 0) exit(1); close(fd); exit(0); CS350 Operating Systems Fall 200710 C-Primer 11 Writing to a File: Example Output % cat test-output u % CS350 Operating Systems Fall 2007 C-Primer 12 Writing to a File: Running on OS/161 (viewing on SPARC) Print offsets and values in Hex (x) % od -x test-output 0000000 0000 0000 0000 0001 0000 0002 0000 0003 0000020 0000 0004 0000 0005 0000 0006 0000 0007 0000040 0000 0008 0000 0009 0000 000a 0000 000b 0000060 0000 000c 0000 000d 0000 000e 0000 000f 0000100 0000 0010 0000 0011 0000 0012 0000 0013 0000120 0000 0014 0000 0015 0000 0016 0000 0017 0000140 0000 0018 0000 0019 0000 001a 0000 001b 0000160 0000 001c 0000 001d 0000 001e 0000 001f 0000200 0000 0020 0000 0021 0000 0022 0000 0023 0000220 0000 0024 0000 0025 0000 0026 0000 0027 0000240 CS350 Operating Systems Fall 200711 C-Primer 13 Writing to a File: Running on OS/161 (viewing on Linux/x86) Print offsets and values in Hex (x) % od -x test-output 0000000 0000 0000 0000 0100 0000 0200 0000 0300 0000020 0000 0400 0000 0500 0000 0600 0000 0700 0000040 0000 0800 0000 0900 0000 0a00 0000 0b00 0000060 0000 0c00 0000 0d00 0000 0e00 0000 0f00 0000100 0000 1000 0000 1100 0000 1200 0000 1300 0000120 0000 1400 0000 1500 0000 1600 0000 1700 0000140 0000 1800 0000 1900 0000 1a00 0000 1b00 0000160 0000 1c00 0000 1d00 0000 1e00 0000 1f00 0000200 0000 2000 0000 2100 0000 2200 0000 2300 0000220 0000 2400 0000 2500 0000 2600 0000 2700 0000240 CS350 Operating Systems Fall 2007 C-Primer 14 Endianness • Some architectures can be started to use either endianness (bi-endian). • System/161 & OS/161 : big-endian • Intel x86 : little-endian • SPARC : historically big-endian Version 9 is bi-endian E.g, x = 0xdeadbeef / 3735928559 / Little endian: Least significant byte at lowest address Word addressed by address of least significant byte 0 .. 7 8.. 15 16..23 24..31 ef be ad de Big Endian: Most significant byte at lowest address Word addressed by address of most significant byte 0 .. 7 8 ..15 16..23 24..31 de ad be ef CS350 Operating Systems Fall 200712 Processes and Threads 1 What is a Process? Answer 1: a process is an abstraction of a program in execution Answer 2: a process consists of • an address space • a thread of execution (possibly several threads) • other resources associated with the running program. For example: – open files – sockets – attributes, such as a name (process identifier) – . . . A process with one thread is a sequential process. A process with more than one thread is a concurrent process. CS350 Operating Systems Fall 2007 Processes and Threads 2 What is an Address Space? • For now, think of an address space as a portion of the primary memory of the machine that is used to hold the code, data, and stack(s) of the running program. • For example: Code Data Stack1 Stack2 0 max addresses • We will elaborate on this later. CS350 Operating Systems Fall 200713 Processes and Threads 3 What is a Thread? • A thread represents the control state of an executing program. • Each thread has an associated context, which consists of – the values of the processor’s registers, including the program counter (PC) and stack pointer – other processor state, including execution privilege or mode (user/system) – a stack, which is located in the address space of the thread’s process CS350 Operating Systems Fall 2007 Processes and Threads 4 Implementation of Processes • The kernel maintains information about all of the processes in the system in a data structure often called the process table. • Information about individual processes is stored in a structure that is sometimes called a process control block (PCB). In practice, however, information about a process may not all be located in a single data structure. • Per-process information may include: – process identifier and owner – current process state and other scheduling information – lists of available resources, such as open files – accounting information – and more . . .... CS350 Operating Systems Fall 200714 Processes and Threads 5 Process Creation Example (Part 1) Process A Kernel system call (CreateProcess) Parent process (Process A) requests creation of a new process. CS350 Operating Systems Fall 2007 Processes and Threads 6 Process Creation Example (Part 2) Process A Kernel Process B system call (CreateProcess) B’s thread is ready, not running system call return Kernel creates new process (Process B) CS350 Operating Systems Fall 200715 Processes and Threads 7 Multiprogramming • multiprogramming means having multiple processes existing at the same time • most modern, general purpose operating systems support multiprogramming • all processes share the available hardware resources, with the sharing coordinated by the operating system: – Each process uses some of the available memory to hold its address space. The OS decides which memory and how much memory each process gets – OS can coordinate shared access to devices (keyboards, disks), since processes use these devices indirectly, by making system calls. – Processes timeshare the processor(s). Again, timesharing is controlled by the operating system. • OS ensures that processes are isolated from one another. Interprocess communication should be possible, but only at the explicit request of the processes involved. CS350 Operating Systems Fall 2007 Processes and Threads 8 Timesharing Example (Part 1) Process A Kernel Process B B’s thread is system call ready, not running or exception or interrupt return A’s thread is ready, not running context switch Kernel switches execution context to Process B. CS350 Operating Systems Fall 200716 Processes and Threads 9 Timesharing Example (Part 2) Process A Kernel Process B system call context switch or exception or interrupt B’s thread is return ready, not running Kernel switches execution context back to process A. CS350 Operating Systems Fall 2007 Processes and Threads 10 Process Interface • A running program may use process-related system calls to manipulate its own process, or other processes in the system. • The process interface will usually include: Creation: make new processes, e.g.,fork/exec/execv Destruction: terminate a process, e.g.,exit Synchronization: wait for some event, e.g.,wait/waitpid Attribute Mgmt: read or change process attributes, such as the process identifier or owner or scheduling priority CS350 Operating Systems Fall 200717 Processes and Threads 11 The Process Model • Although the general operations supported by the process interface are straightforward, there are some less obvious aspects of process behaviour that must be defined by an operating system. Process Initialization: When a new process is created, how is it initialized? What is in the address space? What is the initial thread context? Does it have any other resources? Multithreading: Are concurrent processes supported, or is each process limited to a single thread? Inter-Process Relationships: Are there relationships among processes, e.g, parent/child? If so, what do these relationships mean? CS350 Operating Systems Fall 2007 Processes and Threads 12 Processor Scheduling Basics • Only one thread at a time can run on a processor. • Processor scheduling means deciding how threads should share the available processor(s) • Round-robin is a simple preemptive scheduling policy: – the kernel maintains a list of ready threads – the first thread on the list is dispatched (allowed to run) – when the running thread has run for a certain amount of time, called the scheduling quantum, it is preempted – the preempted thread goes to the back of the ready list, and the thread at the front of the list is dispatched. • More on scheduling policies later. CS350 Operating Systems Fall 200718 Processes and Threads 13 Dispatching: Context Switching mips_switch: / a0/a1 points to old/new thread’s struct pcb. / / Allocate stack space for saving 11 registers. 11 4 = 44 / addi sp, sp, -44 / Save the registers / sw ra, 40(sp) sw gp, 36(sp) sw s8, 32(sp) sw s7, 28(sp) sw s6, 24(sp) sw s5, 20(sp) sw s4, 16(sp) sw s3, 12(sp) sw s2, 8(sp) sw s1, 4(sp) sw s0, 0(sp) / Store the old stack pointer in the old pcb / sw sp, 0(a0) CS350 Operating Systems Fall 2007 Processes and Threads 14 Dispatching: Context Switching / Get the new stack pointer from the new pcb / lw sp, 0(a1) nop / delay slot for load / / Now, restore the registers / lw s0, 0(sp) lw s1, 4(sp) lw s2, 8(sp) lw s3, 12(sp) lw s4, 16(sp) lw s5, 20(sp) lw s6, 24(sp) lw s7, 28(sp) lw s8, 32(sp) lw gp, 36(sp) lw ra, 40(sp) nop / delay slot for load / j ra / and return. / addi sp, sp, 44 / in delay slot / .end mips_switch CS350 Operating Systems Fall 200719 Processes and Threads 15 Implementing Preemptive Scheduling • The kernel uses interrupts from the system timer to measure the passage of time and to determine whether the running process’s quantum has expired. • All interrupts transfer control from the running program to the kernel. • In the case of a timer interrupt, this transfer of control gives the kernel the opportunity to preempt the running thread and dispatch a new one. CS350 Operating Systems Fall 2007 Processes and Threads 16 Preemptive Multiprogramming Example Process A Kernel Process B timer interrupt interrupt return Key: ready thread running thread context switches CS350 Operating Systems Fall 200720 Processes and Threads 17 Blocked Threads • Sometimes a thread will need to wait for an event. Examples: – wait for data from a (relatively) slow disk – wait for input from a keyboard – wait for another thread to leave a critical section – wait for busy device to become idle • The OS scheduler should only allocate the processor to threads that are not blocked, since blocked threads have nothing to do while they are blocked. Multiprogramming makes it easier to keep the processor busy even though individual threads are not always ready. CS350 Operating Systems Fall 2007 Processes and Threads 18 Implementing Blocking • The need for waiting normally arises during the execution of a system call by the thread, since programs use devices through the kernel (by making system calls). • When the kernel recognizes that a thread faces a delay, it can block that thread. This means: – mark the thread as blocked, don’t put it on the ready queue – choose a ready thread to run, and dispatch it – when the desired event occurs, put the blocked thread back on the ready queue so that it will (eventually) be chosen to run CS350 Operating Systems Fall 2007

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.