Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions