This book has been bought for a long time, but has never been carefully read. Recently, the time spent on the game is relatively large. However, it has come to an end for the time being. I started to transfer some time to reading books. After reading the optimization part over the weekend, the main example in the book is a cyclic value calculation. there are indeed many performance bottlenecks in loop iteration.
Simple Example:
For (int I; I <Max; I ++)
{
Float F = f * F; (1)
Float sum + = J [I] + F;
}
Compared to putting (1) out of the loop, it is a significant waste of performance. This is an obvious optimization
In addition, the number of reads and writes of registers is analyzed from the perspective of assembly. For example:
Fun (int * P)
{
Int sum;
For (int I; I <Max; I ++)
{
Sum = I + * P; // the pointer P needs to be set every time, and an operation to read the register is performed multiple times.
}
}
Relative:
Fun (int * P)
{
Int sum;
Int temp = * P;
For (int I; I <Max; I ++)
{
Sum = I + temp; // This saves operation
}
}
Modern CPUs all execute commands in disorder. That is to say, it is not necessarily sequential to decode the CPU into commands and put them in the instruction high-speed buffer zone and then send multiple corresponding execution units.
<2> Process Control
Get process ID: getpid
Get the parent process ID: getppid
Exit Process: Exit
Create a sub-process: fork is more interesting. One call returns two times, and the sub-process returns 0. The parent process returns the sub-process ID.
Wait for the sub-process to terminate: waitpid: the exit status of the sub-process can be determined through the status (normal exit, or due to exceptions, etc.: Example:
While (pid = waitpid (-1, & status, 0)> 0)
If (wifexited (Status) // returns true if the sub-process is terminated normally
....
The next step is to sleep the processes in execution: Calling commonly used sleep (x) can suspend the process for a period of time: or directly calling pause to sleep the process until a signal is received.
* Load and run the program: execve (filename, argv, envp );
* Signal: a higher level exception that allows the process to interrupt other processes. Unlike previous context switches (based on previously mentioned exceptions such as the termination of an interrupt fault and other underlying exceptions, the operating system uses context to implement multiple tasks, that is, to keep the status of a process, switch to another process and then return)
Linux has 30 different types of signals: sighup suspended
* Sending signal: Kill-9 15213 sending signal 9 to process ID: 15213
Alarm (x) sends a sigalrm signal to the calling process at intervals. How to receive signals:
You can see the following function:
Typedef void handler_t (INT );
Handler_t * signal (int signum, handler_t * Handler );