I am working on the Nachos Operating System as part of my coursework. While modifying the codebase of the OS, I encountered the following comment for a member function that allows you to run a function inside a newly forked thread (in this OS, processes are called threads - don't ask me why - refer to the official documentation). What I am most interested in is the docstring above the function. I am particularly interested in the section NOTE of the comment. I am not entirely sure what is going on here. How can a struct be passed to the Fork
function, and how should we write func
if it's supposed to use multiple arguments through a struct? Can someone explain this?
//----------------------------------------------------------------------
// Thread::Fork
// Invoke (*func)(arg), allowing caller and callee to execute
// concurrently.
//
// NOTE: although our definition allows only a single integer argument
// to be passed to the procedure, it is possible to pass multiple
// arguments by making them fields of a structure, and passing a pointer
// to the structure as "arg".
//
// Implemented as the following steps:
// 1. Allocate a stack
// 2. Initialize the stack so that a call to SWITCH will
// cause it to run the procedure
// 3. Put the thread on the ready queue
//
// "func" is the procedure to run concurrently.
// "arg" is a single argument to be passed to the procedure.
//----------------------------------------------------------------------
typedef void (*VoidFunctionPtr)(int arg);
void
Thread::Fork(VoidFunctionPtr func, int arg)
{
// run the function 'func' in side the forked thread using the argument
// 'arg' as its input - look at VoidFunctionPtr type declaration above
}
I have one question that is related to my question on StackOverflow. I shall link it here. But, I am not completely sure if I understand it. Thanks in advance!
PS: Complete source code is linked here: code, thread.h, thread.cc
Edit: I understand that using the standard library thread and other standard library implementations is very safe and helpful, but as part of the coursework, I am not allowed to do this. In any case, I want to understand what the authors of this codebase are trying to say.