2 questions:
1) Is there any Linux/Posix API to know if a process has been invoked as a background process?
linux> myprogram &
To answer your second part, that's usually called a daemon and they're built something like this.
main() pid = fork() if pid is child run program else we are the parent process exit to command prompt