Apr 27, 2014

Threads in Linux

Threads in Linux :

                         A thread is a semi-process, that has its own stack, and executes a given piece of code. Unlike a real process, the thread normally shares its memory with other threads (where as for processes we usually have a different memory area for each one of them). A Thread Group is a set of threads all executing inside the same process. They all share the same memory, and thus can access the same global variables, same heap memory, same set of file descriptors, etc. All these threads execute in parallel (i.e. using time slices, or if the system has several processors, then really in parallel).

                         The advantage of using a thread group instead of a normal serial program is that several operations may be carried out in parallel, and thus events can be handled immediately as they arrive (for example, if we have one thread handling a user interface, and another thread handling database queries, we can execute a heavy query requested by the user, and still respond to user input while the query is executed).

                      The advantage of using a thread group over using a process group is that context switching between threads is much faster then context switching between processes (context switching means that the system switches from running one thread or process, to running another thread or process). Also, communications between two threads is usually faster and easier to implement then communications between two processes.
                  
                 On the other hand, because threads in a group all use the same memory space, if one of them corrupts the contents of its memory, other threads might suffer as well. With processes, the operating system normally protects processes from one another, and thus if one corrupts its own memory space, other processes won't suffer. Another advantage of using processes is that they can run on different machines, while all the threads have to run on the same machine (at least normally).

 Thread Basics:

  • Thread operations include thread creation, termination, synchronization (joins,blocking), scheduling, data management and process interaction.
  • A thread does not maintain a list of created threads, nor does it know the thread that created it.
  • All threads within a process share the same address space.
  • Threads in the same process share:
    • Process instructions
    • Most data
    • open files (descriptors)
    • signals and signal handlers
    • current working directory
    • User and group id
  • Each thread has a unique:
    • Thread ID
    • set of registers, stack pointer
    • stack for local variables, return addresses
    • signal mask
    • priority
    • Return value: errno
  • pthread functions return "0" if OK.
 /*Thread1.c
 *
 *  Created on: Apr 27, 2014
 *      Author: vinod
 */

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *print_message_function( void *ptr );
 
main()
{
pthread_t thread1, thread2;
const char *message1 = "Thread 1";
const char *message2 = "Thread 2";
int  iret1, iret2;
 
/* Create independent threads each of which will execute function */
 
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) 
message1);
if(iret1)
    {
    fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1);
    exit(EXIT_FAILURE);
    }
 
iret2=pthread_create(&thread2,NULL,print_message_function,(void*) message2);
 
if(iret2)
    {
    fprintf(stderr,"Error - pthread_create() return code: %d\n",iret2);
    exit(EXIT_FAILURE);
    }
 
printf("pthread_create() for thread 1 returns: %d\n",iret1);
printf("pthread_create() for thread 2 returns: %d\n",iret2);
 
/* Wait till threads are complete before main continues. Unless we  */
/* wait we run the risk of executing an exit which will terminate      */
/* the process and all threads before the threads have completed.   */
 
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
exit(EXIT_SUCCESS);
}
 
void *print_message_function( void *ptr )
    {
    char *message;
    message = (char *) ptr;
    printf("%s \n", message);
    }


Compile :  cc -pthread thread1.c
           ./a.out

 Output :  pthread_create() for thread 1 returns: 0
                 pthread_create() for thread 2 returns: 0
                Thread 1
                Thread 2
  
References : http://www.cs.kent.edu/~ruttan/sysprog/lectures/multi-thread/multi-thread.html
             http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html 
             http://www.thegeekstuff.com/2012/03/linux-threads-intro/ 
             

No comments:

Post a Comment

Than 'Q'