LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 02-03-2005, 08:14 AM   #1
icoming
Member
 
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;
printf("a");
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, 11:19 AM   #2
anubhuti_k
LQ Newbie
 
Registered: Oct 2004
Location: India
Distribution: Suse 9.1
Posts: 29

Rep: Reputation: 15
Hi
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.
bye
 
  


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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:30 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration