How to suspend a process just before it prints it's output in C?

106 Views Asked by At

I am trying to implement background jobs in C for my shell. The problem is it prints the output next to my prompt like this:

user@hostmachine:/.../$: [output]

I have now learnt that "stty tostop" helps suspend process just before it's output but that only works in a shell. I am trying to implement this functionality in C, so that it doesn't use my terminal during a prompt.

I tried to run "stty tostop" after starting my shell but it din't work. I also asked a related question before and now I am stuck here.

1

There are 1 best solutions below

2
John Bollinger On

Processes in the foreground process group of a given terminal can write to that terminal without being suspended. Others cannot. Thus, to put a process in the background is to put it in a process group other than than one currently in the foreground (setpgrp()).

Use tcsetpgrp() to control which process group is in the foreground on a given terminal. This is part of how you would bring your background process to the foreground.

You might find Glibc's discussion of Implementing a Job-Control Shell useful for more details and related considerations.