首页 > 解决方案 > nix::sched::clone 在 println 后导致分段错误

问题描述

我正在尝试通过调用手动创建线程clone。以下代码导致分段错误,但如果我println!在睡眠前删除代码运行正常。

use nix::sched::{self, CloneFlags};
use std::thread;
use std::time::Duration;

fn task() -> isize {
    println!("from inside thread");
    thread::sleep(Duration::from_secs(2));
    0
}

fn main() {
    const STACK_SIZE: usize = 1024 * 1024;
    let ref mut stack: [u8; STACK_SIZE] = [0; STACK_SIZE];
    let cb = Box::new(|| task());
    let flags = CloneFlags::CLONE_VM
        | CloneFlags::CLONE_FS
        | CloneFlags::CLONE_FILES
        | CloneFlags::CLONE_SIGHAND
        | CloneFlags::CLONE_THREAD;

    sched::clone(cb, stack, flags, None).expect("clone error");
    println!("created thread");
    thread::sleep(Duration::from_secs(5));
}

附加信息:我的实际目标是试验 linux 命名空间,我想在不同的 uts 命名空间中创建线程,所以我需要能够CLONE_NEWUTS在调用clone.

标签: linuxrustsystem-calls

解决方案


推荐阅读