Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-24-2005, 12:08 PM
|
#1
|
LQ Newbie
Registered: Dec 2005
Posts: 4
Rep:
|
clone() documentation
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
Donato,
Italy
|
|
|
12-24-2005, 02:41 PM
|
#2
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
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":
Quote:
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:
Code:
clone (CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 0);
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:And vfork() is implemented as:
Code:
clone (CLONE_VFORK|CLONE_VM|CLONE_FS|CLONE_SIGCHLD, 0);
...
|
Last edited by paulsm4; 12-24-2005 at 02:44 PM.
|
|
|
12-25-2005, 03:18 AM
|
#3
|
LQ Newbie
Registered: Dec 2005
Posts: 4
Original Poster
Rep:
|
thank you! Anyway, again, that doesn't answer completelly my question, cause the clone() syscall is defined as follows:
int clone(int (*fn)(void *), void *child_stack, int flags, void *arg);
So, I need someway to allocate that damned child_stack, but I can't find how to do that...
|
|
|
12-25-2005, 05:31 AM
|
#4
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,794
|
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 ...
|
|
|
12-25-2005, 05:48 AM
|
#5
|
LQ Newbie
Registered: Dec 2005
Posts: 4
Original Poster
Rep:
|
Quote:
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() :°°°
|
|
|
12-25-2005, 02:00 PM
|
#6
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,794
|
This looks contradictory, you first write you'll need to allocate the stack someday, and later that you're not going to use clone() for programming ...
|
|
|
12-25-2005, 02:17 PM
|
#7
|
LQ Newbie
Registered: Dec 2005
Posts: 4
Original Poster
Rep:
|
Quote:
This looks contradictory, you first write you'll need to allocate the stack someday, and later that you're not going to use clone() for programming
|
oh god... I'd like to learn about this system call, that doesn't mean I'm gonna use it. If we were to learn only things we use, we would be nothing 
|
|
|
12-25-2005, 02:26 PM
|
#8
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
Hi -
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.
IMHO .. PSM
Last edited by paulsm4; 12-25-2005 at 02:28 PM.
|
|
|
All times are GMT -5. The time now is 07:05 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|