Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, i am working on a fork() program with child and parent processes. Below i

ID: 3671240 • Letter: H

Question

Hello, i am working on a fork() program with child and parent processes. Below is my current program. My question is what can i do to have the last child process exit correctly. Any help getting this last bug would be greatly appreciated!

My program so far:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

int main(){
  
    pid_t myprocessid, child_pid, getpid();
  
    int mystatus;
  
    myprocessid = getpid();
  
    printf("Parent : pid %d. forked", myprocessid);
  
    child_pid = fork();
  
    if (child_pid == 0)
      
    {
      
        printf("Child1 number 0 with pid: %d. exec'd at ", myprocessid);
      
      
      
        myprocessid = getpid();
      
        printf("Child2: getpid2() with pid %d. ", getpid());
      
        child_pid = fork();
      
        if ( child_pid == 1)
          
        {
          
            perror("error while fork");
          
            return 1;
          
        }
            else if (child_pid == 0)
              
            {
              
                printf("Child3 from parent with pid: %d. ", myprocessid);
              
                myprocessid = getpid();
              
                printf("Child3: pid %d. exec'd ", getpid());
              
                sleep(30);
              
                return 12;
              
            }
      
            else
              
                return 15;
      
    }
  
    else if(child_pid == 1)
      
    {
      
        while ( waitpid(child_pid, &mystatus, WNOHANG) == 0 )
          
            sleep(1);
      
        if ( WIFEXITED(mystatus) )
          
            printf("Child1: Child2 exited with exit status %d. ",WEXITSTATUS(mystatus));
      
        else
          
            printf("Child1: child hasn't ended correctly. ");
      
    }
  
    else {
      
        printf("Parent: pid: %d forked", child_pid);
      
        wait(&mystatus);
      
        if ( WIFEXITED(mystatus))
          
            printf("Parent: child has %d with exit normally ",WEXITSTATUS(mystatus));
      
        else
          
            printf("Parent: child hasn't ended correctly. ");
      
    }
  
    return 0;
  
}

Explanation / Answer

try using exit()

or

You should definitely use _Exit(). exit() calls the functions you added with atexit() and deletes files created with tmpfile(). Since the parent process is really the one that wants these things done when it exists, you should call _Exit(), which does none of these.

The child of fork() should always call _exit().

Calling exit() instead is a good way to cause pending stdio buffers to be flushed twice.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote