Operating Systems - A Primer
Introduction
# who am iSaumil ShahCEO Net-square.Hacker, Speaker, Trainer, Author.M.S. Computer SciencePurdue University.Google: "saumil"LinkedIn: saumilshah
Preview
CPU and Registers
The CPUWe shall discuss the Intel x86 32-bit CPU.
The CPUWe shall discuss the Intel x86 32-bit CPU.The CPU has REGISTERS.
The CPUWe shall discuss the Intel x86 32-bit CPU.The CPU has REGISTERS."Variables" on the CPU, hard-wired.Size is equal to the "machine word". (32 bits)
What do Registers do?
What do Registers do?Registers hold data.
What do Registers do?Registers hold data.They may be numbers......or memory addresses (pointers)
What do Registers do?Registers hold data.They may be numbers......or memory addresses (pointers)Using registers, the CPU can perform computations, read and write memory, and execute instructions.
x86 Registers - the basicsEAXESIEBXEDIECXESPEDXEBPEIPEFLAGSthere are more, but this is good enough for now.
x86 Registers - General PurposeEAXAccumulatorEBXBaseECXCountEDXData
x86 Registers - General PurposeEAXAccumulatorEBXBaseECXCountEDXDataUsed for Integer operations (arithmetic), returning values (EAX), loops (ECX), or anything the compiler wishes to use them for.
x86 Registers - PointersESISource IndexEDIDestination Index
x86 Registers - PointersESISource IndexEDIDestination IndexThese are POINTERS, used for block copy operations.ESI = address of source, EDI = address of destination, ECX = count of bytes/words to be copied.
x86 Registers - Stack RelatedStack PointerESPFrame Pointer (Base Pointer)EBP
x86 Registers - Stack RelatedStack Pointer points to the top of the process stack.Stack PointerESPFrame Pointer (Base Pointer)EBP
x86 Registers - Stack RelatedStack Pointer points to the top of the process stack.Stack PointerESPFrame Pointer (Base Pointer)EBPFrame pointer points to FRAMES within the stack. Frames store data for functions.
x86 Registers - EIPEIPInstruction Pointer (program counter)
x86 Registers - EIPA very important register. Points to a memory address from where the CPU fetches and executes the next instruction. EIP autoincrements after every instruction.EIPInstruction Pointer (program counter)
x86 Registers - FlagsEFLAGSBit Flags
x86 Registers - FlagsIndividual bits indicate status of operations. Boolean values. Never treated as a full register, just individual bit flags.EFLAGSBit Flags
Operating System Basics
The Operating SystemModern Operating Systems are very complex and do many things.We shall discuss only the basic concepts and functions of an OS.
The Operating SystemModern Operating Systems are very complex and do many things.We shall discuss only the basic concepts and functions of an OS.Multitasking and MultiprogrammingProcessesThe Virtual Machine
What is Multiprogramming?
What is Multiprogramming?The ability to store and execute multiple programs in the memory
What is Multiprogramming?The ability to store and execute multiple programs in the memory...and what is Multitasking?
What is Multiprogramming?The ability to store and execute multiple programs in the memory...and what is Multitasking?Running multiple PROCESSES at the SAME TIME.
So what is a "Process"?
So what is a "Process"?A PROCESS is termed as a program that is EXECUTING. A running program.
So what is a "Process"?A PROCESS is termed as a program that is EXECUTING. A running program.A process is an instance of a program.Processes "live" in memory.Programs live on disk. (secondary storage)A program may have multiple processes of its own self.
How do multiple processes run simultaneously?
How do multiple processes run simultaneously?By TIME SLICING and CONTEXT-SWITCHING
How do multiple processes run simultaneously?By TIME SLICING and CONTEXT-SWITCHINGEach process runs for a short time and is then PRE-EMPTED to let another process run.This happens so fast, it appears as if all processes are running simultaneously.This is called MULTITASKING
Processes and Multitasking
MultitaskingProcess QueueP2P3P4P1CPUProcess P1 is running. It shall continue to run for a small period of time, called a QUANTUM.
MultitaskingP2P3P4P1Time SliceInterruptCPUAn INTERRUPT is triggered at the end of the quantum. A snapshot of P1's registers (P1's CONTEXT) is saved.
MultitaskingP2P3P4P1CPUP1 is then PRE-EMPTED. It is brought back into the process queue.
MultitaskingP2P3P4P1CPUIt is P2's turn now. P2's context is loaded into the CPU's registers. This is called CONTEXT SWITCHING.
MultitaskingP1P3P4P2CPUP2 is scheduled to execute on the CPU.
MultitaskingP1P3P4P2CPUP2 runs for the next quantum.
MultitaskingP1P3P4P2Time SliceInterruptCPUP2's context is now saved.
MultitaskingP1P3P4P2CPUP2 is PRE-EMPTED. It is brought back into the process queue. Next it will be P3's turn, and so on.
Virtual Machines and Process Memory Maps
What is a Virtual Machine?
What is a Virtual Machine?A view of ABSTRACTED HARDWARE as presented to a PROCESS by the OS.
What is a Virtual Machine?A view of ABSTRACTED HARDWARE as presented to a PROCESS by the OS.A process sees its OWN view of the CPU, Memory, Storage, and other devices.Every process runs in its own "virtual" machine.This is how the OS ensures process memory and resource segregation.
Do processes have their OWN view of the memory?Yes. Processes see "Virtual Memory"
Do processes have their OWN view of the memory?Yes. Processes see "Virtual Memory"Physical memory is divided into PAGES.A process is assigned pages as needed. This is done by the Memory Management Unit (MMU)From the process' point of view, it seems to have all the memory to itself.
Pages from P1's memory are mapped from physical memory. P1 sees a "virtual address space".Physical MemoryP1's memoryPhysical and Virtual Memory
Physical and Virtual MemorySimilarly for P2.Physical MemoryP1's memoryP2's memory
Can one process access another's memory?No. A process can only access its own virtual memory space.
Can one process access another's memory?No. A process can only access its own virtual memory space.A process CANNOT access physical memory.
The Process' view of memoryProcess Memory has its own VIRTUAL ADDRESS SPACE. Typically 2 to 3 GB.BinaryHeapIt is called a PROCESS MEMORY MAP.Lib3GB (Linux)2GB (Win32)LibLower addresses represented at the top of the map. Higher addresses at the bottom.LibStackProcess' Virtual Memory
Linux Process Memory Map0x08000000BinaryHeapLib3GBLibLibStack0xbfffffffLinux ProcessMemory Map
An exampleHere's how to view a process' memory map in Linux. The process here is PID 1110.saumil@localhost:$ pmap 11101110:   /home/nweb/nweb 8181 /home/nweb 08048000      8K read/exec         /home/nweb/nweb0804a000      4K read/write        /home/nweb/nweb0804b000      8K read/write/exec     [ anon ]40000000     72K read/exec         /lib/ld-2.3.1.so40012000      4K read/write        /lib/ld-2.3.1.so40013000      4K read/write          [ anon ]40019000   1204K read/exec         /lib/i686/libc-2.3.1.so40146000     16K read/write        /lib/i686/libc-2.3.1.so4014a000      8K read/write          [ anon ]bfffe000      8K read/write/exec     [ anon ] total     1336K
The Big Picture
CPUNICStoragePhysical Memory
File SystemNetwork StackSchedulerMMUDriverDriverCPUNICStoragePhysical Memory
kernel spaceSystem CallsFile SystemNetwork StackLoaderSchedulerMMUDriverDriverCPUNICStoragePhysical Memory
user spaceVirtual MemoryPROCESSVirtual MemoryPROCESSVirtual MemoryPROCESSCPUFSN/WCPUFSN/WCPUFSN/Wkernel spaceSystem CallsFile SystemNetwork StackLoaderSchedulerMMUDriverDriverCPUNICStoragePhysical Memory
user spaceVirtual MemoryPROCESSVirtual MemoryPROCESSVirtual MemoryPROCESSCPUFSN/WCPUFSN/WCPUFSN/Wkernel spaceSystem CallsFile SystemNetwork StackLoaderSchedulerMMUDriverDriverCPUNICStoragePhysical Memory
Key Concepts
Review
END

