Question: In Figure 12.43, we might be tempted to free the allocated memory block immediately after line 14 in the main thread, instead of freeing it

In Figure 12.43, we might be tempted to free the allocated memory block immediately after line 14 in the main thread, instead of freeing it in the peer thread. But this would be a bad idea. Why?

Figure 12.43

1 #include "csapp.h" 2 #define N 4 3 4 5 67 8 9 10 11 12 13 14 1567 18 19 20 21 22 23 24 25 26 27 28 void

1 #include "csapp.h" 2 #define N 4 3 4 5 67 8 9 10 11 12 13 14 1567 18 19 20 21 22 23 24 25 26 27 28 void *thread (void *vargp); int main() { } pthread_t tid [N]; int i, *ptr; } for (i = 0; i < N; i++) { ptr Malloc(sizeof (int)); *ptr 1; Pthread_create(&tid[i], NULL, thread, ptr); = = } for (i = 0; i < N; i++) Pthread_join(tid[i], NULL); exit(0); /* Thread routine */ void *thread (void *vargp) { int myid*((int *)vargp); Free (vargp); printf("Hello from thread %d ", myid); return NULL;

Step by Step Solution

3.41 Rating (154 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

If we free the block immediately after the ca... View full answer

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 Computer Systems A Programmers Perspective Questions!