LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 12-24-2005, 12:08 PM   #1
scorpio2002
LQ Newbie
 
Registered: Dec 2005
Posts: 4

Rep: Reputation: 0
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
 
Old 12-24-2005, 02:41 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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:
Code:
clone(SIGCHLD, 0);
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.
 
Old 12-25-2005, 03:18 AM   #3
scorpio2002
LQ Newbie
 
Registered: Dec 2005
Posts: 4

Original Poster
Rep: Reputation: 0
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...
 
Old 12-25-2005, 05:31 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,507

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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 ...
 
Old 12-25-2005, 05:48 AM   #5
scorpio2002
LQ Newbie
 
Registered: Dec 2005
Posts: 4

Original Poster
Rep: Reputation: 0
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() :
 
Old 12-25-2005, 02:00 PM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,507

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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 ...
 
Old 12-25-2005, 02:17 PM   #7
scorpio2002
LQ Newbie
 
Registered: Dec 2005
Posts: 4

Original Poster
Rep: Reputation: 0
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
 
Old 12-25-2005, 02:26 PM   #8
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
X Documentation ? Ikebo Programming 6 10-02-2004 09:02 PM
Documentation For RH 9.0 Crotch Linux - Newbie 2 04-25-2004 05:42 PM
documentation porous Slackware 4 10-27-2003 09:34 PM
need documentation kroll Linux - Networking 1 08-21-2002 02:29 AM
Too much Documentation rhuarc Linux - General 7 01-03-2002 09:03 AM


All times are GMT -5. The time now is 02:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration