Background Processes and Shared Memory Status

Background and Foreground Processes

Starting a process in background is easy. Suppose we have a program named bg and another program named fg. If bg must be started in the background, then do the following:
bg &
If there is an & following a program name, this program will be executed as a background process. You can use Unix command ps to take a look at the process status report:
3719 ... info ...  program name
7156 ... info ...  program name
The ps command will generate some output similar to the above. At the beginning of each line, there is a number, the process ID, and the last item is a program name. If bg has been started successfully, you shall see a line with program name bg.

To kill any process listed in the ps command's output, note its process ID, say 7156, then use the following

kill  7156
The program with process ID 7156 will be killed. If you use ps to inspect the process status output again, you will not see the process with process ID 7156.

Note that any program you start with a command line is, by default, a foreground process. Thus, the following command starts fg as a foreground process:

fg

There is a short form to start both bg (in background) and fg (in foreground) at the same time:

bg & fg

With this technique, the server program can be started as a background process. After the message telling you to start the client, then start the client. The client can be background or a foreground process. In the following, the client is started as a foreground process:

server  -4  2  6  -10 &
client

Since the server and the client will display their output to the same window, you will see a mixed output. Or, you can start processes in different windows.

Checking Shared Memory Status

Before starting your next run, check to see if you have some shared memory segments that are still there. This can be done with command ipcs:
ipcs -m
A list of shared memory segments will be shown. Then, use command ipcrm to remove those un-wanted ones:
ipcrm -m xxxx
where xxxx is the shared memory ID obtained from command ipcs. Note that without removing allocated shared memory segments you may jeopardize the whole system.

Use man ipcs and man ipcrm to read more about these two commands.