The Wayback Machine - https://web.archive.org/web/20240819211915/https://www.geeksforgeeks.org/threading-issues/
Open In App

Threading Issues

Last Updated : 27 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Multitasking is one of the key methods, which can be applied to improve the performance of operations. In a multithreading environment, there are many threading-related problems. We shall discuss in this article threading problems associated with system calls, cancellation of threads, signal handling, thread pools, and thread-specific data.

In addition to threading problems, we shall discuss ways in which they can be handled for continued use of benefits associated with a multithreaded programming environment.

Thread in an Operating System

The thread refers to one string that offers the functionality of cutting a program into multiple jobs performing concurrently parallelly. A thread refers to the smallest unit of computation and comprises a program counter, a register set, and a stack space.

Within a process, a single sequence stream. It is part of a process that executes independently of other processes. Light-weight process. This is used in achieving parallelism by dividing a process’s tasks that are independent paths of execution.

For instance, multiple tabs, a browser, text editor (Spell checking, and formatting of text occur simultaneously with the type and saving of the text by various threads).

Light-Weight-Process-(LWP)

Light-Weight Process (LWP)

What is Multithreading in OS?

Threading refers to a way in which an application traverses whenever running. Multi-Threading makes it possible for any program or operating system process to conduct two or more threads running concurrently. For each user who calls up for a specific program or system service, the program assumes a distinct identification in the form of a thread.

It is also referred to Lightweight Process, a method used in enhancing app speed. It enhances any operation system by minimizing bloated-up threads on its performance. Each thread is associated with a process and there are no threads that do not have processes.

These are also employed for running network servers as well as Web servers. In the multiprocessors this shared memory serves as a good base for execution of any program in a parallel way. Threading is being used extensively nowadays even when transacting and surfing the internet.

The threading section threads codes in very small light pieces so that the CPU loads are distributed equally to allow quick completion of any function. Every thread has its own separate resources by means of which we can perform any process on its own, or increase the amount of threads to make multiple processes work in parallel.

What is Race Condition?

Race condition refers to an OS problem where two or more processes or threads execute concurrently. Their order of execution determines whether they will be killed. In case of race, the precise moments at which events occur cannot be specified, and the product of execution can change according to time. This can lead to some undesirable or erroneous operation of such a system.

What is Process Synchronization?

This ensures that such a synchronization between the installed operating system is aligned with the processor present on the computer system. This implies that, if the next stage of processing requires printing; then the processor will send a command to the operating system and subsequently waits for a response that either prints or fails to print.

The operating system detects it as soon as it is printed and the reply is communicated to the processor which implies that synchronization of these parties are required.

What is Mutex (Mutual Exclusion)?

The term “mutual exclusion” (or MUTEX), which stems from the property of “critical section” refers to a situation where two threads/processes wish to access shared resources simultaneously. However, this issue comes up when a shared resource is unsecured and two or more threads or processes try to use it at once resulting in race conditions, data inconsistencies, and so on.

What is Deadlock?

Two computer processes have access to single resource and neither process can access to this resource. so, deadlock occurs. Here we mean by saying deadlock, a situation wherein two processes crave for the same resource and each of them cannot get access to that very resource leading to deadlock in the existing system.

Threading Issues in OS

  • System Call
  • Thread Cancellation
  • Signal Handling
  • Thread Pool
  • Thread Specific Data
Threading-Issues

Threading Issues

fork() and exec() System Calls

They are the system calls fork() and exec(). Fork() function gives rise to an identical copy of process which initiated fork call. The new duplication process is referred to as a child process, while the invoker process is identified by fork(). The instruction after fork continues the execution with both the parent process and the child process.

Discussing fork() system call, therefore. Let us assume that one of the threads belonging to a multi-threaded program has instigated a fork() call. Therefore, the new process is a duplication of fork(). Here, the question is as follows; will the new duplicate process made by fork() be multi-threaded like all threads of the old process or it will be a unique thread?

Now, certain UNIX systems have two variants of fork(). fork can either duplicate all threads of the parent process to a child process or just those that were invoked by the parent process. The application will determine which version of fork() to use.

When the next system call, namely exec() system call is issued, it replaces the whole programming with all its threads by the program specified in the exec() system call’s parameters. Ordinarily, the exec() system call goes into queue after the fork() system call.

However, this implies that the exec() system call should not be queued immediately after the fork() system call because duplicating all the threads of the parent process into the child process will be superfluous. Since the exec() system call will overwrite the whole process with the one given in the arguments passed to exec().

