Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What if Linux 2.6Alarm signal interruption encounters function blocking

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about Linux 2.6 Alarm signal interruption encountered function blocking how to do, the editor feels very practical, so share with you to learn, I hope you can learn something after reading this article, do not say much, follow the editor to have a look.

It is mentioned in Unix network programming that you can use the alarm + signal mechanism to generate signals to make programs jump out of blocked function calls, as shown in the following example:

The service process uses blocked listening sockets to enter the listening, and sets the alarm of 3s to ensure that there is no client connection within 3s to see whether the service process can jump out of accept blocking after alarm is triggered.

Server.c

... Void alarm_hander (int signo) {printf ("alarm...\ n");} void main () {... Listen (listenfd,queuelen); / / alarm test void * oldfun; oldfun = signal (SIGALRM,alarm_hander); alarm (3); printf ("before accept\ n"); accept (listenfd,NULL,0); printf ("after accept\ n"); signal (SIGALRM,oldfun);....}

Running result:

Root@cloud2:~/slp/NetWrokProgram/server#. / a.out

Before accept

Alarm...

As you can see, after the program processes the alarm signal (alarm_hander printed "alarm...") Message) continue to enter the accept blocking, during which the client sends a connection request to the service port, and then the server prints the following information:

After accept

Root@cloud2:~/slp/NetWrokProgram/server#

It has been proved that after dealing with the signal interruption, Linux will enter the original blocking code instead of executing the next blocking code.

The above is what to do when the Linux 2.6Alarm signal is interrupted and the function is blocked. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report