c语言怎么不允许中途关闭程序

在C语言中,我们可以通过多种方式防止程序被中途关闭,以下是一些常见的方法:,1、使用信号处理函数:在Unix和Linux系统中,我们可以使用signal函数来捕获和处理SIGINT(中断信号),当用户按下Ctrl+C时,操作系统会发送这个信号给程序,我们可以定义一个
信号处理函数,当这个信号被接收时,执行我们的自定义操作,而不是直接退出程序。,2、使用多线程:我们可以创建一个后台线程,这个线程负责监听用户的输入,如果用户试图关闭程序,我们就让这个线程阻塞用户的输入,这样,即使主线程被关闭,程序也可以继续运行。,3、使用循环:我们可以创建一个无限循环,这个循环会一直运行,直到程序被强制关闭,这样,用户就不能通过正常的程序退出方式来关闭程序了。,4、使用异常处理:我们可以使用try/catch语句来捕获和处理异常,当用户试图关闭程序时,我们可以抛出一个异常,然后在catch语句中处理这个异常,这样,我们就可以阻止程序被关闭。,下面是一些代码示例:,1、使用信号处理函数:,2、使用
多线程:,3、使用循环:, ,#include <stdio.h> #include <signal.h> #include <unistd.h> void signalHandler(int signum) { printf(“Interrupt signal (“%d) received. “, signum); // cleanup and close up stuff here // terminate program exit(signum); } int main() { signal(SIGINT, signalHandler); while(1) { printf(“Going to sleep. “); sleep(1); } return 0; },#include <stdio.h> #include <pthread.h> #include <unistd.h> void* listener(void* arg) { while(1) { if(getchar() == ‘q’) { // 如果用户输入’q’,就退出循环 break; } } return NULL; } int main() { pthread_t tid; pthread_create(&tid, NULL, listener, NULL); // 创建一个新的线程来监听用户的输入 while(1) { printf(“Going to sleep. “); sleep(1); } pthread_join(tid, NULL); // 等待线程结束 return 0; },#include <stdio.h> #include <unistd.h> #include <sys/time.h> #include <time.h> #include <errno.h> #include <string.h> #include <stdlib.h> #include <fcntl.h> #include <termios.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/mman.h> #include <sys/ioctl.h> #include <unistd.h> /* For read(), write() */ #include <fcntl.h> /* For open(), O_* constants */ #include <termios.h> /* For tcgetattr(), tcsetattr() */ #include <signal.h> /* For alarm() */ #include <stdlib.h> /* For atoi() */ #include <string.h> /* For memset() */ #include <errno.h> /* For errno */ #include <sys/stat.h> /* For S_IREAD, S_IWRITE */ #include <sys/mman.h> /* For mmap() */ #include <sched.h> /* For alarm() */ #include <sys/types.h> /* For key_t, useconds_t */ #include <time.h> /* For timespec struct */ #include <stdio.h> /* For fopen(), perror() */ #include <unistd.h> /* For close() */ #include <fcntl.h> /* For open() */ #include <termios.h> /* For termios struct, TCIOFLUSH */ #include <sys/select.h> /* For select() */ #define BAUDRATE B9600 /* Baud rate for serial port */ static int set_interface_attribs(int fd) { /* Set terminal attributes */ struct termios tty; /* Create termios structure */ memset(&tty, 0, sizeof tty); /* Clear the termios structure */ if (tcgetattr(fd, &tty) != 1) { /* Get the current attributes of the terminal */ cfsetospeed(&tty, BAUDRATE); /* Set output speed to be our baudrate */ cfsetispeed(&tty, BAUDRATE); /* Set input speed to be our baudrate */ tty.c_cflag |= (CLOCAL | CREAD); /* Turn on local connections and enable receiver */ tty.c_cflag &= ~PARENB; /* Turn off parity bit */ tty.c_cflag &= ~CSTOPB; /* Turn off stop bits */ tty.c_cflag &= ~CSIZE; tty.c_cflag |= CS8; tty.c_cflag &= ~CRTSCTS; tty.c_cc[VMIN] = (unsigned char)0; tty.c_cc[VTIME] = (unsigned char)5; tcflush(fd, TCIOFLUSH); /* Flush the buffer*/ if (tcsetattr(fd,TCSANOW,&tty) != 1) { /* Apply the settings to the termios structure*/ return true; } else { printf(“Error %i from %s:%d “, errno, __FILE__, __LINE__); return false; } } else { printf(“Error %i from %s:%d “, errno, __FILE__, __LINE__); return false; } } return false; } int main() { int serial_port = open(“/dev/ttyACM0”, O_RDWR | O_NOCTTY | O_NDELAY); //打开串口设备,O_NDELAY是为了非阻塞模式,如果没有这个选项,read会阻塞进程 int status = set_interface_attribs(serial_port); //设置串口属性 struct termios tty; memset(&tty,0,sizeof tty); //获取当前终端的设置情况 tcgetattr(serial_port,&tty); //设置波特率为B9600 cfsetospeed(&tty,B9600); cfsetispeed(&tty,B9600); //设置数据位为8位 tty.c_cflag &= ~CSIZE; tty.c_cflag |= CS8; //设置无奇偶校验位 tty.c_cflag &= ~PARENB; //设置停止位为1个字符停止位 tty.c_cflag &= ~CSTOPB; //设置非规范模式(禁用流控制) tty.c_cflag &= ~CRTSCTS; //设置接收超时为2秒 tty.c_cc[VMIN] = (unsigned char)(2<=VMIN?2:0); //读取数据的最小字节数为2个字节 tty.c_cc[VTIME]=(unsigned char)(5<=VTIME?5:0); //读取数据的超时时间为5ms //清空输入缓冲区并应用新设置到终端上 tcflush(serial_port,TCIFLUSH); tcsetattr(serial_port,TCSANOW,&tty); printf(“Done! “); return status; } void *monitorThread(void *arg){ /* This thread will monitor the keyboard input from user and send it to the program running in the background */ int cfd = open(“/dev/console”, O_RDWR); /* Open console device file for reading and writing */ struct termios oldtio, newtio; /* get the current terminal state */ memset(&oldtio,0,sizeof(oldtio,

