1. Gyanastra
  2. Operating Systems
  3. Threads
  4. Introduction to Threads and Task Concept
Free lesson, no account needed 3 min read 5 practice questions after it
Asked in past papers3 times
  • GATE CSE1999
  • ISRO2017
  • UGC NET2016

Operating Systems · Threads

Introduction to Threads and Task Concept

Overview

Running the same program as two separate processes stores the same code and the same data twice in memory, which is unnecessary.
Threads solve this duplication by letting one unit of work share code and data, and when threads are used the unit is called a task rather than a process.

Key concepts

  • Process-per-application waste: when the same program runs as two processes, the same code and the same data are stored twice in memory.
  • Threads: the mechanism that solves the problem of duplicated code and data.
  • Task: a certain amount of work that we want to achieve, carried out using multiple threads. When talking about threads, the word process is replaced by the word task.
  • Initial thread: when a task is started, it has only one thread; more threads are created for the work.

How it works

  1. Identify the problem: running the same code and data as separate processes keeps two copies of the same code and same data in memory, so memory is unnecessarily wasted.
  2. Replace the heavy unit (process) with a lighter unit (thread) so the shared code and data need not be duplicated.
  3. Change terminology: instead of saying "process", say "task" — the unit of work being executed by threads.
  4. A task is defined by the work to be accomplished (for example, serving clients), and this work is carried out by multiple threads.
  5. When a task is started, it has only one thread (the initial/main thread).
  6. Example: a web server task. The work of the task (serving clients) is divided among multiple threads rather than multiple processes.

Worked examples

Web server task: the server task is the work to be achieved; it starts with exactly one thread, and the servicing of clients is divided among multiple threads instead of multiple processes.

Revision summary

  • Same code + same data stored twice (once per process) = wasted memory; threads are the solution.
  • For threads, the term used is task, not process.
  • A task is a certain amount of work that we want to achieve using multiple threads.
  • A task begins with exactly one thread; more threads are created for the work.
  • The web server is the running example: the server task is the work, threads do the servicing.

Previous-year exam insights

  • A thread is the basic unit of CPU utilisation; it comprises a thread ID, program counter, register set and stack, and shares with other threads of the same process the code section, data section and OS resources (open files, signals).
    Asked inGATE 1999
  • Threads of the same process share the virtual address space (memory), while each thread has its own registers, stack and program counter; a thread is scheduled on the CPU, whereas the address space is backed by memory.
    Asked inGATE 1999
  • Thread scheduling is the scheduling of threads (kernel-level entities) rather than processes; gang scheduling schedules a group of related threads to run simultaneously on different processors; fair-share scheduling divides CPU time fairly among users/processes; rate-monotonic scheduling is a static-priority real-time scheduling algorithm.
    Asked inISRO 2017
  • Multithreading lets a program perform several activities concurrently/asynchronously (e.g., Java threads allow the environment to be asynchronous), improving responsiveness and resource sharing.
    Asked inUGC NET 2016
  • Benefits of threads over multiple processes: less memory (no duplication of code/data), cheaper creation and context switch, and easy sharing of data; drawbacks: no protection between threads of a process, so one thread's error can affect all, and synchronisation is needed.
  • User-level threads are managed by a library without kernel knowledge (fast, but a blocking call blocks the whole process); kernel-level threads are managed by the OS (independent scheduling, but costlier); models include many-to-one, one-to-one and many-to-many.
Pages 1–2 of 2