ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hi there! I'm looking for information about the system call clone(). In the man pages I can't find anything really useful. I need some examples and in particular an explanation on how to create the stack for the child process.
thx a lot and marry christmas! biggrin.gif
In "classic Unix", the basic unit of concurrency is "fork()/exec()". To eliminate the overhead involved in copying the child process if you're only going to throw most of it away with "exec()", most *nix also offers a lighter-weight "vfork()".
Linux, in contrast, relies on the "clone()" system call.
Let me answer your question by quoting from Robert Love's most excellent "Linux Kernel Development, 2nd Ed":
Linux has a unique implementation of threads. To the Linux kernel, there is no concept of a thread. Linux implements all threads as standard processes. The Linux kernel does not provide any special scheduling semantics or data structures to represent threads. Instead, a thread is merely a process that shares certain resources with other processes...
... The name "lightweight process" sums up the difference in philosophies between Linux and other systems. To these other operating systems, threads are an abstraction to provide a lighter, quicker execution unit than the heavy process. To Linux, threads are simply a manner of sharing resources between processes (which are already quite lightweight)...
Threads are created like normal tasks, with the exception that the clone() system call is passed flags corresponding to specific resources to be shared:
The previous code results in behavior identical to a normal fork(), except that the address space, filesystem resources, file descriptors and signal handlers are shared. In other words, the new task and its parent are what are popularly called threads.
In contrast, a normal fork() can be implemented as:
By curiosity, why do you want to use the clone system call, which is not designed for casual programming (unlike pthread_*/fork) ?
It will also make your code non portable ...
I'm not gonna use clone() for programming, of course :-) I'm doing a research on how Linux implements Threads, a kind of essay :-) Anyway, it really seems hard to find information about this clone() :°°°
You can probably find dozens of examples by looking in the kernel code (I did a "gid clone" in my 2.6 kernel source, and came up with 284 lines).
Better, you can look up (google or yahoo: your preference) the keywords "clone" and one or more of the "CLONE_*" flags in my previous post, and you'll again find dozens of examples ... including a very lucid (to my mind, at least ;-)) example in Pascal (believe it or not!)
Best, you can buy a copy of Robert Love's "Linux Kernel Development". It's a wonderful book ... and it will definitely help you become knowledgeable enough to write an essay worth reading.