Now, I have to write a c program and use clone()
to make process do things asynchronous. I've read the manual of clone()
; however, I still don't know how to make it work asynchronous. I use flags CLONE_THREAD
, CLONE_VM
and CLONE_SIGHAND
and there's an infinite loop in parameter fn
. I got segmentation fault(core dumped)
first, then using gdb
to debug. Then, I got Program received signal SIGSEGV, Segmentation fault. [Switching to LWP xxx]
. I would like to make the processes switch successfully ?
Below is my code:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#define FIBER_STACK 1024*1024*8
int counter;
void * stack;
int do_something(){
int i;
while(1) {
if (counter == 1000)
{
free(stack);
exit(1);
} else {
counter++;
i++;
}
printf("Process %d running total runs %d, and this process runs %d \n", getpid(), counter, i);
}
}
int main() {
void * stack;
counter = 1;
stack = malloc(FIBER_STACK);
if(!stack) {
printf("The stack failed\n");
exit(0);
}
int i;
for (i = 0; i < 26; i++)
{
clone(&do_something, (char *)stack + FIBER_STACK, CLONE_THREAD|CLONE_SIGHAND|CLONE_VM, 0); // CLONE_VFORK
}
}