Unix Systems Programming (COMP2002)
Trimester 2A, 2025
Assignment
Due: Monday, 22 September 2025, 10:00 AM (GMT+8)
Weight: 20% of the unit
1 Academic Integrity
This is an assessable task, and as such there are strict rules. You must not ask for or accept help from anyone else on completing the tasks. You must not show your work at any time to another student enrolled in this unit who might gain unfair advantage from it. These things are considered plagiarism or collusion. To be on the safe side, never release your code to the public.
Do NOT just copy some C source codes from internet resources (including AI tools such as ChatGPT). If you get caught, it will be considered plagiarism. If you put the reference, then that part of the code will not be marked since it is not your work.
Staff can provide assistance in helping you understand the unit material in general, but nobody is allowed to help you solve the specific problems posed in this assign- ment. The purpose of the assignment is for you to solve them on your own, so that you learn from it. Please see Curtin’s Academic Integrity website for information on academic misconduct (which includes plagiarism and collusion).
The unit coordinator may require you to attend quick interview to answer questions about any piece of written work submitted in this unit. Your response(s) may be referred to as evidence in an Academic Misconduct inquiry. In addition, your assign- ment submission may be analysed by systems to detect plagiarism and/or collusion.
2 Task Details
2.1 Task Summary
In summary, these are the tasks you need to complete:
• Perform file IO operations on a text file with system calls and retrieve integers from it.
• Store the integers in 1D array. (You can store them as integers or string)
• These integers will be tested for primality. (check whether they are prime num- ber or not)
• The program creates one child process for each integer. For example, if there are five integers to be tested, then the parent process will create five child processes.
• Parent process sends each integer to corresponding child process via pipes that were prepared in advance.
• Each child process read the integer from the pipe, and then test the number for primality.
• The primality test result is sent back to the parent process. (child process com- pletes the task)
• Parent process waits for each child process to finish and retrieve the primality results.
• Parent process prints out the result from each children. (Output shows which child process return which result)
• Parent process then clears all the remaining resources before ending the pro- gram.
You need to use the system calls open(), close(), read(), write(), pipe(), fork(), wait() and nanosleep() to achieve the objective. Some library functions are not allowed in this assignment.
2.2 Prohibited Library Functions
Since you need to use system calls open(), close(), read(), and write(), any closely- related library functions are not allowed. This is the list of banned library functions:
• printf(), scanf(), sprintf(), and sscanf()
• fopen() and fclose()
• fprintf(), fscanf(), fgetc(), fgets(), fputc(), fputs() (basically all library functions related to file IO)
• atoi() (function to convert string to integer) (If you need this, write your own function)
• Anything from <string.h> such as strlen(), strcpy(), strstr(), strcat(), strcmp(), strncmp(), etc. If you need to manipulate string, you need to write your own function.
2.3 Allowed Library Functions
You are allowed to use:
• malloc(), calloc(), and free() functions to allocate memories (especially for an array).
• sqrt() function from to calculate square root.
• exit() function for the child processes when they finish the task. (Please free any resource used by child process beforehand)
2.4 Error/Failure Checking
You need to check for error/failure on pipe(), fork() and open() system calls.
2.5 Command Line Arguments
Please ensure that the executable is called “prime”. Your executable should accept only one command-line parameter/argument:
./prime <file_name>
is the text file name containing the data you need to extract. It is your responsibility to check whether the file can be opened properly. You can assume the content of the file is valid. If the amount of the command line arguments are too many or too few, your program should print a message on the terminal and end the program accordingly.
2.6 File I/O
The input text file will look like this:
Figure 1: Content of the numbers text file.
The first integer on the first line indicates the amount of the remaining integers. The remaining of the file consists of integers to be tested for primality. You need to use open(), close(), and read() system calls for this task. It is recommended to read it one character at a time and then process it. Please store all the integers in a 1D array (you can choose to store it as int OR char * (string)).
Note: Since you cannot use fscanf(), you cannot read the integer easily with %d placeholder. You need to write your own algorithm to form the integer from each digit you read from the text file.
2.7 Preparing pipes
You will need to create pipes as a communication channel between the parent and each child process. Since we require two-way communication, it is recommended to create two pipes per child: one for data transfer from parent to child, and one for child to parent. For example, if you create 5 child processes, then you will need 5 pairs of pipes. All pipes should be created before calling fork().
2.8 Child Processes and Data Transfer
This part of the assignment involves simultaneous execution of both parent process and child process. Here are the main steps for both of them:
2.8.1 Parent Process
• For each integer in 1D array (from file IO in previous step), create one child process.
• Parent process will receive the Process ID (PID) of the child process from fork(). It may be beneficial to keep track of these PIDs.
• Parent process then proceed to send the integer via write() to the pipe associated with that specific child process.
• This previous step is repeated until all integers were sent. (to each correspond- ing child process)
• Parent process then wait() for any of the child process to terminate.
• wait() system call will return the Process ID (PID) of the terminated child pro- cess. Parent process use this information to read the response from the correct pipe.
• Parent process then display the response to stdout with write() along with the information which child process completed the task.
• These wait() steps are repeated until all child processes are terminated. After- wards, parent process can proceed to conclude the program after clearing any used resource.
Figure 2: Parent process receives results from child process and displays the result.
2.8.2 Child Process
• Child process read() from the pipe to receive the integer.
• Primality test is done on that received integer. (see section 2.9)
• Child process then return the result back to parent process via pipe. You can choose any response as long as parent process understands the meaning. (For example, you can send "0" for non-prime number, or "1" for a prime number.)
• Child process then end the process with exit(0). Please make sure to clear any resources used by the child process.
Note: Technically, the child process would have the access to the integer array. However, this assignment is designed to assess your ability to pass data from the parent process to the child process. Therefore, only the parent process is permitted to access the integer array.
Note: Since the pairs of pipes have been created beforehand, both parent process and all child processes will have their own copies of all the pipes. Please ensure that you close() all ends of the pipe that are no longer in use. This is important to ensure the read() does not block forever.
2.9 Primality Testing & Sleep
Each child process need to determine whether the number sent by the parent process is a prime number. You need to implement "Trial division to square root" function for this task (Please research this algorithm). The child process need to notify the parent process of the result via write(). For example, you can send "1" for a prime number, or "0" for a non-prime number. (Feel free to send different character as long as the parent process understand the meaning)
To simulate a more time-consuming task, please use nanosleep() system call to sus- pend the program shortly on each iteration of the primality test. (e.g 1 milisecond per iteration) By doing this, some child processes will take longer to finish the task, and as a result, respond slower to the parent process.
Note: Please research some examples on how to use nanosleep().
2.10 Makefile
You should manually write the makefile for this assignment. Feel free to use the makefile format learned in COMP1000 (Unix and C Programming), or use the new learned makefile features from this unit. Without the makefile, we will assume the program cannot be compiled.
2.11 Assumptions
For simplification purpose, these are assumptions you can use for this assignment:
• Input file contains valid datatype. In this case, all the data are integers.
• At least one integer for primality testing in the text file.
• Only positive integers. Therefore, you can assume there is no negative character ’ ’
• The text file will use Linux newline ’\n’ (NOT Windows newline "\r\n")
• There is no duplicate integer in the text file.
• The integers are less than the maximum value in 32-bit signed integer 2147483647.
2.12 Advice/Hints
Here are some key points to consider when attempting this assignment:
• Trial by division to square root is an effective algorithm for primality test. To optimize a bit further, feel free to add quick IF statements for small numbers such as 1 (not prime) or 2 (prime).
• You are required to stored all integers you read from the text file in an array. You can choose to store them as integer (int) or string (char *). Keep in mind that you cannot use atoi() function and any standard String library functions. You need to write your own function.
• The last line on the text file does not contain newline ’\n’ (instead read() will encounter EOF). If your algorithm relies on ’\n’ to separate the integers, then this is something to pay attention on.
• If you use any function, please do not forget to link the library man- ually with -lm.
• For simplicity, child process can use exit(0) function when the task is completed. Please make sure to release all the resources and close the pipe correctly before- hand.
• Larger integers do not necessarily require more time for primality testing. If a number has a small factor such as 2, the test terminates much earlier. However, if the number is prime, larger integer will take longer to verify.
• It is a good practice to close pipe descriptors that are no longer in use by your process. This can prevent read() system call to block indefinitely.
3 Short Report
Please write a short report answering these questions related to the design choice of your program. Your answer should refer to parts of your code that achieves the objective.
• How does your program read() and parse the integers correctly from the text file and store them into 1D array? (Remember, you cannot use fscanf())
• How does your program create and organise the pipes? How do the parent and child processes know which pipe to use?
• How do the child processes know when to stop read()-ing? How do they know there is no more number coming from the parent? What does your program do to ensure the read() does not block forever?
• How does the parent process keep track which child has been terminated and read() the primality test result from the correct pipe?
You can submit the report as PDF or TXT file along with your source code.
4 Marking Criteria
This is the marking distribution for your guidance:
• (5 marks) Have a proper makefile and it can compile and run the program.
• (5 marks) No memory leaks. (You can use valgrind –trace-children=yes ./prime)
• (5 marks) Proper error handling on incorrect command line arguments amount, and issues on read(), fork(), and pipe().
• (30 marks) Integers from the text files are read correctly and stored properly in an array. This includes closing the file correctly.
• (5 marks) Pipes creation is done correctly with proper error handling.
• (10 marks) The parent process creates the correct number of child processes and transfers the integer through the appropriate pipe. Parent process should close() the unused end of the pipes.
• (10 marks) Child process read the integer correctly and return the primality test result correctly to parent process. Child process should close() the unused end of the pipes. Child process’s read() should not get blocked from other child
process.
• (10 marks) "Trial division up to square root" primality test algorithm is im- plemented correctly. This includes using nanosleep() reasonably to simulate time-consuming task.
• (10 marks) Parent process wait for all child processes to terminate, and then display the results on stdout (terminal).
• (10 marks) A short report containing clear and complete answers, with refer- ences to the appropriate sections of the code. A generic answer without refer- ence to your code will not receive mark.
Note: If you use prohibited library functions on specific task, that task will not receive any mark.
5 Final Check and Submission
After you complete your assignment, please make sure it can be compiled and run on our Linux lab environment. If you do your assignment on other environments (e.g on Windows operating system), then it is your responsibility to ensure that it works on the lab environment. In the case of incompatibility, it will be considered “not working’‘ and some penalties will apply. You have to submit a single zip file containing ALL the files in this list:
• Declaration of Originality Form. – Please fill this form digitally and submit it. This is an important document for assignment submission.
• Your essential assignment files – Submit all the .c & .h files and your makefile. Please do not submit the executable and object (.o) files, as we will re-compile them anyway. Do not create any sub-directory within the directory. Every file should exist within the same directory.
• Brief Report - Short report answering the questions. Please save it as a PDF or TXT file.
Note: Please compress all the necessary files into a SINGLE zip file. So, your submission should only has one zip file. If you do not compress the files, we reserve the right to refuse your submission.
The name of the zipped file should be in the format of:
___Assignment.zip
Example: Antoni_12345678_Will-Smith_Assignment.zip
If you forget your tutor’s name, please put the lecturer’s name. Please make sure your submission is complete and not corrupted. You can re-download the submission and check if you can compile and run the program again. Corrupted submission will receive instant zero. Late submission will receive zero mark. You can submit it multiple times, but only your latest submission will be marked. Therefore, please make sure the latest submission is the complete code.