>

Monday, 14 December 2015

Threading

Threading:
A thread is an independent stream of instructions that define a separate path of execution by the operating system. 
Every program starts with one thread: main()
As a programmer, it may help to think of a thread in the following ways:
  • As a routine that runs independently of main()
  • As one of a number of routines, each of which runs at the same time
  • Like a routine that immediately starts running when called while main() continues   
  •        to run    in the background concurrently
  • As a routine that is run at the discretion of the OS
Threads characteristics:
Threads use and exist within processes and have the following characteristics:
  • Threads can be scheduled and run by the operating system as independent entities.
  • Threads duplicate the bare essential resources to enable them to exist as executable code.
  • Each thread has the ability to write to the same point in memory as other threads running. 
  •        The programmer must make sure that threads are properly synchronized in their memory            accesses.
Each thread maintains the following information separately from all other threads.
Stack pointers
Registers
Scheduling properties
A set of pending and blocked signals
Thread-specific data
All other information is shared between threads, including the following:
Pointers with the same value point to the same data.
Reading/writing to the same memory location is allowed, but may require explicit synchronization by the programmer.
If a thread closes a file, it is closed for all threads.

Standard UNIX process versus multithread process:

This stack represents a standard UNIX® process. Within the User Address Space are the stack, text, data, and the heap. Within the stack, the Stack Pointer Program and Counter Registers are shared between the routines and variables. Global data structures and the heap are shared.

This graphic represents a multithread process. The process id is the same for each thread. Each thread gets a new stack pointer, new program counter, and a new register. Just one heap and main memory, one pid, and one set of file pointers remain.


0 comments:

Post a comment