cpthreadsos161

thread_fork working on kernel


I am working on OS161 where C pthread library is not primarily supported. My current objective is to understand the sys calls and make some simple programs run.

my simple function has following code:

int id = 1;

long id2 = 1;

int ret = thread_fork("myThread", (void *)id, id2, void (*function)((void *)id, id2), NULL);

    kprintf("\nHello World\n");
    return;

`

where call to thread_fork is int thread_fork(const char *name, void *data1, unsigned long data2, void (*func)(void *, unsigned long), struct thread **ret);

I ahve changed conf.kern file to include this file while booting and have changed main.c to add this function call. Everything works fine if I remove thread call.

is it not the proper way to implement thread code or Am I going wrong anywhere?


Solution

  • I'm not familiar with OS161, but you've got the syntax for passing a function pointer in C wrong, and you've not given thread_fork anywhere to return the thread pointer.

    First, the function pointer. thread_fork expects a pointer to a function that takes two parameters. Your function should look like this:

    void function(void *data1, unsigned long data2) {
        kprintf("Called with data1=%p, data2=%ld\n", data1, data2);
    }
    

    Then your call to thread_fork looks like this. Note there's storage for the returned thread pointer, which may be necessary if OS161 doesn't handle the NULL case:

    int id1 = 1;
    unsigned long id2 = 2;
    struct thread *thr;
    
    thread_fork("myThread", &id1, id2, function, &thr);
    

    Here's a worked example on function pointers.