signal(SIGINT, int_handler);
时间: 2024-06-01 07:07:57 浏览: 11
这行代码是用来注册一个信号处理函数,当程序接收到 SIGINT 信号时会自动调用 int_handler 函数。通常情况下,SIGINT 信号是由用户在终端上按下 Ctrl+C 时发送给正在运行的程序的,表示要求程序终止运行。通过注册信号处理函数,我们可以在程序接收到 SIGINT 信号时执行一些特定的操作,比如安全地结束程序运行、释放资源等。
相关问题
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <signal.h> #include <fcntl.h> #include <time.h> #define TIMER_FILE "shijian.txt" #define PID_FILE "daemon.pid" static int running = 1; static int shijian_fd; int main(int argc, char *argv[]) { pid_t pid; pid = fork(); if (pid < 0) { printf("Daemon creation failed\n"); exit(1); } if (pid == 0) { time_t qTime= time(NULL); int pid_fd = open(PID_FILE, O_CREAT | O_RDWR, 0644); if (pid_fd < 0) { perror("Error opening pid file"); exit(EXIT_FAILURE); } if (flock(pid_fd, LOCK_EX | LOCK_NB) < 0) { fprintf(stderr, "Another instance is running\n"); exit(EXIT_FAILURE); } char pid_str[16]; int len = sprintf(pid_str, "%d", getpid()); if (write(pid_fd, pid_str, len) != len) { perror("Error writing pid file"); exit(EXIT_FAILURE); } shijian_fd = open(TIMER_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (shijian_fd < 0) { perror("Error creating timer file"); exit(EXIT_FAILURE); } if (setsid() < 0) { printf("Background session creation failed\n"); exit(1); } void sigint_handler(int sig) { running = 0; } signal(SIGINT, sigint_handler); while (running) { char rTime[64]; sprintf(rTime, "%ld\n", time(NULL) - qTime+ 1); if (write(shijian_fd, rTime, strlen(rTime)) != strlen(rTime)) { perror("Error writing timer file"); exit(EXIT_FAILURE); } sleep(1); } close(shijian_fd); unlink(PID_FILE); } if (pid > 0){ printf("The ID of the child process is %d\n",pid); } return 0; }优化以上代码,并且重复率降低
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <time.h>
#define TIMER_FILE "shijian.txt"
#define PID_FILE "daemon.pid"
#define MAX_PID_LEN 16
static volatile int running = 1;
static int shijian_fd;
void sigint_handler(int sig) {
running = 0;
}
int create_daemon() {
pid_t pid = fork();
if (pid < 0) {
printf("Daemon creation failed\n");
exit(1);
}
if (pid == 0) {
time_t qTime = time(NULL);
int pid_fd = open(PID_FILE, O_CREAT | O_RDWR, 0644);
if (pid_fd < 0) {
perror("Error opening pid file");
exit(EXIT_FAILURE);
}
if (flock(pid_fd, LOCK_EX | LOCK_NB) < 0) {
fprintf(stderr, "Another instance is running\n");
exit(EXIT_FAILURE);
}
char pid_str[MAX_PID_LEN];
int len = sprintf(pid_str, "%d", getpid());
if (write(pid_fd, pid_str, len) != len) {
perror("Error writing pid file");
exit(EXIT_FAILURE);
}
shijian_fd = open(TIMER_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (shijian_fd < 0) {
perror("Error creating timer file");
exit(EXIT_FAILURE);
}
if (setsid() < 0) {
printf("Background session creation failed\n");
exit(1);
}
signal(SIGINT, sigint_handler);
while (running) {
char rTime[64];
sprintf(rTime, "%ld\n", time(NULL) - qTime + 1);
if (write(shijian_fd, rTime, strlen(rTime)) != strlen(rTime)) {
perror("Error writing timer file");
exit(EXIT_FAILURE);
}
sleep(1);
}
close(shijian_fd);
unlink(PID_FILE);
exit(EXIT_SUCCESS);
}
printf("The ID of the child process is %d\n", pid);
return pid;
}
int main(int argc, char *argv[]) {
int pid = create_daemon();
while (running) {
sleep(1);
}
return 0;
}
本次优化主要包括以下几个方面:
1. 将子进程的代码封装成一个函数 create_daemon(),使得代码更加清晰易读,方便主函数的调用。
2. 增加了 exit(EXIT_SUCCESS) 语句,保证子进程在正常退出时返回成功状态码。
3. 将 running 声明为 volatile 型变量,以确保信号处理程序正确地更新该变量。
4. 增加了 MAX_PID_LEN 宏定义,防止 pid_str 数组溢出。
5. 函数中的注释以及变量名和函数名都尽可能地符合规范,降低代码的重复率。
boost asio::signal_set
`boost::asio::signal_set` 是 Boost.Asio 库中的一个类,用于异步处理操作系统信号。它允许您注册一个或多个信号,并在信号触发时异步执行回调函数。这对于在异步 I/O 应用程序中处理信号非常有用,例如在服务器应用程序中优雅地关闭或重启。
以下是 `boost::asio::signal_set` 的一些常用方法:
- `signal_set(boost::asio::io_context& io_context, int signal_number)`:构造函数,创建一个 signal_set 对象并关联一个 io_context 和一个信号编号。
- `void async_wait(std::function<void(const boost::system::error_code& error, int signal_number)> handler)`:异步等待信号。当信号触发时,将调用给定的处理程序。
- `void cancel()`:取消任何等待的操作。
示例代码如下:
```c++
#include <iostream>
#include <boost/asio.hpp>
void signal_handler(const boost::system::error_code& error, int signal_number)
{
if (!error)
{
std::cout << "Received signal " << signal_number << std::endl;
}
}
int main()
{
boost::asio::io_context io_context;
boost::asio::signal_set signals(io_context, SIGINT, SIGTERM);
signals.async_wait(signal_handler);
io_context.run();
return 0;
}
```
在上面的示例中,我们创建了一个 `boost::asio::signal_set` 对象并注册了 `SIGINT` 和 `SIGTERM` 信号。然后,我们调用 `async_wait` 方法来异步等待信号,并在信号触发时调用 `signal_handler` 回调函数。最后,我们通过调用 `io_context.run()` 方法来运行异步操作。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)