为什么C语言中clock()返回-1
我正在尝试使用 "time.h"
库中的 clock()
函数来实现错误处理程序。该代码在嵌入式系统(Colibri IMX7 - M4 处理器)内运行。该函数用于监视特定范围内的电流值,如果电流值不正确,该函数应返回错误消息。
该函数将查看是否发生错误,并在第一次运行时将第一次出现的错误保存在clock_t
中作为参考,然后在下一次运行中如果错误仍然存在,则将使用clock()
将当前时间与之前的参考时间进行比较,看看它是否会比特定时间长。
问题是函数clock()
总是返回-1。我应该怎么做才能避免这种情况?另外,为什么我不能将 clock_t
变量声明为静态(例如 staticclock_t start_t =clock()
)?
请参阅下面的函数:
bool CrossLink_check_error_LED_UV_current_clock(int current_state, int current_at_LED_UV)
{
bool has_LED_UV_current_deviated = false;
static int current_number_of_errors_Current_LED_CANNON = 0;
clock_t startTimeError = clock();
const int maximum_operational_current_when_on = 2000;
const int minimum_turned_on_LED_UV_current = 45;
if( (current_at_LED_UV > maximum_operational_current_when_on)
||(current_state!=STATE_EMITTING && (current_at_LED_UV > minimum_turned_on_LED_UV_current))
||(current_state==STATE_EMITTING && (current_at_LED_UV < minimum_turned_on_LED_UV_current)) ){
current_number_of_errors_Current_LED_CANNON++;
if(current_number_of_errors_Current_LED_CANNON > 1) {
if (clock() - startTimeError > 50000){ // 50ms
has_LED_UV_current_deviated = true;
PRINTF("current_at_LED_UV: %d", current_at_LED_UV);
if(current_state==STATE_EMITTING){
PRINTF(" at state emitting");
}
PRINTF("\n\r");
}
}else{
if(startTimeError == -1){
startTimeError = clock();
}
}
}else{
startTimeError = 0;
current_number_of_errors_Current_LED_CANNON = 0;
}
return has_LED_UV_current_deviated;
}
编辑:我之前忘记提及,但我们使用 GCC 9.3.1 arm-none-eabi 编译器和 CMake 来构建可执行文件。我们有一个嵌入系统(由 Toradex 制造的 Colibri IMX7),由 2 个运行的 A7 处理器组成。我们的 Linux(更直观的界面)和用于控制我们设备的程序在没有操作系统的 M4 处理器中运行,只是纯裸机。
I'm trying to implement an error handler using the clock()
function from the "time.h"
library. The code runs inside an embeeded system (Colibri IMX7 - M4 Processor). The function is used to monitor a current value within a specific range, if the value of the current isn't correct the function should return an error message.
The function will see if the error is ocurring and in the first run it will save the first appearance of the error in a clock_t
as reference, and then in the next runs if the error is still there, it will compare the current time using clock()
with the previous reference and see if it will be longer than a specific time.
The problem is that the function clock()
is always returning -1. What should I do to avoid that? Also, why can't I declare a clock_t
variable as static (e.g. static clock_t start_t = clock()
?
Please see below the function:
bool CrossLink_check_error_LED_UV_current_clock(int current_state, int current_at_LED_UV)
{
bool has_LED_UV_current_deviated = false;
static int current_number_of_errors_Current_LED_CANNON = 0;
clock_t startTimeError = clock();
const int maximum_operational_current_when_on = 2000;
const int minimum_turned_on_LED_UV_current = 45;
if( (current_at_LED_UV > maximum_operational_current_when_on)
||(current_state!=STATE_EMITTING && (current_at_LED_UV > minimum_turned_on_LED_UV_current))
||(current_state==STATE_EMITTING && (current_at_LED_UV < minimum_turned_on_LED_UV_current)) ){
current_number_of_errors_Current_LED_CANNON++;
if(current_number_of_errors_Current_LED_CANNON > 1) {
if (clock() - startTimeError > 50000){ // 50ms
has_LED_UV_current_deviated = true;
PRINTF("current_at_LED_UV: %d", current_at_LED_UV);
if(current_state==STATE_EMITTING){
PRINTF(" at state emitting");
}
PRINTF("\n\r");
}
}else{
if(startTimeError == -1){
startTimeError = clock();
}
}
}else{
startTimeError = 0;
current_number_of_errors_Current_LED_CANNON = 0;
}
return has_LED_UV_current_deviated;
}
Edit: I forgot to mention before, but we are using GCC 9.3.1 arm-none-eabi compiler with CMake to build the executable file. We have an embedeed system (Colibri IMX7 made by Toradex) that consists in 2 A7 Processors that runs our Linux (more visual interface) and the program that is used to control our device runs in a M4 Processor without an OS, just pure bare-metal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于c标准库中提供的许多函数,如果您安装了文档(通常是随编译器一起安装的),您可以在shell中使用
man
命令查看文档。对于manclock
,它告诉我:这告诉我们-1意味着处理器时间(
CLOCK_PROCESS_CPUTIME_ID
)不可用。解决方案是使用CLOCK_MONOTONIC
代替。我们可以通过clock_gettime
选择我们想要使用的时钟。For a lot of provided functions in the c standard library, if you have the documentation installed (usually it gets installed with the compiler), you can view documentation using the
man
command in the shell. Withman clock
, it tells me that:This tells us that -1 means that the processor time (
CLOCK_PROCESS_CPUTIME_ID
) is unavailable. The solution is to useCLOCK_MONOTONIC
instead. We can select the clock we want to use withclock_gettime
.回答问题的第二部分:
不允许,因为函数
clock()
的返回值直到运行时才知道,但在 C 中,静态变量的初始值设定项必须是编译时持续的。您可以这样写:
但这可能适合也可能不适合在这种情况下使用,具体取决于零是否是函数的合法返回值。如果可以的话,您将需要类似的内容:
仅当您不能同时运行此函数的两个副本时(它不是可重入),上述内容才是可靠的。
如果您有可用的 POSIX 库,则可以使用 pthread_once_t 执行与上述 bool 相同的操作,但以可重入的方式进行。有关详细信息,请参阅
man pthread_once
。请注意,C++ 在这方面允许更复杂的选项,但您已经询问了 C。
另请注意,将“开始时间”缩写为
start_t
是一个非常糟糕的主意,因为后缀_t 表示“类型”,并且只能用于类型名称。
To answer the second part of your question:
is not allowed because the return value of the function
clock()
is not known until runtime, but in C the initializer of a static variable must be a compile-time constant.You can write:
But this may or may not be suitable to use in this case, depending on whether zero is a legitimate return value of the function. If it could be, you would need something like:
The above is reliable only if you cannot have two copies of this function running at once (it is not re-entrant).
If you have a POSIX library available you could use a pthread_once_t to do the same as the above bool but in a re-entrant way. See
man pthread_once
for details.Note that C++ allows more complicated options in this area, but you have asked about C.
Note also that abbreviating "start time" as
start_t
is a very bad idea, because the suffix_t
means "type" and should only be used for type names.最后的问题是,由于我们在裸机上运行代码,所以时钟()函数无法工作。我们最终在 M4 处理器上使用了我们发现的内部计时器,所以现在一切都很好。感谢您的回答。
in the end the problem was that since we are running our code on bare metal, the clock() function wasn't working. We ended up using an internal timer on the M4 Processor that we found, so now everything is fine. Thanks for the answers.