Download presentation
Presentation is loading. Please wait.
Published byPhilippa Thomas Modified over 9 years ago
1
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 3: Processes
2
3.2 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Remote Procedure Call (RPC)
3
3.3 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Objectives Introduce notion of process Program in execution Basis of all computation Describe various features of processes scheduling, creation, termination, communication Explore interprocess communication Shared memory, message passing
4
3.4 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Process Concept OS executes variety of programs: Batch system – jobs Time-shared systems – user programs or tasks Textbook uses the terms job and process almost interchangeably Process – a program in execution; process execution must progress in sequential fashion
5
3.5 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Parts of a Process Program code (text section) Current activity including: program counter processor registers Stack containing temporary data Function parameters, return addresses, local variables Data section containing global variables Heap containing memory dynamically allocated
6
3.6 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Process Concept (Cont.) Program = passive entity stored on disk (executable) Process is active Program becomes process when exe loaded into memory One program can be several processes E.g., multiple users executing same program
7
3.7 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Process in Memory
8
3.8 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Process State As process executes, it changes state new: process being created running: instructions being executed waiting: process waiting for some event ready: process waiting to be assigned to a processor terminated: process finished execution
9
3.9 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Diagram of Process State
10
3.10 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Process Control Block (PCB) Info associated with process (aka task control block) Process state Program counter CPU registers CPU scheduling info Memory-management info Accounting info I/O status info
11
3.11 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition CPU Switch From Process to Process
12
3.12 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Threads So far, process has single thread of execution Consider multiple program counters per process Multiple locations can execute at once Multiple threads of control -> threads Must have storage for thread details Multiple program counters in PCB
13
3.13 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Process Representation in Linux Represented by the C structure task_struct pid t_pid; /* process identifier */ long state; /* state of the process */ unsigned int time_slice /* scheduling information */ struct task_struct *parent; /* this process’s parent */ struct list_head children; /* this process’s children */ struct files_struct *files; /* list of open files */ struct mm_struct *mm; /* address space of this process */
14
3.14 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Process Scheduling Maximize CPU use => quickly switch processes onto CPU for time sharing Process scheduler selects among available processes for next execution on CPU
15
3.15 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Process Scheduling Maintains scheduling queues of processes Job queue – set of all processes in system Ready queue – set of all processes residing in main memory, ready and waiting to execute Device queues – set of processes waiting for I/O Processes migrate among these queues
16
3.16 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Ready Queue And Various I/O Device Queues
17
3.17 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Representation of Process Scheduling Queueing diagram represents queues, resources, flows
18
3.18 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Schedulers Short-term scheduler (or CPU scheduler) – selects process to be executed next and allocates CPU Sometimes only scheduler in system Short-term scheduler is invoked frequently Order of milliseconds must be fast
19
3.19 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Schedulers Long-term scheduler (or job scheduler) – selects processes to be brought into ready queue Invoked infrequently Order of seconds, minutes may be slow Controls degree of multiprogramming
20
3.20 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Schedulers Processes described as either: I/O-bound– spends more time doing I/O than computations, many short CPU bursts CPU-bound – spends more time doing computations; few very long CPU bursts Long-term scheduler strives for good process mix
21
3.21 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Addition of Medium Term Scheduling Medium-term scheduler added if degree of multiprogramming needs to decrease Remove process from memory, store on disk, bring back from disk to continue execution: swapping
22
3.22 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Multitasking in Mobile Systems Some mobile systems (e.g., early version of iOS) allow only one process to run, others suspended Due to screen real estate, user interface limits iOS Single foreground process Controlled via user interface Multiple background processes In memory, running, but not on display
23
3.23 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Multitasking in Mobile Systems Android runs foreground and background processes Background process uses services to perform tasks Services keep running even if background process is suspended Service has no user interface, small memory use
24
3.24 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Context Switch When CPU switches to another process, system must perform context switch Save the state of old process Load saved state for new process Context of process represented in PCB Context-switch time is true overhead System does no useful work while switching The more complex the OS and PCB the longer the context switch
25
3.25 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Operations on Processes System must provide mechanisms for: process creation, process termination, and so on...
26
3.26 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Process Creation Parent process create children processes Children can create other processes Forms tree of processes Process identified, managed via process identifier PID
27
3.27 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Process Creation Resource sharing options Parent and children share all resources Children share subset of parent’s resources Parent and child share no resources Execution options Parent and children execute concurrently Parent waits until children terminate
28
3.28 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition A Tree of Processes in Linux
29
3.29 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Process Creation (Cont.) Address space Child duplicate of parent Child has a program loaded into it UNIX examples fork() creates new process exec() replaces process memory space with new program
30
3.30 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition C Program Forking Separate Process Review...
31
3.31 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Creating a Separate Process via Windows API YUCK!!!
32
3.32 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Process Termination Process executes last statement Asks operating system to delete it using exit() Returns status data from child to parent (via wait() ) Process’ resources deallocated by operating system
33
3.33 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Process Termination Parent may terminate execution of children via abort() system call Some reasons for doing so: Child has exceeded allocated resources Task assigned to child no longer required Parent is exiting, and OS does not allow orphans
34
3.34 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Process Termination Some Operating Systems do not allow children to exists if parent has terminated If process terminates, then all children also terminated Cascading termination: all children, grandchildren, etc. Termination initiated by OS
35
3.35 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Process Termination Parent may wait for termination of child Using wait() system call wait() returns status info and PID of terminated child If no parent waiting, child becomes zombie If parent terminated without wait(), child becomes orphan
36
3.36 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Multiprocess Architecture – Chrome Browser Many web browsers run as single process If one web site causes trouble, entire browser can hang Google Chrome is a multiprocess, 3 different : Browser Renderer Plug-in
37
3.37 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Interprocess Communication Processes may be independent or cooperating Cooperating: can affect or be affected by others Independent: cannot affect or be affected by others Reasons for cooperating processes: Information sharing Computation speedup Modularity Convenience
38
3.38 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Interprocess Communication Cooperating processes need interprocess communication (IPC) Two models of IPC Shared memory Message passing
39
3.39 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Communications Models (a) Message passing. (b) shared memory.
40
3.40 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Producer-Consumer Problem Paradigm for cooperating processes Producer process produces information that is consumed by a consumer process unbounded-buffer places no limit on buffer size bounded-buffer assumes fixed buffer size
41
3.41 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Interprocess Communication – Shared Memory Area of memory shared among processes Communication under control of users, not OS Requires synchronization of shared memory access Synchronization is discussed in Chapter 5
42
3.42 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Interprocess Communication – Message Passing Mechanism for processes to communicate and synchronize actions Message system – communication without shared variables IPC facility provides two operations: send (message) receive (message) Message size is either fixed or variable
43
3.43 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Message Passing (Cont.) If two processes wish to communicate, they must: Establish a communication link between them Exchange messages via send/receive
44
3.44 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Message Passing (Cont.) Implementation issues: How are links established? Can link be associated with more than two processes? How many links can there be between every pair of communicating processes? What is the link capacity? Is message size fixed or variable? Is a link unidirectional or bi-directional?
45
3.45 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Message Passing (Cont.) Implementation of communication link Physical: Shared memory Hardware bus Network Logical: Direct or indirect Synchronous or asynchronous Automatic or explicit buffering
46
3.46 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Direct Communication Processes must name each other explicitly: send (P, message) – send a message to process P receive (Q, message) – receive a message from Q Properties of communication link Links established automatically Link associated with exactly one pair of processes Exactly one link between pairs Link may be unidirectional or bi-directional
47
3.47 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Indirect Communication Messages are directed and received from mailboxes (AKA ports) Each mailbox has unique id Processes can communicate only if share a mailbox Properties of communication link Link established if processes share common mailbox Link may be associated with many processes Each pair of processes may share several communication links Link may be unidirectional or bi-directional
48
3.48 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Indirect Communication Operations create new mailbox (port) send / receive messages through mailbox destroy mailbox Primitives defined as: send (A, message) – send message to mailbox A receive (A, message) – receive message from mailbox A
49
3.49 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Indirect Communication Mailbox sharing P 1, P 2, and P 3 share mailbox A P 1, sends; P 2 and P 3 receive Who gets the message? Solutions Allow link to associate with at most two processes Allow only one process at a time to execute receive Allow system to arbitrarily select receiver Sender is notified who receiver is
50
3.50 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Synchronization Message passing may be blocking or non-blocking Blocking is considered synchronous Blocking send - sender blocks until message recv Blocking receive - receiver blocks until message available
51
3.51 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Synchronization Non-blocking is asynchronous Non-blocking send - sender sends message, continues Non-blocking receive - receiver receives: A valid message, or Null message Different combinations possible rendezvous - If both send and receive are blocking
52
3.52 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Synchronization (Cont.) Producer-consumer becomes trivial message next_produced; while (true) { /* produce an item in next produced */ send(next_produced); } m essage next_consumed; while (true) { receive(next_consumed); /* consume the item in next consumed */ }
53
3.53 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Buffering Queue of messages attached to link Implemented in one of three ways Zero capacity - no messages are queued Sender must wait for receiver (rendezvous) Bounded capacity - finite length of n messages If link full, sender waits Unbounded capacity - infinite length Sender never waits
54
3.54 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Examples of IPC Systems - POSIX POSIX Shared Memory Process creates shared memory segment shm_fd = shm_open(name, O CREAT | O RDWR, 0666); Also used to open existing segment to share Set size of shared memory object ftruncate(shm fd, 4096); Now process can write to shared memory sprintf(shared memory, "Writing to shared memory");
55
3.55 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Remote Procedure Calls Remote procedure call (RPC) abstracts procedure calls between processes on networked systems Uses ports for service differentiation Stubs - client-side proxy for procedure on server Client-side stub locates server, marshalls parameters
56
3.56 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Remote Procedure Calls Server-side stub receives message Unpacks marshalled parameters Performs procedure on server On Windows, stub code compile from specification Microsoft Interface Definition Language (MIDL)
57
3.57 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Execution of RPC
58
3.58 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Live Coding Review of POSIX API create threads join threads mutexes semaphores condition variables
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.