braceletboy
braceletboy

Reputation: 98

Is it possible to pass a struct pointer to an int argument of a function?

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.

Upvotes: 0

Views: 383

Answers (1)

Eric Postpischil
Eric Postpischil

Reputation: 222477

I want to understand what the authors of this codebase are trying to say.”

They are saying, if you want to pass more data than an int, put it into a structure of your own design, say some struct foo x, and pass (int) &x to Thread::Fork. Implicit in the fact they are instructing you to do this is that the Nachos operating system is supported only for platforms on which such conversions preserve the necessary address information of the pointer and support its conversion back to a pointer to the structure type.

For example, you could define struct foo this way:

struct foo
{
    int a;
    float b;
};

and define func this way:

void func(int arg)
{
    struct foo *p = (struct foo *) arg;
    printf("a is %d.\n", p->a);
    printf("b is %g.\n", p->b);
}

Upvotes: 1

Related Questions