Solving the IDEA intercourse error calling method [manageApp] occurs abnormalities, and error when starting the child level

2022-12-28   ES  

Author’s homepage (3 3 3 3 3 3 3 3):4 4 4 4 (Wang Wenbing) blog

This article:https://blog.csdn.net/HiWangWenBing/article/details/123376014


directory

Preface:

Common way of use

1. Use Sleep () and usleep ()

2. Use the semaphore sigalRM + Alarm ()

3. Use POSIX timer: Time.h

4. Use RTC Hardware Device (suitable for occasions with high real -time requirements)

5. Use Select ()


Foreword:

timer Timer application scenarios are very wide. Under Linux, there are several methods:

(1)sleep

(2)SIGALRM + alarm()

(3)POSIX time

(4) Use RTC Hardware Device

(5)select

Common use of timers

1. Use Sleep () and USLEEP ()

Among them, Sleep accuracy is1 second, USLEEP accuracy is1 subtle

The disadvantages of using this method are relatively obvious. In the Linux system, the Sleep class function cannot guarantee accuracy, especially when the system load is relatively large, Sleep generally has timeout.

while(1)

{

        sleep(1);

}

The periodic of the periodic tunes wake up, and the scheduling is executed.

2. Use the semaphore sigalRM + Alarm ()

The accuracy of this method can reach 1 second, of which the semaphore mechanism of the Linux system is used,

First register the signal quantity SIGALRM processing function, call alarm (), set the timing length, the code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#include <stdio.h>

#include <signal.h>

void timer_handler(int sig)

{

    if(SIGALRM == sig)

    {

        printf("timer\n");

        Alarm (1); // Send Alarm again, 1 represents alarm after 1S

    }

    return ;

}

int main()

{

    signal(SIGALRM, timer_handler); //relate the signal and function

    Alarm (1); // Trigger the tar_handler, 1 indicates the delay time of sending Alarm

Although the

    getchar();

    return 0;

}

Alarm method is very good, it cannot be firstbelow 1 secondaccuracy.

3. Use POSIX timer: Time.h

(1) Overview

The most powerful timer interface comes from the POSIX clock series. The action of creating, initialization, and deleting a timer is divided into three different functions:

  • Timer_create (): Create a timer
  • Timer_Settime (): Initialized timer
  • Timer_delete: Destroy the timer

(2) code example

