What is thread and its use?
-> It is a lite weight process, which will execute the different part of the same process, parallely or
con-currently.
(or)
-> Threads, like processes, are a mechanism to allow a program to do more than one thing at a time.
As with processes, threads appear to run concurrently. Each thread may be executing a different
part of the program at any given time. Threads can be used to achieve concurrency, similar to
processes, which consumes lesser resources than a process.
Where to use threads?
-> To improve the responsiveness of the applications we can use threads
-> Ex:
- Games
- Web servers (To handle multiple clients concurrently(refer fig 1.0))
- GUI applications (to handle background tasks), etc.
-> If the functions are not dependent we can use multiple threads
-> Using multiple threads in the single core CPU is totally depends on the application.
-> If the functions are having dependency we should not use threads.
thread1
client 1
client 2 thread2
Webserver
thread3
client 3
fig 1.0: Multiple clients will be handled by threads.
Single threaded Vs Multi threaded process:
-> Each process will be having its own code, data, heap and stack segments allocated.
-> Each process will have one thread running in default (i.e. main thread).
-> where as in multi-threaded application, Threads will be sharing the memory segments except the
stack segment and registers.
-> Each thread will be having its own stack segments and progam counter. other segments are shared
among the threads.
-> Threads are similar to handling multiple functions in parallel. Since they share same code & data segments,
care to be taken by programmer to avoid issues.
Advantages of threads:
-> Creation time will be less compared to creating process.
-> Because the resources and memory are shared (ex: memory segments, file descriptors, global variables,
etc.)
-> Switching b/w the threads are faster compared to process switch
-> No need of IPC for the communication (Which offers efficiency in the communication)
-> No kernel involved in creation and handling of user threads
-> We can use library functions to create and handle the threads.
pthread library:
-> We will ne using POSIX standard thread (pthreads) to create and handle threads in unix like systems
-> To use pthread related functions and variables we should include pthread.h
-> definitions of the functions are present in the [Link] (not in the standard C library).
-> Since the definitions are not present in the standard C library we should add -lpthread or -pthread
while compiling the program.
Eg: gcc main.c -pthread
Thread creation:
prototype: int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void * (*start_routine)(void *), void *arg);
-> This function is used to start / create a new thread in the calling process.
-> That new thread will call the start_routine and starts the execution.
-> arg will be passed as an argument to start_routine.
-> attr: Thread attribute variable, which is used to mention the attributes (behaviour, property, etc.) of
the new thread.
-> thread: Used to collec the ID of thread
-> Return value : 0 on success, error number of failure.
prototype: int pthread_join(pthread_t pid, void **retval);
-> This function is used to block the execution of the calling thread until specified thread completes
the execution
-> where retval argument is used to collect the return value of the specified thread.
-> After completion of thread, this function will clean-up the the thread resources(avoiding zombie thread).
prototype: pthread_t pthread_self(void):
-> This function return the ID of the calling thread (like getpid() return the ID of calling process).
Thread termination:
-> pthread_exit(): If thread calls this function, it will get terminated.
- this function accept the arugument which can be used to return the value from thread.
-> return from thread:
- Return keyword is also used to terminate a thread
-> pthread_cancel();
- This function is used to send cancellation request to another thread.
- This function accepts the thread ID as an argument.
- Based on thread ID it will sent the cancellation request.
-> exit():
- If any thread calls exit(), which will terminate all the threads running in the process.
-> return from main:
-> This also causes the termination of all the threads running in the process.