Pthreads are created using pthread_create(pthread_t *thread, const pthread_attr_
ID: 3814509 • Letter: P
Question
Pthreads are created using pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg). Its arguments are:
• thread: A handle for the thread, of type pthread_t.
• attr: to fine tune various parameters, or NULL for the default values.
• start_routine: the C routine that the thread will execute once it is created.
• arg: A single argument that may be passed to start_routine. It must be passed by reference as a pointer cast of type void. NULL may be used if no argument is to be passed.
On success, pthread_create() returns 0; on error, it returns an error number.
Pthreads are terminated when the function returns, or the thread can call pthread_exit() which terminates the calling thread explicitly.
Open an editor (vi or gedit) and save the program on the next page as pthread.c.
//compile and link with -pthread
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *pthread_function( void *ptr ){
printf("Hello from the new thread, my pid is %d ", getpid());
}
int main() {
printf("Hello from the calling process, my pid is %d ", getpid());
pthread_t thread;
int ret;
ret = pthread_create( &thread, NULL, pthread_function, NULL);
if (ret){
printf("ERROR; return code from pthread_create() is %d ", ret);
exit(-1);
}
/* Last thing that main() should do */
pthread_exit(NULL);
}
Compile the program (don’t forget the –pthread):
student@Ubuntu:~/labs/lab2$ gcc -pthread pthread.c -o pthread
Run the program:
student@Ubuntu:~/labs/lab2$ ./pthread
What is the output of this program? Note the values for the pids.
You can make a thread wait until another thread form the same thread group has terminated. Use the pthread_join() function.
Open an editor (vi or gedit) and save the program below as pthread2.c.
//compile and link with -pthread
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *pthread_function( void *ptr ) {
char *message;
message = (char *) ptr;
printf("%s ", message);
sleep(1);
}
main() {
pthread_t thread1, thread2;
char *message1 = "Hello from Thread 1";
char *message2 = "Hi from Thread 2";
int iret1, iret2;
/* Create independent threads*/
iret1 = pthread_create( &thread1, NULL, pthread_function, (void*) message1);
sleep(1);
iret2 = pthread_create( &thread2, NULL, pthread_function, (void*) message2);
/* 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);
printf("Thread 1 returns: %d ",iret1);
printf("Thread 2 returns: %d ",iret2);
exit(0);
}
Compile the program (don’t forget the –pthread):
student@Ubuntu:~/labs/lab2$ gcc -pthread pthread2.c -o pthread2
Run the program:
student@Ubuntu:~/labs/lab2$ ./pthread2
What is the output of this program? What happens in what order?
//compile and link with -pthread
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *pthread_function( void *ptr ){
printf("Hello from the new thread, my pid is %d ", getpid());
}
int main() {
printf("Hello from the calling process, my pid is %d ", getpid());
pthread_t thread;
int ret;
ret = pthread_create( &thread, NULL, pthread_function, NULL);
if (ret){
printf("ERROR; return code from pthread_create() is %d ", ret);
exit(-1);
}
/* Last thing that main() should do */
pthread_exit(NULL);
}
Explanation / Answer
First save the programs in pthread1.c and pthread2.c
compile and linking
gcc -c pthread1.c
we get pthread1.o
then link with lpthread
gcc -lpthread -o pthread1 pthread1.o
we get exectable pthread1
reeat same steps to get executable for pthread2.c
------------------------------------------------------------------------------
//output from first program
Hello from the calling process, my pid is 50
Hello from the new thread, my pid is 50
//output second program
Hello from Thread 1
Hi from Thread 2
Thread 1 returns: 0
Thread 2 returns: 0
In first program there is one thread which prints Hello from the new thread, my pid is 50 and exists
But in second program , there are two threads joined , so we get two message from two thread. First message "Hello from Thread 1" from thread1 and "Hi from Thread 2" from thread2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.