I am programming a shell for my CS class and part of the project involves running a process in the background if the '&' character is passed in by the user.
If a process is run in the foreground, I simply execvp
the process and it remains in control of the terminal since it is in the foreground. However, if it is a background process, I must return control to my main shell after starting the execution of the process. I understand that the system call tcsetpgrp(pid_t)
places the process passed in as argument in the foreground, but I don't quite understand how to use it.
Should I call tcsetpgrp
after execvp
if it is a background process? If so, can I obtain the pid of my shell just by calling getpid
?
You should take a look at fork to create the child process. Then, use
exec
in the child to run the desired command.