4

i tried with the waitpid() function, it takes three argument, while i implement a below code , i had a some mistakes, instead of waitpid() function i use the wait() function with the three argument. it work properly i don't how its working. Any one can explain it??

 #include<stdio.h>
  #include<stdlib.h>
 #include<unistd.h>

main()
 {
    pid_t pid;
    int p;

    if((pid=fork())==0){
            printf("Child present\n");
            sleep(2);
            printf("Child terminated\n");
            _exit(0);
    }
    wait(pid,&p,0);                                                                                         
    printf("parent terminated\n");
    return 0;
 }

But the syntax for the wait function is

 pid_t wait(int *status);
Bhuvanesh
  • 1,269
  • 1
  • 15
  • 25

1 Answers1

5

As per the man page here, you need to include <sys/types.h> and <sys/wait.h> header files to use wait() [or waitpid()] which I am not seeing in your code.

It is very much likely that your compiler used some implicit declaration of wait() function, which, in case,

  • accept any number of input argument
  • return an integer.

If you include the aforesaid header files and complie, your compiler should produce the warning regarding

too many arguments to function "wait"

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • after you post a answer, i included that header files and run that code that time also any error or warning messages not displayed – Bhuvanesh Dec 10 '14 at 11:27
  • 4
    @SouravGhosh - there is no reason to think that omitting the prototype for `wait()` (by not including ``) will result in code that `performs nothing`. The linker will go looking for a `wait()` function, and presumably Bhuvanesh has managed to include the correct library file so that a function is found. But omitting the prototype does allow the function to be called with completely the wrong parameters and return type as you correctly say. – AAT Dec 10 '14 at 11:34
  • @AAT right sir, removed that part. Thank you very much for making me correct. – Sourav Ghosh Dec 10 '14 at 11:41
  • @AAT just out of curiosity, was that the reason for downvote or there is something else which i can add to may anser to improve it. – Sourav Ghosh Dec 10 '14 at 11:43
  • @SouravGhosh - I wasn't the downvoter. In fact I have now upvoted you since you've improved the answer. – AAT Dec 10 '14 at 11:44
  • @Bhuvanesh well, it does so for me. Check this [code](http://codepad.org/sgKteo2K). If after adding the header files also you're not getting the error, then, IMHO, your compiler is _broken_ – Sourav Ghosh Dec 10 '14 at 11:48
  • @SouravGhosh Your explanations is good. Can you explain me why without that header file it will allow to take the more than one value. Then we are not declaring the function it is not showing any errors. – Karthikeyan.R.S Dec 10 '14 at 14:07
  • @Karthikeyan.R.S sure!! check [this](http://stackoverflow.com/a/27385968/2173917) answer. – Sourav Ghosh Dec 10 '14 at 14:08