understanding operating systems sixth edition chapter 6 concurrent processes

118
Understanding Operating Systems Sixth Edition Chapter 6 Concurrent Processes

Post on 21-Dec-2015

233 views

Category:

Documents


2 download

TRANSCRIPT

Understanding Operating Systems Sixth Edition

Chapter 6Concurrent Processes

Understanding Operating Systems, Sixth Edition

Learning Objectives

After completing this chapter, you should be able to describe:

• The critical difference between processes and processors, and their connection

• The differences among common configurations of multiprocessing systems

• The significance of a critical region in process synchronization

• The basic concepts of process synchronization software: test-and-set, WAIT and SIGNAL, and semaphores

2

Understanding Operating Systems, Sixth Edition

Learning Objectives (cont'd.)

• The need for process cooperation when several processes work together

• How several processors, executing a single job, cooperate

• The similarities and differences between processes and threads

• The significance of concurrent programming languages and their applications

3

Understanding Operating Systems, Sixth Edition

Concurrent Processes

• Multiprogramming Systems– Systems that use only one CPU, one processor,

which is shared b y several jobs or processes

• Multiprocessing Systems– Single computers with multiple cores as well as linked

computing systems with only one processor each to share processing among them.

4

Understanding Operating Systems, Sixth Edition

Parallel Processing

• Parallel processing, one form of multiprocessing, is a situation in which two or more processors operate in unison.

• Two or more CPUs are executing instructions simultaneously.

• The Processor Manager has to coordinate the activity of each processor as well as synchronize cooperative interaction among the CPUs.

5

Understanding Operating Systems, Sixth Edition

Parallel Processing• There are two primary benefits to parallel

processing systems:– Increased reliability

• The availability of more than one CPU;

• If one processor fails, then the others can continue to operate and absorb the load.

• Not simple to implement– The system must be carefully designed so that:

» The failing processor can inform the other processors to take over.

» The OS must restructure its resource allocation strategies so the remaining processors don’t become overloaded.

6

Understanding Operating Systems, Sixth Edition

Parallel Processing

• There are two primary benefits to parallel processing systems:– Faster processing

• The increased processing speed is often achieved because sometimes Instructions can be processed in parallel, two or more at a time in one of several ways:

– Some systems allocate a CPU to each program or job;– Others allocate a CPU to each working set or parts of it;– Others subdivide individual instructions so that each

subdivision can be processed simultaneously.» Concurrent programming.

• Increased flexibility brings increased complexity.

7

Understanding Operating Systems, Sixth Edition

Parallel Processing

• Two major challenges remain:– How to connect the processors into configurations; – How to orchestrate processor interaction, which

applies to multiple interacting processes as well.• The complexities of the Processor Manager’s task

when dealing with multiple processors as illustrated by the Fast-Food Lunch Stop example.

8

Understanding Operating Systems, Sixth Edition

Parallel Processing

9

Understanding Operating Systems, Sixth Edition

Parallel Processing

• Synchronization is the key to the system’s success because many things can go wrong in a multiprocessing system.

• The system can’t work properly unless every processor communicates and cooperates with every other processor.

10

Understanding Operating Systems, Sixth Edition

Evolution of Multiprocessors• Multiprocessing can take place at several different

levels, each of which requires a different frequency of synchronization.

• Multiprocessing at the job level is fairly benign.– It’s as if each job is running on its own workstation

with shared system resources.

• When multiprocessing takes place at the thread level, a high degree of synchronization is required to:– Disassemble the process;– Perform the thread’s instructions;– Correctly reassemble the process.

11

Understanding Operating Systems, Sixth Edition

Evolution of Multiprocessors (cont'd.)

12

Understanding Operating Systems, Sixth Edition

Introduction to Multi-Core Processors

• Multi-core processors have several processors on a single chip.

• As processors became smaller in size and faster in processing speed, CPU designers began to use nanometer-sized transistors.

• Each transistor switches between two positions – 0 and 1 – as the computer conducts its binary arithmetic at increasingly fast speeds.

13

Understanding Operating Systems, Sixth Edition

Introduction to Multi-Core Processors (cont’d)

• This created problems:– As transistors reached nano-sized dimensions and the

space between transistors became even closer, the electrons have the ability to spontaneously tunnel, at random from one transistor to another, causing a tiny but measurable amount of current to leak.

• The smaller the transistor, the more significant the leak.

– As processors became faster, the heat also climbed and became increasingly difficult to disperse.

• These heat and tunneling issues threatened to limit the ability of chip designers to make processors even smaller.

14

Understanding Operating Systems, Sixth Edition

Introduction to Multi-Core Processors (cont’d)

• Solution:– One solution was to create a single chip with two

“processor cores” in the same amount of space.

– With this arrangement, two sets of calculations can take place at the same time.

– The two cores on the chip generate less heat than a single core of the same size and tunneling is reduced.

• However, the two cores each run more slowly than the single core chip.

– The software has to be structured to take advantage of the double calculation capability of the new chip design.

15

Understanding Operating Systems, Sixth Edition

Introduction to Multi-Core Processors (cont’d)

• Designers have created multi-core processors with predictions that 80 or more cores will be placed on a single chip.

16

Understanding Operating Systems, Sixth Edition

Introduction to Multi-Core Processors (cont’d)

• Multiple processor configuration impacts the OS.– The OS must manage multiple processors, multiple

RAMs, and the processing of many tasks at once.

• However, a dual-core chip is not always faster than a single-core chip.– It depends on the tasks being performed and whether

they’re multi-threaded or sequential.

17

Understanding Operating Systems, Sixth Edition

Typical Multiprocessing Configurations

• Three typical configurations are:– Master/Slave– Loosely Coupled– Symmetric

18

Understanding Operating Systems, Sixth Edition

Master/Slave Configuration

• An asymmetric multiprocessing system.• A single-processor system with additional slave

processors, each of which is managed by the primary master processor.

• The master processor: – Manages the entire system:

• All files, devices, memory, and processors

– Maintains the status of all processes in the system;– Performs storage management activities;– Schedules the work for the other processors;– Executes all control programs.

19

Understanding Operating Systems, Sixth Edition

Master/Slave Configuration (cont'd.)

• This configuration is well suited for computing environments in which processing time is divided between front-end and back-end processors.

• The front-end processor takes care of the interactive users and quick jobs.

• The back-end processor takes care of those with long jobs using the batch mode.

20

Understanding Operating Systems, Sixth Edition

Master/Slave Configuration (cont'd.)

21

Understanding Operating Systems, Sixth Edition

Master/Slave Configuration (cont'd.)

• The primary advantage of this configuration is its simplicity.

• It also has three serious disadvantages.– Its reliability is no higher than for a single processor

system because if the master processor fails, the entire system fails.

– It can lead to poor use of resources because if a slave processor should become free while the master processor is busy, the slave must wait until the master processor becomes free and can assign more work to it.

22

Understanding Operating Systems, Sixth Edition

Master/Slave Configuration (cont'd.)

– It increases the number of interrupts because all slave processors must interrupt the master processor every time they need OS intervention, such as for I/O requests,

• This creates long queues at the master processor level when there are many processors and many interrupts.

23

Understanding Operating Systems, Sixth Edition

Loosely Coupled Configuration• Features several complete computer systems, each with

its own memory, I/O devices, CPU, and OS.• Each processor controls its own resources:

– Files– Access to memory– I/O devices

• Each processor maintains its own commands and I/O management tables.

• The only difference between a loosely coupled multiprocessing system and a collection of independent single-processing systems is that each processor can communicate and cooperate with the other processors.

24

Understanding Operating Systems, Sixth Edition

Loosely Coupled Configuration (cont'd.)

25

Understanding Operating Systems, Sixth Edition

Loosely Coupled Configuration (cont’d)

• When a job arrives for the first time, it’s assigned to one processor.

• Once allocated, the job remains with the same processor until it’s finished.

• Each processor must have global tables that indicate to which processor each job has been allocated.

• To keep the system well balanced and to ensure the best use of resources, job scheduling is based on several requirements and policies.– New jobs might be assigned to the processor with the

lightest load or the best combination of output devices available.

26

Understanding Operating Systems, Sixth Edition

Loosely Coupled Configuration (cont’d)

• This system isn’t prone to catastrophic system failures.

• Even when a single processor fails, the others can continue to work independently.

• However, it can be difficult to detect when a processor has failed.

27

Understanding Operating Systems, Sixth Edition

Symmetric Configuration

• Tightly coupled.

• Has four advantages over loosely coupled configurations:– More reliable;– Uses resources effectively;– Can balance loads well;– Can degrade gracefully in the event of a failure.

• However, it is the most difficult to implement because the processes must be well synchronized to avoid the problems of races and deadlocks.

28

Understanding Operating Systems, Sixth Edition

Symmetric Configuration (cont'd.)

29

Understanding Operating Systems, Sixth Edition

Symmetric Configuration (cont'd.)

• Processor scheduling is decentralized. – A single copy of the OS and a global table listing each

process and its status is stored in a common area of memory so every processor has access to it.

– Each processor uses the same scheduling algorithm to select which process it will run next.

• Whenever a process is interrupted, whether because of an I/O request or another type of interrupt, its processor updates the corresponding entry in the process list and finds another process to run.

30

Understanding Operating Systems, Sixth Edition

Symmetric Configuration (cont'd.)

• This means that the processors are kept busy.• It also means that any given job or task may be

executed by several different processors during its run time.– Because each processor has access to all I/O devices

and can reference any storage unit, there are more conflicts as several processors try to access the same resource at the same time.

• This presents the obvious need for algorithms to resolve conflicts between processors,– Process Synchronization.

31

Understanding Operating Systems, Sixth Edition

Process Synchronization Software

• The success of process synchronization hinges on the capability of the OS to make a resource unavailable to other processes while it is being used by one of them.– The used resource must be locked away from other

processes until it is released.– Only when the resource is released is a waiting

process allowed to use the resource.

• A mistakes in synchronization could leave a job waiting indefinitely (starvation) or, if it’s a key resource, cause a deadlock.

32

Understanding Operating Systems, Sixth Edition

Process Synchronization Software (cont’d)

• Processor 1 and Processor 2 finish with their current jobs at the same time.

• To run the next job, each processor must:– Consult the list of jobs to see which one should be run

next;– Retrieve the job for execution;– Increment the READY list to the next job;– Execute it.

33

Understanding Operating Systems, Sixth Edition

Process Synchronization Software (cont’d)

• Both go to the READY list to select a job.

• Processor 1 sees that Job 74 is the next job and goes to retrieve it.

• A moment later Processor 2 also selects Job 74 and goes to retrieve it.

• Shortly thereafter, Processor 1, having retrieved Job 74 returns to the READY list and increments it, moving Job 75 to the top.

34

Understanding Operating Systems, Sixth Edition

Process Synchronization Software (cont’d)

• A moment later Processor 2 returns; it has also retrieved Job 74 and is ready to process it, so it increments the READY list and now Job 76 is moved to the top and becomes the next job in line to be processed.

• Job 75 has become the missed waiting customer and will never be processed.

• Job 74 is being processed twice.

• This problem can also occur in memory and page allocation tables, I/O tables, application databases, and any shared resource.

35

Understanding Operating Systems, Sixth Edition

Process Synchronization Software (cont'd.)

• This situation calls for synchronization.

• Several synchronization mechanisms are available to provide cooperation and communication among processes.

• The common element in all synchronization schemes is to allow a process to finish work on a critical part of the program before other processes have access to it.

36

Understanding Operating Systems, Sixth Edition

Process Synchronization Software (cont'd.)

• This is applicable both to multiprocessors and to two or more processes in a single-processor (time-shared) processing system. It is called a Critical Region:– It is a critical section and its execution must be

handled as a unit.– Processes within a critical region can’t be interleaved

without threatening the integrity of the operation.

37

Understanding Operating Systems, Sixth Edition

Process Synchronization Software (cont'd.)

• Synchronization is sometimes implemented as a lock-and-key arrangement:– Before a process can work on a critical region, it must

get the key.– Once it has the key

• All other processes are locked out until it finishes;

• Unlocks the entry to the critical region;

• Returns the key so that another process can get the key and begin work.

38

Understanding Operating Systems, Sixth Edition

Process Synchronization Software (cont'd.)

– This sequence consists of two actions:• The process must first see if the key is available;

• If it is available, the process must pick it up and put it in the lock to make it unavailable to other processors.

– For this scheme to work, both actions must be performed in a single machine cycle, otherwise, it is conceivable that while the first process is ready to pick up the key, another one would find the key available and prepare to pick up the key and each could block the other from proceeding any further.

39

Understanding Operating Systems, Sixth Edition

Process Synchronization Software (cont'd.)

• Types of locking mechanisms– Test-and-Set– WAIT and SIGNAL– Semaphores

40

Understanding Operating Systems, Sixth Edition

Test-and-Set• A single, indivisible machine instruction known simply

as TS and was introduced by IBM for its multiprocessing System 360/370 computers.

• In a single machine cycle it tests to see if the key is available. – If it is, it’s set to unavailable.

• The actual key is a single bit in a storage location that can contain a 0 (free) or a 1 (busy).

• We can consider TS to be a function subprogram that has one parameter (the storage location) and returns one value (the condition code: busy/free), with the exception that it takes only one machine cycle.

41

Understanding Operating Systems, Sixth Edition

Test-and-Set (cont’d)• Process 1 would test the condition code using the

TS instruction before entering a critical region.• If no other process was in this critical region, then

Process 1 would be allowed to proceed and the condition code would be changed from 0 to 1.

• Later, when Process 1 exits the critical region, the condition code is reset to 0 so another process can enter.

• If Process 1 finds a busy condition code, then it’s placed in a waiting loop where it continues to test the condition code and waits until it’s free.

42

Understanding Operating Systems, Sixth Edition

Test-and-Set (cont'd.)

• Advantages:– It’s a simple procedure to implement;– It works well for a small number of processes.

• Test-and-Set has two major drawbacks:– First, when many processes are waiting to enter a

critical region, starvation could occur because the processes gain access in an arbitrary fashion.

• Unless a first-come, first-served policy were set up, some processes could be favored over others.

• Starvation

43

Understanding Operating Systems, Sixth Edition

Test-and-Set (cont'd.)

• Test-and-Set has two major drawbacks:– A second drawback is that the waiting processes

remain in unproductive, resource-consuming wait loops, requiring context switching (busy waiting).

• Not only consumes valuable processor time but also relies on the competing processes to test the key.

– Best handled by the OS or the hardware.

44

Understanding Operating Systems, Sixth Edition

WAIT and SIGNAL

• A modification of test-and-set that’s designed to remove busy waiting.

• Two new operations, which are mutually exclusive and become part of the process scheduler’s operations are:– WAIT

• Activated when process encounters a busy condition code.

• WAIT sets the process’s process control block (PCB) to the blocked state and links it to the queue of the processes waiting to enter this particular critical region.

45

Understanding Operating Systems, Sixth Edition

WAIT and SIGNAL (cont’d)– WAIT

• The Process Scheduler then selects another process for execution.

• SIGNAL– Activated when a process exits the critical region and the

condition code is set to “free”.– It checks the queue of processes waiting to enter this critical

region and selects one, setting it to the READY state.– Eventually the Process Scheduler will choose this process for

running.• The addition of the operations WAIT and SIGNAL frees the

processes from the busy waiting dilemma and returns control to the OS, which can run other jobs while the waiting processes are idle (WAIT).

46

Understanding Operating Systems, Sixth Edition

Semaphores

• A non-negative integer variable that’s used as a binary signal (a flag).

• It signals if and when a resource is free and can be used by a process.

• Dijkstra (1965) introduced two operations to overcome process synchronization.– P (proberen means “to test”) – V (verhogen means “to increment”)

47

Understanding Operating Systems, Sixth Edition

Semaphores (cont'd.)

48

Understanding Operating Systems, Sixth Edition

Semaphores (cont'd.)

• Let s be a semaphore variable. The V operation on s is simply to increment s by 1. – V(s): s: = s + 1

• This necessitates a fetch, increment, and store sequence.

• The increment operation must be performed as a single indivisible action to avoid deadlocks.

• s cannot be accessed by any other process during the operation.

49

Understanding Operating Systems, Sixth Edition

Semaphores (cont'd.)

• The operation P on s is to test the value of s and, if it’s not 0, to decrement it by 1.– P(s): If s > 0, then s: = s – 1

• This involves a test, fetch, decrement, and store sequence.

• This sequence must be performed as an individual action in a single machine cycle or be arranged so that the process cannot take action until the operation (test or increment) is finished.

50

Understanding Operating Systems, Sixth Edition

Semaphores (cont'd.)

– The operations to test or increment are executed by the OS in response to calls issued to any one process naming a semaphore as parameter.

– If s = 0, it means that the critical region is busy and the process calling on the test operation must wait until the operation can be executed (s > 0).

51

Understanding Operating Systems, Sixth Edition

Semaphores (cont'd.)

52

Understanding Operating Systems, Sixth Edition

Semaphores (cont'd.)

• Test and increment operations on semaphore s enforce the concept of mutual exclusion, which is necessary to avoid having two operations attempt to execute at the same time.

• The name traditionally given to this semaphore is mutex (MUTual EXclusion):

P(mutex): if mutex > 0 then mutex: = mutex – 1V(mutex): mutex: = mutex + 1

• The concept of a critical region becomes necessary because it ensures that parallel processes will modify shared data only while in the critical region.

53

Understanding Operating Systems, Sixth Edition

Semaphores (cont'd.)

• In sequential computations mutual exclusions is achieved automatically because each operation is handled in order, one at a time.

• In parallel computations the order of execution can change, so mutual exclusion must be explicitly stated and maintained.

• The entire premise of parallel processes hinges on the requirement that all operations on common variables consistently exclude one another over time.

54

Understanding Operating Systems, Sixth Edition

Process Cooperation

• There are occasions when several processes work directly together to complete a common task.

• Two famous examples are the problems of producers and consumers, and of readers and writers.

• Each case requires mutual exclusion and synchronization.

• Each is implemented by using semaphores.

55

Understanding Operating Systems, Sixth Edition

Producers and Consumers

• One process produces some data that another process consumes later.

• Example: – The CPU can generate output data much faster than

a printer can print it.– Since this involves a producer and a consumer of two

different speeds, we need a buffer where the producer can temporarily store data that can be retrieved by the consumer at a more appropriate speed.

56

Understanding Operating Systems, Sixth Edition

Producers and Consumers (cont'd.)

57

Understanding Operating Systems, Sixth Edition

Producers and Consumers (cont’d)– Because the buffer can hold only a finite amount of data,

the synchronization process must delay the producer from generating more data when the buffer is full.

– It must also be prepared to delay the consumer from retrieving data when the buffer is empty.

– This task can be implemented by two counting semaphores:

• One to indicate the number of full positions in the buffer;

• The other to indicate the number of empty positions in the buffer.

• A third semaphore, mutex, will ensure mutual exclusion between processes.

58

Understanding Operating Systems, Sixth Edition

Producers and Consumers (cont'd.)

59

Understanding Operating Systems, Sixth Edition

Producers and Consumers (cont'd.)

60

Understanding Operating Systems, Sixth Edition

Producers and Consumers (cont'd.)

• Producers and Consumers Algorithmempty: = n

full: = 0

mutex: = 1

COBEGIN

repeat until no more data PRODUCER

repeat until buffer is empty CONSUMER

COEND

61

Understanding Operating Systems, Sixth Edition

Producers and Consumers (cont’d)• The concept of producers and consumers can be

extended to buffers that hold records or other data, as well as to other situations in which direct process-to-process communication of messages is required.

62

Understanding Operating Systems, Sixth Edition

Readers and Writers

• When two types of process need to access a shared resource.– Example: file or database

• Example: airline reservation system– The readers are those who want flight information.

• They only read the existing data.

• They don’t modify it.

• Because no one is changing the database, the system can allow many readers to be active at the same time.

• There’s no need to enforce mutual exclusion among them.

63

Understanding Operating Systems, Sixth Edition

Readers and Writers (cont’d)

• Example: airline reservation system– The writers are those who are making reservations on

a particular flight.– Writers must be carefully accommodated because

they are modifying existing data in the database.– The system can’t allow someone to be writing while

someone else is reading (or writing).– It must enforce mutual exclusion if there are groups of

readers and a writer, or if there are several writers in the system.

64

Understanding Operating Systems, Sixth Edition

Readers and Writers (cont’d)• Two solutions using P and V operations

– The first gives priority to readers over writers so readers are kept waiting only if a writer is actually modifying the data.

• This policy results in writer starvation of there is a continuous stream of readers.

– The second policy gives priority to the writers.• As soon as a writer arrives, any readers that are already

active are allowed to finish processing.• All additional readers are put on hold until the writer is done.

– This policy results in reader starvation if a continuous stream of writers is present.

65

Understanding Operating Systems, Sixth Edition

Readers and Writers (cont’d)• To prevent either type of starvation, Hoare (1974)

proposed the following combination priority policy.– When a writer is finished, any and all readers who are

waiting, or on hold, are allowed to read.– When that group of readers is finished, the writer who

is on hold can begin, and any new readers who arrive in the meantime aren’t allowed to start until the writer is finished.

66

Understanding Operating Systems, Sixth Edition

Readers and Writers (cont’d)• The state of the system can be summarized by four

counters initialized to 0:– Number of readers who have requested a resource

and haven’t yet released it (R1 = 0).– Number of readers who are using a resource and

haven’t yet released it (R2 = 0).– Number of writers who have requested a resource

and haven’t yet released it (W1 = 0).– Number of writers who are using a resource and

haven’t yet released it (W2 = 0).

67

Understanding Operating Systems, Sixth Edition

Readers and Writers (cont’d)• This can be implemented using two semaphores to

ensure mutual exclusion between readers and writers.– A resource can be given to all readers, provided that no

writers are processing (W2 = 0).– A resource given to a writer, provided that no readers

are reading (R2 = 0) and no writers writing (W2 = 0).

• Readers must always call two procedures:– The first checks whether the resources can be

immediately granted for reading.– When the resource is released, the second checks to

see if there are any writers waiting.

68

Understanding Operating Systems, Sixth Edition

Readers and Writers (cont’d)• Writers must always call two procedures:

– The first determines if the resource can be immediately granted for writing.

– When the resource is released, the second checks to see if there are any readers waiting.

69

Understanding Operating Systems, Sixth Edition

Concurrent Programming

• Until now we’ve looked at multiprocessing as several jobs executing at the same time on a single processor or on multi-processors.

• Multiprocessing can also refer to one job using several processors to execute sets of instructions in parallel.– Concurrent Processing Systems – Requires a programming language and a computer

system that can support this type of construct.

70

Understanding Operating Systems, Sixth Edition

Applications of Concurrent Programming

• Most programming languages are serial in nature.– Instructions are executed one at a time.

• To resolve an arithmetic expression, every operation is done in sequence following the order prescribed by the programmer and compiler.

71

Understanding Operating Systems, Sixth Edition

Applications of Concurrent Programming (cont’d)

A = 3 * B * C + 4 / (D + E) ** (F – G)

72

Understanding Operating Systems, Sixth Edition

Applications of Concurrent Programming (cont’d)

• All equations follow a standard of order.– You first perform all calculations in parentheses.

– Second, you calculate all exponents.

– Third, you perform all multiplication and division.

– Fourth, you perform the addition and subtraction.

– For each step you go from left to right.• If you performed the calculations in some other order,

you could come up with the wrong answer.

• For many computational purposes, serial processing is sufficient; it’s easy to implement and fast enough for most users.

73

Understanding Operating Systems, Sixth Edition

Applications of Concurrent Programming (cont’d)

• Arithmetic expressions can be processed differently if we use a language that allows for concurrent processing.– COBEGIN– COEND

• Indicates to the compiler which instructions can be processed concurrently.

74

Understanding Operating Systems, Sixth Edition

Applications of Concurrent Programming (cont’d)

• COBEGIN T1 = 3 * B T2 = D + E T3 = F – GCOENDCOBEGIN T4 = T1 * C T5 = T2 ** T3COEND A = T4 + 4 /T5

75

Understanding Operating Systems, Sixth Edition

Applications of Concurrent Programming (cont'd.)

A = 3 * B * C + 4 / (D + E) ** (F – G)

76

Understanding Operating Systems, Sixth Edition

Applications of Concurrent Programming (cont'd.)

• With this system we’ve increased the computation speed, but we’ve also increased the complexity of the programming language and the hardware.

• We’ve placed a large burden on the programmer to explicitly state which instructions can be executed in parallel.– Explicit Parallelism– The automatic detection by the compiler of

instructions that can be performed in parallel is called Implicit Parallelism.

77

Understanding Operating Systems, Sixth Edition

Applications of Concurrent Programming (cont'd.)

• With a true concurrent processing system, the instruction is coded as a single expression.

• It is the compiler that translates the algebraic expression into separate instructions and decides which steps can be performed in parallel and which in serial mode.– The equation Y = A + B * C + D could be rearranged

by the compiler as A + D + B * C so that two operations A + D and B * C would be done in parallel, leaving the final addition to be calculated last.

78

Understanding Operating Systems, Sixth Edition

Applications of Concurrent Programming (cont'd.)

• Concurrent processing can also dramatically reduce the complexity ( See Pages 189 – 190)– Of working with array operations within loops;

• To perform an array operation within a loop in three steps, the instructions might say:

for (j = 1: J <=3; J++)a (j) = b(j) + c(j);

• If we use three processors, the instruction can be performed in a single step:

Processor 1 Processor 2 Processor 3a(1)=b(1)+c(1) a(2)=b(2)+c(2) a(3)=b(3)+c(3)

79

Understanding Operating Systems, Sixth Edition

Applications of Concurrent Programming (cont'd.)

– Of performing matrix multiplication:• Using one processor, the answer to the equation on Page

189 can be computed in 27 steps.

• By multiplying several elements of the first row of Matrix 1 by corresponding elements in Matrix 2, three processors could cooperate to resolve the equation in fewer steps.

• With two processors it takes 18 steps to perform the calculations in parallel.

• With three, it would require even fewer steps.

• Concurrent processing does not necessarily cut processing activity in direct proportion to the number of processors available.

80

Understanding Operating Systems, Sixth Edition

Applications of Concurrent Programming (cont'd.)

– Of conducting parallel searches in databases:• Database searching is a common non-mathematical

application for concurrent processing systems.

• The entire file can be split into discrete sections with one processor allocated to each section.

– This results in a significant reduction in search time.

• Once the item is found, all processors can be deallocated and set to work on the next task.

• A concurrent search is faster than if a single processor was allocated to search the database.

81

Understanding Operating Systems, Sixth Edition

Applications of Concurrent Programming (cont'd.)

– Of sorting or merging files:• By dividing a large file into sections, each with its own

processor, every section can be sorted at the same time.

• Then pairs of sections can be merged together until the entire file is whole again – and sorted.

82

Understanding Operating Systems, Sixth Edition

Threads and Concurrent Programming

• So far we have considered the cooperation and synchronization of traditional processes, also known as heavyweight processes, which have the following characteristics:– They pass through several states from their initial

entry into the computer system to their completion:• Ready, running, waiting, delayed, and blocked

– They require space in main memory where they reside during the execution.

– From time to time they require other resources such as data.

83

Understanding Operating Systems, Sixth Edition

Threads and Concurrent Programming (cont’d)

• These processes are often swapped between main memory and secondary storage during their execution to accommodate multiprogramming and to take advantage of virtual memory.

• Every time a swap occurs, overhead increases because of all the information that has to be saved.

• To minimize the overhead time, most current Oss support the implementation of threads, or lightweight processes, which have become part of numerous application packages.

• Threads are supported at both the kernel and user level and can be managed by either the OS or the application that created them.

84

Understanding Operating Systems, Sixth Edition

Threads and Concurrent Programming (cont’d)

• A thread is a smaller unit within a process, which can be scheduled and executed.

• Threads share the same resources as the process that created them, which now becomes a more passive element because the thread is the unit that uses the CPU and is scheduled for execution.

• Processes might have from one to several active threads, which can be created, scheduled, and synchronized more efficiently because the amount of information needed is reduced.

85

Understanding Operating Systems, Sixth Edition

Threads and Concurrent Programming (cont’d)

• When running a process with multiple threads in a computer system with a single CPU, the processor switches very quickly from one thread to another, giving the impression that the threads are executing in parallel.– It is only in systems with multiple CPUs that the

multiple threads in a process are actually executed in parallel.

• Each active thread in a process has its own process registers, program counter, stack and status, but shares the data area and the resources allocated to its process.

86

Understanding Operating Systems, Sixth Edition

Threads and Concurrent Programming (cont’d)

• Each thread has its own program counter and stack, which is used to store variables dynamically created by a process.– These variables are stored in the stack when the

function is invoked and are destroyed when the function is exited.

• Since threads within a process share the same space and resources they can communicate more efficiently, increasing processing performance.

87

Understanding Operating Systems, Sixth Edition

Threads and Concurrent Programming (cont’d)

• Consider a Web Server:– When a Web Server receives requests for images or

pages, it serves each request with a different thread.– The process that receives all the requests may have

one thread that accepts these requests and creates a new separate thread for each request received.

– This new thread retrieves the required information and sends it to the remote client.

– While this thread is doing its task, the original thread is free to accept more requests.

– Web serves are multiprocessor systems that allow for the concurrent operation of several requests, thus improving throughput and response time.

88

Understanding Operating Systems, Sixth Edition

Threads and Concurrent Programming (cont’d)

– Instead of creating and destroying threads to serve incoming requests, which would increase the overhead, Web servers keep a pool of threads that can be assigned to those requests.

– After a thread has completed its task, instead of being destroyed, it is returned to the pool to be assigned to another request.

89

Understanding Operating Systems, Sixth Edition

Thread States

• As a thread moves through the system it is one of five states, not counting its creation and finished status.

• When an application creates a thread, it is made ready by allocating to it the needed resources and placing it in the READY queue.

• The thread state changes from READY to RUNNING when the Thread Scheduler whose function is similar to that of the Process Scheduler, assigns it a process.

90

Understanding Operating Systems, Sixth Edition

Thread States (cont’d)

• A thread transitions from RUNNING to WAITING when it has to wait for an event outside its control to occur.

• Alternatively, another thread, having completed its task, can send a signal indicating that the waiting thread can continue to execute.

91

Understanding Operating Systems, Sixth Edition

Thread States (cont’d)

92

Understanding Operating Systems, Sixth Edition

Thread States (cont’d)

• With a Word Processor:– The thread that periodically backs up a current

document to disk will be delayed for a period of time after it has completed the backup.

– After the time has expired, it performs the next backup and then is delayed again.

– If this delay was not built into the application, this thread would be forced into a loop that would continuously test to see if it was time to do a backup, wasting processor time and reducing system performance.

93

Understanding Operating Systems, Sixth Edition

Thread States (cont’d)

• A thread transitions from RUNNING to BLOCKED when an application issues an I/O request.– After the I/O is completed, the thread returns too the

READY state.– When a thread transitions from RUNNING to

FINISHED, all its resources are released and it can exit the system.

94

Understanding Operating Systems, Sixth Edition

Thread States (cont'd.)

• The same operations are performed on both traditional processors and threads.

• The OS must be able to support:– Creating new threads;– Setting up a thread so it is ready to execute;– Delaying, or putting to sleep, threads for a specified

amount of time;– Blocking, or suspending, threads that are waiting for

I/O to be completed;– Setting threads on a WAIT state until a specific event

has occurred;95

Understanding Operating Systems, Sixth Edition

Thread States (cont'd.)

– Scheduling threads for execution;– Synchronizing thread execution using semaphores,

events, or conditional variables;– Terminating a thread and releasing its resources.

96

Understanding Operating Systems, Sixth Edition

Thread Control Block

• Just as processes are represented by Process Control Blocks (PCBs), so threads are represented by Thread Control Blocks (TCBs), which contain basic information about a thread such as its ID, state, and a pointer to the process that created it.

97

Understanding Operating Systems, Sixth Edition

Thread Control Block (cont’d)

• A typical TCB:– A thread ID, a unique identifier assigned by the OS

when the thread is created.– The thread state, which changes as the thread

progresses through its execution; state changes, as well as all other entries in the TCB, apply individually to each thread.

– CPU information, which contains everything that the OS needs to know:

• How far the thread has executed;• Which instruction is currently being performed;• What data is being used.

98

Understanding Operating Systems, Sixth Edition

Thread Control Block (cont’d)

• A typical TCB:– Thread priority, used to indicate the weight of this

thread relative to other threads and used by the Thread Scheduler when determining which thread should be selected from the READY queue.

– A pointer to the process that created the thread.– Pointers to other threads created by this thread.

99

Understanding Operating Systems, Sixth Edition

Thread Control Block

• Information about current status and characteristics of thread

100

Understanding Operating Systems, Sixth Edition

Concurrent Programming Languages• Early programming languages did not support the

creation of threads or the existence of concurrent processes.

• They gave programmers the possibility of creating a single process or thread of control.

• Ada – Developed in the late 1970s– One of the first languages to provide specific

concurrency commands.

101

Understanding Operating Systems, Sixth Edition

Java

• Developed by Sun Microsystems, Inc. (1995)

• Designed as a universal software platform for Internet applications.

• Allows programmers to code an application with the capability to run on any computer.

• This type of universal software platform was an attempt to solves several issues:– The high cost of developing software applications for

each of the many incompatible computer architectures available.

102

Understanding Operating Systems, Sixth Edition

Java (cont’d)

– The needs of distributed client-server environments;– The growth of the Internet and the World Wide Web,

which added more complexity to program development.

• Uses both a compiler and an interpreter.– The source code of a Java program is first compiled

into an intermediate language (Java bytecodes) which are platform-independent.

• One can compile a Java program on any computer that has a Java compiler, and the bytecodes can then be run on any compiler that has a Java interpreter.

103

Understanding Operating Systems, Sixth Edition

The Java Platform

• Typically, a computer platform contains the hardware and software where a program runs.

• The Java platform is a software-only platform that runs on top of other hardware-based platforms.

• Has two components:– The Java Virtual Machine (Java VM):

• The foundation for the Java platform and contains the Java interpreter, which runs the bytecodes provided by the compiler.

• Sits on top of many different hardware-based platforms.

104

Understanding Operating Systems, Sixth Edition

The Java Platform (cont'd.)

105

Understanding Operating Systems, Sixth Edition

The Java Platform (cont’d)

• Has two components:– The Java API:

• A collection of software modules that programmers can use in their applications.

• Grouped into libraries (packages) of related classes and interfaces.

– Provide well-tested objects ranging from basic data types to I/I functions, from network interfaces to graphical user interface kits.

106

Understanding Operating Systems, Sixth Edition

The Java Language Environment

• Java was designed to make it easy for experienced programmers to learn.

• It is object-oriented– It takes advantage of modern software development

methodologies and fits well into distributed client-server applications.

• Memory allocation is done at run time, unlike C and C++ where memory allocation is done at compilation time.

107

Understanding Operating Systems, Sixth Edition

The Java Language Environment (cont’d)

• Java’s compiled code references memory via symbolic “handles” that are translated into real memory addresses at run time by the Java interpreter.

• The memory allocation and referencing models are not visible to programmers, but are controlled entirely by the underlying run-time platform.

108

Understanding Operating Systems, Sixth Edition

The Java Language Environment (cont'd.)

• Because Java applications run in distributed environments, security is a very important built-in feature of the language and run-time system.

• It provides compile-time checking and run-time checking, which helps programmers create very reliable software.– While a Java program is executing it can request

particular classes to be loaded from anywhere in the network.

– All incoming code is checked by a verifier, which ensures that the code is correct and can run safely without putting the Java interpreter at risk.

109

Understanding Operating Systems, Sixth Edition

The Java Language Environment (cont'd.)

• With its sophisticated synchronization capabilities, Java supports multithreading at the language level.

• The language library provides the thread class, and the run-time system provides monitor and condition lock primitives.– The thread class is a collection of methods used to

start, run, stop, and check the status of a thread.– Java’s threads are preemptive and, depending on the

platform on which the Java interpreter executes, can also be time-sliced.

110

Understanding Operating Systems, Sixth Edition

The Java Language Environment (cont'd.)

• When a programmer declares some methods within a class to be synchronized, they are not run concurrently.

• These synchronized methods are under the control of monitors that ensure that variables remain in a consistent state.

• When a synchronized method begins to run it is given a monitor for the current object, which does not allow any other synchronized method in that object to execute.

111

Understanding Operating Systems, Sixth Edition

The Java Language Environment (cont'd.)

• The monitor is released when a synchronized method exits, which allows other synchronized methods within the same object to run.

• Java technology continues to be popular with programmers for several reasons:– It offers the capability of running a single program on

various platforms without having to make any changes.

– It offers a robust set of features such as run-time memory allocation, security, and multi-threading.

112

Understanding Operating Systems, Sixth Edition

The Java Language Environment (cont'd.)

– It is used for many Web and Internet applications, and integrates well with browsers that can run Java applets with audio, video, and animation directly in a Web page.

113

Understanding Operating Systems, Sixth Edition

Summary

• Multiprocessing can occur in several configurations:– In a single-processor system where interacting

processes obtain control of the processor at different times;

– In systems with multiple processors where the work of each processor communicates and cooperates with the others and is synchronized by the Processor Manager.

114

Understanding Operating Systems, Sixth Edition

Summary (cont’d)

• Three multiprocessing systems are described:• Master/slave

• Loosely coupled

• Symmetric

• The success of any multiprocessing system depends on the ability of the system to synchronize its processes with the system’s other resources.

115

Understanding Operating Systems, Sixth Edition

Summary (cont’d)

• The concept of mutual exclusion helps keep the processes with the allocated resources from becoming deadlocked.– Mutual exclusion is maintained with a series of

techniques including:• Test-and-Set

• WAIT and SIGNAL

• Semaphores: Test (P), increment (V), and mutex

116

Understanding Operating Systems, Sixth Edition

Summary (cont’d)

• Hardware and software mechanisms are used to synchronize the many processes but care must be taken to avoid the typical problems of synchronization:– Missed Waiting Customers– The Synchronization of Producers and Consumers– The Mutual Exclusion of Readers and Writers.

117

Understanding Operating Systems, Sixth Edition

Summary (cont’d)

• Continuing innovations in concurrent processing, including threads and multi-core processors, are requiring fundamental changes to Oss so they can take advantage of these new technologies.

• These innovations require retooling of the many applications that run on them as well as the OS software.

• Research in this area is expected to grow significantly in the next few years.

118