1 /*threeads strands of execution in a process at any instant*/
2 //In 2004 ..threads came in existance...untill then linux became so advanced ...so ch anges in whole linux were not possible ..so while compiling this program ...thread li brary is used at compile time..
3 //gcc thread.c -lpthread
4 //
5 //strand of execution means perticular set of statements execute at a time in a proce ss
6 //in threads main or creating thread should always exist so that another thread creat ed by it may exist....
7 //threads have their own stack segment means their own local variables which r place d in stack segment
8 //threads share data segment having global variables
9 //At a time two threads may run in dual core processor ...but more than two threads h ave to share the processor equally & executes equally
10 //Thread switching is switching b/w two threads ..if we move from thread of one proce ssor to thread of another processor means that it 1st process scheduling occur then i t may be called thread scheduling but scheduling will not be a write term bcz threads r always switched frm one thread to another
11 //pthread_create is used to create a thread 12 //some function r already void* type & then typecasted further by us while printing . ..because creator of these functions do not know that user of these function is going to use what type of data
13 #include<stdio.h>
14 #include<stdlib.h>
15 void* thread_fun(void*);
16 int main()
17 {
18 int ret;
19 void *retval;
20 char message[]="this is anu";
21 pthread_t tid;
22 printf("i m in main\n");
23 ret=pthread_create(&tid,NULL,thread_fun,(void*)message);//1st arg is thread id . ...comes in tid variable..NULL will be its attribute...thread_fun is address of fucti on.......(void*) is address where argument message of thread_fun will be saved...so a ddress of message will be send
24 if(ret==0)
25 {
26 printf("new thread is created succesfully& executing\n");
27 }
28 printf("waiting for thread to finish\n");
29 pthread_join(tid,&retval);/pthred_join fun_c is used to wait for a perticular thread to finish...the thread created /by function using pthread_join function ..
30 //pthread_join has 1st parameter is its thread id & 2nd parameter is address of a poi nter in which return code "bye" will be saved
31 //sleep(3);
32 printf("retval=%s\n",(char*)retval);//retval was of void* type so typecasted by char*
33 exit(EXIT_FAILURE);
34 }
35 void* thread_fun(void* arg)
36 {
37 printf("message is arg=%s\n",(char*)arg);
38 pthread_exit("bye");//pthead_exit is used to exit frm a thread ..it will return exit code that is "bye" here ...returned to pthread_join
39 }
40
41 //LIMITATIONS OF THREADS
42 //IT is not defined that which thread is executing 1st & which is going to be execute d last..........suppose close() thread is executed 1st & write() thread is executed l ator ..what will happen?
43 //as data segment is shared ...it may be changed by any thread at any time ..so probl em may be created
44 //threads r only useful if we r using all of the processors we had...
45 //if we placed process in waiting state for thread to finish...only one processor or partially another processor will be used
/*A program in which i have created no. of threads ...their thread function will invoke....time b/ //b/w creation of each thread is 2 sec....after 20 sec all the threads will be created ....counter // will goto pthread_join .....where it will wait for return code of i=0 means zero number thread. //..when all the threads will get operated....then they will save thier return code in return cod/ //table......pthread join will pick that code one by one & then return code will be printed one by one from tid 0 to tid 9...this was what we had to examine.... */
2 #include<stdio.h>
3 #include<stdlib.h>
4 #include<pthread.h>
5 int i;
6 void* thread_fun(void*);
7 int main()
8 {
9 void* retval;
10 char message[]="THIS is place of linux";
11 int res;
12 pthread_t tid[10];//array in which thread id will stay
13 for(i=0;i<10;i++)
14 {
15 pthread_create(&tid[i],NULL,thread_fun,(void*)message);//no. of threads will be created after 2 sec each
16 sleep(2);
17 }
18 if(res==0)
19 { printf("thread is created \n");//after creating all the threads it will be printed
21 }
22 //return 0;
23 for(i=0;i<10;i++)
24 {
25 pthread_join(tid[i],&retval);//as pthread_join got code of finished thread..it will wait for a nother return code of another thread ...means it is joining to some thread to synchronise the threads
26 printf("retval=%s...%d\n",(char*)retval,i);//return code of each thread will be printed
27 }
28 return 0;
29 }
30 void* thread_fun(void* arg)//as any thread is created...that thread will jump to related function.
31 {
32 if(i==0)
33 {
34 sleep(25);//thread no. 0 will wait here for 25 sec...
35 printf("in thread [%d]\n",i);//after 25 sec ..0 thread will oprate
36 pthread_exit("yes i got it");//print it & then return its code to pthread_join which will be printed 1st of all.......
37 }
38 else
39 {
40 41 printf("in thread[%d]\n",i);//thread no. 1 to 9 will execute turnwise as they r created
42 printf("message is %s\n ",(char*)arg);
43 sleep(3);//after 3 sec . each thread will be finished
44 pthread_exit("bye");//finished thread will return a code which is collected by pthread_join