Lab 2: Forks and Shared Memory
Due: Wednesday, February 1, 2012 by 10:00 PM
Assignment ID: lab2
Lab description
For this lab, complete Programming Problems 3.15 and 3.19. Name the source code file for Programming Problem 3.15 lab2-fork.cpp and for 3.19 lab2-sharedMemory.cpp.Example Execution
$ ./lab2-fork 23 && ./lab2-sharedMemory 25 Fork example: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711 Shared Memory example: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368
Submission
Submit the source code files using the handin program. For example, for this lab, type the following in a terminal exactly as it appears:handin lab2 lab2-fork.cpp lab2-sharedMemory.cpp
Alternatively, you can submit the assignment from
Rubric:
Points Item ---------- -------------------------------------------------------------- lab2-fork (50 points) _____ / 15 Program compiled properly (on ranger2) _____ / 15 Proper use of fork _____ / 15 Proper use of wait _____ / 5 Executed properly lab2-sharedMemory (50 points) _____ / 10 Program compiled properly (on ranger2) _____ / 10 Setting up shared memory (creating, attaching) _____ / 10 Child process writes to shared memory _____ / 10 Parent process read from shared memory _____ / 5 Dettaches shared memory segment _____ / 5 Executed properly _____ / 100
Hints
- If you get an error like:
error: 'shmget' was not declared in this scope
orerror: 'S_IRUSR' was not declared in this scope
use man (e.g., man shmget) or a search engine to determine the appropriate header file to include. - shmget() allocates a specified amount of memory. shmat() returns a pointer to that memory. You set the pointer type based on how you're going to use that memory. Exercise 3.19 dictates what kind of datatype to use.
- sprintf() is only for char array. You don't need it for this lab.
- If you're having troubles with the shared memory program, temporarily comment out the multi-process code and just write the Fibonacci sequence to shared memory and read it from shared memory (all in a single process). Once you're successful at writing to and reading from the shared memory, add back in the complexity of the child process.