LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 03-18-2011, 11:00 PM   #1
henry1999sg
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());
if(pid=fork()<0)
{
return;
}
printf("fork return pid=%s\n",pid);
if(pid==0)
{
print("child pid=%d\n",getpid());
excvp(*args, args);
exit(1);
}
//-----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, 11:22 PM   #2
Wellesly
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.
Code:
...
if((pid = fork())<0)
{
...
 
1 members found this post helpful.
  


Reply

Tags
[c]


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


All times are GMT -5. The time now is 12:47 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration