Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 313 – Introduction to UNIx process

Similar presentations


Presentation on theme: "CSCE 313 – Introduction to UNIx process"— Presentation transcript:

1 CSCE 313 – Introduction to UNIx process
Reading Reference: Textbook 1 Chapter 2 CSCE 313 – Introduction to UNIx process Tanzir Ahmed CSCE 313 Fall 2018

2 Outline Process Definition API Introduction Processor Virtualization
Concurrent Processes Context Switching Memory Virtualization Address Space Virtual Memory Page Faults 1/14/2019

3 Running Programs in a Computer
“How does Unix run programs? It looks easy enough: you log in, your shell prints a prompt, you type a command and press Enter. Soon a program runs. When the program finishes, your shell prints a new prompt. How does that work? ” The same goes for running a program on Windows (or any other OS), either by double-clicking or typing the name in the command prompt * Understanding Linux/Unix Programming, by Bruce Molay 1/14/2019

4 Main Problems Problem # 1: A Computer Program is merely a set of instruction residing in the disk as a file. How to bring this to life? In other words, how to associate the resources to the instructions in the program? Problem # 2: How to run many programs seamlessly in a computer that has a single (or a few) processor(s) IOW, “How do all programs share the same h/w?” Example: Your laptop running hundreds of applications (e.g., the browser, music player, document processor, a real-time game). Each program better be oblivious/independent of other running programs so that it is easy to write programs

5 Process Definition Process is an instance of a running programming. This happens through its Abstraction Also defined as “A Program in Action” Provided by the OS and used by the program Solves the problems just mentioned To the program, Process is a view of the entire system – memory, CPU, disk and all I/O devices Whatever the program needs, Process has it This view of the system is also called machine state Process is an abstraction, because the program thinks it is the only program running

6 Process Abstraction A program now makes a request to the OS instead of directly running Because OS is in charge, not the program itself For Process abstraction, OS provides API for the following functionalities: Create/Destroy a process Run a process Stop/Resume a process Query status (we will see a Process can be in many states) A few other important controls and wait functions (for synchronization) We will learn (in a few lectures) how to use some of these in C/C++ language Other languages have very similar-looking API functions

7 Abstraction Mechanism
Process provides each program with two key abstractions: Logical control flow for CPU virtualization Each program seems to have exclusive use of the CPU Private address space for Memory virtualization Each program seems to have exclusive use of main memory How are these illusions maintained? Process executions interleaved (multitasking) Private address spaces managed by virtual memory system (will describe that in a bit) 1/14/2019

8 Logical Control Flows Each process has its own logical control flow, which can be far from reality logical control flow actual control flow Time Process A Process B Process C Time Process A Process B Process C Control flows for concurrent processes are physically disjoint in time (except on multi-core machines) However, we can think of concurrent processes as running in ‘parallel’ with each other 1/14/2019

9 Context Switching Control flow passes from one process to another via a context switch Has to store the Process state in a way so that it can be retrieved later exactly where it was left off (not from the start) We will see the mechanism of context switch in more details in a little bit. However, the main steps are these: Store the current process’s stat. This is done by CS code in OS Invoke the scheduler that will pick another process Load that other process into the CPU. Again, done by CS code in OS Process A code Process B code user code context switch OS code Time user code OS code context switch user code 1/14/2019

10 Process Image in Physical Memory
In reality, this is how* processes look in physical memory: 1/14/2019 * A very simplified view, ignoring fragmentation completely

11 Private Address Space Illusion
But each process is made to believe that the memory looks like the following – thanks to Virtual Memory: How big can this be? memory mapped region for shared libraries run-time heap (managed by malloc) user stack (created at runtime) Kernel %esp (stack pointer) brk 0xffffffff 0x 0x read/write segment (.data, .bss) read-only segment (.init, .text, .rodata) loaded from the executable file Depends on machine architecture. For a 32 bit machine 232 = 4GB For a 64 bit machine 264 = 16EB 1/14/2019

12 Question Is a process image really 232 = 4GB long?
It would be nice for us, programmers But typically, physical memory is quite limited Virtual memory in action What is then Virtual Memory? For that, we now need to a little understanding on how memory is organized in a system 1/14/2019

13 Memory Organization Memory is logically divided into pages, which are fixed in size and aligned regions of memory Typical size: 4KB/page But pages are associated to a process only when necessary (i.e., read or written) When not necessary, they are evicted to the disk Thus a running process can be stored back to disk again!!!! The mechanism of such lazy-allocation is through Page Fault If a non-existent page is accessed (R/W), a page fault happens and fault-handler allocates the required page in physical memory 0-4095 Page 0 Page 1 Page 2 Page 3 4KB 8KB 12KB 1/14/2019

14 Mapping from Virtual to Physical Memory
Virtual Memory Physical Memory Virtual Memory Page Table Page Table 0K Page 0 Frame 0 Page 0 4K 4K 4K Page 1 Frame 1 Page 1 8K 8K 8K Frame 2 Page 2 12K 12K Frame 3 16K Frame 4 Private to Process #1 Of Process #2 264 264 1/14/2019

15 Summarizing Virtual Memory
The private address space of a process is made up of pages These pages can be spread according to the wish of the kernel Contiguous memory in processes’ view are not necessarily contiguous in physical memory – they can be physically scattered, but stitched together by Virtual Memory system Because memory scarce, the whole private address space does not need to be allocated all the time: Actual pages can be allocated/mapped only when needed (i.e., read or written) through page faults Even allocated but inactive pages can be swapped back to disk to make room for more active pages Memory accessed is further slowed down by Page Table access Each address must be translated to physical address by looking up in the process’s page table, which is also in memory Thus each memory access is actually 2 memory accesses: 1 for page table, another for the actual memory access 1/14/2019


Download ppt "CSCE 313 – Introduction to UNIx process"

Similar presentations


Ads by Google