I was trying to build an interactive shell which runs within the ZSH shell. Any command would execute in it's own process group (thus I use tcsetpgrp
call to make it the foreground process group, and once the command is executed the the interactive shell would become the foreground process group). However I observe that during the execution of my code I get a SIGTTOU signal, which to the best of my knowledge is delivered when a background process attempts to write to the terminal. With respect to the below example if someone could explain the reason for the aforementioned signal it would be really helpful.
Minimal reproducible example
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
for(;;){
printf(">Network shell\n");
int par = getpid();
int child = fork();
if(child == 0){
int c2 = fork();
if(c2 == 0){
char * args[2];
args[0]="/bin/ls";
args[1] = NULL;
execv(args[0], args);
}
else{
wait(NULL);
tcsetpgrp(STDIN_FILENO, par); //done to bring previous process group back to foreground
tcsetpgrp(STDOUT_FILENO, par);
}
}
else{
setpgid(child, child);
tcsetpgrp(STDIN_FILENO, child);
tcsetpgrp(STDOUT_FILENO, child);
wait(NULL);
}
}
}