I’m writing a simple program in C which opens an internet browser using the exec function. However the program doesn’t terminate until I close the internet browser.
What is the best way to write this program so that it opens the internet browser and then ends?
When you use
exec, your entire process is replaced with the image of the new process and the current one ceases to exist. Instead,forka new process beforehand(and if necessaryand thensetsidto detach it from it’s parent)execthe new process image. The forked process will be replaced with the new process image and the parent will stay alive. Look intowaitpidif you want to hold the parent process open until the child exits.EDIT: For future reference, yes,
setsidis not portable and the semantics aroundforkvary significantly. The spawn-and-execute model still exists on almost every POSIX and non-POSIX platform in some fashion or another even if the host operating system does not explicitly support POSIX.