简体   繁体   中英

Main process doesn't print after calling fork() in a function

I'm trying to write a shell but i can't understand why this "> " is not printed by the main process after the createProcess function ends. Also is there a better way to print something from the main process only? I've used this if (mainPid == getpid() .

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

void createProcess(){
   pid_t  pid;
   pid = fork();
   if (pid == 0){
      printf("Process created\n");
      printf("Child pid is %d\n", getpid());
   }
}

char *read_line(){
   char *line = NULL;
   size_t bufsize = 0;
   getline(&line, &bufsize, stdin);
   return line;
}

int main(){
   pid_t mainPid = getpid();
   char* line = NULL;
   for (;;) {
      if (mainPid == getpid()){
         printf("> ");
         line = read_line();
         if (line[strlen(line) - 1] == '\n')
            line[strlen(line) - 1] = '\0';
         if (strcmp(line, "create") == 0)
            createProcess();
         else printf("Write \"Create\"\n" );
      }
   }
free(line);
return 0;
}  

Expected Output

> create
Process created
Child pid is 2417
> 

You should call fflush(stdout) after printf("> ") to ensure the string is displayed, since it does not end with a line feed, yet stdout is line-buffered. However, the "> " might be displayed before the child process has a chance to write, actually outputting "> Process created" .

Besides, the child process will loop indefinitely in for(;;) just comparing the PIDs, so you should also fix that.

> create

> Process created

Child pid is 5655

So I tested it and got this^ (note the "> " before "Process created"). This means it is printing out just not in the order you want.

Edit this is a fix:

void createProcess(){
      pid_t  pid;
      pid = fork();
      if (pid == 0){
         printf("Process created\n");
         printf("Child pid is %d\n", getpid());
         exit(0);
      }
      if (pid > 0) {
         int stat_loc;
         wait(&stat_loc);
      }
   }

got this output:

> create
Process created
Child pid is 5700
> 

Beware, you have an active loop in the child process, which could cause unexpected side effects.

Here is what happens

for(;;) unterminated loop
| if parent => ok first pass is parent
| fork
  -----------------------------
  | (parent)                   | (child)
  | loop as expected           | still loops!
                               | getpid() != mainPid so won't read a line
                               | and infinitely loops...

Your loop should be:

  while (mainPid == getpid()){  // let child terminate...
     printf("> ");
     line = read_line();
     if (line[strlen(line) - 1] == '\n')
        line[strlen(line) - 1] = '\0';
     if (strcmp(line, "create") == 0)
        createProcess();
     else if (strcmp(line, "exit") == 0) break;  // add clean exit...
     else printf("Write \"Create\"\n" );
  }

Correctly works with Clang compiler...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM