在Unix/Linux中创建一个后台进程的步骤
2012-12-14 20:04
411 查看
http://www.linuxidc.com/Linux/2011-07/39587.htm
在Unix/Linux中创建一个后台进程的步骤
1、调用fork函数,创建一个子进程。
2、先让父进程自然结束。
3、在子进程中调用setpgrp(),把子进程的进程组ID设为子进程的进程ID。
4、在子进程中调用setsid(),创建一个新的Session(会话),这样子进程就与当前的控制终端脱离,也接受不到当前终端的(ctrl + c)消息。
实现代码如下(运行环境:虚拟机下的Ubuntu):
/*
* Author: ACb0y
* FileName: main.cpp
* Create Time: 2011-07-24
* Version: V1.0
*/
#include <iostream>
#include <unistd.h>
using namespace std;
void print()
{
int pid = getpid();
int gid = getpgid(0);
cout << "process group id = " << gid << endl;
cout << "process id = " << pid << endl;
}
int main()
{
//create a child process.
int pid = fork();
if (-1 == pid)
{
cout << "call function fork() error!" << endl;
}
else if (0 == pid) //return from child process.
{
cout << "----------in child process.----------" << endl;
print();
cout << "--------------------------------------" << endl;
//将该进程的进程组ID设置为该进程的进程ID。
setpgrp();
cout << "----------in child process. setpgrp()----------" << endl;
print();
cout << "--------------------------------------" << endl;
//创建一个新的Session,断开与控制终端的关联。也就是说Ctrl+c的触发的SIGINT信号,该进程接收不到。
setsid();
for (int i = 0; i < 5; ++i)
{
sleep(20);
cout << "----------in child process.----------" << endl;
print();
cout << "--------------------------------------" << endl;
}
}
else //return from parent process.
{
cout << "----------in parent process.----------" << endl;
print();
cout << "--------------------------------------" << endl;
}
return 0;
}
在Unix/Linux中创建一个后台进程的步骤
1、调用fork函数,创建一个子进程。
2、先让父进程自然结束。
3、在子进程中调用setpgrp(),把子进程的进程组ID设为子进程的进程ID。
4、在子进程中调用setsid(),创建一个新的Session(会话),这样子进程就与当前的控制终端脱离,也接受不到当前终端的(ctrl + c)消息。
实现代码如下(运行环境:虚拟机下的Ubuntu):
/*
* Author: ACb0y
* FileName: main.cpp
* Create Time: 2011-07-24
* Version: V1.0
*/
#include <iostream>
#include <unistd.h>
using namespace std;
void print()
{
int pid = getpid();
int gid = getpgid(0);
cout << "process group id = " << gid << endl;
cout << "process id = " << pid << endl;
}
int main()
{
//create a child process.
int pid = fork();
if (-1 == pid)
{
cout << "call function fork() error!" << endl;
}
else if (0 == pid) //return from child process.
{
cout << "----------in child process.----------" << endl;
print();
cout << "--------------------------------------" << endl;
//将该进程的进程组ID设置为该进程的进程ID。
setpgrp();
cout << "----------in child process. setpgrp()----------" << endl;
print();
cout << "--------------------------------------" << endl;
//创建一个新的Session,断开与控制终端的关联。也就是说Ctrl+c的触发的SIGINT信号,该进程接收不到。
setsid();
for (int i = 0; i < 5; ++i)
{
sleep(20);
cout << "----------in child process.----------" << endl;
print();
cout << "--------------------------------------" << endl;
}
}
else //return from parent process.
{
cout << "----------in parent process.----------" << endl;
print();
cout << "--------------------------------------" << endl;
}
return 0;
}
相关文章推荐
- Unix/Linux如何创建一个后台进程(run background process)
- linux 一个简单的进程创建例子
- 实验六:分析Linux内核创建一个新进程的过程
- Linux内核创建一个新进程的过程分析
- Linux内核设计第六周学习总结 分析Linux内核创建一个新进程的过程
- linux下如何创建守护进程的步骤
- Linux内核及分析 第六周 分析Linux内核创建一个新进程的过程
- 《Linux 内核分析》第六周:分析Linux内核创建一个新进程的过程
- 分析Linux内核创建一个新进程的过程
- 创建守护进程步骤与setsid() -- linux
- Linux编写一个孤儿进程,这个孤儿进程可以同时创建100个僵死进程。
- Linux内核创建一个进程的过程分析
- 分析Linux内核创建一个新进程的过程
- Linux内核分析课程-- 分析Linux内核创建一个新进程的过程
- linux下创建后台守护进程实例
- unix/linux 系统实现多个进程监听同一个端口
- [转载] 分析Linux内核创建一个新进程的过程
- 分析Linux内核创建一个新进程的过程
- 分析Linux内核创建一个新进程的过程
- 分析Linux内核创建一个新进程的过程