__clone |
Prototype: |
#include <sched.h>
int __clone(int (*fn)(void*), void* stacktop, int flags, void* arg);
|
General Description: |
The low-level system call for creating tasks. You can directly control what is shared between the parent and the child. This is not for amateur programmers: you can create very unpredictable programs. (See Chapter 7 for a complete description of this call.) |
Return Value: |
The process id. If negative, errno has the exact error code. |
Parameters |
fn |
The home for the child-task. Create a function (or procedure) that accepts a void* parameter argument. When the routine attempts to return, the operating system terminates the task for you. |
stacktop |
You must create a stack for the child-task. This parameter points to the top of that stack (the highest address of the data block). Since you provide the stack, the stack is therefore fixed in size and cannot grow like normal task's stacks. |
flags |
Two types of information arithmetically OR'ed together: the VM spaces to share and the termination signal. This flag supports all signal types, and when the task terminates, the operating system raises the signal you define.The available VM spaces are:CLONE_VM Share the data space between tasks. Use this flag to share all static data, preinitialized data, and the allocation heap. Otherwise, copy data space.CLONE_FS Share the file system information: current working directory, root file system, and default file creation permissions. Otherwise, copy settings.CLONE_FILES Share open files. When one task changes the file pointer, the other tasks see the change. Likewise if the task closes the file, the other tasks are not able to access the file any longer. Otherwise, create new references to open inodes.CLONE_SIGHAND Share signal tables. Individual tasks may choose to ignore open signals (using sigprocmask()) without affect peers. Otherwise, copy tables.CLONE_PID Share Process ID. Use this flag carefully: not all the existing tools support this feature. The PThreads library does not use this option. Otherwise, allocate new PID. |
arg |
You can pass a pointer reference to any data value using this parameter. When the operating system completes creating the child task, it calls the routine fn with the arg parameter. If you use this feature, be sure to place the value arg points to in the shared data region (CLONE_VM). |
Possible Errors |
EAGAIN |
The __clone() cannot allocate sufficient memory to copy the parent's page tables and allocate a task structure for the child. |
ENOMEM |
The __clone() failed to allocate the necessary kernel structures because memory is tight. |
Examples |
#define STACKSIZE 1024
void Child(void* arg)
{
/*---child's responsibility---*/
exit(0);
}
...
int main(void)
{ int cchild;
char *stack=malloc(STACKSIZE);
if ( (cchild = __clone(&Child, stack+STACKSIZE-1,
SIGCHLD, 0) == 0 )
|