1. Let the processor go
In Linux, sched_yield takes the initiative to grant execution permission to a system calling and running process. The process is running well. Why does this function need? One case is the locking of the user space thread. If a thread tries to obtain the lock held by another thread, the new thread should let the processor know that the lock becomes available. The user space lock does not support the kernel, which is the most single and efficient way. However, a futexes optimization solution is introduced in Linux threads.
In another case, sched_yield can be called periodically in a processor-intensive program to minimize the impact of the process on the system. In any case, how to schedule a program should be a system task, rather than being managed by the process itself. Eg:
Int main () {int ret, I; ret = sched_yield (); If (ret =-1) {printf ("failed to call sched_yield! \ N ") ;}return 0 ;}
How does one call the kernel? In versions earlier than 2.6, sched_yield has a very small impact. If another running process exists, the kernel switches to the process and places the called process at the end of the list of runable processes. In the short term, the kernel will re-schedule the process. In this case, table tennis may occur, that is, two programs run back and forth until they all run. Some changes have been made in version 2.6:
1. If the process is RR, put it at the end of the runable process and return it.
2. Otherwise, remove it from the list of processes that can be run and put it into the list of processes that expire, so that the process will not run until other processes run out of time slices.
3. Find another process to be executed from the list of executable processes.
2. Process Priority
Looking at CFS, we can see that the Nice value of the process determines how long the process will run, or the percentage occupied. You can set and obtain the nice value of a process by calling nice. The value range is-20 ~ 19. The lower the value, the higher the priority (put on the denominator when calculating the virtual time). The real-time process should be negative. For example:
Int main () {int ret, I; ret = nice (0); printf ("nice value of the current process: % d \ n", RET ); ret = nice (10); printf ("nice value of the current process: % d \ n", RET); Return 0 ;}
Because RET can be-1, we need to combine RET and errno when determining whether the system call fails. Two other system calls can be set more flexibly. getpriority can obtain the highest priority among Process Groups and users in any process. Setpriority sets the priority of all specified processes to Prio, eg:
int main(){ int ret, i; ret = getpriority(PRIO_PROCESS, 0); printf("nice value:%d\n", ret); ret = setpriority(PRIO_PROCESS, 0, 10); ret = getpriority(PRIO_PROCESS, 0); printf("nice value:%d\n", ret); return 0;}
A process has a priority for execution on the processor or for data transmission: I/O priority. Linux has two additional system calls available for display setting and obtaining I/O nice value, but has not yet been exported:
Int ioprio_get (INT which, int who );
Int ioprio_set (INT which, int who, int ioprio );
3. processor affinity
Linux supports a single system with multiple processors. On SMP, the system has two challenges:
1. The scheduler must make full use of all the processors.
2. It is costly to switch the processor for running the program.
The process inherits the processor affinity of the parent process. Linux provides two system calls for obtaining and setting "Hard affinity ". Eg:
Int main () {int ret, I; cpu_set_t set; cpu_zero (& set); ret = sched_getaffinity (0, sizeof (cpu_set_t), & set ); if (ret =-1) printf ("Call failed! \ N "); for (I = 0; I <10; I ++) {int CPU = cpu_isset (I, & set ); printf ("CPU = % I is % s \ n", I, CPU? "Set": "Unset");} cpu_zero (& set); cpu_set (0, & set); cpu_clr (1, & set); ret = sched_setaffinity (0, sizeof (cpu_set_t), & set); If (ret =-1) printf ("Call failed! \ N "); for (I = 0; I <10; I ++) {int CPU = cpu_isset (I, & set ); printf ("CPU = % I is % s \ n", I, CPU? "Set": "Unset");} return 0 ;}4. Linux scheduling policies and priorities
The difference between several Scheduling Methods of processes in Linux and those of processes is not described here. Here we focus on how to obtain and set these values. You can use sched_getscheduler to obtain the scheduling policy of a process. For example:
int main(){ int ret, i; struct sched_param sp; sp.sched_priority = 1; ret = sched_setscheduler(0, SCHED_RR, &sp); if(ret == -1) printf("sched_setscheduler failed.\n"); if(errno == EPERM) printf("Process don't the ability.\n"); ret = sched_getscheduler(0); switch(ret){ case SCHED_OTHER: printf("Policy is normal.\n"); break; case SCHED_RR: printf("Policy is round-robin.\n"); break; case SCHED_FIFO: printf("Policy is first-in, first-out.\n"); break; case -1: printf("sched_getscheduler failed.\n"); break; default: printf("Unknow policy\n"); } return 0;}
The sched_getparam and sched_setparam interfaces can be used to obtain and set a preset policy. Here, not only the ID of a policy is returned, for example:
int main(){ int ret, i; struct sched_param sp; sp.sched_priority = 1; ret = sched_setparam(0, &sp); if(ret == -1) printf("sched_setparam error.\n"); ret = sched_getparam(0, &sp); if(ret == -1) printf("sched_getparam error.\n"); printf("our priority is %d.\n", sp.sched_priority); return 0;}
In Linux, two system calls are provided for obtaining valid priority values, and the maximum and minimum values are returned, for example:
int main(){ int ret, i; struct sched_param sp; ret = sched_get_priority_min(SCHED_RR); if(ret == -1) printf("sched_get_priority_min error.\n"); printf("The min nice value is %d.\n", ret); ret = sched_get_priority_max(SCHED_RR); if(ret == -1) printf("sched_get_priority_max error.\n"); printf("The mmax nice value is %d.\n", ret); return 0;}
With regard to time slice, this concept may be quite different from the traditional operating system courses in Linux. If you are interested, you can look at the concept in CFS. The length of the time slice allocated to the PID can be obtained through sched_rr_get_interval. For example:
int main(){ int ret, i; struct timespec tp; ret = sched_rr_get_interval(0, &tp); if(ret == -1) printf("sched_rr_get_interval error.\n"); printf("The time is %ds:%ldns.\n", (int)tp.tv_sec, tp.tv_nsec); return 0;}5. Preventive measures for Real-Time Processes
Due to the nature of Real-Time Processes, developers should exercise caution when developing and debugging such programs. If a real-time process suddenly loses its temper, the system's response will suddenly slow down. Any CPU-intensive loop continues to run endlessly in a real-time program, as long as there is no higher priority, real-time processes become runable. Therefore, be cautious when designing real-time programs. Such programs are supreme and can be easily transferred across the entire system. The following are some issues and precautions:
1. Because real-time processes can use all resources on the system, be careful not to let other processes in the system wait for processing time.
2. The loop may run until the end.
3. Be careful when waiting, that is, the real-time process waits for resources occupied by a low-priority process.
4. when developing a real-time process, keep a terminal on and run the real-time process with a higher priority. In an emergency, the terminal still responds, allowing you to terminate the out-of-control real-time process.
5. Use CHRT to set and obtain real-time attributes.
6. resource restrictions
Linux imposes several resource limits on processes, which are the maximum number of kernel resources that a process can consume. The restrictions are as follows:
1. rlimit_as: The maximum address space.
2. rlimit_core: the maximum size of core files.
3. rlimit_cpu: Maximum CPU consumption time.
4. rlimit_data: the upper limit of the Data Segment and heap.
5. rlimit_fsize: the maximum size of files that can be created.
6. rlimit_locks: Maximum number of file locks.
7. rlimit_memlock: Maximum number of bytes that a process without cap_sys_ipc can lock into memory.
8. rlimit_msgqueue: the number of bytes that can be allocated in the message queue.
9. rlimit_nice: you can adjust your friendly value to a lower value at most.
10. rlimit_nofile: Maximum number of file descriptors.
11. rlimit_nproc: the maximum number of processes on the system.
12. rlimit_rss: the number of pages in the memory is online.
13. rlimit_rtprio: the upper limit of the real-time priority that can be requested by a process that does not have the cap_sys_nice capability.
14. rlimit_sigpending: the upper limit of semaphores in the queue, Which is exclusive to Linux.
15. rlimit_stack: Maximum stack size.
I will not talk about this much. I will take a closer look at it when it comes to practical use. For example:
int main(){ int ret, i; struct rlimit rlim; rlim.rlim_cur = 32*1024*1024; rlim.rlim_max = RLIM_INFINITY; ret = setrlimit(RLIMIT_CORE, &rlim); ret = getrlimit(RLIMIT_CORE, &rlim); if(ret == -1) printf("getrlimit error.\n"); printf("RLIMIT_CORE limits: soft=%ld hard=%ld\n", rlim.rlim_cur, rlim.rlim_max); return 0;}