Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-18-2011, 10:00 PM   #1
LQ Newbie
Registered: Mar 2011
Posts: 7

Rep: Reputation: 0
fork() always return 0 even in the parent process

my code is very simple:
int pid, status;
printf("parent process id=%d, to call fork\n",getpid());
printf("fork return pid=%s\n",pid);
print("child pid=%d\n",getpid());
excvp(*args, args);
//-----below the codes are executed by only parent process
else if (pid>0)
while(wait(&status)!=fpid) ;

but everytime I can only get two fork return pid=0. In parent process, the pid returned by fork() should be >0 and equal to the child process id.

what is wrong with the code ?

thanks... Guru !
Old 03-18-2011, 10:22 PM   #2
LQ Newbie
Registered: Jan 2011
Distribution: Arch Linux
Posts: 16

Rep: Reputation: 6
Your first if statement sets pid to fork() < 0. Since what fork() returns is never less than 0, you always get 0 for "false".

Adding parenthesis will help.
if((pid = fork())<0)
1 members found this post helpful.



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
fork(), variable values between child and parent process???? dearvivekkumar Programming 6 02-24-2010 05:18 AM
return value from child to parent process prabhat ranjan Programming 3 02-12-2008 06:40 PM
Is it posible to "fork" a process and run the parent first? cdog Programming 2 02-28-2007 04:44 PM
Getting a parent to communicate with its child -- fork() kamel Programming 3 06-02-2004 03:04 AM
child-parent-&-fork mukul Programming 2 03-24-2001 01:12 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:36 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration