You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

服务器编译运行Fortran代码及top、bg、kill等命令相关问题咨询

Fortran Server Workflow: Compilation, Process Status, and Key Commands

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 nano or vim:

    nano hello.f90
    
  • Paste a simple test program (like this "Hello World" example) into the file:

    program hello
        print *, "Hello from Fortran on the server!"
    end program hello
    

    Save and exit (for nano: Ctrl+O, Enter, then Ctrl+X).

  • Compile the source code into an executable:

    gfortran hello.f90 -o hello
    

    The -o hello flag specifies the name of the output executable; omit it and the compiler will create an executable named a.out by default.
    If you're using Intel's Fortran compiler (ifort), the command is almost identical:

    ifort hello.f90 -o hello
    
  • Run the compiled executable:

    ./hello
    

    You 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/ifort command 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 ps to filter for Fortran compiler processes:
      ps aux | grep gfortran
      
      If you see a line with your gfortran command, the process is active. If only the grep command shows up, the compilation has finished.
    • Or use pgrep for a simpler output (just the Process ID, or PID, if the process exists):
      pgrep gfortran
      
  • Verify success after completion: Run echo $? right after compilation finishes. A return value of 0 means 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. Press q to exit top, or h to 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 pressed Ctrl+Z to pause it, typing bg will 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 is SIGTERM, which asks the process to clean up resources and exit gracefully. If a process is unresponsive, you can force-kill it with kill -9 <PID> (this sends SIGKILL, which can't be ignored by the process, so use it as a last resort). You can get a process's PID from top, ps, or pgrep.

内容的提问来源于stack exchange,提问作者Solidification

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:23:10