thread_fork 在内核上工作 [英] thread_fork working on kernel

查看:60
本文介绍了thread_fork 在内核上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 OS161 上工作,主要不支持 C pthread 库.我目前的目标是了解系统调用并运行一些简单的程序.

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;

`

其中对 thread_fork 的调用是 int thread_fork(const char *name,void *data1, unsigned long data2,void (*func)(void *, unsigned long),结构线程 **ret);

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);

我在启动时更改了 conf.kern 文件以包含此文件,并已更改 main.c 以添加此函数调用.如果我删除线程调用,一切正常.

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?

推荐答案

我对 OS161 不熟悉,但是你在 C 中传递函数指针的语法错误,而且你没有给出 thread_fork 任何地方返回线程指针.

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.

首先是函数指针.thread_fork 需要一个指向带有两个参数的函数的指针.您的函数应如下所示:

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);
}

然后您对 thread_fork 的调用看起来像这样.请注意,返回的线程指针有存储空间,如果 OS161 不处理 NULL 情况,这可能是必要的:

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);

这是一个关于函数指针的工作示例.

这篇关于thread_fork 在内核上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