Programmatically check if a process is being run in the background

前端 未结 4 988
粉色の甜心
粉色の甜心 2021-01-19 16:19

2 questions:

1) Is there any Linux/Posix API to know if a process has been invoked as a background process?

linux> myprogram &
4条回答
  •  天命终不由人
    2021-01-19 16:44

    To answer your second part, that's usually called a daemon and they're built something like this.

    main()
        pid = fork()
        if pid is child
              run program
        else we are the parent process
           exit to command prompt
    

提交回复
热议问题