服务器编译运行Fortran代码及top、bg、kill等命令相关问题咨询
Hey there! Let's break down your questions one by one to get you up and running with Fortran on your server smoothly.
1. Compiling & Running a Simple Fortran Program
Most servers come with the GNU Fortran compiler (gfortran) pre-installed; if not, you can usually install it via your package manager (e.g., sudo apt install gfortran on Debian/Ubuntu, sudo dnf install gcc-gfortran on RHEL/CentOS). Here's the step-by-step:
First, create a Fortran source file using a terminal text editor like
nanoorvim:nano hello.f90Paste a simple test program (like this "Hello World" example) into the file:
program hello print *, "Hello from Fortran on the server!" end program helloSave and exit (for
nano: Ctrl+O, Enter, then Ctrl+X).Compile the source code into an executable:
gfortran hello.f90 -o helloThe
-o helloflag specifies the name of the output executable; omit it and the compiler will create an executable nameda.outby default.
If you're using Intel's Fortran compiler (ifort), the command is almost identical:ifort hello.f90 -o helloRun the compiled executable:
./helloYou should see your printed message in the terminal if everything worked.
2. Checking Compilation Process Status
Compilation behavior depends on whether you run it in the foreground or background:
- Foreground compilation: If you run the
gfortran/ifortcommand without adding&at the end, the terminal will be blocked until compilation finishes. You'll know it's running if you don't see your command prompt return. When it finishes, you'll get the prompt back, plus any error messages (if compilation failed) or no output (if it succeeded). - Background compilation: If you start compilation with
&(e.g.,gfortran hello.f90 -o hello &), it runs in the background, and you keep access to your terminal. To check if it's running:- Use
psto filter for Fortran compiler processes:
If you see a line with yourps aux | grep gfortrangfortrancommand, the process is active. If only thegrepcommand shows up, the compilation has finished. - Or use
pgrepfor a simpler output (just the Process ID, or PID, if the process exists):pgrep gfortran
- Use
- Verify success after completion: Run
echo $?right after compilation finishes. A return value of0means compilation succeeded; any non-zero value means there were errors.
3. What Do top, bg, and kill <PID> Do?
These are essential terminal commands for managing processes on Linux/Unix servers:
top: This is a real-time system monitor that shows all running processes, their resource usage (CPU, memory), PID, the user running them, and how long they've been active. By default, it sorts processes by CPU usage. Pressqto exittop, orhto view a help menu with more features (like sorting by memory or filtering processes).bg: Use this to resume a paused foreground process and send it to the background. For example, if you started compilation in the foreground and pressedCtrl+Zto pause it, typingbgwill kick it back into running in the background. You'll see a message like[1]+ gfortran hello.f90 -o hello &confirming it's now in the background.kill <PID number>: This sends a termination signal to the process with the specified PID. The default signal isSIGTERM, which asks the process to clean up resources and exit gracefully. If a process is unresponsive, you can force-kill it withkill -9 <PID>(this sendsSIGKILL, which can't be ignored by the process, so use it as a last resort). You can get a process's PID fromtop,ps, orpgrep.
内容的提问来源于stack exchange,提问作者Solidification

