Linux-MySleep function implementation and race condition

  
        (1) The overall framework of the function implementation
1> The function interface 2 needed to be used> The overall process idea of ​​the implementation 3> The analysis of the possible operation of the program operation 4> Optimization results and solutions (II) Three main Function Interface

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:
Function Name
Parameter
Return Value
alarm() unsigned int Seconds returns 0 or the remaining time after the signal response pause() void only the error return value sigaction() (int signo, const struct sigaction *restrict act, struct sigaction *restrict oact) No research here sigaction structure

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.

Copyright © Windows knowledge All Rights Reserved