[lkml]   [2001]   [Apr]   [25]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
Subjecta fork-like C-wrapper for clone(), DONE!
Hi all,

Some days before I asked for a fork-like C-wrapper for clone() which
could be used like fork() thinking that somebody could have done it
before but I only received two e-mails saying that probably it
wasn't worth it or even it was complete non-sense.

Therefore, I've done it myself. Code follows.

Please, before beginning to flame me for doing "such kind of non-
standard threading model", I've to say that IMHO it has some merit.
After all it was E.W.Dijkstra who invented it in the late sixties.

Greetings from Barcelona

Francesc Oller

cut here ------------------------------

* Implementation of Dijkstra's parbegin/parend using clone()
* Modified from original Linus' clone.c example
* A proof of concept for academic purposes
* (c) Francesc Oller 2001, Linus Torvalds
* Under GPL license

#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sched.h>

#include <linux/unistd.h>

#include "tinyx.h"

#define STACKSIZE 4096 /* 4 KB */

pid_t myclone(void)
long retval;
long *childsp;
register long * motherbp __asm__ ("%ebp");

* allocate new stack for child
childsp = malloc(STACKSIZE);
if (!childsp)
return -1;
childsp = (long *)(((char *)childsp) + STACKSIZE);
*--childsp = *(motherbp + 1); /* push return address */
*--childsp = *motherbp; /* push mother's bp */

* Do clone() system call. We need to do the low-level stuff
* entirely in assembly as we're returning with a different
* stack in the child process and we couldn't otherwise guarantee
* that the program doesn't use the old stack incorrectly.
* Parameters to clone() system call:
* %eax - __NR_clone, clone system call number
* %ebx - clone_flags, bitmap of cloned data
* %ecx - new stack pointer for cloned child
* In this example %ebx is CLONE_VM | CLONE_FS | CLONE_FILES |
* CLONE_SIGHAND which shares as much as possible between parent
* and child. (We or in the signal to be sent on child termination
* into clone_flags: SIGCHLD makes the cloned process work like
* a "normal" unix child process)
* The clone() system call returns (in %eax) the pid of the newly
* cloned process to the mother, and 0 to the cloned process. If
* an error occurs, the return value will be the negative errno.
* Prior to the creation of the child process, we have stored
* return adress and caller's bp in child's stack. Child will
* restore caller's bp and jmp to the post-clone adress. The
* "_exit()" system call at the child's body end will terminate
* the child.
__asm__ __volatile__(
"int $0x80\n\t" /* Linux/i386 system call */
"testl %0,%0\n\t" /* check return value */
"jne 1f\n\t" /* jump if mother */
"popl %%ebp\n\t" /* restore caller's bp */
"ret\n" /* jmp to return address */
:"=a" (retval)
:"0" (__NR_clone),
"c" (childsp));

if (retval < 0) {
errno = -retval;
retval = -1;
return retval;

int show_same_vm;

int main()
int fd;

fd = open("/dev/null", O_RDWR);
if (fd < 0) {
printf("mother:\t fd = %d\n", fd);
show_same_vm = 10;
printf("mother:\t vm = %d\n", show_same_vm);

case -1: perror("clone"); exit(1);
case 0:
printf("child:\t fd = %d\n", fd);
show_same_vm = 5;
printf("child:\t vm = %d\n", show_same_vm);
printf("mother:\t vm = %d\n", show_same_vm);
if (write(fd, "c", 1) < 0)
printf("mother:\t child closed our file descriptor\n");

* The implementation of this construct is left as an exercise
* to the reader ;-)
printf("I'm thread 1\n");
printf("I'm thread 4, child of thread 1\n");
printf("I'm thread 5, child of thread 1\n");
printf("I'm thread 6, child of thread 1\n");
printf("I'm thread 2\n");
printf("I'm thread 3\n");
return 0;

cut here -----------------------------
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
More majordomo info at
Please read the FAQ at

 \ /
  Last update: 2005-03-22 12:52    [W:0.251 / U:0.144 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site