ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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?
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..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.