Real-time process

I. Basic Concepts

1. CPU affinity

Description: CPU affinity (affinity) refers to the binding relationship between a process and cpu, is a scheduling feature, divided into soft and hard affinity affinity

(1) soft affinity

  A process will always be scheduled on the same processor, migration between processor load unless it can be considered in the case of very uneven

(2) Hard affinity

  A process will be directly binding on the specified processor

Usually the maximum value of the host CPU is 1024 (0-1023), and a common processor system No. 0-3, i.e. four processors

API functions related to:

. 1  #define _GNU_SOURCE    
 2   #include <sched.h> 
. 3 int sched_setaffinity (PID pid_t, size_t cpusetsize, const * cpu_set_t mask); // set the process affinity
. 4 int sched_getaffinity (PID pid_t, size_t cpusetsize, cpu_set_t * mask); // get cpu affinity

2. Real-Time Systems

Description: If a system is limited to the "open time", i.e. the number of requests and responses between a minimum and must be performed, the system is called real-time system, real-time systems hardware and software into real-time systems

(1) hardware real-time systems

  Hard real-time systems requires not only perform the task correctly, but also to be punctual arrival time that is specified in the task must be completed, otherwise the system would have disastrous consequences

(2) Real-Time System Software

  Soft real-time systems it refers to the purpose of the system is to enable execution of each task as quickly as possible, without requiring a limit of a task is completed how long, that is, the system exceeds the allotted time will not have a huge impact on the system, The system can still run

A more detailed explanation can refer https://www.cnblogs.com/CodeWorkerLiMing/p/11508899.html

 

3. Real-time process

Description: Relative to non-real-time processes, real-time process has priority, by calling the system can be in a non-critical region of the state of implementation of non-real-time process, the first higher-priority process calls

And a host of top command can be used to view the system processes the operation

 

II. Examples

1. Requirements

  Create multiple sub-processes, select one or more processes which run on the specified cpu, observe the operation

2. related API functions

. 1  #define _GNU_SOURCE
 2   #include <sched.h>
 . 3  
. 4   void CPU_ZERO (cpu_set_t * SET ); // erase
 . 5  
. 6   void CPU_SET ( int CPU, cpu_set_t * SET ); // Set the CPU SET
 . 7   void CPU_CLR ( int cpu, cpu_set_t * set ); // remove the CPU set
 8   int   CPU_ISSET ( int cpu, cpu_set_t * set ); // test whether a set cpu

 

3. Code

 1 #define _GNU_SOURCE
 2 #include<stdio.h>
 3 #include <stdlib.h>
 4 #include <time.h>
 5 #include <sched.h>
 6 #include <sys/types.h>
 7 #include <unistd.h>
 8 
 9 
10 
11 int main(){
12     cpu_set_t set;
13     size_t size;
14     
15     CPU_ZERO(&set);  //清空cpu的set
16     
17     int ret = sched_getaffinity(0, sizeof(size), &set);
18     for(int i = 0; i < CPU_SETSIZE; i++){
19        int cpu = CPU_ISSET(i, &set);    //测试cpu是否set
20        if(cpu != 0) printf("cpu %d is set\n", i);
21        else printf("cpu %d is unset\n", i);
22     }
23 
24    CPU_CLR(0, &set);  //移除0号cpu的set
25    ret = sched_setaffinity(0 , the sizeof (size), & SET );
 26 is  
27     // creates 10 sub-process 
28  
29     int PID, K;
 30     for ( int I = 0 ; I < 10 ; I ++ ) {
 31 is        PID = the fork ();
 32        K = I;
 33 is        IF (PID == 0 ) BREAK ;
 34 is     }
 35  
36     // the 6th sub-process to run on a number of 0 cpu 
37 [     IF (PID == 0 && K == 6 ) {
38       CPU_ZERO(&set);
39       CPU_SET(0, &set);
40       sched_setaffinity(0, sizeof(size), &set);
41    }
42 
43    while(1){
44        sleep(10);
45    }
46 
47     return 0;
48 }

Guess you like

Origin www.cnblogs.com/spontanous/p/12116666.html