#include <stdio.h> 
 #include <signal.h> 
 #include <time.h> 
 #include <string.h> 
 #include <stdlib.h> 
 #include <nistd.h> 
  
 #, The function will be called by the regular interrupt cycle 
 void timer_thread (Union Sigval V) 
 {{ 
     # v.sival_int information transmitted from the timer soft interruption/signal 
     Printf ("Timer_thread Function! %D \ n", v.sival_int); 
 } 
  
 int Main () 
 {{ 
     # 定 定 定 
     Timer_t Timerid; 

     # Time soft interrupt information 
     struct sigevent evp; 
     // Qing Zero initialization 
     MEMSET (& EVP, 0, SIZEOF (Struct Sigevent)); 
    
     # The creator of the timer convey the information of the processor of the given timer: 
     evp.sigev_value.sival_int = 111; 
     #, Start a new thread to call the callback function 
     evp.sigev_notify = SIGEV_THREAD; 
     # Time -to -callback function 
     evp.sigev_notify_function = Timer_thread; 
  
      # Create a timer 
     // int Timer_Create (CLOCKID_T CLOCKID, Struct SIGEVENT *EVP, Timer_t *Timerid); 
     // CLOCKID: The type of timer: 
     // Clock_realTime, CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID 
     // EVP-address of the environmental value, the structural member explains the notification method and processing method of the timer's expiration, etc. 
     // Timerid-The timer identifier of the user stored back 
    
     If (Timer_Create (CLOCK_REALTIME, & EVP, & Timerid) == -1) 
     {{ 
         Perror ("Fail To Timer_create"); 
         exit (-1); 
     } 
  
      # Set the parameter of the timer: 
     // XXX Int Timer_Settime (Timer_T Timerid, int Flags, Const Struct Itimerspec *New_value, Struct Itimerspec *Old_value) 
     // Timerid-timer logo 
     // FLAGS-0 means relative time, 1 means absolute time 
     // new_value-the new initial value and interval of the timer, as as the IT below 
     // OLD_VALUE-usually 0, that is, the fourth parameter is often null. If it is not NULL, the previous value of the timer 
      
     // For the first time IT.IT_VALUE is so long, every time IT.IT_INTERVAL is so long every time, that is to say, when it.it_value becomes 0, it will load the value of IT.IT_INTERVAL 
     Struct itimerspec it; 
     it.it_interval.tv_sec = 1; 
     it.it_interval.tv_nsec = 0; 
     it.it_value.tv_sec = 1; 
     it.it_value.tv_nsec = 0; 
  
     If (Timer_Settime (Timerid, 0, & It, NULL) == -1) 
     {{ 
         Perror ("Fail To Timer_Settime"); 
         exit (-1); 
     } 
  
     pause (); 
  
     Return 0; 
 } 

 # 当 The current counting value of the timer 
 /* 
  * int Timer_gettime (timer_t timerid, struct iTimerspec * curr_value); 
  * Get the value of the timer specified by TimerId, fill in CURR_VALUE 
  * 
  */

(3) The difference between CLOCK_MONOTONIC and CLOCK_REALTIME:

CLOCK_MONOTONIC:is Monotonic Time, Monotonic Time literally means monotonous time. In fact, it refers to the time after the system starts, which is recorded by variables Jiffies. At each start of the system, the Jiffies initialized to 0. Each time to a Timer Interrupt, Jiffies plus 1, which means that it represents the number of ticks that passed after the system starts. Jiffies must be monotonous.

CLOCK_REALTIME:is Wall Time. Wall Time literally means hanging clock time, which actually refers to the real time, which is recorded by variable Xtime. Each time the system is started, the RTC time on the CMOS is read into Xtime. This value is “the number of seconds experienced from 1970-01-01 and the nano-seconds experienced in this second”. Update Xtime.
WALL TIME is not necessarily monotonous. Because WALL TIME refers to the actual time in reality, if the system is to synchronize with a node in the network, or by the system administrator feels that this Wall Time is inconsistent with the actual time, it may change this Wall Time arbitrarily. The simplest example is that our users can go to any modification of the system. The time to be modified should be Wall Time, that is, Xtime, it can even be written in RTC and is saved permanently. Some application software may use this Wall Time. For example, I used to use VMware Workstation. As soon as the prompt was prompting the probation period, but as long as the system time was adjusted for one year in advance, and there would be no prompts. This is probably because it is because it is because it is because it is because When starting, use Gettimeofday to read Wall Time, and then determine whether it expires. As long as the Wall Time is changed, you can deceive it.

Therefore, if you need strict timers that are not affected by the system, you need to use itCLOCK_MONOTONIC type.

4. Use RTC Hardware Device (suitable for occasions with high real -time requirements)

RTC mechanism uses the Real Time Clock mechanism provided by system hardware interruptions,

By reading RTC hardware/dem/RTC, set the RTC frequency with IOCTL (), the code is as follows:

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

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

#include <stdio.h>

#include <linux/rtc.h>

#include <sys/ioctl.h>

#include <sys/time.h>

#include <sys/types.h>

#include <fcntl.h>

#include <unistd.h>

#include <errno.h>

#include <stdlib.h>

int main(int argc, char* argv[])

{

    unsigned long i = 0;

    unsigned long data = 0;

    int retval = 0;

    int fd = open ("/dev/rtc", O_RDONLY);

    if(fd < 0)

    {

        perror("open");

        exit(errno);

    }

    /*Set the freq as 4Hz*/

    if(ioctl(fd, RTC_IRQP_SET, 1) < 0)

    {

        perror("ioctl(RTC_IRQP_SET)");

        close(fd);

        exit(errno);

    }

    /* Enable periodic interrupts */

    if(ioctl(fd, RTC_PIE_ON, 0) < 0) 

    {

        perror("ioctl(RTC_PIE_ON)");

        close(fd);

        exit(errno);

    }

    while(1)

    {

         # periodic awakening by hardware

# 5 is blocked in synchronization of the kernel equipment.

        if(read(fd, &data, sizeof(unsigned long)) < 0)

        {

# Error treatment

            perror("read");

            close(fd);

            exit(errno);

        }

        printf("timer\n");

    }

    /* Disable periodic interrupts */

    ioctl(fd, RTC_PIE_OFF, 0);

    close(fd);

    return 0;

}

Advantages:This method is more convenient. The RTC provided by the system hardware is adjustable and very high.

Disadvantages:

(1)Need to write a kernel driver in the kernel space, and virtually produce a hardware device.

(2) User space program passesRead (FD, & Data, SIZEOF (UNSIGNED Long) blocks the device.

(3) Cyclone interrupt service procedure cycleAwakeningBlocking ReadUser space program.

Since the application is awakened by the kernel interrupt service program, the real -time nature is relatively high.

5. Use Select ()

This method is relatively unpopular, setting the timer by using Select () to set;

Principles use the fifth parameter of the Select () method, the first parameter is set to 0, the three file descriptors set are set to NULL, the fifth parameter is the time structure, the code is as follows:

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

#include <sys/time.h>

#include <sys/select.h>

#include <time.h>

#include <stdio.h>

/*seconds: the seconds; mseconds: the micro seconds*/

void setTimer(int seconds, int mseconds)

{

    struct timeval temp;

    temp.tv_sec = seconds;

    temp.tv_usec = mseconds;

# Select is always blocked and returned after timeout, playing a regular role.

    select(0, NULL, NULL, NULL, &temp);

    printf("timer\n");

    return ;

}

int main()

{

    int i;

    for(i = 0 ; i < 100; i++)

        setTimer(1, 0);

    return 0;

}

This method accuracy can reach the subtle level, and there are many on the Internet based on select ()Multi -threadtimer, indicating that select () stability is still very good.

Summary:If the system requirements are relatively low, you can consider using simple Sleep (), after all, a line of code can be solved;

If the system requirements are relatively high, the RTC mechanism and select () mechanism can be considered.


Author’s homepage (7 7 7 7 7 7 7 7):7 7 7 7 (Wang Wenbing) blog

This article website:

source

Related Posts

[python] Pandas implements two tables to check highlights, go to kinglake

Based on PostgreSQL and Postgis, Mars coordinate system, Baidu coordinate system, WGS84 coordinate system, CGCS2000 coordinate system

WeChat Mini Program Introduction to Weui component library (detailed successful case, with diagram)

qt (5.10.0) for Android

Solving the IDEA intercourse error calling method [manageApp] occurs abnormalities, and error when starting the child level

Random Posts

Android: Pass data and error information Parcel: Unable to Marshal Value when jumping with the Parcelable interface

Smartgit Ignore ignore the files that do not want to be managed by Git

python2.7 and 3.5 dual versions coexist and the use of PIP

bzoj4199 [luogup2178] wine tasting conference [noI2015] (suffix array+and check) Icefox

Collection -01 -Comparable and Comparators