wait() system call

 

⚙️ The wait() System Call

Purpose:

The wait() system call is used by a parent process to pause execution until one of its child processes terminates.

This ensures synchronization — the parent process continues only after the child has completed its work.


Syntax:

pid_t wait(int *status);
  • Return value:

    • Returns the process ID of the terminated child on success.

    • Returns -1 if there are no child processes.

  • Parameter:

    • status is an integer pointer used to store the exit status of the child.

    • You can pass NULL if you don’t need it.


Behavior:

  • When the parent calls wait(), it is blocked until a child process finishes execution.

  • Once the child terminates, the parent resumes.

  • This avoids zombie processes, since wait() collects the child’s exit status.


Example:

pid_t pid = fork(); if (pid == 0) { // Child process printf("Child process running\n"); } else { // Parent process waits wait(NULL); printf("Parent process running after child\n"); }

Output:

Child process running Parent process running after child

✅ Here, wait() ensures synchronization — the parent executes only after the child finishes.

Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

ps command