25
Operating Systems Operating Systems COMP 4850/CISG 5550 COMP 4850/CISG 5550 Processes Processes Introduction to Threads Introduction to Threads Dr. James Money Dr. James Money

Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Embed Size (px)

Citation preview

Page 1: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Operating SystemsOperating SystemsCOMP 4850/CISG 5550COMP 4850/CISG 5550

ProcessesProcessesIntroduction to ThreadsIntroduction to Threads

Dr. James MoneyDr. James Money

Page 2: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

ProcessesProcesses

• A A processprocess is the abstraction of a is the abstraction of a running program. This includes the running program. This includes the code, associated memory, among code, associated memory, among other items.other items.

• Most operating systems allow more Most operating systems allow more than one process to be runnable at a than one process to be runnable at a given time.given time.

Page 3: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Process ModelProcess Model

• A process is an executing programA process is an executing program

• We mentally think of the process We mentally think of the process having a virtual CPU.having a virtual CPU.

• However, in reality, the CPU switches However, in reality, the CPU switches between active processes in a rapid between active processes in a rapid fashion.fashion.

• This switching is called This switching is called multiprogramming.multiprogramming.

Page 4: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Process ModelProcess Model

Page 5: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Process CreationProcess Creation

• We need a way to create and We need a way to create and terminate a processterminate a process

• There are a number of ways a There are a number of ways a process is created.process is created.

Page 6: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Process CreationProcess Creation

• Processes are created when:Processes are created when:– System initializationSystem initialization– Execution of a process creation system Execution of a process creation system

call by a running processcall by a running process– A user request to create a new processA user request to create a new process– Initiation of a batch jobInitiation of a batch job

Page 7: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Types of processesTypes of processes

• There are two types of processes:There are two types of processes:– Foreground – interact with userForeground – interact with user– Background – run a particular function.Background – run a particular function.

•These are called These are called daemonsdaemons..

•Typically these handle mail, web pages, Typically these handle mail, web pages, remote file requests, etc.remote file requests, etc.

Page 8: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Creating ProcessesCreating Processes

• UNIX:UNIX:– fork()fork()– Followed by exec()Followed by exec()

• WindowsWindows– CreateProcessCreateProcess

• Separate address space for each Separate address space for each processprocess

Page 9: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Process TerminationProcess Termination

• Eventually a processes ends or Eventually a processes ends or terminatesterminates due to one of the due to one of the following:following:– Normal, voluntary exitNormal, voluntary exit– Error exit, voluntaryError exit, voluntary– Fatal error, involuntaryFatal error, involuntary– Killed by another process, involuntaryKilled by another process, involuntary

Page 10: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Process HierarchiesProcess Hierarchies

• When processes create new When processes create new processes, you get a hierarchy in processes, you get a hierarchy in UNIX, which also called a UNIX, which also called a process process groupgroup..

• One process creates a new one, which One process creates a new one, which is called the is called the child processchild process..

• The process that create the new The process that create the new process is called the process is called the parent processparent process..

• Windows has no hierarchy.Windows has no hierarchy.

Page 11: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Process StatesProcess States

• Many times processes interact with Many times processes interact with other processother process

• Example:Example:– cat chapter1 chapter2 chapter3|grep cat chapter1 chapter2 chapter3|grep

treetree

• Grep may run before cat starts it’s Grep may run before cat starts it’s outputoutput

• This state is called This state is called blockedblocked..

Page 12: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Process StatesProcess States

• There are three statesThere are three states– Running – using the CPURunning – using the CPU– Ready – runnable, but stoppedReady – runnable, but stopped– Blocked – waiting for an external event Blocked – waiting for an external event

to occurto occur

Page 13: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Process StatesProcess States

• Examples of transitions between Examples of transitions between states:states:

Page 14: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Process StatesProcess States

• This model results in a This model results in a schedulerscheduler, , which handles which process is which handles which process is executing a given instant.executing a given instant.

• We think of the scheduler being the We think of the scheduler being the base of the operating systembase of the operating system

• The scheduler handles interrupts and The scheduler handles interrupts and scheduling.scheduling.

Page 15: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Implementing ProcessesImplementing Processes

• To implement, we use a To implement, we use a process process tabletable, with one entry per process., with one entry per process.

• Each entry is a called a Each entry is a called a process process control blockcontrol block..

Page 16: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Implementing ProcessesImplementing Processes

• Each PCB hasEach PCB has– Process stateProcess state– Program counterProgram counter– Stack pointerStack pointer– Memory allocationsMemory allocations– Open filesOpen files– Scheduling informationScheduling information– Other info needed for runningOther info needed for running

Page 17: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

PCBsPCBs

Page 18: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Handling InterruptsHandling Interrupts

Page 19: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

ThreadsThreads

• Normally, each process has one Normally, each process has one thread thread of executionof execution, or shortened, a , or shortened, a threadthread..

• Many times it is desirable to have Many times it is desirable to have multiple threadsmultiple threads

• Sometimes called Sometimes called lightweight processeslightweight processes..• We use the term We use the term multithreadingmultithreading to refer to refer

to the fact multiple threads are running to the fact multiple threads are running in a single process.in a single process.

Page 20: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Thread ModelThread Model

Page 21: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Thread ModelThread Model

• Different threads != different processesDifferent threads != different processes• No memory protection in threads, so No memory protection in threads, so

they can wipe other threads memory they can wipe other threads memory valuesvalues

• Threads have unique:Threads have unique:– Program countersProgram counters– RegistersRegisters– StackStack– State informationState information

Page 22: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Thread ModelThread Model

Page 23: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Thread ModelThread Model

• Threads have three states like Threads have three states like processes, plus a final one:processes, plus a final one:– RunningRunning– BlockedBlocked– ReadyReady– And terminatedAnd terminated

• Threads have independent stacksThreads have independent stacks

Page 24: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Thread ModelThread Model

Page 25: Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money

Thread FunctionsThread Functions

• thread_create() – create a new thread_create() – create a new thread from a function pointerthread from a function pointer

• thread_exit() – exit a threadthread_exit() – exit a thread

• thread_wait() – wait for a thread to thread_wait() – wait for a thread to finishfinish

• thread_yield() – voluntarily give up thread_yield() – voluntarily give up CPU to other threadsCPU to other threads