Question-1 What is thread? Explain thread structure. Explain different types of thread. OR Explain thread in brief.
- A program has one or more locus of execution. Each execution is called a thread of execution.
- In traditional operating systems, each process has an address space and a single thread of execution.
- It is the smallest unit of processing that can be scheduled by an operating system.
- A thread is a single sequence stream within in a process. Because threads have some of the properties of processes, they are sometimes called lightweight processes. In a process, threads allow multiple executions of streams.
- Process is used to group resources together and threads are the entities scheduled for execution on the CPU.
- The thread has a program counter that keeps track of which instruction to execute next.
- It has registers, which holds its current working variables.
- It has a stack, which contains the execution history, with one frame for each procedure called but not yet returned from.
- Although a thread must execute in some process, the thread and its process are different concepts and can be treated separately.
- What threads add to the process model is to allow multiple executions to take place in the same process environment, to a large degree independent of one another.
- Having multiple threads running in parallel in one process is similar to having multiple processes running in parallel in one computer.
In former case, the threads share an address space, open files, and other resources.
- In the latter case, process share physical memory, disks, printers and other resources.
- In Fig. above (a) we see three traditional processes. Each process has its own address space and a single thread of control.
- In contrast, in Fig. above (b) we see a single process with three threads of control.
- Although in both cases we have three threads, in Fig. above (a) each of them operates in a different address space, whereas in Fig. above (b) all three of them share the same address space.
- Like a traditional process (i.e., a process with only one thread), a thread can be in any one of several states: running, blocked, ready, or terminated.
- When multithreading is present, processes normally start with a single thread present.This thread has the ability to create new threads by calling a library procedure thread_create.
- When a thread has finished its work, it can exit by calling a library procedure thread_exit.
- One thread can wait for a (specific) thread to exit by calling a procedure thread_join.
This procedure blocks the calling thread until a (specific) thread has exited.
Another common thread call is thread_yield, which allows a thread to voluntarily give up the CPU to let another thread run.