Day 74 - Threads

A thread is a basic unit of CPU utilization. It is also called a lightweight process. It behaves like a process in a process and does not have its own process control block. Usually multiple threads are created with a single process. If a process has multiple threads it can perform multiple tasks at the same time.

A thread consists of the following.

  • Thread ID
  • Program Counter
  • Register Set
  • Stack

A thread share the following with its peer threads in a particular task:

  • Code Section
  • Data Section
  • Operating System source available to the task, such as open files and signals.

Single vs Multi-Thread Process

Modern computers have multiple process all running multiple threads to do more work at the same time. For example a browser might be running a single thread for displaying images and other thread to render html pages or a word processing program can have a thread for spellchecking and other thread for displaying graphics.

Another more common example of using multi-threads process is in web-servers. A web-server may be serving many requests at the same time and if it were to be a single-thread process it would only serve one user at a time. Instead a server creates a thread for every reuqest it get from a client and serves the client with the requested data like HTML pages or images etc.

Benefits Of Multi-threading Processes

There are four major benefits of Multi-Threadinf Processes.

  1. Responsiveness. A program's responsiveness to the user can be increased my multithreading the process. This will cause the program to run some of its parts even if one or two are been blocked by something.
  2. Resource Sharing. Processes can share memory through techniques like shared memory and message passing. and these techniques must be explicitly arranged by the programmer.
  3. Economy. Multithreading is more economical because the threads can share the resources of the same process to which they belong.
  4. Scalability. The benefits of multithreading can be even greater in a multiprocessor architecture, where threads may be running in parallel on different processing cores.

zainscizainsci