Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following Pthread C program is intended to synchronize parent and child thread execution to produce the following output. parent: begin child parent: end However,

image text in transcribedimage text in transcribed The following Pthread C program is intended to synchronize parent and child thread execution to produce the following output. parent: begin child parent: end However, when this program executes, the parent thread occasionally freezes prior to print "parent: end". - First, explain why this happens. - Second, copy the program source to the answer box and fix the problem(s). If you need, you can refer to Pthread reference link. \#include pthread.h \#include stdio.h \#include stdlib.h pthread_mutex_t m= PTHREAD_MUTEX_INITIALIZER; pthread_cond_t c= PTHREAD_COND_INITIALIZER; void thr_exit() \{ pthread_mutex_lock(\&m); pthread_cond_signal(\&c); pthread_mutex_unlock(\&m); \} void *child(void *arg) \{ printf("child "); thr_exit(); \} void thr_join() \{ pthread_mutex_lock(\&m) pthread_cond_wait (&c,&m); pthread_mutex_unlock(\&m); \} int main(int argc, char *argv[]) \{ printf("parent: begin "); pthread_t p; pthread_create(\&p, NULL, child, NULL); thr_join(); printf("parent: end "); return 0 ; \}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Professional SQL Server 2000 Database Design

Authors: Louis Davidson

1st Edition

1861004761, 978-1861004765

More Books

Students also viewed these Databases questions