Exercises

11.1 A shared memory segment cannot be extended. Write a C program that has, as an argument, an attached shared memory segment and some data. This program will create and attach a new and bigger shared memory segment. It will copy the data from the old shared memory segment to the new one and will also store there the additional data. It will then detach from the old memory segment and destroy it. It essentially does the same as realloc() when that function cannot physically extend a memory segment. Here, though, there are some synchronization problems that realloc() does not have to deal with. First, your program must "lock" the segment for the duration so that no other process can use it. Then it must make sure that all other processes know that the segment "has moved" and "where". Try to complete this exercise using semaphores and shared memory segments only.

11.2 Write a simple C program that forks one or more child processes. Use the logging functions from Appendix D, but do not create a shared memory segment for logi . Create a single log before the very first fork() so that all the child processes will inherit it. Check the log and the sequential numbers of messages from all the processes involved.

11.3 Repeat Exercise 11.2 with one change: let the program create a shared memory segment before the first fork() and let logi point to it. Check the log and the sequential numbers of messages from all the processes involved. Compare this with the log from Exercise 11.2.

11.4 Repeat Exercise 11.2 with the following change: each child process will open its own and different log after the fork() . Compare the logs.

11.5 Write a simple multithreaded C program in which one thread creates a dynamic object and a different thread deallocates it. What kind of "communication" must take place?

11.6 Write a simple multithreaded C++ program using smart pointers that are "thread aware" - that is, deallocation takes place only in the thread that is the "owner".

11.7 Write a simple multithreaded C program in which some linked data structure is built and then the data structure is destroyed . Deliberately write the program so that a memory leak can occur. Your program should be a more sophisticated version of the example from this chapter.



Memory as a Programming Concept in C and C++
Memory as a Programming Concept in C and C++
ISBN: 0521520436
EAN: 2147483647
Year: 2003
Pages: 64

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net