Based on the internal sleep function of the system, you can use the alarm() function, pause() function, and sigaction() function to understand the running process.
Function Parameters and Return Values
Markdown Extra Table Syntax:
Struct sigaction { void (*sa_handler)(int); void (sa_sigaction)(int, siginfo_t , void *); sigset_t sa_mask; int sa_flags; void (*sa_restorer)(void); } Signal handler can use void (sa_handler) (int) or void (*sa_sigaction)(int, siginfo_t , void ). Which one to use depends on whether the SA_SIGINFO bit is set in sa_flags. If it is set, use void (*sa_sigaction)(int, siginfo_t, void *). In this case, you can send additional information to the handler; by default, void (*sa_handler) ) (int), the value of the signal can only be sent to the handler at this time. Sa_handler This parameter is the same as the parameter handler of signal(), which represents the new signal processing function. For other meanings, please refer to signal(). Sa_mask is used to set the signal set specified by sa_mask to be temporarily put on hold while processing this signal. Sa_restorer This parameter is not used. Sa_flags is used to set other related operations for signal processing. The following values are available. Sa_flags can also set other flags: SA_RESETHAND: When the signal handler is called, reset the signal handler to the default value SIG_DFL SA_RESTART: If the signal interrupts a system call of the process, the system automatically starts the system call SA_NODEFER: In general, the kernel will block the given signal when the signal handler is running. However, if the SA_NODEFER flag is set, the kernel will not block the signal when the signal handler is running.
mysleep implementation code
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include<stdio.h> #include<signal.h> void myhandler(int sig) {} int mysleep(int timeout) { struct sigaction act,oact; act .sa_handler = myhandler; sigemptyset(&act.sa_mask); Act.sa_flags = 0; sigaction(SIGALRM,&act,&oact); alarm(timeout); pause(); int ret = alarm(0); sigaction(SIGALRM,&oact,NULL); return ret; } int main() { while(1) { printf("mysleep start...\ "); mysleep(3); printf("mysleep success\ "); } return 0; }</signal .h></stdio.h> Execution procedure
1) The main function calls the mysleep function. The mysleep function calls sigaction to register the handler function myhandler for the SIGALRM signal. The myhandler function here doesn't do anything, but it can't be less, because the default processing of the SIGALRM signal is to terminate the process. 2) Call alarm(timeout) to set the alarm. 3) Call pause to wait, the kernel switches to another process. 4) After timeout seconds, the alarm expires and the kernel sends SIGALRM to the process. 5) Before processing the user state of the process from the kernel state, the pending signal is processed, and the SIGALRM signal is found, and the processing function is myhandler. 6) Then switch to user mode to execute the myhandler function. When entering the myhandler function, the SIGALRM signal is automatically masked. When returning from the myhandler function, the SIGALRM signal is automatically unmasked. Then automatically execute the system call sigreturn to enter the kernel again, and then return to the main control flow of the user state to continue the execution process, that is, the main function calls the mysleep function.
This article describes the implementation of malloc and its malloc in the heap expansion operation,
First find the iptables file Generally under /etc/sysconfig Then vi iptables Just like add
caused by Linux clone virtual machine The virtual machine Vmware cloned a Red Hat Enterpris
vi editor is the most commonly used text editor in Linux system, vi has the reputation of the editor
Small note IptabLes and IptabLex virus cleanup process
Taglist and ctags installation in Ubuntu, simple and clear
Several Linux commands for understanding the performance of Linux systems
The difference between buffer and cache in linux free command
Linux can ping IP, ping nowhere domain
Linux ITDB+LDAP implements the functions that ITDB imports into LDAP users
Build your own Linux system reflection (2)
Linux resource limit level summary
Four shifting engines for the Googles BBR TCP congestion control algorithm
How to delete the extra options in the Win8 right-click menu
Cleverly install old sound card in Win XP
Where is the win10 control panel? Win10 official version of the control panel update open method
Win10 system prompts to activate windows10 to set to activate windows" reasons and solutions
How to add the taskbar notification icon to Win10 Preview?
Understand what software damage to the hard disk is
How can I repair the search function of XP computer?
Win7 "display driver has stopped responding and has recovered" situation analysis