Reimplement Exercise 4.11 to use multiple threads. Because the current working directory is a process attribute, it is shared by all threads, so you can’t use chdir(2). Instead, use the *at functions to allow 16 worker threads to process different parts of the ?le system at the same time. How much faster is this version compared to the version from Exercise 4.11?
What will be an ideal response?
Because the user-de?ned function passed to ftw is passed a full pathname, we must maintain the absolute pathname, so we really don’t need to use any *at functions. But that would be no fun, so we’ll use fstatat(2) instead oflstat(2). With multiple threads, we need to protect the data structures that the threads access. Weuse a mutex to protect the list of directories to scan, and a condition variable for the threads to wait for work. We also use a spin lock to protect the counters we keep for the ?le types we encounter.The main thread starts things offand then waits for the worker threads to complete. The solution is shown in Figure33. When comparing its performance to that of the program from Exercise 4.11, you should ?nd that Exercise 4.11runs in less user time and less system time, but with moreelapsed (clock) time. ![12522|458x392](upload://9ggzPvP6UrrgjlNwzJs6H0EW7FR.png)
![12523|378x500](upload://8JJzdHFlYZmPU8cZlOT3RgKiqqp.png)
![12524|336x500](upload://xWz2NtaQ0C9LPBIYwjItd9NxX1X.png)![12525|349x500](upload://wqNC2Cm2C9SdAl0ua7HLgoOw2uY.png)
![12526|353x500](upload://dGk3q1ow7yqOt0sfELKen7nziFQ.png)
![12527|482x228](upload://d58Yu9lQn8pKdxUvuOXzd3RdfoL.png)
You might also like to view...
What is the average number of bookings for each hotel in August?
What will be an ideal response?
List the ways in which a website can be promoted and marketed to reach its target audience.?
What will be an ideal response?