LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 02-09-2008, 01:31 AM   #1
cryincold
Member
 
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40

Rep: Reputation: 15
QUAINT !!! is this a gcc bug ? pls check out this simple C program.()


Quote:
#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 )
{
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.
 
Old 02-09-2008, 01:35 AM   #2
cryincold
Member
 
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40

Original Poster
Rep: Reputation: 15
and, when i use gdb to set a break point at:

while (n++)
printf("Parent ...\n");

But gdb cannot stop there !!!
 
Old 02-09-2008, 01:36 AM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,371

Rep: Reputation: Disabled
You're incrementing n every time in the loop, so of course it will run infinitely (since every non-zero value means "true").
 
Old 02-09-2008, 01:59 AM   #4
cryincold
Member
 
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40

Original Poster
Rep: Reputation: 15
Hi, Nylex.

Im sure u made a small mistake.
That's n++, not ++n, and n is initialized as 0. so, the first time the program evaluate n++, it get 0(FALSE).

I change the code slightly as below, then the output is nothing. There is no substantially difference between the 2 versions.

int main(int argc, char* argv[])
{
if ( fork() == 0 )
{

int n = 0;
while (n++)
printf("Child ...\n");
}

int n = 0;
while (n++)
printf("Parent ...\n");
exit(0);
}
 
Old 02-09-2008, 02:00 AM   #5
jeetu
LQ Newbie
 
Registered: Sep 2007
Posts: 4

Rep: Reputation: 0
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.
 
Old 02-09-2008, 02:16 AM   #6
cryincold
Member
 
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40

Original Poster
Rep: Reputation: 15
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 ...
 
Old 02-09-2008, 02:32 AM   #7
cryincold
Member
 
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40

Original Poster
Rep: Reputation: 15
Tku Tku ....

I got it !!!!
 
Old 02-09-2008, 02:38 AM   #8
IBall
Senior Member
 
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Rep: Reputation: 55
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....

--Ian
 
Old 02-09-2008, 02:38 AM   #9
cryincold
Member
 
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40

Original Poster
Rep: Reputation: 15
Feel sooooooooooo embarrassed ...

sooooooooo awkward ...

I ever thought child process will not achieve the next while loop.
 
Old 02-09-2008, 02:43 AM   #10
cryincold
Member
 
Registered: Sep 2007
Location: Zhuhai, China
Distribution: Debian, etch
Posts: 40

Original Poster
Rep: Reputation: 15
Tku iBall.
it's so kind of u to help me. ^_^
I made a mistake.
Newbie to Unix programming. TKU.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A quaint phenomenon about the MBR zwylinux Linux - Software 6 06-04-2006 09:41 AM
Bug in the spell check module (I think). pdeman2 LQ Suggestions & Feedback 4 05-26-2006 07:25 PM
this is simple i think but im a newwb pls help rasiel Mandriva 15 11-24-2004 06:26 AM
find a simple Windows program to check my hardware is compatibale with Linux or no czy11421 Linux - Newbie 3 05-07-2004 06:00 AM
Mplayer again...pls help... simple one yenonn Linux - Software 2 08-19-2003 11:06 PM


All times are GMT -5. The time now is 02:58 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration