Can preventive multitasking of native code be implemented in user space on Linux?

I am wondering if it is possible to implement proactive multitasking of native code within a single process in user space on Linux. (That is, temporarily suspend some launch of your own code, save context, swap in another context and resume execution, everything is organized by user space, but with the help of calls that can enter the kernel.) I thought that this could be done using a signal handler for SIGALRMboth the family *context(), but it turns out that the whole family is *context() async-signal-unsafe , so this approach does not work with guarantee. I found a gistwhich implements this idea, so it seems that sometimes it works on Linux, although sometimes POSIX it does not need to work. This gist sets this as a signal handler on SIGALRM, which causes several calls *context():

void
timer_interrupt(int j, siginfo_t *si, void *old_context)
{
    /* Create new scheduler context */
    getcontext(&signal_context);
    signal_context.uc_stack.ss_sp = signal_stack;
    signal_context.uc_stack.ss_size = STACKSIZE;
    signal_context.uc_stack.ss_flags = 0;
    sigemptyset(&signal_context.uc_sigmask);
    makecontext(&signal_context, scheduler, 1);

    /* save running thread, jump to scheduler */
    swapcontext(cur_context,&signal_context);
}

Does Linux provide any guarantee that makes this approach right? Is there any way to do this right? Is there a completely different way to do this right?

(By “implement in user space”, I don't want to say that we never enter the kernel. I mean the difference from the preventive multitasking implemented by the kernel.)

+4
source share
1 answer

. ( - , , , , undefined ).

volatile sig_atomic_t ( sig_atomic_t) (. (7), (7), sigreturn (2)...) (, ) , , , , .. .

, , . , C ( ), C, . , .

. . poll (2), fcntl (2) F_SETFL O_NONBLOCK ..

, , . GCC -fsplit-stack ( splitstacks GCC).

( ) , . AFAIK Go - goroutines. ABI, . .

( Linux , . clone (2)).

PS. , , MPS MTA (, Chicken Scheme).

+1

Source: https://habr.com/ru/post/1691812/


All Articles