3

I'm looking at modifying a toy OS system and I'm just trying to learn some of the code and what it does. I have been given a "Thread" structure which has as a member a "pcb" structure, which is a process control block that interfaces the thread to the underlying physical hardware I guess.

Anyways, in this "pcb" structure there's the initialization function which initializes the pcb of a newly created thread. Here's the function definition:

void md_initpcb(struct pcb *, char *stack, void *data1, unsigned long data2,
    void (*func)(void *, unsigned long));

With regards to the code, what is the meaning of the last argument? Does it relate to the code or instructions

Conceptually, I'm confused about how stuff fits into the bigger picture. From what I know, a Thread is a unit of execution of code; for example it can relate to user programs, so switching between threads rapidly gives the illusion of running processes in parallel. Alright so this Thread then needs its own stack, registers (don't understand), and some control (the pcb).

Sorry if this is kind of all over the place. For reference, I'm starting the OS161 project.

Thanks.

sarnold
  • 102,305
  • 22
  • 181
  • 238
JDS
  • 16,388
  • 47
  • 161
  • 224
  • 1
    Function pointer syntax is particularly confusing at first, but you eventually learn to recognise it. – dreamlax Jan 18 '12 at 23:13

2 Answers2

7

It's a function pointer. You pass it the address of a function that returns void and takes a void pointer and an unsigned long.

So, for example, if you have a function:

void myfunc(void *data, unsigned long number);

Then you could pass it as the fourth argument to md_initpcb.

This function is the code that the thread you're creating is going to execute. When it finishes, the thread will finish too.

  • thank you, I that's what I intuitively thought (pass the instructions that the thread will execute) – JDS Jan 18 '12 at 23:23
2

void (*func)(void *, unsigned long) mean that func is a pointer to a function that take void* and unsigned long, and return void. I guess that this parameter is the function that the new thread will run, and data1 and data2 are the parameters that the new thread will pass to this function.

asaelr
  • 5,438
  • 1
  • 16
  • 22