原始克隆系统调用 [英] Raw Clone system call

查看:148
本文介绍了原始克隆系统调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用原始克隆系统,但是找不到任何适当的文档. 我试图编写一个小程序来尝试它,但是最终出现了段错误.

I am trying to use the raw clone system, but I could not find any proper documentation. I tried to write a small program to try it, but this ends up with a segmentation fault.

我不明白我哪里错了.

I cannot understand where I am wrong.

这是小应用程序:

define STACK_SIZE 0x10000
define BUFSIZE 200

#define _GNU_SOURCE

void hello (){
    fprintf(stderr,"Hello word\n"); 
    _exit(0); 
}


int main()  
{

int res; 
void *stack = mmap(0, STACK_SIZE, PROT_READ|PROT_WRITE,
                       MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  pid_t ptid, tid; 

  printf("Stack %p\n", stack + STACK_SIZE);
  memset(stack, 0, STACK_SIZE); 

  res= syscall(SYS_clone,CLONE_SIGHAND|CLONE_FS|CLONE_VM|CLONE_FILES,stack + STACK_SIZE, &tid,&ptid,NULL );

  if (!res)
      hello(); 

  printf("Clone result %x\n", res); 
  waitpid(-1, NULL, __WALL); 

 return 0; 
}

推荐答案

如果可以使用pthread,我不能说我建议您使用克隆.我对克隆有关malloc()之类的功能的经验很差.

I can't say I recommend going with clone if you can use pthreads. I've had bad experience with functions such as malloc() in relation to clone.

您是否查看过手册页以获得文档?

Have you looked at the man page for documentation?

这是为我运行的示例.我并没有真正检查您的代码以查看为什么它会崩溃.

Here is an example that runs for me. I didn't really examine your code to see why it might be crashing.

#define _GNU_SOURCE
#include <stdio.h>
#include <sched.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <linux/sched.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>

// Allow us to round to page size
#define ROUND_UP_TO_MULTIPLE(a,b) \
( ( (a) % (b) == 0) ? (a) : ( (a) + ( (b) - ( (a) % (b) ) ) ) )

struct argsy {
    int threadnum;
};

int fun(void * args) {
    struct argsy * arguments = (struct argsy *) args;
    fprintf(stderr, "hey!, i'm thread %d\n", arguments->threadnum);
    return 0;
}

#define N_THREADS 10
#define PAGESIZE 4096

struct argsy arguments[N_THREADS];

int main() {
    assert(PAGESIZE==getpagesize());

    const int thread_stack_size = 256*PAGESIZE;
    void * base = malloc((((N_THREADS*thread_stack_size+PAGESIZE)/PAGESIZE)*PAGESIZE));
    assert(base);
    void * stack = (void *)ROUND_UP_TO_MULTIPLE((size_t)(base), PAGESIZE);

    int i = 0;
    for (i = 0; i < N_THREADS; i++) { 
        void * args = &arguments[i];
        arguments[i].threadnum = i;
        clone(&fun, stack+((i+1)*thread_stack_size), 
            CLONE_FILES | CLONE_VM,
            args);
    }

    sleep(1);

    // Wait not implemented
    return 0;
}

这篇关于原始克隆系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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