Date: Wed, 19 Aug 1998 12:16:21 -0400 (EDT) From: Luoqi Chen <luoqi@watermarkgroup.com> To: bright@www.hotjobs.com, hackers@FreeBSD.ORG Subject: Re: sfork()? Message-ID: <199808191616.MAA06764@lor.watermarkgroup.com>
next in thread | raw e-mail | index | archive | help
You need to set up separate stack for the child, this cannot be done without any assembly. We need a wrapper function for rfork() in libc. Below is a small test program I wrote a while ago, you may adapt it to your needs if you want. -lq #include <sys/syscall.h> #include <stdio.h> #include <unistd.h> void callfunc(int (*func)(void *), void *arg) { _exit((*func)(arg)); } int rforkcall(void *stack, void *func, void *arg) { register int *sp = (int *)stack; *--sp = (int)arg; *--sp = (int)func; *--sp = 0; *--sp = (int)callfunc; __asm__( "pushl %2;\n\t" "pushl $0;\n\t" "movl %1,%%eax;\n\t" ".byte 0x9a; .long 0; .word 7;\n\t" "cmpl $0, %%edx; je 1f;\n\t" "movl %0,%%esp; ret;\n1:\n\t" "addl $8,%%esp" : : "r" (sp), "g" (SYS_rfork), "g" (RFPROC|RFMEM) : "eax", "edx"); } int n = 0; int a(char *s) { fprintf(stderr, "%s %d\n", s, n++); return 0; } char stack1[4096]; char stack2[4096]; main() { rforkcall(stack1+4096, (void *)a, "thread"); rforkcall(stack2+4096, (void *)a, "thread"); a("main"); } > can someone explain why this doesn't work? > > (the child process just segfaults, even without the printfs) > > ----- CUT > > #include <stdio.h> > #include <unistd.h> > > volatile int t=0; > > int main(void){ > > pid_t pid; > > if(pid = rfork(RFPROC | RFMEM)){ > printf("(parent)pid = %d\n",pid); > while(1){ > printf("%d\n",t); > sleep(1); > } > } else { > printf("doesn't die here..."); > t = 1; > printf("changed t... "); > sleep(10000); > } > > printf("pid = %d about to exit\n",pid); > return(0); > } > > ----- END > > > Alfred Perlstein - Programmer, HotJobs Inc. - www.hotjobs.com > -- There are operating systems, and then there's BSD. > -- http://www.freebsd.org/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199808191616.MAA06764>