Sprintfbuf http10 s srn errnum shortmsg writenfd

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: d then let the peer thread dereference the pointer and assign it to a local variable. 1 2 3 4 5 6 void *thread(void *vargp) { int connfd = *((int *)vargp); /* ... */ return NULL; } However, this would be wrong because it introduces a race between the assignment statement in the peer thread and the accept statement in the main thread. If the assignment statement completes before the next accept, then the local connfd variable in the peer thread gets the correct descriptor value. However, if the assignment completes after the accept, then the local confd variable in the peer thread gets the descriptor number of the next connection. The unhappy result is two threads are now performing input and output on the same descriptor. In order to avoid the potentially deadly race, we must assign each connected descriptor returned by accept to its own dynamically allocated memory block, as shown in lines 21-22. Now consider the thread routine in lines 28-38. To avoid memory leaks, we must detach the thread so that its...
View Full Document

Ask a homework question - tutors are online