LinuxQuestions.org
Visit Jeremy's Blog.
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
  Search this Thread
Old 10-06-2008, 02:26 PM   #1
LinuxInfo
LQ Newbie
 
Registered: Sep 2008
Posts: 21

Rep: Reputation: 15
Hi, I ran this following code...... need help on this.


Respected Sir,

I tired the following code:

#include<stdio.h>
int main()
{
int i;
i = fork();
if (i > 0)
{
wait(i); /*will wait for child*/
printf("%d%s", i, " This is child");
}
else if(i == 0)
{
wait(getppid()); /*will wait for parent*/
printf("child process");
}
return 0;
}

Here, according to the code, this shold never give any output because both parent and child should wait for each other. However, they are not waiting and printing outputs immediately. Could you please let me know why its happening like this?

Thanks.
 
Old 10-06-2008, 03:01 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
AFAICT wait() can only be used on child processes.

Apart from that, you should have included the proper header files (unistd.h, sys/types.h and sys/wait.h) and used the proper type for PID's (not 'int' but 'pid_t').

Then the compiler would have complained with an error telling you there was something wrong with the first argument of wait(). You could then have looked up the man page of wait(2) and see that you used wait() the wrong way.

wait() can be called in two ways:
Code:
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
So, if you call wait() with one argument, it needs to be a pointer-to-int (then wait() will write the status to the int). Not a pid_t and not an int.

If want to call wait() with a PID (pid_t type) you need to give 3 arguments.

And also the man page says it assumes to be waiting for child processes only.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
i think i ran out of space simeandrews Linux - General 6 09-01-2005 02:54 PM
Can't get X to come up after I ran FSCK zLinuxz Linux - General 3 08-10-2005 03:51 PM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
XP ran away and won't come back:::help? roesingape Linux - Software 3 06-13-2003 05:49 AM
PHP Ran Away rverlander Linux - General 1 08-02-2002 12:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:37 AM.

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
Open Source Consulting | Domain Registration