Technically, a thread is defined as an independent stream of instructions that can be scheduled to run as such by the operating system.
What did it mean ? :D Let's think of a main function running a set of procedures independently, moving one step forward, imagine all of these procedures being able to be scheduled to run simultaneously and/or independently by the operating system. That would describe a "multi-threaded" program.
Before understanding thread, a little bit knowledge about process might help.A process is created by the operating system, and requires a fair amount of "overhead". Processes contain information about program resources and program execution state, including:
Process ID process group ID, user ID, and group ID, registers, stack, heap, working directory, program instructions, shared libraries etc.
Threads use and exist within these process resources, yet are able to be scheduled by the operating system and run as independent entities largely because they duplicate only the bare essential resources that enable them to exist as executable code.
Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h header file and a thread library - though this library may be part of another library, such as libc, in some implementations.
Threads are considered light weight while processes heavy weight :
When compared to the cost of creating and managing a process, a thread can be created with much less operating system overhead. Managing threads requires fewer system resources than managing processes.
Moving to the implementation by the program :
It is pre requisite to include pthread.h header file
the functions used are:
pthread_create(thread,attr,start_function,arg)
This function is used to create a new thread and make it executable, this function can be called any number of times. The arguements are :
- thread: unique identifier for the new thread returned by the subroutine.
- attr: Used to set thread attributes you can set it to NULL.
- start_function: This is the function where thread begin it's execution once it is created.
- arg: a single arguement that may be passed to start_function. You can set it to NULL if no arguement is to be passed.
By default a thread is created with certain attributes we can get default attributes by pthread_attr_init()
pthread_exit(0) This function terminates the thread whether it's function was completed or not, after execution of this function, the thread gets terminated.
Although, there are other ways to terminate thread-
- main() finishes first, without calling pthread_exit explicitly itself
- The thread returns normally from its starting function. Its work is done.
- The thread is canceled by another thread via the pthread_cancel() function
pthread_join(thread,NULL) is used in main so that main function waits until the termination of thread that has been passed in it as a parameter.
The above explanation can be better understood by the program:
#include<stdio.h>
#include<pthread.h>
#include<math.h>
void *isprime(void *p);
int main(){
pthread_t thd;
pthread_create(&thd,&attr,isprime,NULL); // line A
// or these lines instead of line A//
/*
pthread_attr_t attr; // set of thread attributes
pthread_attr_init(&attr); // get the default attributes
pthread_create(&thd,&attr,isprime,NULL);
*/
pthread_join(thd,NULL);
}
void *isprime(void *p){
int i;
int num1;
printf("Enter Number: ");
scanf("%d",&num1);
for(i = 2; i <= sqrt(num1); i++)
if((int)num1%i==0){
printf("Not a Prime Number\n");
pthread_exit(0);
}
printf("Prime");
pthread_exit(0);
}
NOTE:
To run the above program the command used in GNU Linux is
gcc -pthread
To use sqrt function from math.h we must add -lm to the command
gcc -pthread -lm
we are required to do so to include the standard library.
Explanation :
pthread_t is used to handle reference to the thread.
pthread_create() and pthread_join, their working has been explained above.
Please read the comments in the program to see the difference of code if we pass the attributes in parameter.
Lastly , the start_function declared here is isprime,
**It is necessary to define it's return type as (void *) and the parameter passed in it must (void *) type.
No comments:
Post a Comment