后台服务程序不受终端的控制,常驻内存中,没有互交的页面。
周期性或通过事件唤醒的方式来执行任务
./main.exe & 在后台运行
#include <sys/types.h>
#include <unistd.h>
linux查看进程可以用:ps -ef,ps aux这两个命令。
ps -ef | grep name
kill pid
killall name 结束进程
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
//根据pid是否大于0判断是父进程还是子进程
if (pid > 0) { //父进程
std::cout << "Parent process" << '\\n';
return 0;
}
//子进程
std::cout << "Child process" << '\\n';
return 0;
}
#include<stdio.h>
#include<iostream>
#include <unistd.h>
#include <sys/types.h>
using namespace std;
void exetash()
{
cout<<"do one job!"<<endl;
}
int main()
{
if(fork()>0) return 0; //离开终端控制
int ii=0;
while(1)
{
exetash();
sleep(5);
}
}
signal 是进程之间相互传递消息的方法,全称为软中断信号 将会中断sleep函数
基本概念
通知进程发生事件,调用kil函数发送软中断信号
只是通知进程,无法传输数据
三种方法
signal(SIGINT,SIG_IGN)signal(SIGINT,func)| SIGKILL | 9 | 强制退出 -9 |
|---|---|---|
| SIGSEGV | 11 | 无效的内存引用 |
| SIGTERM | 15 | kill killall |
| SIGCHID | 20,17 | 子进程结束信号 |
| SIGINT | 2 | Ctrl + c |
signal库函数
signal(SIGINT,SIG_IGN)