Thread_create (thread, func, args) Create a new thread \'thread\' to run func(ar
ID: 3805264 • Letter: T
Question
Thread_create (thread, func, args) Create a new thread 'thread' to run func(args) thread_yield() relinquish processor voluntarily. Scheduler will recall it later per policy Thread_join(thread) In parent, wait for forked thread to exit, then return thread_exit(ret)Quit thread and clean up (store the value ret in the current thread's data structure), wake up joiner if any What would get printed in the following code which contains POSIX standard notation with prefix "p"? void main () {pthread_t thread;/^*define Posix thread^*/pthread_ create (&thread;, NULL, ?&helper;, NULL); printf("Hello World! 2 "); exit (0);} void^*helper(void^*arg) {printf (HelloWorld! 1 "); pthread _exit(0);} How can we modify the code above to always printout "Hello World 1" followed by "Hello World! 2"?Explanation / Answer
Answers:
(2a). This code prints different things when we run it for several times, The output is not fixed as it is based on the threads.
On the UBUNTU operating system, I wrote this code and executed on the terminal and I got different outputs like:
Hello World! 2
Hello World! 1
Hello World! 1
-----------------
Hello World! 2
-----------------
Hello World! 2
Hello World! 1
like wise...
Sometimes it only printed 2, sometimes 2 and 1 one time, sometimes 2 and two times 1.
So the right answer is there are multiple things printed if we run it several times.
The reason behind the multiple answers is threading. In this program, inside main(), one thread is created so either the parent thread or the child thread which gets the processor starts its execution. So, the output completely depends on context switching done in CPU which decides which is the next process to get executed.
(2b). Here, the complete code I can write for this is as follows:
Source Code:
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
void *helper(void*);
void main()
{
pthread_t thread;
pthread_create(&thread, NULL, &helper, NULL);
pthread_join(thread, NULL);
printf("Hello World! 2 ");
exit(0);
}
void *helper(void* arg)
{
printf("Hello World! 1 ");
pthread_exit(0);
}
Output:
I executed it for around 10 times and every time got the same output:
Hello World! 1
Hello World! 2
Description:
The thread is assigned to run the helper() method in the code. This method prints "Hello World! 1" and the main(0 thread prints "Hello World! 2". That means we need to make a program in such a way that it first completes the execution of child thread and when it is exited then only the parent thread i.e. the main() thread is terminated.
To achieve this, we can use a function called pthread_join() inside the main() thread and inside this function, we are supposed to pass a thread variable and a return type which we can specify NULL for simplicity as we are not returning anything from the thread.
The task of this function is to make sure that the main() is not terminated until the child thread specified as a parameter of pthread_join() is exited. So, the child thread is always executed first then only the main() thread is executed, So, this prints the same result every time and it is what we need.
Please comment if there is any query. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.