Search code examples
cforkwaitpid

why the wait() function takes more argument?


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);

Solution

  • 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"