Using the Clone() System Call

Linux has introduced a new call, clone(), which allows a new level of process context creation.
About the Author

Joey Bernard is a self-described uber-geek. He has one degree in physics and is working on a second degree in computer science. He hopes to become a fully ordained priest of the Church of Tux. All donations will be accepted.

______________________

Joey Bernard has a background in both physics and computer science. This serves him well in his day job as a computational research consultant at the University of New Brunswick. He also teaches computational physics and parallel programming.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

nice article

saurabh_verma's picture

Good read .

I think when we use clone()

Jayashree K J's picture

I think when we use clone() , it is a wrapper in C-library. Then after calling this wrapper , it in turn calls the actual clone(2) system call. If this is true then you have to mention it properly.
Looking for ur future & early updations, I remain.

Jayashree K J.
Bangalore ( NTPL ).
31st May, 2006.

How can Joey Bernard will be

Anonymous's picture

How can Joey Bernard will be able to become a fully ordained priest of the Church of Tux, because his current profession is as a computer scientist ???????

How can Joey Bernard will be

Anonymous's picture

How can Joey Bernard will be able to become a fully ordained priest of the Church of Tux, because his current profession is as a computer scientist ???????

Re: Using the Clone() System Call

Anonymous's picture

WOW! i have been looking for manual for clone for weeeks!!! its been driving me up the wall!!!!!!!!!!!!!!!!!!! thankyou so much!!!

Leon

great article.

sathish's picture

truely a good article.

Herr Fähnders, hier ist doch

IN-B3's picture

Herr Fähnders, hier ist doch alles auf Englisch! :(((((

Hier ist die Uebersetzung:

Translator's picture

Hier ist die Uebersetzung:

http://babelfish.altavista.com/babelfish/trurl_pagecontent?url=http%3A%2...

Viel Spass beim Lesen

Re: Using the Clone() System Call

joeybernard's picture

I've come back from the proverbial grave. While googling myself, I came across this verrry old article I had written. With more experience under my belt, I realize I was very stupid in parts of this article. I will be posting a link to an updated version here in the next few weeks if anyone who finds this article wants a corrected version.

Re: Using the Clone() System Call

Anonymous's picture

Please do post an updated version. I found this article very helpful.

Re: Using the Clone() System Call

Anonymous's picture

Hi
Kindly send me updated link
Please do the necessary changes u r mentioning

also can u mention some link i could use for clone()

I am a university student
Thanks
Vipul

u03138@cs.unipune.ernet.in

Re: Using the Clone() System Call

Anonymous's picture

I was wondering why in the source code (listing 2) do you use a void **, when void * could very well be used for the child stack pointer

Rohit

Hey, the stack grows downwards!

Anonymous's picture

Hi,

I'm Pablo, a humble reader from Argentina.

I found your article very interesting, however I couldn't run the code from figure 2. I did compiled it, but the execution of the clone call, lead to a segmentation fault.

I took a look at the clone's man page and it said that the you should pass a pointer to the stack pointer of the task, not the beginning of the stack.

So, as the stack decreases, and it's empty when the thread begins, the right address would not be child_stack, but child_stack + 65383.

Only that way the code runs fine.

Hope it helps.

Regarding child_stack of clone

vijay's picture

How you are calculating as 65383.
How much should we need to allocate for malloc.
how to calculate malloc size.

Re: Hey, the stack grows downwards!

Anonymous's picture

Well, it worked for me. I overlooked the -static option to cc the first time and got a segmentation fault.

Re: Hey, the stack grows downwards!

Anonymous's picture

Using:

child_stack + 65383

still segments for me. Where does 65383
come in? 4 * 16384 -> 65536 anyhow!

child_stack + 4000

works for me i.e. address is a definitely safe
one to use.

David (djjohn@essex.ac.uk)

Re: Hey, the stack grows downwards!

Anonymous's picture

You should write instead:

#define CHILD_STACK_SIZE 16384

clone( child_process, child_stack + CHILD_STACK_SIZE / sizeof( void ** ), CLONE_VM | CLONE_FILES, NULL );

Which is equivalent to CHILD_STACK_SIZE / 4 on most systems.

Re: Hey, the stack grows downwards!

Anonymous's picture

Hi I am manjeet,

even I faced the same problem (of segmentation fault) but

couldnt get out of it even after changing the child_stack pointer ...

Re: Using the Clone() System Call

Chupacabra's picture

I am Chupacabra! Fear my use of the Clone() System Call!

CNN.com

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState