Вы находитесь на странице: 1из 2

CS 4760

Assignment # 2

Operating Systems
Due Date: October 11, 2011

Important: Please do all assignments on hoare

Semaphores and Message Passing


The goal of this homework is to become familiar with semaphores in the Unix operating system. In addition, you will implement a concept and provide it as a statically linked library, and use it to solve the problem you solved in assignment 1. Create a library archive to implement a monitor. Since you do not have condition variables, you will have to implement those using semaphores. Problem: In many applications, we have a number of processes trying to write into a single le. If any two processes open a le to append something to the le (think of logs as an example), the exact timing at which the le is closed becomes extremely important. This is the critical section problem. For this assignment, we are going to simulate the writing of logs. In a nutshell, the problem can be specied as follows. Your job is obviously to write the code to simulate the above problem. The main program will fork a producer and n consumers, with n supplied as a command line parameter. Let the default for n be 10. Thus, your master process performs just forking and signal handling. You may also want to allocate/deallocate shared memory in the master process. The resources to be protected by functions inside monitor are the next buer number (string) to be checked in shared memory, and the log les. The process palin will, at random times, wake up and look for the next string to be checked. Obviously, buers (and associated ags) need to be allocated in shared memory. Make sure that the buers are readable by palin. Only one process should be able to read a buer at any time though ve processes can read ve buers simultaneously. You have to make sure that the synchronization between dierent processes is very well achieved. Make sure you never have more than 20 processes in the system at any time, even if the program is invoked with n being more than 20. Add the pid of the child to the le as comment in the log le. The preferred output format for log le is: PID Index String

where Index is the logical number for consumer process, assigned internally by your code, and varies between 0 and n 1. The child process will be execed by the command equivalent to the shell comand palin xx key where xx is the index number of the child process and key is the key to shared memory. We want to have three log les: one containing admin data such as the attempts to get into critical section, the second containing all palindroms, and the third containing all non-palindromes. Use the same specs for child processes as from previous assignment. The multiple processes compete to get exclusive access to the le to write into, participating in the race condition. They also compete to nd the next string to be processed. You will use the monitor you wrote to gain access to these resources such that only one process can see/modify the number or gain access to the les to write into those. In addition, the main process should print a message when an interrupt signal (^C) is received. Make sure that all the children/grandchildren are killed by main process when this happens, and all the shared memory is deallocated. The grandchildren kill themselves upon receiving interrupt signal but print a message on stderr to indicate that they are dying because of an interrupt, along with the identication information. Make sure that the processes handle multiple interrupts correctly. As a precaution, add this feature only after your program is well debugged.

Semaphores and Message Passing

Implementation
The code for and child processes should be compiled separately and the executables be called master and palin. Other points to remember: You are required to use fork, exec (or one of its variants), wait, and exit to manage multiple processes. Use shmctl suite of calls for shared memory allocation. Also make sure that you do not have more than twenty processes in the system at any time. This should be easy because you have static child processes to do the work in this project. What to handin Handin an electronic copy of all the sources, README, Makele(s), and results. Create your programs in a directory called username.2 where username is your login name on hoare. Once you are done with everything, remove the executables and object les, and issue the following commands: % cd % ~sanjiv/bin/handin cs4760 2 Do not forget Makefile (with sux rules), RCS, and README for the assignment. If you do not use RCS, you will lose 10 points. Omission of a Makefile (with sux rules) will result in a loss of another 10 points, while README will cost you 5 points. Print messages when a process waits for and acquires a semaphore into the log le.

Вам также может понравиться