Transcript process

Operating Systems
COMP 4850/CISG 5550
Processes
Introduction to Threads
Dr. James Money
Processes
• A process is the abstraction of a running
program. This includes the code,
associated memory, among other items.
• Most operating systems allow more than
one process to be runnable at a given
time.
Process Model
• A process is an executing program
• We mentally think of the process having a
virtual CPU.
• However, in reality, the CPU switches
between active processes in a rapid
fashion.
• This switching is called multiprogramming.
Process Model
Process Creation
• We need a way to create and terminate a
process
• There are a number of ways a process is
created.
Process Creation
• Processes are created when:
– System initialization
– Execution of a process creation system call by
a running process
– A user request to create a new process
– Initiation of a batch job
Types of processes
• There are two types of processes:
– Foreground – interact with user
– Background – run a particular function.
• These are called daemons.
• Typically these handle mail, web pages, remote file
requests, etc.
Creating Processes
• UNIX:
– fork()
– Followed by exec()
• Windows
– CreateProcess
• Separate address space for each process
Process Termination
• Eventually a processes ends or terminates
due to one of the following:
– Normal, voluntary exit
– Error exit, voluntary
– Fatal error, involuntary
– Killed by another process, involuntary
Process Hierarchies
• When processes create new processes,
you get a hierarchy in UNIX, which also
called a process group.
• One process creates a new one, which is
called the child process.
• The process that create the new process is
called the parent process.
• Windows has no hierarchy.
Process States
• Many times processes interact with other
process
• Example:
– cat chapter1 chapter2 chapter3|grep tree
• Grep may run before cat starts it’s output
• This state is called blocked.
Process States
• There are three states
– Running – using the CPU
– Ready – runnable, but stopped
– Blocked – waiting for an external event to
occur
Process States
• Examples of transitions between states:
Process States
• This model results in a scheduler, which
handles which process is executing a
given instant.
• We think of the scheduler being the base
of the operating system
• The scheduler handles interrupts and
scheduling.
Implementing Processes
• To implement, we use a process table,
with one entry per process.
• Each entry is a called a process control
block.
Implementing Processes
• Each PCB has
– Process state
– Program counter
– Stack pointer
– Memory allocations
– Open files
– Scheduling information
– Other info needed for running
PCBs
Handling Interrupts
Threads
• Normally, each process has one thread of
execution, or shortened, a thread.
• Many times it is desirable to have multiple
threads
• Sometimes called lightweight processes.
• We use the term multithreading to refer to
the fact multiple threads are running in a
single process.
Thread Model
Thread Model
• Different threads != different processes
• No memory protection in threads, so they
can wipe other threads memory values
• Threads have unique:
– Program counters
– Registers
– Stack
– State information
Thread Model
Thread Model
• Threads have three states like processes,
plus a final one:
– Running
– Blocked
– Ready
– And terminated
• Threads have independent stacks
Thread Model
Thread Functions
• thread_create() – create a new thread
from a function pointer
• thread_exit() – exit a thread
• thread_wait() – wait for a thread to finish
• thread_yield() – voluntarily give up CPU to
other threads