0

wait() is a system call in Linux system:

int wait(int *status)

Wait for a child to exit; exit status in *status; returns child PID.

int pid = fork();
if(pid > 0){
    printf("parent: child=%d\n", pid);
    pid = wait((int *) 0);
    printf("child %d is done\n", pid);
} else if(pid == 0){
    printf("child: exiting\n");
    exit(0);
} else {
    printf("fork error\n");
}

I feel confused about pid = wait((int *) 0);,the sign of int wait(int *status) says it needs a pointer of int,but when calling it bypid = wait((int *) 0),is it converting 0 to a pointer,and pid becomes the exact pid of the child? If I am wrong,could anyone pleadse explain what is happening to me?

Shengxin Huang
  • 647
  • 1
  • 11
  • 25
  • 1
    `(int *)0` is a "null pointer". This response (which *ISN'T* the "accepted" answer - but it should be) explains it's significance in [wait()](https://man7.org/linux/man-pages/man2/waitid.2.html): https://stackoverflow.com/a/690023/421195 – paulsm4 Mar 22 '21 at 01:28
  • In modern code, writing it as `wait(NULL)` would be more idiomatic. – Nate Eldredge Mar 22 '21 at 01:32

0 Answers0