Digital Principles and Computer Organization: Unit II (a): Synchronous Sequential Logic

Threading Issues

Process Management - Introduction to Operating Systems

Some other issue related to the thread is discussed in this section. The issue includes fork and exec system call, thread cancellation, signal handling, thread pool etc.

Threading Issues

Some other issue related to the thread is discussed in this section. The issue includes fork and exec system call, thread cancellation, signal handling, thread pool etc.

The fork ( ) and exec () system call

• In a multithreaded programming environment, fork and exec system calls are changed. UNIX operating system uses two versions of fork system calls.

1. Fork call duplicates all threads.

2. In second option, only thread created by fork duplicates.

• Based on the application, system uses fork system calls. Sometimes, duplicating all the threads are unnecessary. If we call exec immediately after fork then there is no use of duplicating threads.

• Forking provides a way for an existing process to start a new one, but what about the case where the new process is not part of the same program as parent process? This is the case in the shell; when a user starts a command it needs to run in a new process, but it is unrelated to the shell.

This is where the exec system call comes into play. An exec will replace the contents of the currently running process with the information from a program binary. Thus the process the shell follows when launching a new program is to firstly fork, creating a new process, and then exec the program binary it is supposed to run.

Thread Cancellation

Cancellation allows one thread to terminate another. One reason to cancel a thread is to save system resources such as CPU time. When your program determines that the thread's activity is no longer necessary then thread is terminated.

• Thread cancellation is a task of terminating a thread before it has completed.

• The thread cancellation mechanism allows a thread to terminate the execution of any other thread in the process in a controlled manner. Each thread maintains its own cancelability state. Cancellation may only occur at cancellation points or when the thread is asynchronously cancelable.

The target thread can keep cancellation requests pending and can perform application-specific cleanup when it acts upon the cancellation notice.

A thread's initial cancelability state is enabled. Cancelability state determines whether a thread can receive a cancellation request. If the cancelability state is disabled, the thread does not receive any cancellation requests.

• Target thread cancellation occurs in two different situations:

1. Asynchronous cancellation

2. Deferred cancellation

Asynchronous cancellation: Target thread is immediately terminated. With the help of another thread, target thread is cancelled. When a thread holds no locks and has no resources allocated, asynchronous cancellation is a valid option.

• Deferred cancellation: When a thread has enabled cancellation and is using deferred cancellation, time can elapse between the time it's asked to cancel itself exes and the time it's actually terminated.

Signal Handling

Signal is used to notify a process that a particular event has occurred. Signal may be synchronous or asynchronous. All types of signals are based on the following pattern:

1. At a specific event, signal is generated.

2. Generated signal is sent to a process/thread.

3. Signal handler is used for handling the delivered signal.

• Synchronous signals: An illegal memory access, division by zero is the example of synchronous signals. These signals are delivered to the same process which performed the operation for generating the signal.

• Asynchronous signals: Terminating a process with certain keystrokes are signals that are generated by an event external to the running process.

• Synchronous signal is sent to same process whether as asynchronous signal is sent to another process.

Signals may be handled in different ways:

1. Some signals may be ignored. For example changing the windows size.

2. Other signals may be handled by terminating the program. For example illegal access of memory.

Delivery of signals in multithreaded programs is more complex than single thread.

• Following are the condition where/ how should the signals be delivered to threads/process:

a. Thread applies the signal are received the signal.

b. Every thread in the process received the signal.

c. Deliver the signal to limited threads in the process

d. All the signals are received to particular thread for the process.

The method for delivering a signal depends on the type of signals generated.

1. Synchronous signals is sent to the thread which causes the signal.

2. All the thread received asynchronous signals.

Thread Pool

A thread pool offers a solution to both the problem of thread life-cycle overhead and the problem of resource thrashing. By reusing threads for multiple tasks, the thread-creation overhead is spread over many tasks.

• Thread pools group CPU resources, and contain threads. used to execute tasks associated with that thread pool. Threads host engines that execute user tasks, run specific jobs such as signal handling and process requests from a work queue.

• Multithreaded server has potential problems and these problems are solved by using thread pool. Problems with multithreaded server :

1. Time for creating thread

2. Time for discarding thread

3. Excess use of system resources.

Advantages of thread pools :

1. Servicing a request with an existing thread is usually faster than waiting to create a thread.

2. Thread pool size is fixed.

Digital Principles and Computer Organization: Unit II (a): Synchronous Sequential Logic : Tag: : Process Management - Introduction to Operating Systems - Threading Issues