QUAINT !!! is this a gcc bug ? pls check out this simple C program.()
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.
int main(int argc, char* argv[])
{
int n = 0;
if ( fork() == 0 )
{
while (n++)
printf("Child ...\n");
}
while (n++)
printf("Parent ...\n");
}
As expected, both in Child and Parent will evaluate n++ in while(n++) and get false. So, nothing will be printed ,whatever the process is Parent or Child.
How strange it is. The output is infinite:
...
Parent ...
Parent ...
Parent ...
...
I am totally puzzled ! Could anybody straighten me out ?
TKS in advance.
hi, myself jitender. what i think that's wrong in ur program is that the statement is being executed infinte times
while(n++)
printf(parent...);
see the compiler is not executing the statement above it
if(fork()==0)
{
while(n++)
printf(child...);
}
bcoz it's not checkin the condition fork()==0;
nd the statement below it is executing infinte times bcoz the condition u have mentioned
while(n++)
is true every time the compiler checks it
so mention the limits of n nd the try it out it will definitely goona work.
nd plz do reply back that whether u the program runs or not.
Hi, jeetu.
tku for your reply ...
Im sorry for my English. I can't follow your mean .
Im puzzled, after i change the position of the sentence "int n = 0;" to the new position as showed at #4.
Then output is nothing.
...
frustrated ...
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088
Rep:
jeetu: Can you try posting in proper sentences with correct spelling and punctuation please? It would make your post easier to read and therefore easier to understand.
cryincold: You can't assume that a C compiler will always initialise variables!
It is always best to explicitly initialise variables before using them, as it ensures that you will get the expected results.
Out of curiousity, what is the point of the infinite loops? Why not
Code:
if(n++)
? In fact, why not:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
int n = 0;
if ( fork() == 0 )
{
printf("Child ...\n");
}
else
printf("Parent ...\n");
}
In this code, the child process will print "Child ..." and the parent will print "Parent ...". Surely this would be what you want?
Having said that, I always thought that the child got an exact copy of all local variables that the parent has when it is created. So the child process should get n=0 and the parent should have n=0....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.