Threads in Operating System | OS Tutorials YASH PAL, May 21, 2026May 21, 2026 As we discussed, in the context of process management in an operating system, we observe that at any given instant, only one process, either P0 or P1, is in the execution state. Most of the time, the system performs no useful work, reducing its efficiency. When we move from a uniprocessor system to a multiprocessor system, where multiple sets of registers exist and hence more switching is required, then this problem will be increased. For the solution of this problem under such a situation, a new concept called “Threads” is introduced.Table of Contents Threads in the Operating SystemThread – DefinitionMotivation for ThreadsProcesses vs. ThreadsSimilarities between Threads and ProcessesDifferences between Threads and ProcessesWhy Threads?Thread StatesThread OperationsThreading IssuesBenefits of ThreadsThread UsageKernel & User Level ThreadsUser Level ThreadsAdvantages of User-Level ThreadsDisadvantages of User-Level ThreadsOvercome from DisadvantagesKernel Level ThreadsAdvantages of Kernel-level ThreadsDisadvantages of Kernel-level threadsDifference Between User Level and Kernel Level ThreadCombining User and Kernel-Level ThreadsThread Implementation ConsiderationsAdvantages of Threads over Multiple ProcessesDisadvantages of Threads over Multiple ProcessesApplication that Benefits from ThreadsAn application that cannot benefit from ThreadsResources used in Thread Creation and Process CreationWindows XP ThreadsThreads in the Operating SystemThreads represent a software approach to improving the performance of operating systems by reducing the overhead of process switching. A thread has some of the characteristics of a process, but it is possible to have several threads sharing the same memory space. Despite the fact that a thread must execute in a process, the process and its associated threads are different concepts. Processes are used to group resources together, and threads are the entities scheduled for execution on the CPU.Thread – DefinitionA thread is a single sequence stream within a process. Because threads have some of the properties of processes, they are sometimes called lightweight processes (LWP) and are a basic unit of CPU utilisation, and consist of a program counter, a register set and a stack space. It shares with peer threads its code section, data section and operating system resources such as open files and signals, collectively known as a task.A heavyweight (traditional) process is equal to a task with one thread. A task does nothing if no threads are in it, and a thread must be in exactly one task. Threads operate in many respects in the same manner as processes. Threads can be in one of several states:ReadyBlockedRunningTerminatedThe CPU rapidly switches back and forth among the threads, giving the illusion that they are running in parallel.Like a traditional process, i.e., a process with one thread, a thread can be in any of several states (Running, Blocked, Ready or Terminated). Each thread has its own stack.Since a thread will generally call different procedures, and thus have a different execution history. This is why the thread needs its own stack.A Thread has or consists of a program counter (PC), a register set, and a stack space.Threads are not independent of one another like processes; as a result, threads share their code and data sections, OS resources, and other resources, such as open files and signals.Motivation for ThreadsThe use of threads can improve the performance of applications by allowing concurrent (parallel) execution.The treads of a process typically cooperate to solve a problem.The threads of a process can cooperate more efficiently than could separate processes.Processes vs. ThreadsAs we mentioned earlier, in many respects, threads operate in the same way as processes. Some of the similarities and differences are:Similarities between Threads and ProcessesLike processes, threads share the CPU, and only one thread is active (running) at a time.Like processes, threads within a processes, threads within a processes execute sequentially.Like processes, threads can create children.And like a process, if one thread is blocked, another thread can run.Differences between Threads and ProcessesUnlike processes, threads are not independent of one another.Unlike processes, all threads can access every address in the task.Unlike processes, threads are designed to assist one another. Note that processes might or might not assist one another because processes may originate from different users.Why Threads?The following are some reasons why we use threads in designing operating systems.A process with multiple threads makes a great server, for example, a printer server.Because threads can share common data, they do not need to use interprocess communication.Because of the very nature, threads can take advantage of multiprocessors.Threads are cheap in the sense thatThey only need a stack and storage for registers; therefore, threads are cheap to create.Threads use very few resources of the operating system on which they are working. That is, threads do not need a new address space, global data, program code or operating system resources.Context switching is fast when working with threads. The reason is that we only have to save and/or restore PC, SP and registers.But this cheapness does not come free; the biggest drawback is that there is no protection between threads.Thread StatesLife Cycle of a Thread: The life cycle of any thread passes through the different stages, is given in Figure 1.Born State: The thread state in which a new thread begins life.Dead State: The state of a thread after it has been terminated.Sleeping State: A blocked thread state which can be transitioned to ready when notified of a timeout event.Waiting State: A blocked thread state which can be transitioned to ready when notified of an event.The blocked state can effectively be divided into the sleeping state (blocked for a designated time interval), the waiting state (waiting for an asynchronous event), and the blocked state (waiting for an I/O event).Like processes, threads share the CPU, and only one thread at a time is active (running).Threads can create child threads, and can block waiting for system calls to complete; if one thread is blocked, another thread can run.Unlike processes, threads are not independent of one another because all threads can access every address in the task; a thread can read or write over any other thread’s stack.This structure does not protect threads; such protection should not be necessary.Figure 1: Thread Lifecycle (States) DiagramThe traditional approach of a single thread of execution per process, in which the concept of a thread is not recognised, is referred to as a single-threaded approach.Figure 2 shows the two arrangements is the left half are single threaded approaches.Figure 2: Thread and Processes DiagramMS-DOS is an example of an OS that supports a single user process and a single thread.Java Runtime Environment is an example of a system of one process with multiple threads.In a multithreaded environment, a process is defined as the unit of resource allocation and a unit of protection. The following are associated with processes.A virtual address space that holds the process image.Protected access to processors, other processes (for interprocess communication), files, and I/O resources (devices and channels).Within a process, there may be one or more threads, each with the following:A thread execution state (Running, Ready, etc)A saved thread context when not running; one way to view a thread is as an independent program counter operating within a process.An execution stackSome per-thread static storage for Local variables.Access to the memory and resources of its process is shared with all other threads in that process.From the Process Management point of view, the distinction between threads and processes is shown in Figure 3.Figure 3: Single-Threaded and Multithreaded Process Model DiagramIn a single-threaded process model (i.e. there is no distinct concept of thread), the representation of a process includes its process control block and user address space, as well as user and kernel stacks to manage the call/return behaviour of the execution of the process.While the process is running, it controls the processor registers. The content of these registers is saved when the process is not runningIn a multithread environment, there is still a single process control block and user address space associated with the process, but now, there is a separate control block for each thread containing register values, priority, and other thread-related state information.Thread OperationsCreate – thread creation is a subset of process creation, and is faster.Exit (terminate)SuspendResumeSleepAwakenCancel (Cancel signals can be masked.)Join (Thread operation in which the calling thread is blocked until the thread it joins terminates.)Threading IssuesSemantics of fork() and exec() system callsThread cancellationSignal handlingThread poolsThread-specific dataScheduler activationsBenefits of ThreadsThe key benefits of threads derive from the performance implications:It takes far less time to create a new thread in an existing process than to create a new process.Thread creation is ten times faster than process creation. (in UNIX).It takes less time to terminate a thread than a process.It takes less time to switch between two threads within the same process than to switch between processes.Threads enhance efficiency in communication between different executing programs. Threads within the same process share memory and files; they can communicate with each other without invoking the kernel.The thread construct is also useful on a single processor to simplify the structure of a program that is logically doing several different functions.Thread UsageThe use of a thread in a single-user multiprocessing system is as follows:Foreground and Background Work: Consider an example of a spreadsheet program, one thread could display means and read user input, while another thread executes user commands and updates the spreadsheet. This arrangement often increases the speed of the application by allowing the program to prompt for the next command before the previous command is complete.Asynchronous Processing: Asynchronous elements in the program can be implemented as threads.Speed of Execution: A multithreaded process can compute one batch of data while reading the next batch from a device. On a multiprocessor system, multiple threads from the same process may be able to execute simultaneously. Thus, even though one thread may be blocked for an I/O operation to read in a batch of data, another thread may be executing.Modular Program Structure: Programs that involve a variety of activities or a variety of sources and destinations of input and output may be easier to design and implement using threads.Kernel & User Level ThreadsThere are two broad categories of thread implementation.User Level Threads (ULTS)Kernel Level Threads (KLTs) or Kernel-supported Threads.User Level ThreadsUser-level threads are implemented in user-level libraries rather than via system calls, so thread switching does not need to call the operating system and cause an interrupt to the kernel.In fact, the kernel knows nothing about user-level threads and manages them as if they were a single-threaded process.Figure 4 illustrates the pure ULT approach.User-level threads can be used when the operating system does not support threading. The execution of the threads is controlled by the application.Any application can be programmed to be multithreaded by using a thread library, which is a package of routines for ULT management.The thread library contains code for creating and destroying threads, for passing messages and data between threads, for scheduling thread execution, and for saving and restoring thread contexts.User-level threading can be more portable than kernel-level threading, since operating system support is not used.A blocking I/O from any thread of a user-level threaded process will block all threads of the process.Figure 4: Pure user-level thread DiagramAdvantages of User-Level ThreadsThe most obvious advantage of this technique is that a user-level threads package can be implemented on an Operating System that does not support threads. Some other advantages areUser-level threads can run on any operating system. They do not require any modification to the underlying kernel to support ULTs. The threads library is a set of application-level functions shared by all applications.Scheduling can be application-specific.Simple Representation: Each thread is represented simply by a PC, registers, stack and a small control block, all stored in the user process address space.Simple Management: This simply means that creating a thread, switching between threads and synchronisation between threads can all be done without the intervention of the kernel. This saves the overhead of two mode switches (user to kernel; kernel back to user)Fast and Efficient: Thread switching is not much more expensive than a procedure call.Disadvantages of User-Level ThreadsThere is a lack of coordination between threads and the operating system kernel. Therefore, the process as a whole gets one time slice irrespective of whether the process has one thread or 1000 threads within. It is up to each thread to relinquish control to other threads.User-level threads require non-blocking system calls, i.e., a multithreaded kernel. Otherwise, the entire process will be blocked in the kernel, even if there are runnable threads left in the processes. For example, if one thread causes a page fault, the process blocks.In a pure ULT strategy, a multithreaded application cannot take advantage of multiprocessing. A kernel assigns one process to only one processor at a time. Therefore, only a single thread within a process can execute at a time.Overcome from DisadvantagesProblems can be overcome by writing an application as multiple processes rather than multiple threads.But, this approach eliminates the advantages of threads, i.e. each switch becomes a process switch rather than a thread switch, which results in greater overhead.Jacketizing – To overcome the problem of blocking threads, a technology referred to as jacketizing. The purpose of jacketizing is to convert a blocking system call into a nonblocking system call.Kernel Level ThreadsIn this method, the kernel knows about and manages the threads. No runtime system is needed in this case. Instead of a thread table in each process, the kernel has a thread table that keeps track of all threads in the system.In addition, the kernel also maintains the traditional process table to keep track of processes. The operating system kernel provides system calls to create and manage threads.Windows is an example of this approach. Kernel-level threading requires operating system support.A blocking I/O from a thread of a kernel-level threaded process will block only that thread.Figure 5 depicts the pure KLT approach.Figure 5: Pure kernel-level thread DiagramThe Kernel maintains context information for the process as a whole and for individual threads within the process.Scheduling by the kernel is done on a thread basis.This approach overcomes the drawback of the ULT approach.Advantages of Kernel-level ThreadsBecause the kernel has full knowledge of all threads, the scheduler may decide to give more time to a process having a large number of threads than to a process having a small number of threads.Kernel-level threads are especially good for applications that frequently block.Another advantage of the KLT approach is that kernel routines themselves can be multithreaded.Disadvantages of Kernel-level threadsThe principal disadvantage of the KLT approach compared to the ULT approach is that the transfer of control from one thread to another within the same process requires a mode switch to the kernel.The kernel-level threads are slow and inefficient. For instance, thread operations are hundreds of times slower than those of user-level threads.Since the kernel must manage and schedule threads as well as processes. It requires a full thread control block (TCB) for each thread to maintain information about threads. As a result, there is significant overhead and an increase in kernel complexity.Difference Between User Level and Kernel Level ThreadUser Level ThreadKernel Level Thread1User-level threads are faster to create and manageKernel-level threads are slower to create and manage2Implemented by a thread library at the user levelAn operating system supports the kernel thread directly3A user-level thread can run on any operating systemKernel-level threads are specific to an operating system4Support provided at the user level is called a user-level threadSupport may be provided by the kernel, which is called kernel-level threads5Multithread application cannot take advantage of multiprocessingKernel routers themselves can be multithreadedTable for the difference between User-level and Kernel-level ThreadsCombining User and Kernel-Level ThreadsSome operating systems provide a combined ULT/KLT facility as shown in Figure 6.Figure 6: Combined User & Kernel Level Threads DiagramIn a combined system, thread creation is done completely in user space. The multiple ULTs from a single application are mapped onto some (smaller or equal) number of KLTs.The programmer may adjust the number of KLTs for a particular application and processor to achieve the best overall results.In a combined approach, multiple threads within the same application can run in parallel on multiple processors, and a blocking system call need not block the entire process.The approach should combine the advantages of the pure ULT and KLT approaches and minimise the disadvantages.Solaris is a good example of an OS using a combined approach.Thread Implementation ConsiderationsThread Signal DeliverySynchronous signal: a signal generated due to the execution of the currently running thread’s execution.Asynchronous signal: signal generated for reasons unrelated to the current instruction of the running thread.Signal mask: A data structure which blocks specified signals from being delivered to a thread.Pending signal: A signal which has not yet been delivered. A signal can be pending because the recipient thread is not running, or has masked the signal.Signal handler: Code that is executed in response to a particular signal type.Thread Termination: Thread cancellation should be masked while critical sections are being executed.Threads differ from traditional multitasking operating system processes in that:processes are typically independent, while threads exist as subsets of a process.processes carry considerable state information, whereas multiple threads within a process share state as well as memory and other resources.processes have separate address spaces, whereas threads share their address space.processes interact only through system-provided inter-process communication mechanisms.Context switching between threads in the same process is typically faster than context switching between processes.Advantages of Threads over Multiple ProcessesContext Switching: Threads are very inexpensive to create and destroy, and they are inexpensive to represent. For example, they require space to store the PC, the SP, and the general-purpose registers, but they do not require space to share memory information, Information about open files of I/O devices in use, etc. With so little context, it is much faster to switch between threads. In other words, it is relatively easier to make a context switch using threads.Sharing: Treads allow the sharing of a lot of resources that cannot be shared in a process, for example, sharing code sections, data sections, Operating System resources like open files, etc.Disadvantages of Threads over Multiple ProcessesBlocking: The major disadvantage is that if the kernel is single-threaded, a system call of one thread will block the whole process, and the CPU may be idle during the blocking period.Security: Since there is extensive sharing among threads, there is a potential problem of security. One thread may overwrite the stack of another thread (or damage shared data), although it is very unlikely since threads are meant to cooperate on a single task.Application that Benefits from ThreadsA proxy server satisfying the requests for several computers on a LAN would benefit from a multi-threaded process.In general, any program that has to do more than one task at a time could benefit from multitasking.For example, a program that reads input, processes it, and outputs could have three threads, one for each task.An application that cannot benefit from ThreadsAny sequential process that cannot be divided into parallel tasks will not benefit from threads, as they would block until the previous one completes. For example, a program that displays the time of the day would not benefit from multiple threads.Resources used in Thread Creation and Process CreationWhen a new thread is created, it shares its code section, data section and operating system resources like open files with other threads. But it is allocated its own stack, register set and a program counter.The creation of a new process differs from that of a thread mainly in the fact that all the shared resources of a thread are needed explicitly for each process.So though two processes may be running the same piece of code, they need to have their own copy of the code in the main memory to be able to run.Two processes also do not share other resources. This makes the creation of a new process very costly compared to that of a new thread.Figure 7: Thread Creation DiagramWindows XP ThreadsEach thread containsA thread IDRegister setSeparate user and kernel stacksPrivate data storage areaThe register set, stacks, and private storage area are known as the context of the threads. The primary data structures of a thread include:ETHREAD (executive thread block)KTHREAD (kernel thread block) engineering subjects Operating System Operating System