Get the App
SLTechnology News&Howtos  ›  Development  › 

What if Linux 2.6Alarm signal interruption encounters function blocking

Shulou Source: shulou.com Published: 2022-06-03 05:56:45 10月04日 Update

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.

Tags: Blocking signals services functions what to do code information clients clients more knowledge programs articles processes processing monitoring utility examples sockets that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi OPPO Reno Docker Linux MySQL