The Ampersand

In my program, I'm using argc and argv to accept command line arguments. However, if I have to get the '&' to work i.e. make it run the child as a background process, do I have to write some special code in C or does Unix handle it automatically? If I have to add the special code, how does it look like?

If you mean you want to run a shell script or command-line request then you have to
call fork(), one of the exec calls. This calls a shell like /usr/bin/sh to execute the command. In the parent, you DO NOT call wait() or waitpid() until your parent is ready to exit, or expects the child to be done or wants to wait idly while the child runs.

You can use the WNOHANG option (not on all systems) to get waitpid() to simply check status of the child and return immediately regardless. Meaning: no waiting.

The shell will happily spawn your program in a parallel process when invoked with "&" and not wait for it until the shell command "wait" is executed.

However there are special things you may want to do if

(a) you want to continue in the background when the current terminal closes.

(b) always run in the background no matter how the user runs it.

The first can be achieved by either using "nohup" to launch the program, or secondly detach from the controlling terminal and ignore SIGHUP.

The second can be achieved by having "if (fork()) exit(0)" as your first executable line.