原创文章,作者:admin,如若转载,请注明出处:https://www.vaicdn.com/news/37335.html

(0)
adminadmin
上一篇 2024 年 4 月 14 日 上午7:55
下一篇 2024 年 4 月 14 日

相关推荐

  • Hostsailor-复活节半价1.5$/月/512M内存25G硬盘/512G流量@1Gbps/罗马尼亚/荷兰/美国

    Hostsailor是一家罗马尼亚的主机商,提供各种类型的虚拟私有服务器(VPS)和云服务器,他们推出了一项非常吸引人的优惠活动:复活节半价优惠,在这个优惠活动中,用户可以以1.5…

    2024 年 4 月 14 日
  • 云平台和服务器集群的关系是什么意思

    云平台和服务器集群的关系是什么?,在现代的信息技术领域,云计算已经成为了一个重要的话题,它改变了我们存储、处理和管理数据的方式,而在云计算中,云平台和服务器集群是两个核心概念,它们…

    2024 年 4 月 14 日
  • 高防cdn购买

    cdn(Content Delivery Network,内容分发网络)是一种分布式的网络系统,它可以将网站的静态资源(如图片、视频、CSS文件等)通过节点服务器分发到用户访问的地…

    2024 年 4 月 13 日
  • 1M带宽VPS:轻松享受高速网络体验 (vps带宽1m)

    在当今的数字化时代,虚拟私有服务器(vps)已经成为了众多企业和个人用户托管网站、应用程序和数据库的首选解决方案,1M带宽的VPS因其高速的网络体验而受到广泛欢迎,本文将详细介绍1…

    2024 年 4 月 14 日
  • 2021服务器销量

    2023年最畅销服务器排名公布,随着云计算和大数据技术的不断发展,服务器市场也在持续繁荣,各大厂商纷纷推出性能更强大、稳定性更高的服务器产品,以满足企业和个人用户的需求,在2023…

    2024 年 4 月 13 日
  • 高防服务器国外

    韩国高防服务器丢包的原因,在互联网行业中,服务器的稳定性和安全性是至关重要的,在实际运行过程中,服务器可能会出现各种问题,其中之一就是丢包现象,丢包是指数据在传输过程中,由于网络拥…

    2024 年 4 月 13 日
  • 计算机服务器结构测试软件有哪些

    计算机服务器结构测试软件主要用于分析和评估服务器硬件、网络配置和系统性能,这些软件工具能够帮助管理员识别潜在的瓶颈,监控系统资源使用情况,并确保服务器的健康运行,以下是一些常用的计…

    2024 年 4 月 14 日
  • 过期备案域名查询怎么查询

    过期备案域名查询,是指通过一定的技术手段,查询已经超过备案有效期的网站域名信息,这种查询可以帮助用户了解一个域名是否已经过期,以及过期时间等相关信息,在互联网高速发展的今天,拥有一…

    2024 年 4 月 13 日
  • javascript的document对象的属性

    javascript中的document对象是浏览器提供的全局对象,它代表了整个HTML文档,通过操作document对象,我们可以获取和修改网页的内容、样式以及执行各种与文档相关…

    2024 年 4 月 13 日
  • 解锁神域,轻松操作Godaddy主机:新网域名解析教程 (新网域名解析godaddy主机教程)

    解锁神域,轻松操作Godaddy主机:新网域名解析教程,在互联网世界中,域名和网站托管服务是构建在线存在的基石,Godaddy作为世界上最大的域名注册商和网站托管服务提供商之一,它…

    2024 年 4 月 13 日