Did you know LQ has a Linux Hardware Compatibility List?
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-03-2005, 09:14 AM   #1
Registered: Feb 2004
Posts: 96

Rep: Reputation: 15
how to use clone()

I read man about clone(), and write a little program as follow:
#include <sched.h>
#include <stdio.h>

int fn(void *p)
int i=0;
return i;

char stack[30];

int main()
int i=0;
i=clone(fn , (void *)stack , CLONE_FS , NULL);

printf("%d" , i);
But it can not run correctly. The program can not print anything at all.
How to use clone()?
Old 02-03-2005, 12:19 PM   #2
LQ Newbie
Registered: Oct 2004
Location: India
Distribution: Suse 9.1
Posts: 29

Rep: Reputation: 15
I modified ur code a bit & it worked:

Here is the complete code:
#include <sched.h>
#include <stdio.h>

int fn()
int i=0;
printf("in fn\n");
return (i);

int main()
char stack[30];
int i;
i=clone(fn , stack , CLONE_FS , NULL);

printf("i = %d\n" , i);

the output given was :
in fn
i = 7102

where i is the pid of the child process that was executing fn,; The mistake u were makink was the u were typecasting stack as void *. If u read Programming Principles by Pratt then u'll find that void* will confuse the compiler. I had run the code using gcc.


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
PowerPoint clone? joshknape Linux - Software 4 11-21-2005 01:36 AM
Why elvis as vi clone? Lallo Slackware 4 06-24-2005 04:43 PM
Pagemaker Clone nuxor Linux - Software 6 04-13-2005 11:38 AM
Tv out, clone the monitor to the TV ludeKing Linux - Hardware 1 05-26-2004 05:26 AM
how-to clone a floppy? lothario Debian 2 05-06-2004 10:12 AM

All times are GMT -5. The time now is 04:38 AM.

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