THREADS
What is a Thread?
A thread is a flow of execution
through the process code, with its
own program counter, system
registers and stack. A thread is also
called a light weight process.
Threads provide a way to improve
application performance through
parallelism.
Each thread belongs to exactly one
process and no thread can exist
outside a process.
Threads (Cont.)
 Concurrent execution on single-core system:
 Parallelism on a multi-core system:
Difference between Process and Threads :
S.N. Process Threads
1. Process is heavy weight or resource
intensive.
Thread is light weight taking lesser
resources than a process.
2. Process switching needs interaction
with operating system.
Thread switching does not need to
interact with operating system.
3. In multiple processing environments
each process executes the same
code but has its own memory and file
resources.
All threads can share same set of
open files, child processes.
4. If one process is blocked then no
other process can execute until the
first process is unblocked.
While one thread is blocked and
waiting, second thread in the same
task can run.
5. Multiple processes without using
threads use more resources.
Multiple threaded processes use
fewer resources.
6. In multiple processes each process
operates independently of the others.
One thread can read, write or change
another thread's data.
Creating a Thread
The pthread_create function creates a new thread
Thread Joining
 It is quite possible that output created by a thread needs to be
integrated for creating final result
 So the main program may need to wait for threads to complete
actions
 The pthread_join() function helps to achieve this purpose .
Thread Attribute
 Thread attributes provide a mechanism for fine-
tuning the behavior of individual threads
 Recall that pthread_create accepts an
argument that is a pointer to a thread attribute
object
 If you pass a null pointer, the default thread
attributes are used to configure the new thread
 However, you may create and customize a
thread attribute object to specify other values
for the attributes
 There are multiple attributes related to
a particular thread, that can be set
during creation
 Some of the attributes are mentioned as
follows:
• Detach state
• Priority
• Stack size
• Name
• Thread group
• Scheduling policy
• Inherit scheduling
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void* routine() {
printf("Hello from threadsn");
sleep(3);
printf("Ending threadn");
}
int main(int argc, char* argv[]) {
pthread_t p1, p2;
if (pthread_create(&p1, NULL, &routine, NULL) != 0) {
return 1;
}
if (pthread_create(&p2, NULL, &routine, NULL) != 0) {
return 2;
}
if (pthread_join(p1, NULL) != 0) {
return 3;
}
if (pthread_join(p2, NULL) != 0) {
return 4;
}
return 0;
}
Joinable & Detached Threads
 A thread may be created as a joinable thread (the default) or as a detached thread
 A joinable thread, like a process, is not automatically cleaned up by GNU/Linux when it
terminates
 Thread’s exit state hangs around in the system (kind of like a zombie process) until
another thread calls pthread_join to obtain its return value. Only then are its resources
released
 A detached thread, in contrast, is cleaned up automatically when it terminates
 Because a detached thread is immediately cleaned up, another thread may not
synchronize on its completion by using pthread_join or obtain its return value
Creating detached Threads
 In order to create a dethatched thread, the thread
attribute needs to be set during creation
 Two functions help to achieve this
Thread Cancellation
 It is possible to cancel a particular thread under normal circumstances, a
thread terminates normally or by calling pthread_exit.
 However, it is possible for a thread to request that another thread terminate.
This is called cancelling a thread
Race Condition
A race condition is a situation that develops when many threads share a resource
or execute the same piece of code in a multithreaded context.
(A) (B)
(C) (D) (E)
Critical Section
Critical Section (Cont.)
Critical Section (Cont.)
Mutual Exclusion
Mutual Exclusion (Cont.)
Mutual Exclusion (Cont.)
Semaphores
 A semaphore is a counter that can be used to synchronize multiple threads
Semaphores (Cont.)

Understanding Threads in operating system

  • 1.
  • 2.
    What is aThread? A thread is a flow of execution through the process code, with its own program counter, system registers and stack. A thread is also called a light weight process. Threads provide a way to improve application performance through parallelism. Each thread belongs to exactly one process and no thread can exist outside a process.
  • 3.
    Threads (Cont.)  Concurrentexecution on single-core system:  Parallelism on a multi-core system:
  • 4.
    Difference between Processand Threads : S.N. Process Threads 1. Process is heavy weight or resource intensive. Thread is light weight taking lesser resources than a process. 2. Process switching needs interaction with operating system. Thread switching does not need to interact with operating system. 3. In multiple processing environments each process executes the same code but has its own memory and file resources. All threads can share same set of open files, child processes. 4. If one process is blocked then no other process can execute until the first process is unblocked. While one thread is blocked and waiting, second thread in the same task can run. 5. Multiple processes without using threads use more resources. Multiple threaded processes use fewer resources. 6. In multiple processes each process operates independently of the others. One thread can read, write or change another thread's data.
  • 5.
    Creating a Thread Thepthread_create function creates a new thread
  • 6.
    Thread Joining  Itis quite possible that output created by a thread needs to be integrated for creating final result  So the main program may need to wait for threads to complete actions  The pthread_join() function helps to achieve this purpose .
  • 7.
    Thread Attribute  Threadattributes provide a mechanism for fine- tuning the behavior of individual threads  Recall that pthread_create accepts an argument that is a pointer to a thread attribute object  If you pass a null pointer, the default thread attributes are used to configure the new thread  However, you may create and customize a thread attribute object to specify other values for the attributes  There are multiple attributes related to a particular thread, that can be set during creation  Some of the attributes are mentioned as follows: • Detach state • Priority • Stack size • Name • Thread group • Scheduling policy • Inherit scheduling
  • 8.
    #include <stdlib.h> #include <stdio.h> #include<pthread.h> #include <unistd.h> void* routine() { printf("Hello from threadsn"); sleep(3); printf("Ending threadn"); } int main(int argc, char* argv[]) { pthread_t p1, p2; if (pthread_create(&p1, NULL, &routine, NULL) != 0) { return 1; } if (pthread_create(&p2, NULL, &routine, NULL) != 0) { return 2; } if (pthread_join(p1, NULL) != 0) { return 3; } if (pthread_join(p2, NULL) != 0) { return 4; } return 0; }
  • 9.
    Joinable & DetachedThreads  A thread may be created as a joinable thread (the default) or as a detached thread  A joinable thread, like a process, is not automatically cleaned up by GNU/Linux when it terminates  Thread’s exit state hangs around in the system (kind of like a zombie process) until another thread calls pthread_join to obtain its return value. Only then are its resources released  A detached thread, in contrast, is cleaned up automatically when it terminates  Because a detached thread is immediately cleaned up, another thread may not synchronize on its completion by using pthread_join or obtain its return value
  • 10.
    Creating detached Threads In order to create a dethatched thread, the thread attribute needs to be set during creation  Two functions help to achieve this
  • 11.
    Thread Cancellation  Itis possible to cancel a particular thread under normal circumstances, a thread terminates normally or by calling pthread_exit.  However, it is possible for a thread to request that another thread terminate. This is called cancelling a thread
  • 12.
    Race Condition A racecondition is a situation that develops when many threads share a resource or execute the same piece of code in a multithreaded context. (A) (B) (C) (D) (E)
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
    Semaphores  A semaphoreis a counter that can be used to synchronize multiple threads
  • 20.