Hews
Hews

Reputation: 581

Child executing entire process

The following commands are inputted into this code: "CREATE", then "QUIT". When "CREATE" is called, the process needs to fork and further code executed within the child.

My output should be this (with the input being printed):

- INPUT: CREATE
- CALLED_CREATE
- CHILD_PROCESS
- INPUT: QUIT

However, I have this:

- INPUT: CREATE
- CALLED_CREATE
- INPUT: QUIT
- INPUT: CREATE
- CALLED_CREATE
- CHILD_PROCESS
- INPUT: QUIT

My understanding of fork() is that the parent process will continue executing the code if it's available to the parent, and likewise for the child - beginning from the instruction, fork(). Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include "locker.h"

int main(){

    char user_input[100];

    int active_input = 0;

    int this_pid;

    while(active_input == 0){

        //receive user_input
        scanf("%s", user_input);
        printf("INPUT: %s\n", user_input);

        if(strcmp(user_input, "CREATE") == 0){

            printf("CALLED_CREATE\n");

            if((this_pid = fork()) < 0){
                perror("Failed to fork process");
                return EXIT_FAILURE;
            }

            //child
            if(this_pid == 0){

                printf("CHILD_PROCESS\n");
            }

            //parent
            if(this_pid > 0){

                //printf("PARENT_PROCESS\n");
            }
        }

        if(strcmp(user_input, "QUIT") == 0){
            active_input = 1;
            break;
        }
    }
}

Thanks for your help :)

Upvotes: 0

Views: 39

Answers (1)

Karel Kubat
Karel Kubat

Reputation: 1675

I suspect that the main problem is that you don't stop the child process. E.g., after you enter once CREATE, the child process will print CHILD PROCESS, but will continue looping in its own process, while the parent does the same.

You should make sure that the child process exits after doing something meaningful. Also, as noted above by Chrono Kitsune, the parent needs to wait() for the child process to reap its exit status and prevent zombies.

Other than that, your user input buffer is 100 chars, opening your program to buffer overflow attacks. But that's a different story.

Your code, slightly reworked which I think does what you want:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>

int main() {
  char user_input[100];
  int this_pid;

  while (1) {
    //receive user_input
    scanf("%s", user_input);
    printf("INPUT: %s\n", user_input);

    if(strcmp(user_input, "CREATE") == 0){
      printf("CALLED_CREATE\n");

      if((this_pid = fork()) < 0){
        perror("Failed to fork process");
        return EXIT_FAILURE;
      }

      //child
      if (this_pid == 0){
        printf("CHILD_PROCESS\n");
        // do some interesting stuff here, and then don't forget to
        exit(0);
      }
    }

    if(strcmp(user_input, "QUIT") == 0)
      break;
  }
}

Upvotes: 1

Related Questions