LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 10-02-2011, 10:23 AM   #1
prafu
LQ Newbie
 
Registered: Aug 2010
Posts: 9

Rep: Reputation: 0
Talking A "clone" doubt


include<stdio.h>
#include<sched.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
int fn(void *a)
{
int *p;
printf("Value of a is = %d",*p);
printf("\n");
return 0;
}
int main()
{
int *i,a=125;
pid_t child;
printf("\n This is parent with id : %d",getppid());
i=(int*)malloc(100*sizeof(int))+100;
child=clone(&fn,i,CLONE_PARENT,&a);
printf("\n Got the child id as : %d",child);
return 0;
}

In the above program, In function "fun()" the first printf statement (i.e, printing the value of a) is not printing anything in the screen. If i give the second printf(simply a new line) i'm getting the output.

1. can anyone explain me why?

2. My friends are telling that I should not use "printf", if I create a process by using "clone()". They are telling me to use "write()" instead.
If they are telling the correct answer, also explain me why I should not use printf in clone?
 
Old 10-02-2011, 12:51 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
In stdio, output to a terminal is line buffered by default. Characters are saved in a buffer until either a newline is output or input is read from any stream attached to the terminal device. If you want the output to appear immediately and do not want to include a newline, you can call "fflush(stdout)" after the printf() call.
 
1 members found this post helpful.
Old 10-03-2011, 12:01 PM   #3
prafu
LQ Newbie
 
Registered: Aug 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Thumbs up

Thanks for the post. now i can explain this to my friends.
 
Old 10-03-2011, 01:55 PM   #4
mjones490
Member
 
Registered: Sep 2005
Distribution: LFS
Posts: 60

Rep: Reputation: 22
Hey, Prafu. Hey, Prafu's friends.

Quote:
Originally Posted by prafu View Post
2. My friends are telling that I should not use "printf", if I create a process by using "clone()". They are telling me to use "write()" instead.
If they are telling the correct answer, also explain me why I should not use printf in clone?
printf() calls write(), and is thread safe. Why NOT use printf?
 
Old 10-04-2011, 12:12 PM   #5
prafu
LQ Newbie
 
Registered: Aug 2010
Posts: 9

Original Poster
Rep: Reputation: 0
we are just started learning programming... to be honest we don't even know printf() calls write() (sounds weird!!!... but it is true)so, I don't know what to answer... anyway learned that now..
 
  


Reply



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
doubt with "make check" of gcc in chapter 6 lfs_rocks Linux From Scratch 1 02-13-2008 12:15 AM
k3b: To choose "normal" or "clone" copying grautu Linux - Software 1 01-09-2006 06:43 PM
Created "htdoc enabled dir" in apache doubt??? tuxfood Linux - Software 0 09-19-2005 12:40 AM
doubt in editing "grub.conf" in multi distro machine b0nd Linux - Newbie 6 08-06-2005 09:46 AM
"Doubt in Selecting Options in HTML" manikantha Programming 1 09-25-2004 12:11 AM

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

All times are GMT -5. The time now is 12:21 AM.

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