–> “threads” are units of execution within a process…a process contains at
least on thread(called main thread) which executes the “main() function in the
–> program may create more threads to accomplish multi-tasking inside a
–> all threads in a process , execute in parallel by getting their own time
slice from the “linux schedular”.
–> but in linux kernal, multiple threads in a single process, are the same as
the “multiple unrelated processes, which share the address space, open files
and other kernal resources….this process is implemented using “clone()
–> when a process dies, all the threads in the process also dies.
–> “threads” in the process share all the resources except some hich makes
them schedule individually on the processor…it includes SP,IP,CPU registres,
scheduling proprties and TSD(thread specific data)..
–>threads are called “lightweight process” bcz they r not creating problem/
overhead in new process creation.
–> there is no child-parent relationship between threads….but thread creation
is allowed from any thread…
–> Multi threading is heavily used in GUI application for e.g we can have
“one thread which is handling user interface events” , other threads handling
corresponding data base for the user…
Threads Vs. Process :-
–>thread creation is many times faster than process creation…
–> “context switching of threads” is much faster than than “context switching
–> “Debugging of multi threaded program” is easier than that of “multiprocess”
program …..use gdb
–> “process” uses a lot of kernal memory , whereas threads uses less kernal
Note :- linux provides “pthreads API implementation for applications to create
and manage multiple threads within a process”…..IEEE standard 10003.1c or
POSIX=Portable Operating System Interface [for Unix].
Services offered by POSIX.1c are :-
4)signal handling ….
Note :- Debugging a multi-threaded program is much harder than debugging a
single threaded programs due to “unpredictable nature of schedular in
scheduling threads on the processors.
Note :- Bugs in a multi-threaded application are hard to re-produce bcz they
occur only when they are scheduled in a particular manner , which is
hard to predict ..
Note :- Threads should use “only re-entrant functions” which are “threadsafe”
to ensure data integrity and unpredictable behaviour….for e.g “errno”
CREATING THREADS :-
threads shares the address space of the process in which they are running
and have their own stack to run the thread function ….
pthread_create() :- function used for creating threads ….
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
1st arg:- pthread-identifier which will be set to thread-identifer when the
thread is successfully created ..
2nd arg :- thread attribute object …
3rd arg :- ptr to function which takes void* as arguement and returns void*
4th arg :- ptr to arguemnt which shall be passed to start_routine
terminating a thread :- use pthread_exit() function …
void pthread_exit(void *retval);
is value returned from thread.
Note :- pthread_exit() function terminates the calling thread and makes the
value “retval” available to any successful join with terminating thread ..
Note :- after a thread is terminated , result of access to local varibale of thread
is undefined …
PTHREAD_JOIN() :- this function blocks the current thread until the thread
“thread” terminates ..
int pthread_join(pthread_t thread, void **value_ptr);
–> upon success, pthread_join() will return zero
–> pthread_join() function suspends the execution of calling threads until the
target thread terminates
Note :- value passed to “pthread_exit()” by terminating thread is made
available in location referenced by “last arguement of Pthread_join()”.
Please login or Register to submit your answer