Bhuvanesh
Bhuvanesh

Reputation: 1279

Why the child process terminated ?? please explain me?

I work with the tcsetpgrp() function, i run this code in gcc complier. I want to change the STDOUT_FILENO to a new group, which was created by the child process.

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

int
main (void)
{
  printf("Parent pgid=%d\n", getpgrp());
  printf("STDOUT(parent)=%d\n", tcgetpgrp(STDOUT_FILENO));
  pid_t pid;
  if(0 == (pid = fork()))
    {
      setpgid(0, 0);     
      printf("child pgid=%d\n", getpgrp());
      if(0 != tcsetpgrp(STDOUT_FILENO, 0))
        perror("Error");
      printf("After changing %d\n", tcgetpgrp(STDOUT_FILENO));
      exit(0);
    }
  wait(0);
  return 0;
}

in that child process when the tcsetpgrp() function reaches the child process terminated and the exit status doesn't report to parent.

Upvotes: 1

Views: 853

Answers (1)

jch
jch

Reputation: 5651

When the child process calls tcsetpgrp, it receives the SIGTTOU signal, which causes it to stop. As the child process is stopped, the parent process blocks in the call to wait, waiting for the child to terminate.

The simple solution would be to ignore the SIGTTOU signal in the child, just after the call to fork:

signal(SIGTTOU, SIG_IGN)

There is another issue with your code — you're trying to change the terminal's process group to 0, which doesn't make any sense. You probably wanted to say:

tcsetpgrp(STDOUT_FILENO, getpgrp())

Upvotes: 2

Related Questions