The Wait System Call in C Programming

Wait System Call
Practical No 3
Wait System Call in C
A call to wait() blocks the calling process until one of its child
processes exits or a signal is received.
 After child process terminates, parent continues its execution after
wait system call instruction.
Child process may terminate due to any of these:
It calls exit();
It returns (an int) from main
It receives a signal (from the OS or another process) whose default
action is to terminate.
 
C program to demonstrate working of wait()
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
  int main()
{
    pid_t cpid;
    if (fork()== 0)
        exit(0);           /* terminate child */
    else
        cpid = wait(NULL); /* reaping parent */
 
printf("Parent pid = %d\n",
getpid());
    printf("Child pid = %d\n", cpid);
    return 0;
}
Another C program to demonstrate working of
wait()
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
 int main()
{
    if (fork()== 0)
        printf("HC: hello from child\n");
    else
    {
        printf("HP: hello from parent\n");
        wait(NULL);
        printf("CT: child has terminated\n");
    }
 printf("Bye\n");
    return 0;
}
waitpid-example.c
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    perror("fork failed");
    exit(1);
  }
 exit(3);
  } else {
    int stat_val;
    waitpid(pid, &stat_val, 0);
    //waitpid(pid, &stat_val, WNOWAIT);
    if (WIFEXITED(stat_val))
      printf("Child exited with code %d\n", WEXITSTATUS(stat_val));
    else if (WIFSIGNALED(stat_val))
      printf("Child terminated abnormally, signal %d\n", WTERMSIG(stat_val));
    printf("exit code: %d, signal: %d, raw stat_val: 0x%x %d", WEXITSTATUS(stat_val), WTERMSIG(stat_val), stat_val, stat_val);
  }
  return 0;
}
 if (pid == 0) {
    int i;
    for (i = 3; i > 0; i--) {
      printf("This is the child\n");
      sleep(1);
    } exit(3);
  } else {
    int stat_val;
    waitpid(pid, &stat_val, 0);
    //waitpid(pid, &stat_val);
        printf("exit code: %d, signal: %d, raw stat_val: 0x%x
%d");
  }
  return 0;
}
*get_exitstatus.c*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(){
  pid_t c_pid, pid;
  int status;
  c_pid = fork(); //duplicate
  if( c_pid == 0 ){
    //child
    pid = getpid();
 printf("Child: %d: I'm the child\n", pid, c_pid);
    printf("Child: sleeping for 2-seconds, then exiting with status 12\n");
    //sleep for 2 seconds
    sleep(2);
    //exit with statys 12
    exit(12);
  }else if (c_pid > 0){
    //parent
    //waiting for child to terminate
    pid = wait(&status);
    if ( WIFEXITED(status) ){
      printf("Parent: Child exited with status: %d\n", WEXITSTATUS(status));
    }
  }else{
    //error: The return of fork() is negative
    perror("fork failed");
    _exit(2); //exit failure, hard
  }
  return 0; //success                                                                                                                                                        
}
Slide Note
Embed
Share

The wait system call in C programming is used to block the calling process until one of its child processes exits or a signal is received. This practical guide provides detailed explanations and C program examples to demonstrate the working of the wait system call, including handling child processes, parent-child communication, and process termination. Explore the concepts and practical implementation of the wait system call in C programming.

  • C Programming
  • Wait System Call
  • Child Processes
  • Process Termination
  • Parent-Child Communication

Uploaded on Oct 10, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Wait System Call Practical No 3

  2. Wait System Call in C A call to wait() blocks the calling process until one of its child processes exits or a signal is received. After child process terminates, parent continues its execution after wait system call instruction. Child process may terminate due to any of these: It calls exit(); It returns (an int) from main It receives a signal (from the OS or another process) whose default action is to terminate.

  3. C program to demonstrate working of wait() #include<stdio.h> #include<stdlib.h> #include<sys/wait.h> #include<unistd.h> int main() { pid_t cpid; if (fork()== 0) exit(0); /* terminate child */ else cpid = wait(NULL); /* reaping parent */ printf("Parent pid = %d\n", getpid()); printf("Child pid = %d\n", cpid); return 0; }

  4. Another C program to demonstrate working of wait() #include<stdio.h> #include<sys/wait.h> #include<unistd.h> int main() { if (fork()== 0) printf("HC: hello from child\n"); else { printf("HP: hello from parent\n"); wait(NULL); printf("CT: child has terminated\n"); } printf("Bye\n"); return 0; }

  5. waitpid-example.c if (pid == 0) { int i; for (i = 3; i > 0; i--) { printf("This is the child\n"); sleep(1); } exit(3); } else { int stat_val; waitpid(pid, &stat_val, 0); //waitpid(pid, &stat_val); printf("exit code: %d, signal: %d, raw stat_val: 0x%x %d"); } return 0; } #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main(void) { pid_t pid; pid = fork(); if (pid < 0) { perror("fork failed"); exit(1); }

  6. printf("Child: %d: I'm the child\n", pid, c_pid); printf("Child: sleeping for 2-seconds, then exiting with status 12\n"); *get_exitstatus.c*/ #include <unistd.h> #include <stdio.h> #include <stdlib.h> //sleep for 2 seconds sleep(2); //exit with statys 12 exit(12); }else if (c_pid > 0){ //parent //waiting for child to terminate pid = wait(&status); #include <sys/types.h> #include <sys/wait.h> int main(){ if ( WIFEXITED(status) ){ printf("Parent: Child exited with status: %d\n", WEXITSTATUS(status)); } pid_t c_pid, pid; int status; }else{ //error: The return of fork() is negative perror("fork failed"); _exit(2); //exit failure, hard } c_pid = fork(); //duplicate if( c_pid == 0 ){ //child pid = getpid(); return 0; //success }

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#