What Is Implicit Declaration of Function Wait?

What Is Implicit Declaration of Function Wait?

The phrase "implicit declaration of function wait" typically arises in the context of C programming when referring to a compiler error or warning. This happens when you attempt to call a function without having declared it first. In C, all functions should be declared before they are used, either by including the appropriate header file or by providing a function prototype.

For the specific case of the wait function, which is commonly used in Unix-like operating systems to wait for a process to change state, it is typically declared in the header <sys/wait.h>. If you try to use wait without including this header, you will get an "implicit declaration of function" warning or error, because the compiler does not have prior knowledge of its existence or how it should be called.

Here is an example of how you might correctly include the declaration for the wait function:

Implicit Wait Function Example

#include <sys/wait.h>

// ... your code ...

pid_t child_pid;
int status;

child_pid = fork();

if (child_pid == 0) {
    // Child process
    // ... do child stuff ...
} else {
    // Parent process
    wait(&status); // Correctly declared, no warning
    // ... do parent stuff ...

In the above example, including <sys/wait.h> informs the compiler about the wait function's prototype, which eliminates the implicit declaration warning or error.

When you're programming in C on a Unix-like system, and you're dealing with processes, you may need to use the wait function. This function is crucial in process control, as it allows a parent process to wait for its child processes to terminate or change state.

The following is the detailed tutorial on implicit declaration of function Wait in C:

Understanding the wait Function in C

The wait function is part of the POSIX standard and is used in conjunction with process-creation system calls like fork(). The wait function suspends the calling process until one of its child processes exits or takes a signal that causes it to terminate.

The prototype for wait is found in <sys/wait.h>:

pid_t wait(int *wstatus);

Here, wait takes a pointer to an integer variable where the status of the exited child process will be stored. The function returns the Process ID (PID) of the terminated child or -1 on error.

Step-by-Step Guide to Using wait

Step 1: Include Necessary Headers

First, include the headers for working with processes and the wait function:

#include <stdio.h>   // Standard I/O functions
#include <stdlib.h>  // Standard library functions
#include <sys/wait.h> // For wait()
#include <unistd.h>  // For fork()

Step 2: Fork a Process

Create a child process using fork(). This system call creates a new process by duplicating the current process.

pid_t pid = fork();

Step 3: Check for fork() Errors

Always check the return value of fork():

  • If fork() returns a negative value, the creation of a child process was unsuccessful.
  • If fork() returns zero, we are in the child process.
  • If fork() returns a positive value, we are in the parent process, and the return value is the PID of the newly created child process.
if (pid < 0) {
    perror("fork failed");

Step 4: Separate Parent and Child Processes

Use the PID to determine if the code is running in the parent or child process and perform actions accordingly.

if (pid == 0) {
    // Child process
    printf("This is the child process with PID %d\n", (int)getpid());
    // Child process does something here...
} else {
    // Parent process
    printf("This is the parent process, waiting for PID %d to finish.\n", pid);
    // Parent process does something else here...

Step 5: Use wait in the Parent Process

In the parent process, call wait to wait for the child process to finish execution.

int status;
pid_t finished_pid = wait(&status);

if (finished_pid < 0) {
    perror("wait failed");

Step 6: Check the Exit Status

After the child process has finished, you can examine the status code to determine how the child process terminated.

if (WIFEXITED(status)) {
    printf("Child process finished with status: %d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
    printf("Child process terminated by signal: %d\n", WTERMSIG(status));

Step 7: Complete the Parent Process

Continue execution in the parent process as needed.

// Parent process continues execution here...

Step 8: Compile and Run Your Program

Save your program to a file, for example, process_control.c, and compile it using gcc:

gcc process_control.c -o process_control

Run your compiled program:


Understanding wait Options

The wait function has several related calls like waitpidwaitid, and macros to interpret the status value like WIFEXITED and WEXITSTATUS. These can provide more control over how you wait for child processes and how you retrieve their termination status.


The wait function is essential for proper process synchronization in C. By following the steps outlined in this tutorial, you should now be able to use wait effectively in your programs to manage child processes. Remember to include the necessary headers and to check the status of the processes correctly to ensure your parent and child processes coordinate smoothly.