This means that in cases like this; a fork() which only replicates one invoking thread will do.

Thread Cancellation

The process of prematurely aborting an active thread during its run is called ‘thread cancellation’. So, let’s take a look at an example to make sense of it. Suppose, there is a multithreaded program whose several threads have been given the right to scan a database for some information. The other threads however will get canceled once one of the threads happens to return with the necessary results.

The target thread is now the thread that we want to cancel. Thread cancellation can be done in two ways:

  • Asynchronous Cancellation: The asynchronous cancellation involves only one thread that cancels the target thread immediately.
  • Deferred Cancellation: In the case of deferred cancellation, the target thread checks itself repeatedly until it is able to cancel itself voluntarily or decide otherwise.

The issue related to the target thread is listed below:

How is it managed when resources are assigned to a canceled target thread?

Suppose the target thread exits when updating the information that is being shared with other threads.

However, in here, asynchronous threads cancellation whereas thread cancels its target thread irrespective of whether it owns any resource is a problem.

On the other hand, the target thread receives this message first and then checks its flag to see if it should cancel itself now or later. They are called the Cancellation Points of threads under which thread cancellation occurs safely.

Signal Handling

Signal is easily directed at the process in single threaded applications. However, in relation to multithreaded programs, the question is which thread of a program the signal should be sent.

Suppose the signal will be delivered to:

  • Every line of this process.
  • Some special thread of a process.
  • thread to which it applies

Alternatively, you could give one thread the job of receiving all signals.

So, the way in which the signal shall be passed to the thread depends on how the signal has been created. The generated signals can be classified into two types: synchronous signal and asynchronous signal.

At this stage, the synchronous signals are routed just like any other signal was generated. Since these signals are triggered by events outside of the running process, they are received by the running process in an asynchronous manner, referred to as asynchronous signals.

Therefore, if the signal is synchronous, it will be sent to a thread that generated such a signal. The asynchronous signal cannot be determined into which thread in a multithreaded program delivery it should go. The asynchronous signal that is telling a process to stop, will result in all threads of the process receiving the signal.

Many UNIX UNITS have addressed, to some extent, the problem of asynchronous signals. Here, the thread is given an opportunity to identify the relevant or valid signals and those that it does not support. Windows OS on the other hand, has no idea about signals but does use ACP as equivalent for asynchronous signals adopted in Unix platforms.

In contrast with UNIX where a thread specifies that it can or cannot receive a thread, all control process instances (ACP) are sent to a particular thread.

Thread Pool

The server develops an independent thread every time an individual attempts to access a page on it. However, the server also has certain challenges. Bear in mind that no limit in the number of active threads in the system will lead to exhaustion of the available system resources because we will create a new thread for each request.

The establishment of a fresh thread is another thing that worries us. The creation of a new thread should not take more than the amount of time used up by the thread in dealing with the request and quitting after because this will be wasted CPU resources.

Hence, thread pool could be the remedy for this challenge. The notion is that as many fewer threads as possible are established during the beginning of the process. A group of threads that forms this collection of threads is referred as a thread pool. There are always threads that stay on the thread pool waiting for an assigned request to service.

Thread-Pool

Thread Pool

A new thread is spawned from the pool every time an incoming request reaches the server, which then takes care of the said request. Having performed its duty, it goes back to the pool and awaits its second order.

Whenever the server receives the request, and fails to identify a specific thread at the ready thread pool, it may only have to wait until some of the threads are available at the ready thread pool. It is better than starting a new thread whenever a request arises because this system works well with machines that cannot support multiple threads at once.

Thread Specific Data

Of course, we all know that a thread belongs to data of one and the same process, right?. The challenge here will be when every thread in that process must have its own copy of the same data. Consequently, any data uniquely related to a particular thread is referred to as thread-specific data.

For example, a transaction processing system can process a transaction in individual threads for each one.I Each transaction we perform shall be assigned with a special identifier which in turn, shall uniquely identify that particular transaction to us. The system would then be in a position to distinguish every transaction among others.

Because we render each transaction separately on a thread. In this way, thread-specific datas will allow associating every thread with a definite transaction and some transaction ID. For example, libraries that support threads, namely Win32, Pthreads and Java, provide for thread-specific data (TSD).

Hence, these are the threading problems that arise in multithreaded programming environments. Additionally, we examine possible ways of addressing these concerns.

As a result, multithreading may be regarded as an integral operation within computer programming enhancing task concurrency and improved system performance. however, it is also associated with some problems and threading issues. There were numerous threading concerns such as system calls, thread cancellation, threads’ pools and thread specific date.

