PrakW9S03 Multithreading Using Posix Thread
PrakW9S03 Multithreading Using Posix Thread
PrakW9S03 Multithreading Using Posix Thread
What is a Thread?
A thread is a single sequence stream within in a process. Because threads have some of the properties of
processes, they are sometimes called lightweight processes.
Why Multithreading?
Threads are popular way to improve application through parallelism. For example, in a browser, multiple
tabs can be different threads. MS word uses multiple threads, one thread to format the text, other thread to
process inputs, etc.
Threads operate faster than processes due to following reasons:
1) Thread creation is much faster.
2) Context switching between threads is much faster.
3) Threads can be terminated easily
4) Communication between threads is faster.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //Header file for sleep()
#include <pthread.h>
int main()
{
pthread_t thread_id;
printf("Before Thread\n");
pthread_create(&thread_id, NULL, myThreadFun, NULL);
pthread_join(thread_id, NULL);
printf("After Thread\n");
exit(0);
}
int main()
{
int i;
pthread_t tid;
pthread_exit(NULL);
return 0;
}
• Threads
• Why Threads?
• Processes Vs Threads
• User-Level Threads
• Kernel-Level Threads
• Advantages of Threads over Multiple Processes
• Disadvantages of Threads over Multiprocesses
• Application that Benefits from Threads
• Application that cannot benefit from Threads
• Resources used in Thread creation and Process Creation
• Context Switch
• Major Steps of Context Switching
• Action of Kernel to Context switch among threads
• Action of kernel to Context switch among processes
EOF