An Introduction to threads

15
An Introduction to threads Presented by: Zahra sadeghi

Transcript of An Introduction to threads

An Introduction to

threads

Presented by:

Zahra sadeghi

zahra sadeghi 2

history

First: in late 60’s

early 80’s : Unix community

Windows 95 and OS/2

Today: all operating systems have multithreaded capability

zahra sadeghi 3

Terminology

Thread:

single sequence of execution

independent path of execution

Multiprogramming: Running multiple processes

concurrently

Multithreading: Running multiple threads within one

process concurrently

i.e: having several code executions within a single process

zahra sadeghi 4

Process and thread

A process is the "heaviest" unit of kernel scheduling.

process has its own memory area and data.

A thread is the "lightest" unit of kernel scheduling.

the thread shares memory and data with the other threads.

A process/program,consists of many threads. (At

least one thread exists within each process.)

zahra sadeghi 5

Are threads actually needed!!!

We are able to do the normal work with the computer (like editing/saving a file) or drawing a graphic and listening to music etc without getting bothered with the print job.

separate threads are executing all these tasks.

the database or a web server interacts with a number of users simultaneously.

they maintain a separate thread for each user and hence can maintain the state of all the users.

If the program is run as one sequence then it is possible that failure in some part of the program will disrupt the functioning of the entire program.

threads can execute independent of others

zahra sadeghi 6

drawbacks

Is a Costly concept

Many threads : less processor time

memory : storing the context information of each thread.

Good designed program

zahra sadeghi 7

Multi_threading

Each thread has its own run-time stack and

CPU state

Two different threads can act on the same object and same static fields concurrently

zahra sadeghi 8

example

zahra sadeghi 9

A thread control problem

multi-processor system:

the operating system can allocate individual threads to the separate processors, which thus fastens the execution of the program.

the distribution of the threads on several processors is faster than sharing time-slices on a single processor.

zahra sadeghi 10

Race Condition

if shared data is accessed in an unsynchronized manner

Two threads are simultaneously reading or modifying some shared data

outcome :the order in which the program's threads are allocated CPU time

zahra sadeghi 11

On a single processor system,

threads can be run either in a preemptive mode or in a cooperative mode.

threads

preemptive cooperative

zahra sadeghi 12

preemptive mode

the operating system distributes the processor time between the threads

interrupts the threads at regular intervals

The processor time given to each thread is so small

The switching between threads is so fast that it appears as if all the threads are running simultaneously.

zahra sadeghi 13

Cooperative mode

each thread can control the CPU for as long as it needs it.

one thread can starve all the others

Windows 3.x uses this kind of implementation.

A, B and C can be

considered as three

threads and are context

switched at the points

indicated by the small

circles.

zahra sadeghi 14

Implemntation of cooperative mode

jmp_buf is defined in the ANSI C header file setjmp.h.

setjmp() : to save the execution environment before switching out

longjmp() : to switch to a place recorded in a particular jump buffer.

With this technique, we can implement a non-preemptive threaded system with minimal capability in about 100 lines!

Note that a separate stack is required for each executing function.

zahra sadeghi 15

Links & White Papers on Threads

http://jamesthornton.com/linux/FAQ/Threads-

FAQ/ThreadLibs.html

http://linuxdevices.com/articles/AT6753699732.html

http://www.gridbus.org/~raj/asc98.html