Information Technology and Electrical Engineering
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
Information Technology and Electrical Engineering
CSSE2310/CSSE7231
Assignment 3 (version 1.0)
Marks: 75 (for CSSE2310), 85 (for CSSE7231)
Weighting: 15%
Due: 4:00pm Friday 13 May, 2022
Introduction 1
The goal of this assignment is to demonstrate your skills and ability in fundamental process management and 2
communication concepts, and to further develop your C programming skills with a moderately complex program. 3
You are to create two programs – the first is called sigcat which is like the Unix utility cat, however it 4
has enhanced signal handling functionality. The second, and major program, is called hq, and it is used to 5
interactively spawn new processes, run programs, send and receive output and signals to those process, and 6
manage their lifecycle. The assignment will also test your ability to code to a programming style guide, to use 7
a revision control system appropriately, and document important design decisions (CSSE7231 only). 8
Student Conduct 9
This is an individual assignment. You should feel free to discuss general aspects of C programming and 10
the assignment specification with fellow students, including on the discussion forum. In general, questions like 11
“How should the program behave if 〈this happens〉?” would be safe, if they are seeking clarification on the 12
specification. 13
You must not actively help (or seek help from) other students or other people with the actual design, structure 14
and/or coding of your assignment solution. It is cheating to look at another student’s assignment code 15
and it is cheating to allow your code to be seen or shared in printed or electronic form by others. 16
All submitted code will be subject to automated checks for plagiarism and collusion. If we detect plagiarism or 17
collusion, formal misconduct actions will be initiated against you, and those you cheated with. That’s right, if 18
you share your code with a friend, even inadvertently, then both of you are in trouble. Do not post your 19
code to a public place such as the course discussion forum or a public code repository, and do not allow others 20
to access your computer - you must keep your code secure. 21
Uploading or otherwise providing the assignment specification or part of it to a third party including online 22
tutorial and contract cheating websites is considered misconduct. The university is aware of these sites and 23
they cooperate with us in misconduct investigations. 24
You must follow the following code referencing rules for all code committed to your SVN repository (not 25
just the version that you submit): 26
Code Origin Usage/Referencing
Code provided to you in writing this semester by
CSSE2310/7231 teaching staff (e.g. code hosted on Black-
board, posted on the discussion forum, or shown in class).
May be used freely without reference. (You must be able
to point to the source if queried about it.)
Code you have personally written this semester for
CSSE2310/7231 (e.g. code written for A1 reused in A3)
May be used freely without reference. (This assumes
that no reference was required for the original use.)
Code examples found in man pages on moss.
May be used provided the source of the code is
referenced in a comment adjacent to that code.
Code you have personally written in a previous enrolment
in this course or in another ITEE course and where that
code has not been shared or published.
Code (in any programming language) that you have taken
inspiration from but have not copied.
Other code – includes: code provided by teaching staff only
in a previous offering of this course (e.g. previous A1 solu-
tion); code from websites; code from textbooks; any code
written by someone else; and any code you have written
that is available to other students.
May not be used. If the source of the code is referenced
adjacent to the code then this will be considered code
without academic merit (not misconduct) and will be
removed from your assignment prior to marking (which
may cause compilation to fail). Copied code without
adjacent referencing will be considered misconduct and
action will be taken.
27
6959462-30065-6649710
The course coordinator reserves the right to conduct interviews with students about their submissions, for 28
the purposes of establishing genuine authorship. If you write your own code, you have nothing to fear from this 29
process. If you are not able to adequately explain your code or the design of your solution and/or be able to 30
make simple modifications to it as requested at the interview, then your assignment mark will be scaled down 31
based on the level of understanding you are able to demonstrate. 32
In short - Don’t risk it! If you’re having trouble, seek help early from a member of the teaching staff. 33
Don’t be tempted to copy another student’s code or to use an online cheating service. You should read and 34
understand the statements on student misconduct in the course profile and on the school web-site: https: 35
//www.itee.uq.edu.au/itee-student-misconduct-including-plagiarism 36
Specification – sigcat 37
sigcat reads one line at a time from stdin, and immediate writes and flushes that line to an output stream. 38
The output stream by default is stdout, however the output stream can be changed at runtime between stdout 39
and stderr by sending sigcat the SIGUSR1 and SIGUSR2 signals. 40
Full details of the required behaviour are provided below. 41
Command Line Arguments 42
Your program (sigcat) accepts no commandline arguments. 43
./sigcat 44
Any arguments that are provided can be silently ignored. 45
sigcat basic behaviour 46
Upon starting, sigcat shall enter an infinite loop reading newline-terminated lines from standard input, and 47
emitting them to an output stream. It is assumed that the data read from standard input is non-binary, i.e. 48
does not contain null (\0) characters. 49
• At program start, the output stream is to be standard output stdout. 50
• sigcat shall remain in this loop until it receives end of file on stdin. 51
• Upon reaching EOF on stdin, sigcat shall exit with exit code 0. 52
• No further error checking is required in sigcat. 53
sigcat signal handling 54
sigcat shall register a handler or handlers for all signals of numeric value 1 through to 31 inclusive – except 9 55
(KILL) and 19 (STOP) which are not able to be caught. 56
Upon receiving a signal, sigcat is to emit, to the current output stream, the following text: 57
58
sigcat received 59
where is replaced with the signal name as reported by strsignal() (declared in
). 60
Note that this line is terminated by a newline and sigcat must flush its output buffer after every emitted line 61
of text. 62
Examples include: 63
sigcat received Quit
sigcat received Hangup
The signals SIGUSR1 and SIGUSR2 have special meaning to sigcat. After printing the output as specified 64
above, upon receipt of either of these signals sigcat shall further 65
• set its output stream to standard output (stdout) if SIGUSR1 is received 66
• set its output stream to standard error (stderr) if SIGUSR2 is received 67
In this way, sigcat can be instructed to direct its output to either stdout or stderr by sending it the 68
appropriate signals. 69
6959462-30065-6649710
Specification – hq 70
hq reads commands from its standard input one line at a time. All of hq’s output goes to stdout– and all 71
commands are terminated by a single newline. The commands are documented below, and allow the user to 72
run programs, send them signals, manage their input and output streams, report on their status and so on. 73
Full details of the required behaviour are provided below. 74
Command Line Arguments 75
hq accepts no commandline arguments. 76
./hq 77
Any arguments that are provided may be silently ignored. 78
hq basic behaviour 79
hq prints and flushes a prompt “> ” (greater than symbol followed by a single space) to stdout then waits to 80
read a command from stdin. 81
The following table describes the commands that must be implemented by hq, and their syntax. Additional 82
notes on each command will follow. 83
Command Usage Comments
spawn spawn [][] ...
Run in a new process, optionally the with ar-
guments provided. Arguments or program names containing
spaces may be quoted in double quotes. The new process’s
stdin and stdout must be connected to hq by pipes, so that
they can be accessed with the send and rvc commands. The
new process’s stderr should be unchanged.
report report [] Report on the status of or all jobs if no argumentprovided
signal signal Send the signal ( – an integer) to the given job()
sleep sleep Cause hq to sleep for the number of seconds specified. may be fractional, e.g. sleep 1.5
send send Send to the job. Strings containing spaces must bequoted with double quotes
rcv rcv Attempt to read one line of text from the job specified anddisplay it to hq’s stdout
eof eof Close the pipe connected to the specified job’s stdin, thus caus-ing that job to receive EOF on its next read attempt.
cleanup cleanup Terminate and reap all child processes spawned by hq by sendingthem signal 9 (SIGKILL).
84
The following apply to all command handling and inputs: 85
• Upon reaching EOF on stdin, hq shall terminate and clean up any jobs (see the cleanup command 86
below), and exit with status 0. 87
• hq shall block or otherwise ignore SIGINT (Control-C). 88
• Any invalid commands provided to hq (i.e. a word at the start of a line is not one of the above) shall 89
result in the following message to stdout: 90
Error: Invalid command
Note that empty input lines (user just presses return) shall just result in the prompt being printed again. 91
• All commands have a minimum number of arguments (possibly zero such as for report and cleanup). 92
If this minimum number of arguments is not provided, the following error message shall be emitted to 93
standard output: 94
Error: Insufficient arguments
6959462-30065-6649710
Extraneous arguments, if provided, shall be silently ignored. 95
• All numerical arguments, if present, must be complete and valid numbers. e.g. “15” is a valid integer, 96
but “15a” is not. Similarly, “2.7” is a valid floating point value, but “2.7foobar” is not. Your program 97
must correctly identify and report invalid numerical arguments (see details below for each command). 98
• Any text arguments, including strings and program names, may contain spaces if the argument is sur- 99
rounded by double quotation marks, e.g. "text with spaces". A helper function is provided to assist 100
you with quote-delimited parsing, see the “Provided Library” section on page 9 for usage details. 101
• One or more spaces may be present before the command and there may be more than one space between 102
arguments. The provided helper function will deal with this for you. 103
• Where a command takes a jobID argument then a valid jobID is the ID of any job created using spawn– 104
even if the execution failed or the job has exited. 105
spawn 106
The spawn command shall cause hq to fork() a new process, setup pipes such that the child’s stdin and 107
stdout are directed from/to hq, and exec() the requested program. The $PATH variable is to be searched for 108
executable programs. 109
Each spawned process is to be allocated an integer job identifier (jobID), starting from zero. Jobs are created 110
and job IDs should increment even if the exec() call fails. 111
hq should emit the following to stdout: 112
New Job ID [N] created
where N is replaced by the integer value, e.g. 113
New Job ID [34] created
If at least one argument is not provided (the program name), then spawn shall emit the following message: 114
Error: Insufficient arguments
If the exec() call fails then your child process is to exit with exit status 99. 115
report 116
The report command shall output information on all jobs spawned so far, with a header row and in the following 117
format: 118
> report
[Job] cmd:status
[0] cat:running
[1] ls:exited(0)
[2] sleep:signalled(9)
...
The cmd field shall be the name of the job (the value of the first argument given to the spawn command). 119
The status field shall be one of running – if the job has not yet exited; exited – if the job has terminated 120
normally – with the exit status shown in parentheses; or signalled – if the job terminated due to a signal – 121
with the signal number shown in parentheses. Jobs are to be reported in numerical order. 122
report may optionally take a single integer argument, which is a job ID. If provided, and valid, then only 123
the status of that job shall be reported. (The header line is also output.) 124
> report 1
[Job] cmd:status
[1] ls:exited(0)
If an invalid job ID is provided (i.e. non-numerical value or job was never spawned), then an error message 125
is to be emitted to standard output as demonstrated below: 126
6959462-30065-6649710
> report 45
Error: Invalid job
> report abc
Error: Invalid job
signal 127
The signal command shall cause a signal to be sent to a job. Exactly two integer arguments must be specified 128
– the target job ID, and the signal number. If fewer arguments are provided, the following error is emitted: 129
Error: Insufficient arguments
If the job ID is invalid, emit: 130
Error: Invalid job
If the signal number is invalid (non-numeric, less than 1 or greater than 31): 131
Error: Invalid signal
If all arguments are valid, the signal shall be sent to the targetted job. (There is no need to check whether 132
the job is still running.) 133
sleep 134
The sleep command shall cause the hq program to sleep for the number of seconds specified in its single 135
mandatory argument. Non-integer numbers, such as 1.5, are considered valid1. 136
If no duration is provided, emit the error message: 137
Error: Insufficient arguments
If a negative or non-numerical duration is provided, emit the error message to stdout: 138
Error: Invalid sleep time
send 139
The send command shall send a single line of text, whose contents are the second argument to the command, to 140
the identified job. Send requires exactly two arguments, the job ID and the string to be sent. Because arguments 141
are separated by spaces, to send a line containing spaces, the text must be contained in double quotes. A helper 142
function is provided to assist you with quote-delimited parsing, see see the “Provided Library” section on page 143
9 for usage details. 144
If less than two arguments are provided, send shall emit: 145
Error: Insufficient arguments
If an invalid job ID is provided, emit the error message: 146
Error: Invalid job
Example of usage: 147
> send 0 "hello job, quotes matter!"
> send 2 textwithoutspaces
> send 1 ""
> send 4 text with spaces but these extra words are all ignored
It is possible that the receiving process has exited – it is your job to manage any SIGPIPE signals so that 148
your program does not terminate in this situation. You are not required to detect or report if a job specified in 149
a send command is in this state – simply ensure that hq continues to run. 150
1The strtod() function may prove useful here
6959462-30065-6649710
rcv 151
rcv shall attempt to read one line of text from the identified job, and print it to standard output. One mandatory 152
argument is required – the job ID. If not specified, then emit 153
Error: Insufficient arguments
If an invalid job ID is specified, emit 154
Error: Invalid job
rcv must not block if there is no output available from the job. To facilitate this, a helper function 155
is_ready() is provided – see the “Provided Library” section on page 9. 156
If there is no output available to read from the job, rcv shall emit 157
If end-of-file is (or has previously been) received from the pipe connected to the job, then rcv shall emit 158
Otherwise, rcv shall emit to standard output, the line of text read from the job, e.g. 159
> rcv 0
Hello from the job!
eof 160
The eof command shall close the stdin of the given job. One mandatory argument is required – the job ID. If 161
not specified, then emit 162
Error: Insufficient arguments
If an invalid job ID is specified, eof shall emit 163
Error: Invalid job
If the job ID is valid, hq does not output anything. 164
cleanup 165
The cleanup command shall send the SIGKILL (numerical value 9) signal to all jobs and wait() on them to 166