5 This assignment is designed to help you:
6 1. get familiar with Linux system,
7 2. get familiar with C++ programming,
8 3. get familiar with system calls related to process management.
9 You are required to implement your solution in C++ (other languages are not allowed). Your
10 work will be tested on the Linux server cs3103-01.cs.cityu.edu.hk.
12 Be sure to study the man pages for the various systems calls and functions suggested in this
13 assignment. The system calls are in Section 2 of the man pages, so you should type (for example):
14 $ man 2 waitpid
15 2 Requirements
16 2.1 Prompt for user input
17 Your PMan needs to show the prompt for user input. For example, when you run PMan by type in
18 $ ./PMan
19 It prompts
20 PMan: >
21 for user input.
22 2.2 Background Execution of a Program
23 PMan allows a program to be started in the background—that is, the program is running, but PMan
24 continues to accept input from the user. You will implement a simplified version of background
25 execution that supports executing processes in the background.
26 If the user types: bg foo, PMan will start the program foo in the background and print out
27 the process id (pid) of foo. That is, the program foo will execute, and PMan will first print out
28 the pid of foo and then continue to execute and give the prompt to accept more commands.
29 To simplify your task, PMan only allows one program running in the background.
30 For instance, if the background process foo is still running, PMan will not accept any further bg
31 command. In other words, if there is a background process is still running, when the user types bg
32 test2, PMan should print There is a background process still running.
33 Your PMan needs to support the following commands:
34 1. The command bgkill pid will send the TERM signal to the background process with process
35 ID pid to terminate that process.
36 2. The command bgstop pid will send the STOP signal to the background process with process
37 ID pid to stop (temporarily) that process.
38 3. The command bgstart pid will send the CONT signal to the background process with process
39 ID pid to re-start that process (which has been previously stopped).
40 4. The command exit will execute bgkill pid, if there is any background process, and then
42 Note: (1) If pid is invalid, PMan should print The pid is not a valid process id of a
43 background process. (2) After you use bgkill to terminate the background process, PMan
44 should able to run bg again because there is no background process running.
45 See the man page for the kill() system call for details.
46 To summarize, your PMan should support the following commands: bg, bgkill, bgstop,
47 bgstart, and exit. If the user types an unrecognized command, an error message is given by
48 PMan, e.g.,
49 PMan:> ttest
50 PMan:> ttest: command not found
51 3 Odds and Ends
52 3.1 Implementation Hints
53 1. Use fork() and execvp() so that the parent process accepts user input and the child process
54 executes the background process.
55 2. Use a variable bpid to record the pid of the background process, and use a variable status
56 to record the status of the background process (i.e., terminated, stopped, running).
57 3.2 Helper programs
59 1. This demo program can be used to act as a background process for testing your PMan as its
60 execution can be visualized by displaying a word every few seconds.
61 2. This program takes three arguments, word, interval, and times.
62 3. The first argument word is a single word to be displayed repeatedly.
63 4. The second argument interval is the number of seconds between two consecutive displays of
64 the word.
65 5. The third argument times is the number of times the word to be displayed.
66 6. For example, the following command displays the word running 10 times in 2-second interval.
67 PMan:> bg demo running 2 10
69 1. This example program shows how to display a list of all arguments passed to it.
70 2. To compile the program, use the following command:
71 cs3103-01:/home/lec/vlee> g++ args.cpp -lreadline -lhistory -o args
72 3.3 Warning
73 Since you will use fork() in your assignment, it is important that you do not create a fork()
74 bomb, which easily eats up all the pid resources allocated to you.
75 If this happens, you can try to use the command “kill” to terminate your processes
76 (http://cslab.cs.cityu.edu.hk/supports/unix-startup-guide). However, if you cannot log
77 into your account any more, you need to ask CSLab for help to kill your processes.
78 3.4 Code Quality
79 We cannot specify completely the coding style that we would like to see but it includes the following:
80 1. Proper decomposition of a program into subroutines (and multiple source code files when
81 necessary)—A 500 line program as a single routine won’t suffice.
82 2. Comment—judiciously, but not profusely. Comments also serve to help a marker, in addition
83 to yourself. To further elaborate:
84 (a) Your favorite quote from Star Wars or Douglas Adams’ Hitch-hiker’s Guide to the Galaxy
85 does not count as comments. In fact, they simply count as anti-comments, and will result
86 in a loss of marks.
87 (b) Comment your code in English. It is the official language of this university.
88 3. Proper variable names—leia is not a good variable name, it never was and never will be.
89 4. Small number of global variables, if any. Most programs need a very small number of global
90 variables, if any. (If you have a global variable named temp, think again.)
91 5. The return values from all system calls and function calls listed in the assignment
92 specification should be checked and all values should be dealt with appropriately.
93 4 Marking
94 Your program will be tested on our CSLab Linux servers (cs3103-01, cs3103-02, cs3103-03). You
95 should tell TA how to compile and run your code in your text file. TAs are not supposed to fix the
96 bugs, either in your source code or in your make file. If an executable file cannot be generated and
97 running successfully on our Linux servers, it will be considered as unsuccessful.
Table 1: Marking scheme.
Error handling 5%
programming style and in-program comments 10%
98 5 Submission
99 1. This assignment is to be done individually or by a group of two students. You are encouraged
100 to discuss the high-level design of your solution with your classmates but you must implement
101 the program on your own. Academic dishonesty such as copying another students work or
102 allowing another student to copy your work, is regarded as a serious academic offence.
103 2. Each submission consists of two files: a source program file (.cpp file) and a text file containing
104 all possible outputs produced by your program (.txt file).
105 3. Use your student ID(s) to name your submitted files, such as 5xxxxxxx.cpp, 5xxxxxxx.txt for
106 individual submission, or 5xxxxxxx 5yyyyyyy.cpp, 5xxxxxxx 5yyyyyyyy.txt for group sub107 mission. Only ONE submission is required for each group.
108 4. Submit the files to Canvas.
109 5. The deadline is 10:00 a.m., 21-FEB-19 (Thursday). No late submission will be accepted.
111 Contact Miss LIANG, Yu at email@example.com or your course lecturer
112 The End