Program 5 - Processes and Signals (100 points) COMPSCI 253: Intro to Systems Programming 1 Objectives Using system calls to create and manage processes under Linux and Microsoft Windows Using Visual Studio on Microsoft Windows 2 Introduction In this assignment, you will develop a simple systems program that allows you to experiment with various system calls on Linux and Microsoft Windows. 3 Specification 3.1 Linux (50 points) We will develop the program in two phases. Waiting for Godot! Write a program that creates as many processes as the number of CPUs on your system (using the fork system call). Each created process generates 300 million random integers using the random() function. Set the seed on each process using srandom() with the process id (obtained using getpid()). We will use the random numbers for something inane: count how many numbers were within the range 90..110. After generating 300 million random numbers, each process sleeps for 5 seconds. Each created process repeats this calculation followed by the sleep ad nauseum. Watch the load using the system monitor. The number of CPUs can be determined via the following system call: sysconf( SC NPROCESSORS CONF) Struck by lightning while waiting for Godot! Take the same program and add an alarm interval as a command line argument, where godot is the name of the program executable: godot <alarm interval> The program now sets up a signal handler and an alarm. When alarm arrives, it kills all the running child processes using the kill() system call. Also add a loop at the end of the main program that uses the waitpid() system call to determine what signal caused the child process to terminate. Please print the full information on the signal using the strsignal call (check its man page). Note that the alarm timer isn t inherited by the child processes. 1
3.2 Sample Output (on Linux) Here is the expected output (on Linux): [amit@kohinoor amit]: godot Usage: godot <alarm interval> [amit@kohinoor amit]: godot 20 original process, pid = 10127 0th child, pid = 10128 1th child, pid = 10129 2th child, pid = 10130 3th child, pid = 10131 Process 10129 finished round 0 count = 5 Process 10128 finished round 0 count = 2 Process 10130 finished round 0 count = 3 Process 10131 finished round 0 count = 3 Process 10129 finished round 1 count = 5 Process 10128 finished round 1 count = 3 Process 10130 finished round 1 count = 3 Process 10131 finished round 1 count = 7 Process 10129 finished round 2 count = 9 Process 10128 finished round 2 count = 3 Process 10130 finished round 2 count = 4 Process 10131 finished round 2 count = 11 Received alarm signal! pid = 10128 0th child killed with signal 9 (Killed) pid = 10129 1th child killed with signal 9 (Killed) pid = 10130 2th child killed with signal 9 (Killed) pid = 10131 3th child killed with signal 9 (Killed) [amit@kohinoor amit]: 3.3 MS Windows API (50 points) Rewrite the same program using MS Windows API. Note that all the examples for the MS Windows API are in the folder: lab/ms-windows/files-processes Make sure you update them through svn if you have them already checked out. Create a new Visual Studio project named godot. To find out the number of CPUs on your system, use the following call: GetSystemInfo(...) The MS Windows API doesn t have the random() and srandom() functions. Instead use the rand() and srand() functions. 2
Windows doesn t have a kill() system call like in Linux. So we will use the TerminateProcess() call to kill the processes and set an exit value. We can use GetExitCodeProcess() to get the exit code in the main process. We will arbitrarily use the exit code of 9 to denote termination of a child process by the parent. Note that the MS Windows API doesn t have an alarm signal. However, they do have a call CreateTimerQueueTimer(...) to create and use a timer. Check the example alarm-test.c and the example timeout.c in the folder lab/ms-windows/files-processes/ to see an example of using an alarm under the Windows API. Notes Make sure to follow the instructions in the lecture notes on setting the Visual Studio project. If you load one of the provided examples in Visual Studio and it doesn t compile or has warnings, then you did not set up the project properties correctly! The most common problem is to not have character setting be Multi-Byte (instead of the default Unicode). Note that if you use the Add Existing Item option to add a source file, then Visual Studio doesn t actually copy the file to the project. To copy the file to a project, copy the file using the File Explorer into the Visual Studio project. Back in Visual Studio, select Project Show All Files. Then go to the Solution Explorer pane, right click on the file you have added and choose Include File in Project option. Make sure to remove the large database file in the Visual Studio solution. This file is in your Visual Studio project folder and has an extension.sdf. How to stop Visual Studio from putting the large database file (with extension.sdf) inside your project. Go to Tools Options Text Editor C/C++ Advanced In the Fallback Location set Always Use Fallback Location to True and Do Not Warn If Fallback Location Used to True. In Fallback Location you can either put a path like C: Temp or if you leave it blank then Visual Studio will use the temporary directory in your AppData folder. 3
3.4 Sample Output on MS Windows Here is the expected output on MS Windows. Note that we are using the rand() random number generator on Windows, which generates numbers in the range [0... 32767]. Hence the counts are higher than using random() under Linux, which generates numbers in a much larger range. The following output was captured from running the executable from powershell. PS Z:\Shared\Visual Studio\godot\Debug>.\godot.exe Usage: godot <alarm interval> PS Z:\Shared\Visual Studio\godot\Debug>.\godot.exe 20 Number of processors: 4 Created 0th child, pid = 4188 Created 1th child, pid = 14656 Created 2th child, pid = 14568 Created 3th child, pid = 13944 Process 4188 finished round 0 count = 174215 Process 14656 finished round 0 count = 174990 Process 14568 finished round 0 count = 173415 Process 13944 finished round 0 count = 173786 Process 14656 finished round 1 count = 349279 Process 4188 finished round 1 count = 349549 Process 14568 finished round 1 count = 347307 Process 13944 finished round 1 count = 347448 Received alarm! pid = 4188 0th child killed with exit code 9 pid = 14656 1th child killed with exit code 9 pid = 14568 2th child killed with exit code 9 pid = 13944 3th child killed with exit code 9 PS Z:\Shared\Visual Studio\godot\Debug> 4 Documentation Include a unified README file at the top-level of your submission that describes the results and observations for the Linux and MS Windows version. 5 Submitting the Assignment Your executable should be called godot with the alarm interval as a command line argument. You should also have a Makefile that compiles and links your Linux program. The Makefile should be at the top-level of your submission. Prepare your directory for submission by removing all executables and object files. You should only have the source code, README file, Makefiles files before submitting. For the MS Windows version, please submit the full solution folder from Visual Studio after running clean on the project (without a rebuild). The executable name should still be godot 4
with the same command line argument. Put the Windows part in a separate subfolder named ms-windows. Make sure to remove the large database file with extension.sdf from the Visual Studio project before submitting! Put all the required files for the assignment in a directory (files are listed below): Makefile godot.c README ms-windows/ Change to that directory and submit using the command appropriate for your section from the table below. section submit command 1 submit spanter cs253 p5 2 submit marissa cs253-2 p5 3 submit amit cs253 p5 4 submit marissa cs253-4 p5 5