• No results found

Page 1.0 Introduction

2.0 Objectives

3.0 Difference between process and thread 3.1 Characteristics of threads 3.2 Benefits of threads 3.3 Multithreading 3.4 Race condition 4.0 Conclusion

5.0 Summary

6.0 Tutor Marked Assignment

7.0 Further Reading and Other Resources 1.0 Introduction

In many operating systems, the traditional concept of process has been split into two parts: one dealing with resource ownership (process) and one dealing with the stream of instruction execution (thread). A single process may contain multiple threads. A multithreaded organization has advantage both in the structure of applications and in performance. The unit examines the features and benefits of threads. It also discusses multithreading and race condition.

2.0 Objectives

At the end of this unit, the reader should be able to:

(a) Distinguish between Process & Thread

(b) Know the characteristics and benefits of threads

(c) Distinguish between Thread Control Block (TCB) and Process Control Block(PCB) (d) Know multithreading and race condition

3.0 Difference between Process & Thread

Process: This is a general term for a program which is being executed. All work done by the CPU contributes to the execution of processes. Each process has a descriptive information structure associated with it (normally held by the kernel) called a process control block which keeps track of how far the execution has progressed and what resources the process holds.

Thread: (sometimes called a lightweight process) is different from process or task in that a thread is not enough to get a whole program executed. A thread is a kind of stripped down process, it is just one ‘active hand’ in a program, something which the CPU is doing on behalf of a program, but not enough to be called a complete process.

A thread is the basic unit of CPU utilization, which means that it is not a process that is doing the running - it's threads. Therefore, every process has at least one thread (the main thread).

This means that now we have to divide the metadata between a process and a thread:

A Process has its address space, global variables (that is, non-thread specific), handles (like open files), child processes (when applicable), signals and signal handlers and some accounting information).

A Thread, on the other hand, is an executing unit, and as such has its stack, registers, variables and current execution state. This is a part of the Thread Control Block.

Some benefits of threads over processes include: more lightweight (takes less time to create and destroy them), context switching is faster, they share memory and files without calling the kernel's system calls.

The Thread Model

Figure 1: (a) Three processes each with one thread (b) One process with three threads

Source: Abraham Silberschatz, Peter Baer Galvin, and Greg Gagne (2005). Operating Systems Concepts, 7th Edition, John Wiley & Sons Inc; ISBN 0471417432

Threads remember what they have done separately, but they share the information about what resources a program is using, and what state the program is in. A thread is only a CPU assignment. Several threads can contribute to a single task. When this happens, the information about one process or task is used by many threads. Each task must have at least one thread in order to do any work.

3.1 Characteristics of Threads:

The TCB (thread control block) consist of – program counter

– register set – stack space

Figure 2: Threads and Processes

Source: Abraham Silberschatz, Peter Baer Galvin, and Greg Gagne (2005). Operating Systems Concepts, 7th Edition, John Wiley & Sons Inc; ISBN 0471417432

Thus the TCB is a reduced PCB

• A traditional process is equal to a task with one thread

• All threads in a process share the state of that process

• They reside in the exact same memory space (user memory), see the same code and data

• When one thread alters a process variable (say, the working directory), all the others will see the change when they next access it

• If one thread opens a file to read it, all the other threads can also read from it.

• Because no system calls are involved, threads are fast

• There are no kernel structures affected by the existence of threads in a program, so no kernel resources are consumed -- threads are cheap

• The kernel does not even know that threads exist 3.2 Benefits of Threads

• It takes less time to create a new thread than a process

• It takes less time to terminate a thread than a process

• It takes less time to switch between two threads within the same process

• Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel.

• Suspending a process involves suspending all threads of the process since all threads share the same address space.

• Termination of a process, terminates all threads within the process

3.3 Multithreading

• Operating system supports multiple threads of execution within a single process

• MS-DOS supports a single thread

• UNIX supports multiple user processes but only supports one thread per process

• Windows 2000, Solaris, Linux, Mach, and OS/2 support multiple threads 3.4 Race Condition

We have a race condition if two processes or threads want to access the same item in shared memory at the same time. It is where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. To prevent race conditions, concurrent processes must coordinate or be synchronized.

Activity A

a) What is the advantage of using threads compared to processes?

b) Distinguish between a process and a thread.

4.0 Conclusion

A thread is a kind of stripped down process - it is just one `active hand' in a program - something which the CPU is doing on behalf of a program, but not enough to be called a complete process.

The TCB (thread control block) consist of: program counter, register set and stack space.

Threads take less time to be created than a process. Multithreading in Operating system supports multiple threads of execution within a single process. We have a race condition if two processes or threads want to access the same item in shared memory at the same time.

5.0 Summary

In this unit, we have learnt that:

(a) A Process is different from a Thread (b) TCB is a reduced PCB

(c) Windows 2000, Solaris, Linux, Mach, and OS/2 support multiple threads 6.0 Tutor Marked Assignment

(a) Briefly explain the term race condition.

(b) Enumerate the characteristics of multithreading

7.0 Further Reading and Other Resources

Abraham Silberschatz, Peter Baer Galvin, and Greg Gagne (2005). Operating Systems Concepts, 7th Edition, John Wiley & Sons Inc; ISBN 0471417432

Chapin S. and Maccabe A., eds. “Multiprocessor Operating Systems: Harnessing the Power.”

Special issue of IEEE Concurrency, April-June 1997.

C. M. Krishna & Kang G. Shin (2006), Real-Time Systems, McGraw-Hill International editions.

Goodheart B. and Cox J. The Magic Garden Explained: The Internals of UNIX System V Release 4. Englewood Cliffs, NJ: Prentice Hall, 1994.

Jean Bacon & Tim Harris (2003), Operating Systems, Concurrent and Distributed Software Design, Pearson Education Publishers.

William Stallings (2005), Operating Systems, Internals & Design principles, fifth edition, Pearson Hall.

Gary Nutt (2003), Operating Systems, third edition. Addison Wesley.

Kai Hwang and Fye A. Briggs (2006), Computer Architecture & Parallel Processing, McGraw- Hill, Book Company.

MODULE 2: SCHEDULING, THREADS AND SYNCHRONIZATION

Related documents