CSE2005 ETH Reference Material I Module2 Threads
CSE2005 ETH Reference Material I Module2 Threads
Heap
Un-initialized data
Initialized data
Text ( program code)
Responsiveness
Interactive application can delegate background functions to a thread and keep
running
Resource Sharing
Several threads can access the same address space
Economy
Allocating memory and new processes is costly. Threads are much ‘cheaper’ to
initiate.
Scalability
Use threads to take advantage of multiprocessor architecture
Multithreaded Server Architecture
thread
thread
thread
thread
Threads and Multicore Programming
thread
thread
• Examples
– Windows XP/2000
– Solaris
– Linux
– Tru64 UNIX
– Mac OS X
Multithreading Models
User Thread – to - Kernel Thread
• Many-to-One
• One-to-One
• Many-to-Many
Many-to-One
Literally:
pthread_t pthread_self()
http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
http://codebase.eu/tutorial/posix-threads-c/
Threads vs. Processes
• Advantages of multithreading
– Sharing between threads is easy
– Faster creation
• Disadvantages of multithreading
– Ensure threads-safety
– Bug in one thread can spread to other threads, since they share the
same address space
– Threads must compete for memory
• Considerations
– Dealing with signals in threads is tricky
– All threads must run the same program
– Sharing of files, users, etc
Example 1
#include<stdio.h> if ( pthread_create( &thread1, NULL, thread1_fun, NULL )
<0 )
#include<pthread.h>
{
#include<stdlib.h>
perror("pthread_create");
#include<string.h> exit( 1 );
void * thread1_fun(void * a) { }
printf("Thread 1 is running.....\n\n");
printf("Thread 1's ID is %ld\n", pthread_self()); if ( pthread_create( &thread2, NULL, thread2_fun, NULL )<
0)
}
{
void * thread2_fun(void *address) { perror("pthread_create");
printf("Thread 2 is running.....\n\n"); exit( 1 );
printf("Thread 2's ID is %ld\n", pthread_self()); }
} pthread_join( thread1 , NULL);
int main() { pthread_join( thread2 , NULL);
printf("Main Thread is about to terminate\n");
pthread_t thread1, thread2;
printf("Main Thread has started running.....\n\n");
return 0;
printf("Main Thread's ID is %ld\n", }
pthread_self());
Example 2
#include<stdio.h> int main() {
#include<pthread.h> int n =5 ;
#include<stdlib.h> char str[50];
#include<string.h> strcpy(str, "VIT Vellore");
void * thread1_fun(void *num) { pthread_t thread1, thread2;
int *n = (int *) num; if ( pthread_create( &thread1, NULL, thread1_fun,
int i; (void *) &n ) <0 )
long fact =1; {
for( i = 1 ; i <= *n; i++) perror("pthread_create");
fact*= i; exit( 1 );
printf("The factorial of %d is %ld\n", *n , fact); }
}
void * thread2_fun(void *address) { if ( pthread_create( &thread2, NULL, thread2_fun,
char *str = (char *) address; (void *) str ) <0 )
int len = 0; {
for( len = 0 ; str[len] != '\0' ; len++ ) ; perror("pthread_create");
printf("The string length of %s is %d\n", str , exit( 1 );
len); }
} pthread_join( thread1 , NULL);
pthread_join( thread2 , NULL);
return 0;
}
Example 3
#include<stdio.h> int main() {
#include<pthread.h> int n =5 ;
#include<stdlib.h> char str[50];
#include<string.h> //Same as the previous program but thread1_fun strcpy(str, "VIT Vellore");
returns a value fact = (long *) malloc(sizeof(long));
long * fact; pthread_t thread1, thread2;
void * thread1_fun(void *num) { if ( pthread_create( &thread1, NULL, thread1_fun, (void *) &n ) <0 )
int *n = (int *) num; {
int i; perror("pthread_create");
*fact=1; exit( 1 );
for( i = 1 ; i <= *n; i++) }
*fact*= i;
printf("The factorial of %d is %ld\n", *n , *fact); if ( pthread_create( &thread2, NULL, thread2_fun, (void *) str ) <0 )
return fact; {
} perror("pthread_create");
void * thread2_fun(void *address) { exit( 1 );
char *str = (char *) address; }
int len = 0; pthread_join( thread1 , (void **)&fact);
for( len = 0 ; str[len] != '\0' ; len++ ) ; pthread_join( thread2 , NULL);
printf("The string length of %s is %d\n", str , len);
} printf("The factorial of %d is %ld\n", n , *fact);
return 0;
}