At the next level are second level domain names such

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: *vargp); int main() { pthread_t tid[N]; int i, *ptr; for (i = 0; i < N; i++) { ptr = Malloc(sizeof(int)); *ptr = i; 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\n", myid); return NULL; } code/threads/norace.c Figure 11.31: A correct version the program in Figure 11.30 without a race. When we run this program on our system, we now get the correct result: unix> ./norace Hello from thread 0 Hello from thread 1 11.7. OTHER SYNCHRONIZATION ERRORS Hello from thread 2 Hello from thread 3 599 We will use a similar technique in Chapter 12 when we discuss the design of threaded network servers. Practice Problem 11.5: In Figure 11.31, we might be tempted to free the allocated memory block immediately after line 15 in the main thread, instead of freeing it in the peer thread. But this would be a bad idea. Why? Practice Problem 11.6: A. In Figure 11.31, we eliminated the race by allocating a separate block for each int...
View Full Document

Ask a homework question - tutors are online