如何向clone()调用的函数传递参数?

发布于 2024-12-19 05:18:23 字数 376 浏览 2 评论 0原文

我必须在主函数中使用clone()系统调用来获取2个线程。 (我知道,还有其他选项,但在这种情况下,它必须是clone())。

系统调用工作并且两个线程都到达指定的函数(foo)。但在这个函数中,我需要它们调用具有此签名的另一个函数:(

void increment(int* a, int b)

旁注:它将 b * 1 添加到 a。(= a+b))

重要的是,a 和 b 都在 main 中声明-function,我不知道如何将它们传递给 foo.

我已经尝试过不同的事情,但没有成功。我得到了提示:使用适配器。 但我不知道该怎么做。 (我也不知道如何在带有 int 的克隆中使用参数。)

有什么建议吗?

I have to use the clone() system call in the main-function to get 2 threads. (I know, there are other options, but in this case, it has to be clone()).

The system call works and both threads arrive in the designated function (foo). But in this function I need them to call another function with this signature:

void increment(int* a, int b)

(Sidenote: It adds b * 1 to a. (= a+b))

The important thing is, that both, a and b, are declared in the main-function and I don't know how to pass them to foo.

I already tried different things, but without success. I've gotten a hint: Use an adapter.
But I have no clue how to do this. (I also dont know how to use the args in clone with int.)

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅笑轻吟梦一曲 2024-12-26 05:18:23

clone() 的参数之一是 void* arg。这允许您将 void 指针传递给您的函数。为了传递 int 指针和 int,您必须创建一个结构体,其中包含 int 指针和 int 分别分配给 ab,然后将指针转换为该结构体转换为 void 指针。然后在函数内反转该过程。

我的 C 有点生疏,我还没有编译这个,所以不要引用我的内容,但它应该看起来大致像这样:

struct clone_args {
    int* a;
    int b
};

int main(int argc, char* argv[])
{
    struct clone_args args;
    args.a = a;
    args.b = b;
    void* arg = (void*)&args;
    clone(fn, ..., arg, ...);
}

int fn(void* arg)
{
    struct clone_args *args = (struct clone_args*)arg;
    int* a = args->a;
    int b = args->b;
}

注意:当 fn< 时,请注意您创建的结构仍在范围内/code> 被调用,因为它没有被复制。您可能必须malloc它。

One of the arguments to clone() is a void* arg. This lets you pass a void pointer to your function. In order to pass an int pointer and an int instead, you have to create a struct with an int pointer and int assigned to a and b respectively, then cast a pointer to that struct into a void pointer. Then inside the function you reverse the process.

My C is a little rusty and I haven't compiled this, so don't quote me on it, but it should look roughly like this:

struct clone_args {
    int* a;
    int b
};

int main(int argc, char* argv[])
{
    struct clone_args args;
    args.a = a;
    args.b = b;
    void* arg = (void*)&args;
    clone(fn, ..., arg, ...);
}

int fn(void* arg)
{
    struct clone_args *args = (struct clone_args*)arg;
    int* a = args->a;
    int b = args->b;
}

Note: take care that the struct you create is still in scope when fn is called, as it isn't copied. You might have to malloc it.

∞觅青森が 2024-12-26 05:18:23

这是示例代码:

#define stacksize 1048576  
typedef struct
{
int ii;
int jj;
} someinput1;

static int              /* Start function for cloned child */
childFunc(someinput1 *struc)
{

     printf("Child:  PID=%ld PPID=%ld\n", (long) getpid(), (long) getppid());

     printf("Hi!! I am child process created by Clone %ld \n",(long) getpid());
     printf("Value of x %d %d\n",struc->ii,struc->jj);
} 


int main()
{
someinput1 inputtest;
int i;
char *stack;                    /* Start of stack buffer */
char *stack1;                 /* End of stack buffer */
pid_t pid;


stack = malloc(stacksize);
stack1 = stack + stacksize;  

for (i = 0;i<5;i++)
{
    inputtest.ii = i+5;
    inputtest.jj = inputtest.ii + 10;
    pid = clone(childFunc, stack1, NULL,  (void *) (&inputtest));
    printf("clone returned -- %ld \n", (long) pid);
}

sleep(1);          
exit(EXIT_SUCCESS);
}

Here is the example code:

#define stacksize 1048576  
typedef struct
{
int ii;
int jj;
} someinput1;

static int              /* Start function for cloned child */
childFunc(someinput1 *struc)
{

     printf("Child:  PID=%ld PPID=%ld\n", (long) getpid(), (long) getppid());

     printf("Hi!! I am child process created by Clone %ld \n",(long) getpid());
     printf("Value of x %d %d\n",struc->ii,struc->jj);
} 


int main()
{
someinput1 inputtest;
int i;
char *stack;                    /* Start of stack buffer */
char *stack1;                 /* End of stack buffer */
pid_t pid;


stack = malloc(stacksize);
stack1 = stack + stacksize;  

for (i = 0;i<5;i++)
{
    inputtest.ii = i+5;
    inputtest.jj = inputtest.ii + 10;
    pid = clone(childFunc, stack1, NULL,  (void *) (&inputtest));
    printf("clone returned -- %ld \n", (long) pid);
}

sleep(1);          
exit(EXIT_SUCCESS);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文