Title: Categories of Processes
1Categories of Processes
- Independent process do not affect others
- Cooperating process affect each other
- Share memory and/or resources.
- Overlap I/O and processing
- Communicate via message passing.
- I/O-bound Short CPU bursts between I/O
- CPU-bound process Long CPU bursts, little I/O
Note The term, job, normally refers to a batch
system process
2A Process in Memory
3Process States
- As a process executes, it changes state
- new The process is being created
- running Instructions are being executed
- waiting The process is waiting for some event
to occur - ready The process is waiting to be assigned to
a process - terminated The process has finished execution
4What makes up a process?
- Process Control Block
- Process Id
- Process State
- Scheduling Information
- Child and Parent data
- Per Thread
- Program Counter
- CPU Registers
- Stack
- Allocated resources
- Accounting information
- I/O or wait status information
- Memory-management data
- Program Instructions (text area)
- Program Data and Heap
Process States
5Processing ManagementInvolves migrating
processes between queues
- Job queue All system processes
- Ready queue All processes in memory and ready
execute - Device queues All processes waiting for an I/O
request completes - Process migration between the various queues
6Process Schedulers
- Long-term (What multiprogramming degree?)
- Medium-term (Which to swap?)
- Short-Term (which to run?)
7Context Switches
Taking control from one process and giving it to
another
- Save the old process state and load the saved new
process state - Context-switch time
- pure overhead, no useful work done
- Cost dependent on hardware support
- Time slicing gives CPU cycles in a round-robin
manner
8Process Creation
- Parent spawn children, children spawn others
- Resource sharing options
- Parent and children share all resources
- Children share some parent resources
- No sharing
- Execution options
- Parent and children execute concurrently
- Parent waits until children terminate
- Address space options
- Child is a duplicate of the parent
- Child space has a program loaded into it
9A Solaris process tree
10POSIX Example
11Win32 Example
12Java Example
13Process Termination
- Process tells OS that is done (exit)
- Output data from child to parent (via wait)
- Resources are deallocated
- Parent may terminate children processes (abort)
- Child has exceeded allocated resources
- Child is no longer needed
- If parent is exiting
- Some operating system do not allow child to
continue if its parent terminates. - cascading termination Terminate children when
parent terminates
14Inter-process Communication
Message Passing Shared Memory
15Producer-Consumer Problem
- Cooperating processes paradigm
- producer process creates information
- consumer utilizes the information
- Two modes
- unbounded-buffer producer creates with no
practical limit - bounded-buffer assumes a limited buffer with
fixed size
16Shared-Memory Solution
- public interface Buffer
- public abstract void produce(Object item)
- public abstract Object consume()
- public class BoundedBuffer implements Buffer
- private int count, in, out private
Object buffer - public BoundedBuffer(int size)
- count in out 0 buffer new
Object(size) - public void produce(Object item)
- while (count buffer.length)
- bufferin item in (in
1)buffer.length count - public Object consume()
- while (count 0)
- Object item bufferout
- out (out 1)buffer.length count--
- return item
17Message Passing
- Message system processes send and receive to
communicate - send(message)
- receive(message)
- To communicate, P and Q must
- establish a communication link between them
- exchange messages via send/receive
- Communication link can be
- physical (e.g., shared memory, hardware bus)
- logical (e.g., logical properties)
18Implementation Questions
- How are links established?
- Number of processes sharing a link?
- Number of links per process?
- Link capacity?
- Variable or fixed messages?
- Unidirectional or bi-directional?
- Synchronous or asynchronous?
- Direct or indirect communication?
- Copying or use memory maps?
- Which communication standard?
- Persistent or transient?
19Direct Communication
- Processes name each other explicitly
- send (P, message) send to process P
- receive(Q, message) receive from process Q
- Properties of communication link
- Links are established automatically
- A link connects exactly one pair processes
- Between each pair there exists exactly one link
- Links are usually bi-directional
20Indirect Communication
- Messages sent/received using ports (mailboxes)
- Each mailbox has a unique id
- Processes communicate by sharing a mailbox
- Properties of communication links
- Processes share common mailbox links
- A link may be associated with many processes
- Processes may share many communication links
- Link may be unidirectional or bi-directional
- Operations
- create or destroy mailboxes
- send and receive messages through mailbox
- send(A, message) send a message to mailbox A
- receive(A, message) receive a message from
mailbox A
21Indirect Communication Issue
- Mailbox sharing
- P1, P2, and P3 share mailbox A
- P1, sends P2 and P3 receive
- Who gets the message?
- Possible Solutions
- Links shared by at most two processes
- Only one process can receive messages
- Arbitrarily select the receiver and notify sender
22Synchronization
- Blocking Synchronous
- Blocking send The sender block until the message
is received - Blocking receive The receiver block until a
message is available - Non-blocking Asynchronous
- Non-blocking send has the sender send the message
and continue - Non-blocking receive has the receiver receive a
valid message or null
23Buffering
- Buffering The linke queues messages
- Queue length1. Zero capacity queue length
0Sender waits for receiver (rendezvous)2. Boun
ded capacity queue lengthnSender must wait
if the link is full - 3. Unbounded capacity infinite length
Sender never waits
24Message Passing Solution
- public class Unbounded
- private Vector queue
- public Unbounded()
- queue new Vector()
- public void send(Object item)
- queue.addElement(item)
- public Object receive()
- if (queue.size() 0)
- return null
- else
- return queue.remove()
- // Producer
- while(true)
- mailBox.send(new Date())
-
- // Consumer
- while(true)
- Date msg (Date)receive()
- if (msg ! null)
- System.out.println(msg)
25Message Passing in Windows XP
Server establishes a port
When client wants to communicate, a handle is
sent to server and client
Shared memory if gt256 bytes
Small messages copied, larger messages use memory
mapping
26Socket Communication
- Socket an endpoint for communication
- Concatenation of IP address and port
- 161.25.19.81625 refers to port 1625 on host
161.25.19.8 - Communication consists between a pair of sockets
27Java Socket Server
28Java Socket Client
29Remote Procedure Calls
- Remote procedure call (RPC) abstracts procedure
calls between processes on networked systems. - Client Side
- Stubs proxy for the server side procedure
- marshalls parameters for communication
- Server-side
- stub receives, unpacks, and calls procedure
- Result sent back to waiting client
- Issues reliability, external data representation
(XDR)
30RPCExecution
31Remote Method Invocation
- Remote Method Invocation (RMI) is a Java
mechanism similar to RPCs. - RMI allows a Java program to invoke remote
methods with remote objects - Issues Reliability, remote objects, XDR
32Marshalling Parameters
33RMI Server
34RMI Client
35Threads
A path of execution through a process
36Motivation for Threads
- Responsiveness An application continues
executing when a blocking call occurs - Resource Sharing All threads can share an
application's resources - Economy Creating new processes is expensive time
wise - Threads can execute on different processors
37User and Kernel Threads
- User threads - Thread management done by
user-level threads library. - Kernel threads Thread management directly
supported by the kernel. - Tradeoffs Kernel thread handling incurs more
overhead. User threads stops the application on
every blocking call. - Most modern operating systems support kernel
threads.
38Many-to-One Model
- Thread management can be handled by the run time
library. - Any blocking call will suspend application
execution - Threads all run on the same processor
- Examples Green threads and GNU portable threads
39One to One
- Thread management done in the kernel
- There is an upper limit on the total number of
threads - Blocking OS calls don't suspend application
execution
40Many-to-Many
- Each application is assigned a pool of kernel
threads - No limit on the number of user threads
- Application doesn't suspend execution on blocking
OS calls - Coordination required between the thread library
and the kernel
Note Two level is many to many, but allows one
to one capability also
41Java Threads
- Java threads are managed by the JVM
- Java threads may be created by
- Implementing the Runnable interface
- Extending the Thread class
- Java threads start by calling the start method
- Allocate memory for the thread
- Call the start method
42Java Threads Example
- public class Driver extends Thread
- private static int sum 0
- private int upper
- public Driver(int upper)
- this.upper upper this.start()
- public static void main(String args)
- Driver thread new Driver(Integer.parseInt
(args0)) - thread.join() System.out.println(sum)
- public void run()
- sum 0 for (int i0 iltupper i) sum
i
43Java Thread States
- The isAlive() method returns true if a thread is
not dead. - The getState() method returns values of DONE,
PENDING, STARTED
44Java Producer/Consumer
- class Producer implements Runnable
- private VectorltDategt mbox
- public Producer(VectorltDategt mbox)
this.mbox mbox - public void run()
- while(true) Thread.currentThread.sl
eep((int)Math.random(100)) - mbox.add(new
Date()) - class Consumer implements Runnable
- private Channel mbox
- public Consumer(Channel mbox) this.mbox
mbox - public void run()
- while(true)
- Thread.currentThread.sleep((int)Math.ran
dom(100)) - if ((!mbox.isEmpty())
System.out.println((Date)mbox.remove(0)) - public class Factory
- public Factory()
- VectorltDategt mailBox new VectorltDategt()
- new Producer(mailBox).start() new
Consumer(mailBox).start() - public static void main(String args)
Factory server new Factory()
45Threading Issues
- How do we spawn a new process from a thread?
Spawn all threads, or just the executing one? - How do we cancel a thread? What if it is working
with system resources? Approaches Asynchronous
or Synchronous cancellation? - How do we minimize overhead of continually
creating threads? Answer Thread pools - How do we communicate with threads? Linux Signal
handling, Windows Asynchronous procedure calls. - How do we create data that is local to a specific
thread? Answer Thread specific data - How are threads scheduled for execution? One
possibility Light weight processes
46Java Thread Cancellation
- Spawn the thread and then interrupt it
- Thread thread new MyThread().start()
- Execute for a while
- Interrupt the thread thread.interrupt()
- Check if interrupted in the thread's run method
- Thread.currentThread.isInterrupted()
- Thread.currentThread.interrupted()
- Note The interrupted() method resets the
interrupted status. The isInterrupted() method
does not.
47Signal Handling
- UNIX systems use signals to notify a process that
a an event has occurred - Signal handler methods process signals
- Signal is generated by particular event
- Signal is delivered to a process
- Signal is handled
- Options
- Deliver the signal to the thread waiting for the
signal - Deliver the signal to every thread in the process
- Deliver the signal to selected threads in the
process - Assign a specific thread to receive all signals
48Thread Pools
A group of threads created in advance that await
work to do
- Advantages
- Eliminates overhead thread creation and
destruction overhead - Applications can control the size of the pool
- Java Options
- Single thread executor - pool of size
1.Executors.newSingleThreadExecutor() - Fixed thread executor - pool of fixed
size.Executors.newFixedThreadPool(int
nThreads) - Cached thread pool - pool of unbounded
sizeExecutors.newCachedThreadPool()
49Thread Pool Example
- public class Task implements Runnable
- public void run()
- System.out.println(new Date())
-
- public class Pool
- public static void main(String args)
- int tasksInteger.parseInt(args0.trim())
- ExecutorService pool
- Executors.newCachedThreadPool()
- for (int i0 iltnumTasks i)
- pool.execute(new Task())
- pool.shutdown()
50Thread Specific Data
- Each thread can have its own local data
- Useful when you are using thread pools
- Scope
- Static goes with the class
- Normal goes with instantiated objects
- ThreadLocal goes with each thread
- class Service
- private static ThreadLocal errorCode new
ThreadLocal() - public static void transaction()
- try
- // some operation
- catch (Exception e) errorCode.set(e)
-
- public static Object getErrorCode() return
errorCode.get()
51Light Weight Processes Up-calls
- How many kernel threads
- Too many means extra OS overhead
- Too few means processes block
- The kernel assigns a group of kernel threads to
a process - Kernel up-calls the thread library
- If the process is about to block
- If a blocked kernel thread frees
- Threads are freed and allocated with thread
library help - User level thread processing minimizes OS
overhead - Kernel thread contains minimal resources needed
for scheduling processes (hence LWP)