1

I need to use tcsetgrp function to make my background process to foreground process, but The parameter of tcsetgrp is a file descriptor associated with my terminal. What is the file descriptor of my terminal or its int value?

EDITED

According to the below comments here is the snippet of my program.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
void sig_handler(int sig){

    printf("The grp get signal is  %d\n",tcgetpgrp(0));
}
int main(){

    printf("The controllling terminal is %d\n",tcgetpgrp(0));
    pid_t pid=fork();
    if(pid>0){
        signal(SIGINT,sig_handler);
        setpgid(pid,pid);
        tcsetpgrp(0,getpgid(pid));
        //tcsetpgrp(1,getpgid(pid));
        printf("Wait is over on %d",wait(NULL));
        tcsetpgrp(0,getpgid(getpid()));
        //tcsetpgrp(1,getpgid(getpid()));
        printf("The parent got control\n");
        while(1)
            //sleep(1);
            printf("Hello\n");

        return 0;
    }
    else{
        printf("The controllling terminal is %d\n",tcgetpgrp(0));
        printf("The controllling terminal is %d\n",tcgetpgrp(1));
        execlp("cat","cat",NULL);
    }
    return 0;

}

Here current the cat program is the foreground and everything is working fine.I am able to read from input console and send to to the ouput console.But the problem arises when I press *cntrl-c* my foreground process(cat) terminates but my parent is unable to get the control of th terminal even I have mentioned tcsetpgrp(0,getpid()) after wait().My shell prompt comes and my parent remains as background process

1

In your program, by default file descriptors 0 (stdin), 1 (stdout), and 2 (stderr) will be associated with the terminal, unless you use redirection or pipes in the command you use to invoke the program.

  • I've found that The function takes two options, a file descriptor for the terminal tty, which is usually 0 for standard input, and a pid for the foreground process group (frankly speaking, I don't know why standard input is considered as tty file descriptor). Note that: Only the parent, the shell, should ever call tcsetpgrp(). – patryk.beza Aug 12 '15 at 16:13
0

There are a couple of ways to the get the file descriptors:

  • You can run something like: lsof -p $$ | grep /dev/pts or
  • ls /proc/$$/fd

File descriptors 0 (stdin), 1 (stdout), and 2 (stderr) are all standard FDs used by all programs.

  • actually I am making a program where I forked a process but it went to the background as the parent is controlling the terminal.So I need to bring back it to terminal control.My parent is waiting on the child and the child is reading from terminal.but the child can't as it doesnt have acess to the terminal .So a deadlock situation arrives.Though i have sent my child to a different group.and that why the child group went to the background – Kalu Aug 12 '15 at 15:01
  • 1
    @Kalu It might be better to include these kind of details in your question, actually... You didn't tell us too much about your original problem. – John WH Smith Aug 12 '15 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.