10.07.2015 Views

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

278 APPENDIX F. ANSWERS TO QUICK QUIZZESThese limitations require that script-based parallelismuse coarse-grained parallelism, with each unitof work having execution time of at least tens ofmilliseconds, and preferably much longer.Those requiring finer-grained parallelism are welladvised to think hard about their problem to seeif it can be expressed in a coarse-grained form.<strong>If</strong> not, they should consider using other parallelprogrammingenvironments, such as those discussedin Section 3.2.Quick Quiz 3.4:Why does this wait() primitive need to be socomplicated? Why not just make it work like theshell-script wait does?Answer:<strong>So</strong>me parallel applications need to take specialaction when specific children exit, and thereforeneed to wait for each child individually. In addition,some parallel applications need to detect the reasonthat the child died. As we saw in Figure 3.3, it isnot hard to build a waitall() function out of thewait() function, but it would be impossible to dothe reverse. Once the information about a specificchild is lost, it is lost.possibly separately compiled. In such a case,pthread_exit() allows these other functions toend the thread’s execution without having to passsome sort of error return all the way back up tomythread().Quick Quiz 3.7:<strong>If</strong> the C language makes no guarantees in presenceof a data race, then why does the Linux kernelhave so many data races? Are you trying to tellmethattheLinuxkerneliscompletelybroken???Answer:Ah, but the Linux kernel is written in a carefullyselected superset of the C language that includesspecial gcc extensions, such as asms, that permitsafe execution even in presence of data races. Inaddition, the Linux kernel does not run on a numberof platforms where data races would be especiallyproblematic. For an example, consider embeddedsystems with 32-bit pointers and 16-bit busses. Onsuch a system, a data race involving a store to anda load from a given pointer might well result in theload returning the low-order 16 bits of the old valueof the pointer concatenated with the high-order 16bits of the new value of the pointer.Quick Quiz 3.5:<strong>Is</strong>n’t there a lot more to fork() and wait() thandiscussed here?Answer:Indeed there is, and it is quite possible that thissection will be expanded in future versions toinclude messaging features (such as UNIX pipes,TCP/IP, and shared file I/O) and memory mapping(such as mmap() and shmget()). In the meantime,there are any number of textbooks that cover theseprimitives in great detail, and the truly motivatedcan read manpages, existing parallel applicationsusing these primitives, as well as the source code ofthe Linux-kernel implementations themselves.Quick Quiz 3.6:<strong>If</strong> the mythread() function in Figure 3.5 can simplyreturn, why bother with pthread_exit()?Answer:In this simple example, there is no reason whatsoever.However, imagine a more complex example,where mythread() invokes other functions,Quick Quiz 3.8:<strong>What</strong> if I want several threads to hold the samelock at the same time?Answer:The first thing you should do is to ask yourself whyyou would want to do such a thing. <strong>If</strong> the answeris “because I have a lot of data that is read bymany threads, and only occasionally updated”, thenPOSIX reader-writer locks might be what you arelooking for. These are introduced in Section 3.2.4.Quick Quiz 3.9:Why not simply make the argument tolock_reader() on line 5 of Figure 3.6 be apointer to a pthread_mutex_t???Answer:Because we will need to pass lock_reader() topthread_create(). Although we could cast thefunction when passing it to pthread_create(),function casts are quite a bit uglier and harder toget right than are simple pointer casts.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!