From owner-freebsd-hackers Wed Aug 19 09:17:02 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id JAA07932 for freebsd-hackers-outgoing; Wed, 19 Aug 1998 09:17:02 -0700 (PDT) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from lor.watermarkgroup.com (lor.watermarkgroup.com [207.202.73.33]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id JAA07925 for ; Wed, 19 Aug 1998 09:17:00 -0700 (PDT) (envelope-from luoqi@watermarkgroup.com) Received: (from luoqi@localhost) by lor.watermarkgroup.com (8.8.8/8.8.8) id MAA06764; Wed, 19 Aug 1998 12:16:21 -0400 (EDT) (envelope-from luoqi) Date: Wed, 19 Aug 1998 12:16:21 -0400 (EDT) From: Luoqi Chen Message-Id: <199808191616.MAA06764@lor.watermarkgroup.com> To: bright@www.hotjobs.com, hackers@FreeBSD.ORG Subject: Re: sfork()? Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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 #include #include 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 > #include > > 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