Introduction to Operating Systems CS-2301 B-term 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)
Introduction to Operating Systems CS-2301 B-term 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
Introduction to Operating Systems CS-2301 B-term 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)
Introduction to Operating Systems CS-2301 B-term 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
Introduction to Operating Systems CS-2301 B-term 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
Introduction to Operating Systems CS-2301 B-term 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.
Introduction to Operating Systems CS-2301 B-term 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.
Introduction to Operating Systems CS-2301 B-term 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
Introduction to Operating Systems CS-2301 B-term Questions about Processes?
Introduction to Operating Systems CS-2301 B-term 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
Introduction to Operating Systems CS-2301 B-term 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 0x00105C, … –… the machine accesses 0x61605C OS creates illusion by swapping blocks of memory from RAM to disk and back as needed –Called pages
Introduction to Operating Systems CS-2301 B-term Virtual Memory for Process (Windows & Linux) 0x xFFFFFFFF address space program code (text) static data heap (dynamically allocated) stack (dynamically allocated) PC SP Every process has one of these, separate from every other process.
Introduction to Operating Systems CS-2301 B-term 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
Introduction to Operating Systems CS-2301 B-term Virtual Memory in Embedded Systems Implemented by partitioning RAM No swapping in or out May not even have a disk!
Introduction to Operating Systems CS-2301 B-term Virtual Memories in Embedded System OS Kernel stack Process 1 stack Process 2 0x x0000FFFF Physical memory stack Process 3
Introduction to Operating Systems CS-2301 B-term Questions about Processes or Virtual Memory? Not in Kernighan & Ritchie
Introduction to Operating Systems CS-2301 B-term 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
Introduction to Operating Systems CS-2301 B-term Thread Interface – POSIX standard #include 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
Introduction to Operating Systems CS-2301 B-term Thread Interface – POSIX standard #include 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 Pointer to a function– see §5.7 of K & R
Introduction to Operating Systems CS-2301 B-term Thread Interface – POSIX standard #include 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 Arguments to that function
Introduction to Operating Systems CS-2301 B-term Thread Example pthread_create(tp, &f, &args) pthread_join(tp) main function f g f h f
Introduction to Operating Systems CS-2301 B-term Thread Example pthread_create(tp, &f, &args) pthread_join(tp) main function f g f h f Two threads run at the same time
Introduction to Operating Systems CS-2301 B-term Virtual Memory for Multiple Threads 0x xFFFFFFFF Virtual address space code (text) static data heap thread 1 stack PC (T2) SP (T2) thread 2 stack thread 3 stack SP (T1) SP (T3) PC (T1) PC (T3)
Introduction to Operating Systems CS-2301 B-term 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 …
Introduction to Operating Systems CS-2301 B-term 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
Introduction to Operating Systems CS-2301 B-term 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
Introduction to Operating Systems CS-2301 B-term Questions about Threads? Not in Kernighan & Ritchie
Introduction to Operating Systems CS-2301 B-term 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
Introduction to Operating Systems CS-2301 B-term 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!
Introduction to Operating Systems CS-2301 B-term 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
Introduction to Operating Systems CS-2301 B-term 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 Older systems also used magnetic tape, paper tape, trays of punched cards, etc.
Introduction to Operating Systems CS-2301 B-term File Access in C See Kernighan & Ritchie, Chapter 8 Raw file access Without simplifying stream functions – e.g., –scanf, fscanf, printf, fprintf, 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
Introduction to Operating Systems CS-2301 B-term 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)
Introduction to Operating Systems CS-2301 B-term 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) NTFS and Linux file systems on hard drives
Introduction to Operating Systems CS-2301 B-term 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) Typical camera chips, flash drives, floppies
Introduction to Operating Systems CS-2301 B-term Questions about Files?
Introduction to Operating Systems CS-2301 B-term 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
Introduction to Operating Systems CS-2301 B-term 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
Introduction to Operating Systems CS-2301 B-term Sockets and Connections Defer to another course
Introduction to Operating Systems CS-2301 B-term General Questions?