CITS2002 Systems Programming  
 

Unit home

Final exam

help2002

Lecture & Workshop
recordings on LMS

Schedule

FAQ

C textbooks

OS textbooks

Information resources


Extra reading

Past projects

Recent feedback


Working effectively

Look after yourself!

Workshop 6: Creating and managing processes using system-calls

The following question, taken from the 2019 final examination, generated a lot of interesting discussion after the exam.

"and the exam just made me not ever want to play chess",   "...is gonna haunt us as long as we live",   "This unit needs to be fully externally audited",   "cops must have heard about that chess question in 2002".

For this week's workshop we'll tackle the problem (which, really, isn't that difficult!), and you don't even need to know how to play chess!


Q. Imagine that there exists a command-line program named goodchessmove that considers the state of an ongoing chess game, chooses a random piece belonging to the player whose turn it is next, and attempts to find a good legal move for that piece. goodchessmove employs a random, heuristic algorithm, and so different invocations may choose a different move for any one piece.

A typical command-line invocation of goodchessmove would be:

prompt> goodchessmove gamestate chosenmove

where gamestate is the name of a file containing the current state of the game (board positions, history of moves, whose turn it is, etc) and chosenmove is a file into which the program writes its output.

On a multi-core processor, several different instances of goodchessmove can run simultaneously and, if many instances are run over time, a different program could rank all output files to decide the best next move.


Write a C11 program to execute an indicated number of instances of goodchessmove on a multi-core computer.
A typical command-line invocation of this program would be:

prompt> manychessmoves 40 gamestate goodmove

which requests that 40 distinct instances of goodchessmove each determine a good next move for the game stored in the file named gamestate, and that the instances write their output to unique files named goodmove-1, goodmove-2, ... goodmove-40.

The manychessmoves program calls the library function:

int numberOfCores(void);

to determine how many cores are available on the current computer (1, 2, 4...) and attempts to keep this number of instances of goodchessmove running at any one time until all have completed.

Use the C11 functions and system-calls fork(), execl(), and wait() to implement the manychessmoves program.

On success, manychessmoves will exit with an exit-status of 0, or with 1, as quickly as possible, if it encounters any form of failure.


BEFORE the workshop session you're strongly encouraged to think how you would do it. You are not expected to have developed and tested a perfect solution before the workshop, but you should at least scratch out your approach on paper.

From the information recently presented in lectures, we know:

  • Processes may 'split in two' by calling fork().
  • Child processes typically run a different program by calling execl().
  • Parent processes call wait() to wait for any child process to terminate.
  • A process may call fork() at any time, and have multiple child processes running. While all of these process are executing 'in parallel', it's not really 'parallel computing' unless they're communicating.

So, before the workshop session, think how you can combine the (above) information that we already know to develop a solution.

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Presented by [email protected]