Question: 1 / * WARNING: This code is buggy! * / 2 #include csapp.h 3 void * thread ( void * vargp ) ; 4

1/* WARNING: This code is buggy! */
2 #include "csapp.h"
3 void *thread(void *vargp);
4
5 int main()
6{
7 pthread_t tid;
8
9 pthread_create(&tid, NULL, thread, NULL);
10 exit(0);
11}
12
13/* Thread routine */
14 void *thread(void *vargp)
15{
16 sleep(1);
17 printf("Hello, world!
");
18 return NULL;
19}
-----------------------------------code/conc/hellobug.c
Wondering if my answers for A and B are correct.
A. The program in Figure 12.46 has a bug. The thread is supposed to sleep for
1 second and then print a string. However, when we run it on our system,
nothing prints. Why?
My answer: Theres a bug because the Exit(0); function in line 10 terminates the whole process, including all threads, before the thread gets a chance to run.
B. You can fix this bug by replacing the exit function in line 10 with one of two
different Pthreads function calls. Which ones?
My answer: You fix this by replacing Exit(0); with pthread_exit(NULL); or with pthread_join(tid, NULL);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!