FAQs on Threading Issues

1. What is a race condition in multithreading programming?

A race condition refers to an issue that arises in an OS when two or more processes or threads execute concurrently.

2. what is the use of thread pool in multi-threading?

Thread pool is an effective management technique that prevents thread exhaustion from overuse of resources, thereby reducing the outflow of new threads in the system.

3. What’s there with thread-specific data and importance of using it in multithreading?

Thread specific data allows each thread to possess a separate copy of data for the purpose of ensuring isolated data and independent thread.



Similar Reads

Difference between Multi-tasking and Multi-threading
Introduction : Multi-tasking and multi-threading are two techniques used in operating systems to manage multiple processes and tasks. Multi-tasking is the ability of an operating system to run multiple processes or tasks concurrently, sharing the same processor and other resources. In multi-tasking, the operating system divides the CPU time between
4 min read
Multi Threading Models in Process Management
Multi threading- It is a process of multiple threads executes at same time. There are two main threading models in process management: user-level threads and kernel-level threads. User-level threads: In this model, the operating system does not directly support threads. Instead, threads are managed by a user-level thread library, which is part of t
6 min read
What is Hyper-Threading?
In history, there was only a single CPU system. Later on, the system improves and the CPU contains the cores. Like dual-core CPU, 4-Core CPU, 8-Core CPU, and so on. Core means these are logical CPUs. To again improve the system, Hyper-Threading technology is used. In Hyper-Threading one core contains 1 or more threads and those threads again behave
3 min read
Design Issues for Paging
Paging is an important concept in memory management. In Paging, the operating system divides each incoming program into pages of equal sizes of blocks. The section of disks is called blocks. The section of main memory is called page frames. Fixed-size blocks are called frames and the breaking of logical memory into blocks of the same size is called
7 min read
Operating System Tutorial
An Operating System(OS) is software that manages and handles the hardware and software resources of a computer system. It provides interaction between users of computers and computer hardware. An operating system is responsible for managing and controlling all the activities and sharing of computer resources. An operating system is a low-level Soft
6 min read
Operating System Services
An operating system is software that acts as an intermediary between the user and computer hardware. It is a program with the help of which we are able to run various applications. It is the one program that is running all the time. Every computer must have an operating system to smoothly execute other programs. The OS coordinates the use of the ha
6 min read
Memory Management in Operating System
The term memory can be defined as a collection of data in a specific format. It is used to store instructions and process data. The memory comprises a large array or group of words or bytes, each with its own location. The primary purpose of a computer system is to execute programs. These programs, along with the information they access, should be
14 min read
What is an Operating System?
Operating System lies in the category of system software. It basically manages all the resources of the computer. An operating system acts as an interface between the software and different parts of the computer or the computer hardware. The operating system is designed in such a way that it can manage the overall resources and operations of the co
9 min read
Kernel in Operating System
A kernel is a central component of an operating system that manages the operations of computers and hardware. It basically manages operations of memory and CPU time. It is a core component of an operating system. Kernel acts as a bridge between applications and data processing performed at the hardware level using inter-process communication and sy
10 min read
Operating Systems Structures
The operating system can be implemented with the help of various structures. The structure of the OS depends mainly on how the various standard components of the operating system are interconnected and melded into the kernel. A design known as an operating system enables user application programs to communicate with the machine's hardware. Given it
8 min read
Real Time Operating System (RTOS)
Real-time operating systems (RTOS) are used in environments where a large number of events, mostly external to the computer system, must be accepted and processed in a short time or within certain deadlines. such applications are industrial control, telephone switching equipment, flight control, and real-time simulations. With an RTOS, the processi
6 min read
Functions of Operating System
An Operating System acts as a communication bridge (interface) between the user and computer hardware. The purpose of an operating system is to provide a platform on which a user can execute programs conveniently and efficiently. An operating system is a piece of software that manages the allocation of Computer Hardware. The coordination of the har
12 min read
States of a Process in Operating Systems
In an operating system, a process is a program that is being executed. During its execution, a process goes through different states. Understanding these states helps us see how the operating system manages processes, ensuring that the computer runs efficiently. There must be a minimum of five states. Even though the process could be in one of thes
9 min read
Types of Operating Systems
An Operating System performs all the basic tasks like managing files, processes, and memory. Thus operating system acts as the manager of all the resources, i.e. resource manager. Thus, the operating system becomes an interface between the user and the machine. It is one of the most required software that is present in the device.  Operating System
10 min read
Introduction of Operating System - Set 1
An operating system acts as an intermediary between the user of a computer and computer hardware. In short its an interface between computer hardware and user. The purpose of an operating system is to provide an environment in which a user can execute programs conveniently and efficiently.  An operating system is software that manages computer hard
14 min read
Introduction of System Call
A system call is a programmatic way in which a computer program requests a service from the kernel of the operating system it is executed on. A system call is a way for programs to interact with the operating system. A computer program makes a system call when it requests the operating system's kernel. System call provides the services of the opera
11 min read
Process Table and Process Control Block (PCB)
While creating a process, the operating system performs several operations. To identify the processes, it assigns a process identification number (PID) to each process. As the operating system supports multi-programming, it needs to keep track of all the processes. For this task, the process control block (PCB) is used to track the process’s execut
7 min read
Semaphores in Process Synchronization
Semaphores are a tool used in computer science to help manage how different processes (or programs) share resources, like memory or data, without causing conflicts. A semaphore is a special kind of synchronization data that can be used only through specific synchronization primitives. Semaphores are used to implement critical sections, which are re
15+ min read
Introduction to Linux Shell and Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understanding shell scripting we have to get familiar with th
7 min read
Virtual Memory in Operating System
Virtual Memory is a storage allocation scheme in which secondary memory can be addressed as though it were part of the main memory. The addresses a program may use to reference memory are distinguished from the addresses the memory system uses to identify physical storage sites and program-generated addresses are translated automatically to the cor
15 min read
Inter Process Communication (IPC)
A process can be of two types: Independent process.Co-operating process. An independent process is not affected by the execution of other processes while a co-operating process can be affected by other executing processes. Though one can think that those processes, which are running independently, will execute very efficiently, in reality, there ar
14 min read
Last Minute Notes – Operating Systems
See Last Minute Notes for all subjects all subjects here. Operating Systems: It is the interface between the user and the computer hardware. Types of Operating System (OS): Batch OS - A set of similar jobs are stored in the main memory for execution. A job gets assigned to the CPU, only when the execution of the previous job completes.Multiprogramm
15 min read
Paging in Operating System
Paging is a memory management scheme that eliminates the need for a contiguous allocation of physical memory. The process of retrieving processes in the form of pages from the secondary storage into the main memory is known as paging. The basic purpose of paging is to separate each procedure into pages. Additionally, frames will be used to split th
10 min read
Thread in Operating System
A thread is a single sequence stream within a process. Threads are also called lightweight processes as they possess some of the properties of processes. Each thread belongs to exactly one process. In an operating system that supports multithreading, the process can consist of many threads. But threads can be effective only if the CPU is more than
6 min read
Introduction of Deadlock in Operating System
A deadlock is a situation where a set of processes is blocked because each process is holding a resource and waiting for another resource acquired by some other process. In this article, we will discuss deadlock, its necessary conditions, etc. in detail. What is Deadlock?Deadlock is a situation in computing where two or more processes are unable to
12 min read
CPU Scheduling in Operating Systems
Scheduling of processes/work is done to finish the work on time. CPU Scheduling is a process that allows one process to use the CPU while another process is delayed (in standby) due to unavailability of any resources such as I / O etc, thus making full use of the CPU. The purpose of CPU Scheduling is to make the system more efficient, faster, and f
15+ min read
Introduction of Process Management
A process is a program in execution. For example, when we write a program in C or C++ and compile it, the compiler creates binary code. The original code and binary code are both programs. When we actually run the binary code, it becomes a process. A process is an 'active' entity instead of a program, which is considered a 'passive' entity. A singl
10 min read
Process Schedulers in Operating System
In computing, a process is the instance of a computer program that is being executed by one or many threads. Scheduling is important in many different computer environments. One of the most important areas of scheduling is which programs will work on the CPU. This task is handled by the Operating System (OS) of the computer and there are many diffe
7 min read
Operating System Interview Questions
Operating System (OS) is Software that facilitates computer software to communicate and operate computer hardware with the computer software. An operating system acts as a GUI between the User and the computer System. In Other words, an OS acts as an intermediary between the user and the computer hardware, managing resources such as memory, process
15+ min read
Complete Tutorial on LRU Cache with Implementations
What is LRU Cache?Cache replacement algorithms are efficiently designed to replace the cache when the space is full. The Least Recently Used (LRU) is one of those algorithms. As the name suggests when the cache memory is full, LRU picks the data that is least recently used and removes it in order to make space for the new data. The priority of the
15+ min read
Article Tags :
three90RightbarBannerImg