ASSIGNMENT 3
PART 1: In this assignment, you are going to create shared memory using shmget and mmap system calls.
Tasks
You have to complete the code for * implementing the server-shm.c and client-shm.c * implementing the writer-mmap.c and reader-mmap.c * handling errors wherever needed in the code
PART 2: Here, you are going to create threads, and learn how to pass arguments to and return results from threads. You have to implement the functions in thread.c to * create a thread in create_with_arg and pass the seconds argument to the thread routine in a heap allocated buffer, * implement the thread routine, which should sleep for the specified amount of seconds and should return 1000 + seconds in the heap allocated buffer, and * join with the specified thread in join_and_get_result, and return the value that the thread has calculated.
To complete the exercise you will need the manual for pthread_create and pthread_join. If you cannot access it, then you may need to install documentation for glibc using sudo apt-get install glibc-doc.
A test harness is already written for you in main.c. A correct execution of the test program will look something like this:
ghoshu@ghoshu-VirtualBox:~/cs3281/assignment-3/build$ ./main
creating thread 1 with argument 1
thread 1 (139796861376256) is succesfully created
creating thread 2 with argument 2
thread routine started sleeping for 1 seconds
thread 2 (139796852983552) is succesfully created
joining with thread 1 (139796861376256)
thread routine started sleeping for 2 seconds
thread routine finished sleeping for 1 seconds
join for thread 1 returned 1001
joining with thread 2 (139796852983552)
thread routine finished sleeping for 2 seconds
join for thread 2 returned 1002
Tasks
– Implement the create_with_arg function. Use a heap allocated buffer to pass the argument to the thread. Make sure that you check the return value of pthread_create and exit the process if it returns an error.
– Create the threading routine function that will be called in the new thread. The thread should print out the thread routine lines above in the sample output, sleep for the specified amount of time and then return 1000 + seconds in the same memory buffer it got its argument.
– Implement the join_and_get_result function. Make sure that you check the return value of pthread_join and properly handle the case when the thread was cancelled before join was called. Return the value that the thread has calculated.
– Make sure you are freeing the memory you used to communicate with the thread routine.
Evaluation
Your assignment will be graded according to the following criteria:
– 20 POINTS for correct implementation of server-shm.c and client-shm.c
– 10 POINTS for correct implementation of error handling in server-shm.c and client-shm.c
– 20 POINTS for correct implementation of writer-mmap.c and reader-mmap.c
– 10 POINTS for correct implementation of error handling in writer-mmap.c and reader-mmap.c
– 10 POINTS for a correct implementation of create_with_arg.
– 10 POINTS for a correct implementation of the thread routine function.
– 10 POINTS for a correct implementation of join_and_get_result.
– 10 POINTS for proper memory management (no race conditions and all buffers are freed).