Operating Systems - A Primer

  • 1.
  • 2.
  • 3.
    # who amiSaumil ShahCEO Net-square.Hacker, Speaker, Trainer, Author.M.S. Computer SciencePurdue University.Google: "saumil"LinkedIn: saumilshah
  • 4.
  • 5.
  • 6.
    The CPUWe shalldiscuss the Intel x86 32-bit CPU.
  • 7.
    The CPUWe shalldiscuss the Intel x86 32-bit CPU.The CPU has REGISTERS.
  • 8.
    The CPUWe shalldiscuss the Intel x86 32-bit CPU.The CPU has REGISTERS."Variables" on the CPU, hard-wired.Size is equal to the "machine word". (32 bits)
  • 9.
  • 10.
    What do Registersdo?Registers hold data.
  • 11.
    What do Registersdo?Registers hold data.They may be numbers......or memory addresses (pointers)
  • 12.
    What do Registersdo?Registers hold data.They may be numbers......or memory addresses (pointers)Using registers, the CPU can perform computations, read and write memory, and execute instructions.
  • 13.
    x86 Registers -the basicsEAXESIEBXEDIECXESPEDXEBPEIPEFLAGSthere are more, but this is good enough for now.
  • 14.
    x86 Registers -General PurposeEAXAccumulatorEBXBaseECXCountEDXData
  • 15.
    x86 Registers -General PurposeEAXAccumulatorEBXBaseECXCountEDXDataUsed for Integer operations (arithmetic), returning values (EAX), loops (ECX), or anything the compiler wishes to use them for.
  • 16.
    x86 Registers -PointersESISource IndexEDIDestination Index
  • 17.
    x86 Registers -PointersESISource IndexEDIDestination IndexThese are POINTERS, used for block copy operations.ESI = address of source, EDI = address of destination, ECX = count of bytes/words to be copied.
  • 18.
    x86 Registers -Stack RelatedStack PointerESPFrame Pointer (Base Pointer)EBP
  • 19.
    x86 Registers -Stack RelatedStack Pointer points to the top of the process stack.Stack PointerESPFrame Pointer (Base Pointer)EBP
  • 20.
    x86 Registers -Stack RelatedStack Pointer points to the top of the process stack.Stack PointerESPFrame Pointer (Base Pointer)EBPFrame pointer points to FRAMES within the stack. Frames store data for functions.
  • 21.
    x86 Registers -EIPEIPInstruction Pointer (program counter)
  • 22.
    x86 Registers -EIPA very important register. Points to a memory address from where the CPU fetches and executes the next instruction. EIP autoincrements after every instruction.EIPInstruction Pointer (program counter)
  • 23.
    x86 Registers -FlagsEFLAGSBit Flags
  • 24.
    x86 Registers -FlagsIndividual bits indicate status of operations. Boolean values. Never treated as a full register, just individual bit flags.EFLAGSBit Flags
  • 25.
  • 26.
    The Operating SystemModernOperating Systems are very complex and do many things.We shall discuss only the basic concepts and functions of an OS.
  • 27.
    The Operating SystemModernOperating Systems are very complex and do many things.We shall discuss only the basic concepts and functions of an OS.Multitasking and MultiprogrammingProcessesThe Virtual Machine
  • 28.
  • 29.
    What is Multiprogramming?Theability to store and execute multiple programs in the memory
  • 30.
    What is Multiprogramming?Theability to store and execute multiple programs in the memory...and what is Multitasking?
  • 31.
    What is Multiprogramming?Theability to store and execute multiple programs in the memory...and what is Multitasking?Running multiple PROCESSES at the SAME TIME.
  • 32.
    So what isa "Process"?
  • 33.
    So what isa "Process"?A PROCESS is termed as a program that is EXECUTING. A running program.
  • 34.
    So what isa "Process"?A PROCESS is termed as a program that is EXECUTING. A running program.A process is an instance of a program.Processes "live" in memory.Programs live on disk. (secondary storage)A program may have multiple processes of its own self.
  • 35.
    How do multipleprocesses run simultaneously?
  • 36.
    How do multipleprocesses run simultaneously?By TIME SLICING and CONTEXT-SWITCHING
  • 37.
    How do multipleprocesses run simultaneously?By TIME SLICING and CONTEXT-SWITCHINGEach process runs for a short time and is then PRE-EMPTED to let another process run.This happens so fast, it appears as if all processes are running simultaneously.This is called MULTITASKING
  • 38.
  • 39.
    MultitaskingProcess QueueP2P3P4P1CPUProcess P1is running. It shall continue to run for a small period of time, called a QUANTUM.
  • 40.
    MultitaskingP2P3P4P1Time SliceInterruptCPUAn INTERRUPTis triggered at the end of the quantum. A snapshot of P1's registers (P1's CONTEXT) is saved.
  • 41.
    MultitaskingP2P3P4P1CPUP1 is thenPRE-EMPTED. It is brought back into the process queue.
  • 42.
    MultitaskingP2P3P4P1CPUIt is P2'sturn now. P2's context is loaded into the CPU's registers. This is called CONTEXT SWITCHING.
  • 43.
  • 44.
  • 45.
  • 46.
    MultitaskingP1P3P4P2CPUP2 is PRE-EMPTED.It is brought back into the process queue. Next it will be P3's turn, and so on.
  • 47.
    Virtual Machines andProcess Memory Maps
  • 48.
    What is aVirtual Machine?
  • 49.
    What is aVirtual Machine?A view of ABSTRACTED HARDWARE as presented to a PROCESS by the OS.
  • 50.
    What is aVirtual Machine?A view of ABSTRACTED HARDWARE as presented to a PROCESS by the OS.A process sees its OWN view of the CPU, Memory, Storage, and other devices.Every process runs in its own "virtual" machine.This is how the OS ensures process memory and resource segregation.
  • 51.
    Do processes havetheir OWN view of the memory?Yes. Processes see "Virtual Memory"
  • 52.
    Do processes havetheir OWN view of the memory?Yes. Processes see "Virtual Memory"Physical memory is divided into PAGES.A process is assigned pages as needed. This is done by the Memory Management Unit (MMU)From the process' point of view, it seems to have all the memory to itself.
  • 53.
    Pages from P1'smemory are mapped from physical memory. P1 sees a "virtual address space".Physical MemoryP1's memoryPhysical and Virtual Memory
  • 54.
    Physical and VirtualMemorySimilarly for P2.Physical MemoryP1's memoryP2's memory
  • 55.
    Can one processaccess another's memory?No. A process can only access its own virtual memory space.
  • 56.
    Can one processaccess another's memory?No. A process can only access its own virtual memory space.A process CANNOT access physical memory.
  • 57.
    The Process' viewof memoryProcess Memory has its own VIRTUAL ADDRESS SPACE. Typically 2 to 3 GB.BinaryHeapIt is called a PROCESS MEMORY MAP.Lib3GB (Linux)2GB (Win32)LibLower addresses represented at the top of the map. Higher addresses at the bottom.LibStackProcess' Virtual Memory
  • 58.
    Linux Process MemoryMap0x08000000BinaryHeapLib3GBLibLibStack0xbfffffffLinux ProcessMemory Map
  • 59.
    An exampleHere's howto view a process' memory map in Linux. The process here is PID 1110.saumil@localhost:$ pmap 11101110: /home/nweb/nweb 8181 /home/nweb 08048000 8K read/exec /home/nweb/nweb0804a000 4K read/write /home/nweb/nweb0804b000 8K read/write/exec [ anon ]40000000 72K read/exec /lib/ld-2.3.1.so40012000 4K read/write /lib/ld-2.3.1.so40013000 4K read/write [ anon ]40019000 1204K read/exec /lib/i686/libc-2.3.1.so40146000 16K read/write /lib/i686/libc-2.3.1.so4014a000 8K read/write [ anon ]bfffe000 8K read/write/exec [ anon ] total 1336K
  • 60.
  • 61.
  • 62.
  • 63.
    kernel spaceSystem CallsFileSystemNetwork StackLoaderSchedulerMMUDriverDriverCPUNICStoragePhysical Memory
  • 64.
    user spaceVirtual MemoryPROCESSVirtualMemoryPROCESSVirtual MemoryPROCESSCPUFSN/WCPUFSN/WCPUFSN/Wkernel spaceSystem CallsFile SystemNetwork StackLoaderSchedulerMMUDriverDriverCPUNICStoragePhysical Memory
  • 65.
    user spaceVirtual MemoryPROCESSVirtualMemoryPROCESSVirtual MemoryPROCESSCPUFSN/WCPUFSN/WCPUFSN/Wkernel spaceSystem CallsFile SystemNetwork StackLoaderSchedulerMMUDriverDriverCPUNICStoragePhysical Memory
  • 66.
  • 67.
  • 68.