代码之家  ›  专栏  ›  技术社区  ›  domlao

如何在后台进程中运行linux应用程序?

  •  0
  • domlao  · 技术社区  · 16 年前

    我正在学习如何在Linux操作系统平台上编程,以及如何在后台进程中运行我的应用程序。

    我怎样才能杀死或终止在后台运行的应用程序?我的意思是我不需要执行kill shell命令在后台终止我的应用程序?或者,如果应用程序满足一个条件,那么它将自杀或终止自己。

    非常感谢。

    3 回复  |  直到 16 年前
        1
  •  4
  •   Yann Ramin    16 年前

    你想后台监视你的程序。这通常是由 fork() 以及其他一些系统调用。

    还有更多细节 here

    kill . 守护进程最好将其进程ID(PID)写入一个已知的文件中,这样就可以很容易地找到它。

        2
  •  2
  •   Tim Post Samir J M Araujo    16 年前

    了解 fork() exec() wait() kill() ,有时使用起来更方便 daemon(3) 如果存在的话。

    注意事项:

    • 不存在于所有BSD中(但可能被命名为其他名称)

    如果可移植性不是一个主要问题,那么它是相当方便的。If便携性 一个主要的问题是,您可以随时编写自己的实现并使用它。

    从手册页:

    SYNOPSIS
           #include <unistd.h>
    
           int daemon(int nochdir, int noclose);
    
    DESCRIPTION
           The daemon() function is for programs wishing to detach themselves from the
           controlling terminal and run in the background as system daemons.
    
           If nochdir is zero, daemon() changes the calling process's current working directory
           to the root directory ("/"); otherwise, the current working directory is left 
           unchanged.
    
           If noclose is zero, daemon() redirects standard input, standard output and standard
           error to /dev/null; otherwise, no changes are made to these file descriptors.
    
        3
  •  1
  •   Ignacio Vazquez-Abrams    16 年前

    fork(2) 给你一个新的过程。在孩子身上你跑了一个 exec(3) 函数将其替换为新的可执行文件。父级可以使用 wait(2) kill(2) 可用于向另一进程发送信号。