- 论坛徽章:
- 0
|
在UNIX C里面如何将一个程序做成后台进程
以下代码段摘自《Unix Systems Programming, Communication, Concurrency, and Threads》
- Program 3.7 runback.c
- The runback program creates a child process to execute a command string in the background.
- #include <stdio.h>;
- #include <stdlib.h>;
- #include <unistd.h>;
- #include <sys/wait.h>;
- #include "restart.h"
- int makeargv(const char *s, const char *delimiters, char ***argvp);
- int main(int argc, char *argv[]) {
- pid_t childpid;
- char delim[] = " \t";
- char **myargv;
- if (argc != 2) {
- fprintf(stderr, "Usage: %s string\n", argv[0]);
- return 1;
- }
- childpid = fork();
- if (childpid == -1) {
- perror("Failed to fork");
- return 1;
- }
- if (childpid == 0) { /* child becomes a background process */
- if (setsid() == -1)
- perror("Child failed to become a session leader");
- else if (makeargv(argv[1], delim, &myargv) == -1)
- fprintf(stderr, "Child failed to construct argument array\n");
- else {
- execvp(myargv[0], &myargv[0]);
- perror("Child failed to exec command");
- }
- return 1; /* child should never return */
- }
- return 0; /* parent exits */
- }
复制代码
不过如果要实现比较完善的功能的话,还需要增加很多东西。 |
|