next up previous
Next: LATEX, PostScript, PDF, Printing Up: unix_guide Previous: Read Files, Write Files,

Compile and Run

Summary of this section

Suppose you have written a C program file called solve_everything.c. To compile,

HAL9000> cc solve_everything.c
Here cc is the ``C Compiler". In Linux this is just linked to the `gnu cc' or gcc. So might as well use gcc in place of cc. Your choice.

If solve_everything.c is a simple program that doesn't require any library files, this will produce an executable file called a.out. You might want to check by typing ls to see if it is really there. If it is there, just type

HAL9000> a.out
and all the world's problem will be solved.

However, C programs are usually more complex and you don't want to use a.out for the name of every program you write. Also, you are going to need the library functions. The most important of all libraries, at least for me, is the math library. If you use any math in your program, that is, if there is a line in your program that says,

#include <math.h>
you need this library. Also, large programs usually have many separate .c files. So if you want to call your program solve_this, and if you need the math library, do
HAL9000> gcc -o solve_this solve_everything.c others_*.c -lm
The -o here means ``output" and -lm means ``link math library". The name of the library file used in this case is libm.so or libm.a. If the library you need is called libWhatever.so or libWhatever.a, then use -lWhatever.

Now you can run this program by typing

HAL9000> solve_this
Doing so makes you lose the command prompt while the program is running. To get the prompt back with the job running in the background, do
HAL9000> solve_this &

For Fortran programs, use

HAL9000> g77 -o solve_this solve_everything.f other_files.f
You can also use f77 in place of g77, but that's just another name for g77 anyway. To run it
HAL9000> solve_this 

or

HAL9000> solve_this &
as before. Note that you don't need to specify a library for math functions in Fortran.

If something goes wrong and you want to stop the program running in the foreground, the interrupt signal in Unix is <ctrl>c which kills the program, or <ctrl>z which merely suspends the program. There are two ways to resume the suspended program. Sometimes you want to run the program in the background. That is, you want the program to resume but also want the command prompt back. No problem. Just say

HAL9000> bg
If there are many suspended runs, do first
HAL9000> jobs
to get the job number and do
HALL9000> bg %3
to restart, for instance, the job #3.

On the other hand, sometimes you want the program to resume in the foreground. That is, you don't want your command prompt back. In that case, just say

HAL9000> fg

or

HAL9000> fg %3
This job can be interrupted by <ctrl>c and <ctrl>z again.

Sometimes, your program may completely freeze a window while running in the foreground. In that case, you need to know the process number to kill it. To list processes started in that shell only (in practice, this means processes started in that xterm),

ps

To list all your processes

ps -u

If your job can take longer than 30 minutes, you should put it in the batch que: The format is as follows. To run a long job on hal9000

 
qsub -M -tjob_name hall9000 batch_script
For details, do
info qsub
If you run a program which does not give you a command prompt, it will be shut off after 30 minutes unless it is submitted using qsub.

You can also check the status of machines and jobs by doing

qstat 
or
qstat machine_name

To kill a process or a background job, first do

jobs
to get the job number or do
ps -u
to get the process number. Then
kill %3
to kill the job number 3, for instance, or
kill process_number
to kill a process associated with process_number. Sometimes the process cannot be killed by simple kill. Then you have force it. To overkill,
kill -9 %job_number 

or 

kill -9 process_number


next up previous
Next: LATEX, PostScript, PDF, Printing Up: unix_guide Previous: Read Files, Write Files,
Sangyong Jeon 2007-09-18