Skip to main content

Posts

Showing posts with the label thread

1 Introduction to thread in Linux

1 Introduction to thread in Linux What is thread?     Thread is the independent piece of code executed parallel y when main program executed.  Why Thread ?     Consider you are creating student report program in C, It should contain number of students written final exam, and their name and total mark.  What actually you do? Answer is this . You will write main program which find number of students, list their name and total mark. So step by step you do. But if you use thread,  main program list students name and total mark. same time thread will find total number of students. So we can save some time if we use thread.  Threading is an example of parallel programming. Working environment :     Here used pthread (POSIX standard thread) library under Ubuntu(Linux).     gcc compiler used to compile the program Compilations :     There is need to add pthread linker when compiling thread program with the help of gcc co...

2 Thread creation in Linux

2 Thread creation in Linux Main program implicitly create the starting thread. Other thread can be created by programmer with the help of  pthread_create API. int pthread_create ( pthread_t * thread , const pthread_attr_t * attr ,   void * ( * start_routine ) ( void * ) , void * arg ) ; pthread_create available in pthread.h header file pthread_create arguments :  thread - It is the unique identifier for each thread. This variable has pthread_t type attr - This the variable used to set the properties for the thread. Set to NULL for default. start_routine - This is the pointer function, this function executed when thread created arg - argument passed to thread routine.  When thread finishes the work pthread_exit routine will terminate the thread. PROGRAM : #include <stdio.h> #include <stdlib.h> #include <pthread.h> void * t_function ( void * ) ;   void * t_function ( void * arg ) //thread handler {     long t_num = (...