FOR FREE MATERIALS

Thread

 

A thread is the smallest sequence stream of an executable program and it is basically a component of a process that is the reason a thread is called light weight process and one process may contain multiple threads. Generally, a process can perform a single thread of execution at a time. 

 

For example, when a process is running a word-processor program, a single thread of instructions is being executed. This single thread of control allows the process to perform only one task at a time. The user cannot simultaneously type in characters and run the spell checker within the same process. But in most modern operating systems a process can allow to have multiple threads. 

 

For multicore systems, this feature is beneficial because multiple threads can run in parallel. On a system that supports threads, the PCB is expanded to include information for each thread. Each thread is signified simply by a program counter, registers, stack, and a small control block and all stored in the user process address space.

 

Fig: 10 Thread

 

Multi-Threading:

Multi-threading system allows a single process to have multiple threads run simultaneously within the framework of that process i.e. more than one thread in a process can run concurrently. Multiple threads can run simultaneously by a single CPU using time-sharing or run in parallel by a multiprocessing system (more than one CPUs). 

 

Unlike single-threaded, the multi-threaded systems need some synchronization when two or more threads try to access and change shared resources (race conditions). In that Operating System handle it by mutexes and semaphores. 

 

Advantages of Threads 

1) A process with more than one thread can make a great server like a printer server.

 

2) Context switching is fast because, at the time of context switching very few things like PC, SP and registers are going to save.

 

3) Threads do not need to use interprocess communication.

 

4) In a multithreading system, threads can take advantage of a multiprocessing system.

 

5) In a multithreading system, the execution of the process becomes fast. 

 

There are two main ways to implement a threads package either at user space or by kernel level.

 

i) User Space (User level): User threads are supported above the kernel and managed without kernel support.

 

ii) Kernel Space (Kernel level): Kernel threads are supported and managed directly the Operating system. 

 

Generally all modern operating systems like Mac OS X, Windows, Linux, and Solaris support kernel-level thread. 

 

Thread Libraries:

A thread library provides the programmer with an API for creating and managing threads. Either system provides the entire library in user space or kernel space. Nowadays Three main libraries are using; Windows, POSIX Pthread, and Java. Windows thread library is a kernel-level library in the windows system.

 

#Pthreads  

#Difference between Multi-thread, Mult-tasking, Multi-Programming, Multi-Processing