Introduction to Operating Systems continued CS2301 System Programming

  • Slides: 39
Download presentation
Introduction to Operating Systems (continued) CS-2301, System Programming for Non -majors (Slides include materials

Introduction to Operating Systems (continued) CS-2301, System Programming for Non -majors (Slides include materials from The C Programming Language, 2 nd ed. , by Kernighan and Ritchie and from C: How to Program, 5 th ed. , by Deitel and Deitel) CS-2301 B-term 2008 Introduction to Operating Systems 1

Review – Four fundamental Abstractions • Processes & threads • Multiplexing of processor(s) to

Review – Four fundamental Abstractions • Processes & threads • Multiplexing of processor(s) to create the illusion of many of them • Virtual memory • Multiplexing of physical memory and disk blocks to create illusion of own memory per process • Files & persistent storage • Organizing principles about long-term data storage • Sockets & connections • Organizing principles about network communication CS-2301 B-term 2008 Introduction to Operating Systems 2

Review – Abstraction • The distillation of a complex mechanism into a simple, conceptual

Review – Abstraction • The distillation of a complex mechanism into a simple, conceptual model • User of abstraction does not need to worry about details • Implementer of abstraction does not need to worry about how user will use it (within limits) CS-2301 B-term 2008 Introduction to Operating Systems 3

Definition – Process • A particular execution of a program • Different from all

Definition – Process • A particular execution of a program • Different from all other executions of that program • Different from executions of other programs • The OS uses one or more CPUs to make it look like each process has own CPU • Can execute at same time! • Uses interrupts to manage and enforce multiplexing of CPU CS-2301 B-term 2008 Introduction to Operating Systems 4

Definition – Interrupt • A mechanism by which the processor suspends execution of the

Definition – Interrupt • A mechanism by which the processor suspends execution of the current, running program and gives control to the OS • OS saves the state of the interrupted program so that it can be restarted later • OS then takes appropriate action CS-2301 B-term 2008 Introduction to Operating Systems 5

Review – Why Processes? • Independent applications can share a computing system • Enables

Review – Why Processes? • Independent applications can share a computing system • Enables applications to overlap computation with external activities • Exploit parallelism in modern processors • Vehicle for managing resources, security, etc. CS-2301 B-term 2008 Introduction to Operating Systems 6

Resources Assigned to a Process • Memory • Virtual or real • Processor time

Resources Assigned to a Process • Memory • Virtual or real • Processor time • Priorities • Deadlines for real-time activities • Privileges • Security, authentication, etc. • Files and file space • For long-term storage, temporary storage • Devices • For input and output activity, sensors, etc. CS-2301 B-term 2008 Introduction to Operating Systems 7

Note • Every command on Linux command line creates a separate process • May

Note • Every command on Linux command line creates a separate process • May run in foreground or background • Double-clicking on icon in Windows often creates a separate process • May run in same window or another window • Processes in embedded systems are usually created at system startup CS-2301 B-term 2008 Introduction to Operating Systems 8

Questions about Processes? CS-2301 B-term 2008 Introduction to Operating Systems 9

Questions about Processes? CS-2301 B-term 2008 Introduction to Operating Systems 9

Four fundamental Abstractions • Processes & threads • Multiplexing of processor(s) to create the

Four fundamental Abstractions • Processes & threads • Multiplexing of processor(s) to create the illusion of many of them • Virtual memory • Multiplexing of physical memory and disk blocks to create illusion of own memory per process • Files & persistent storage • Organizing principles about long-term data storage • Sockets & connections • Organizing principles about network communication CS-2301 B-term 2008 Introduction to Operating Systems 10

Definition – Virtual Memory • The illusion that each process has its own memory

Definition – Virtual Memory • The illusion that each process has its own memory – Separate from virtual memories of other processes – (Often) more memory than machine has installed • Implemented by translating “logical” addresses of program into “physical” address of RAM (random access memory) – Every memory reference, both program and data, on-the-fly • E. g. , when the program utters 0 x 00105 C, … – … the machine accesses 0 x 61605 C • OS creates illusion by swapping blocks of memory from RAM to disk and back as needed – Called pages CS-2301 B-term 2008 Introduction to Operating Systems 11

Virtual Memory for Process Eve ry p roce thes 0 x. FFFF e, se

Virtual Memory for Process Eve ry p roce thes 0 x. FFFF e, se ss has ever p o y ot arate f ne of her r proc om ess. address space (Windows & Linux) stack (dynamically allocated) SP heap (dynamically allocated) static data 0 x 0000 CS-2301 B-term 2008 program code (text) Introduction to Operating Systems PC 12

Virtual Memory (continued) • Typical virtual memory size – 4 GBytes • Per process

Virtual Memory (continued) • Typical virtual memory size – 4 GBytes • Per process — even in a 1 GByte computer! • “Inactive” pages are not resident in RAM • Reference results in interrupt called a page fault • OS brings in page from disk, (maybe) swaps one out • Unused pages not filled in by OS • Dereferencing pointer results in Segmentation Fault • New pages created by OS as needed • When stack or heap grows or programs are loaded CS-2301 B-term 2008 Introduction to Operating Systems 13

Virtual Memory in Embedded Systems • Implemented by partitioning RAM • No swapping in

Virtual Memory in Embedded Systems • Implemented by partitioning RAM • No swapping in or out • May not even have a disk! CS-2301 B-term 2008 Introduction to Operating Systems 14

Questions about Processes or Virtual Memory? Not in Kernighan & Ritchie CS-2301 B-term 2008

Questions about Processes or Virtual Memory? Not in Kernighan & Ritchie CS-2301 B-term 2008 Introduction to Operating Systems 16

Threads • A refinement of concept of process • Short for “thread of control”

Threads • A refinement of concept of process • Short for “thread of control” • Concurrent execution of a function within the context of a process • Needs own stack in order to call other functions • Shares heap, static data, and code with other threads of same process • Reason • Application may need to manage concurrency of its own computation with external events CS-2301 B-term 2008 Introduction to Operating Systems 17

Thread Interface – POSIX standard #include <pthread. h> • int pthread_create(pthread_t *thread, const pthread_attr_t

Thread Interface – POSIX standard #include <pthread. h> • int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void*(*start_routine) (void), void *arg) – creates a new thread of control – new thread begins executing at start_routine • pthread_exit(void *value_ptr) – terminates the calling thread • pthread_join(pthread_t thread, void **value_ptr) – blocks the calling thread until the thread specified terminates • pthread_t pthread_self() – Returns the calling thread's identifier CS-2301 B-term 2008 Introduction to Operating Systems 18

– n o cti R n fu & a to of K r te

– n o cti R n fu & a to of K r te 5. 7 n i Po see § Thread Interface – POSIX standard #include <pthread. h> • int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void*(*start_routine) (void), void *arg) – creates a new thread of control – new thread begins executing at start_routine • pthread_exit(void *value_ptr) – terminates the calling thread • pthread_join(pthread_t thread, void **value_ptr) – blocks the calling thread until the thread specified terminates • pthread_t pthread_self() – Returns the calling thread's identifier CS-2301 B-term 2008 Introduction to Operating Systems 19

Thread Interface – POSIX standard #include <pthread. h>ents at h t to um n

Thread Interface – POSIX standard #include <pthread. h>ents at h t to um n g r • int pthread_create(pthread_t *thread, const A nctio fu void*(*start_routine) (void), pthread_attr_t *attr, void *arg) – creates a new thread of control – new thread begins executing at start_routine • pthread_exit(void *value_ptr) – terminates the calling thread • pthread_join(pthread_t thread, void **value_ptr) – blocks the calling thread until the thread specified terminates • pthread_t pthread_self() – Returns the calling thread's identifier CS-2301 B-term 2008 Introduction to Operating Systems 20

Thread Example pthread_create(tp, &f, &args) f main function g main function f h main

Thread Example pthread_create(tp, &f, &args) f main function g main function f h main function f pthread_join(tp) CS-2301 B-term 2008 Introduction to Operating Systems 21

Thread Example pthread_create(tp, &f, &args) Two threads run at the same time f main

Thread Example pthread_create(tp, &f, &args) Two threads run at the same time f main function g main function f h main function f pthread_join(tp) CS-2301 B-term 2008 Introduction to Operating Systems 22

Virtual Memory for Multiple Threads 0 x. FFFF thread 1 stack SP (T 1)

Virtual Memory for Multiple Threads 0 x. FFFF thread 1 stack SP (T 1) thread 2 stack SP (T 2) thread 3 stack SP (T 3) Virtual address space heap static data 0 x 0000 CS-2301 B-term 2008 PC (T 2) PC (T 1) PC (T 3) code (text) Introduction to Operating Systems 23

Why Threads? • To enable development of applications with concurrent activities inside them •

Why Threads? • To enable development of applications with concurrent activities inside them • Need to share data (difficult with separate virtual memories) • Examples • • • Web server over common data pages Transaction processor over common data base Applications within a mobile phone or PDA Applications with different speeds of devices … CS-2301 B-term 2008 Introduction to Operating Systems 24

Example Thread Application • One or more threads to get data from sensor •

Example Thread Application • One or more threads to get data from sensor • 1000 times per second; cannot afford to miss a reading • Places data on queue • Another thread processes and displays data • Removes and processes each data item from queue • Displays system state on control panel • User thread • Allows operator to adjust system parameters • While other threads are still running CS-2301 B-term 2008 Introduction to Operating Systems 25

Additional Functions & Data Required • pthread_mutex_t — mutual exclusion lock – Enables a

Additional Functions & Data Required • pthread_mutex_t — mutual exclusion lock – Enables a thread to “lock” some data so that other threads do not touch in mid-operation • pthread_cond_t — condition variable – Enables a thread to wait for an event to happen – Signaled by another thread • See man pages for details CS-2301 B-term 2008 Introduction to Operating Systems 26

Questions about Threads? Not in Kernighan & Ritchie CS-2301 B-term 2008 Introduction to Operating

Questions about Threads? Not in Kernighan & Ritchie CS-2301 B-term 2008 Introduction to Operating Systems 27

Four fundamental Abstractions • Processes & threads • Multiplexing of processor(s) to create the

Four fundamental Abstractions • Processes & threads • Multiplexing of processor(s) to create the illusion of many of them • Virtual memory • Multiplexing of physical memory and disk blocks to create illusion of own memory per process • Files & persistent storage • Organizing principles about long-term data storage • Sockets & connections • Organizing principles about network communication CS-2301 B-term 2008 Introduction to Operating Systems 28

Definition – File • A (potentially) large amount of information or data that lives

Definition – File • A (potentially) large amount of information or data that lives a (potentially) very long time • Often much larger than the memory of the computer • Often much longer than any computation • Sometimes longer than life of machine itself • (Usually) organized as a linear array of bytes or blocks • Internal structure is imposed by application • (Occasionally) blocks may be variable length • (Often) requiring concurrent access by multiple threads or processes • Even by processes on different machines! CS-2301 B-term 2008 Introduction to Operating Systems 29

Implementations of Files • Usually on disks (or devices that mimic disks) • Magnetic

Implementations of Files • Usually on disks (or devices that mimic disks) • Magnetic – hard drive or floppy • Optical – CD, DVD • Flash drives – electronic memory, organized as disks • Requirement • Preserve data contents during power-off or disasters • Directory / Folder • Special kind of file that points to other files • Associates names with files CS-2301 B-term 2008 Introduction to Operating Systems 30

sed u o als per s em e, pa t s y ed s

sed u o als per s em e, pa t s y ed s p h a r t c e n d c Ol gneti of pu ma trays Usually on disks (or devices that mimic disks) , tape , etc. s • Magnetic – hard drive or floppy card • Optical – CD, DVD • Flash drives – electronic memory, organized as disks Implementations of Files • • Requirement • Preserve data contents during power-off or disasters • Directory / Folder • Special kind of file that points to other files • Associates names with files CS-2301 B-term 2008 Introduction to Operating Systems 31

File Access in C • See Kernighan & Ritchie, Chapter 8 • Raw file

File Access in C • See Kernighan & Ritchie, Chapter 8 • Raw file access • Without simplifying stream functions – e. g. , – scanf, fscanf, printf, fgetc, etc. • read and write raw disk blocks • Seek to a file position – lseek, fseek — sets file pointer to specified location – Subsequent read, write, etc. , start there – ftell – returns file pointer CS-2301 B-term 2008 Introduction to Operating Systems 32

Organizations of Files • Contiguous • Blocks stored contiguously on storage medium • E.

Organizations of Files • Contiguous • Blocks stored contiguously on storage medium • E. g. , CD, DVD, some large database systems • Access time to any block is O(1) • Linked • Blocks linked together – File Allocation Table (FAT) • Access time is O(n) • Indexed • Blocks accessed via tree of index blocks (i-nodes) • Access time is O(log n) • However, base of logarithm may be very large (>100) CS-2301 B-term 2008 Introduction to Operating Systems 33

Organizations of Files • Contiguous • Blocks stored contiguously on storage medium • E.

Organizations of Files • Contiguous • Blocks stored contiguously on storage medium • E. g. , CD, DVD, some large database systems • Access time to any block is O(1) • Linked • ile es • Blocks linked together – File Allocation Table (FAT) f x iv nu i r d L nd • Access time is O(n) ard a h S n NTF stems o sy Indexed • Blocks accessed via tree of index blocks (i-nodes) • Access time is O(log n) • However, base of logarithm may be very large (>100) CS-2301 B-term 2008 Introduction to Operating Systems 34

Organizations of Files • Contiguous • • Blocks stored contiguously on storage medium s,

Organizations of Files • Contiguous • • Blocks stored contiguously on storage medium s, p i h • E. g. , CD, DVD, some large database systems a c pies r e cam s, flop l • Access time to any block is O(1) a ic p ive y r T d sh a l f Linked • Blocks linked together – File Allocation Table (FAT) • Access time is O(n) • Indexed • Blocks accessed via tree of index blocks (i-nodes) • Access time is O(log n) • However, base of logarithm may be very large (>100) CS-2301 B-term 2008 Introduction to Operating Systems 35

Questions about Files? CS-2301 B-term 2008 Introduction to Operating Systems 36

Questions about Files? CS-2301 B-term 2008 Introduction to Operating Systems 36

Four fundamental Abstractions • Processes & threads • Multiplexing of processor(s) to create the

Four fundamental Abstractions • Processes & threads • Multiplexing of processor(s) to create the illusion of many of them • Virtual memory • Multiplexing of physical memory and disk blocks to create illusion of own memory per process • Files & persistent storage • Organizing principles about long-term data storage • Sockets & connections • Organizing principles about network communication CS-2301 B-term 2008 Introduction to Operating Systems 37

Sockets and Connections • Connection: – a serial conversation over a network between two

Sockets and Connections • Connection: – a serial conversation over a network between two end points • e. g. , processes, threads, tasks on different computers – organized as a sequence of messages or datagrams – distinct from all other connections • Socket: – An end point of a connection – An abstraction that allows a process to send or receive only the information of that connection – Multiplexed on network with all other connections CS-2301 B-term 2008 Introduction to Operating Systems 38

Sockets and Connections • Defer to another course CS-2301 B-term 2008 Introduction to Operating

Sockets and Connections • Defer to another course CS-2301 B-term 2008 Introduction to Operating Systems 39

General Questions? CS-2301 B-term 2008 Introduction to Operating Systems 40

General Questions? CS-2301 B-term 2008 Introduction to Operating Systems 40