static timer_t timer;
void timer_handle(union sigval sig)
{
printf("pthread=%lu ptr=%p\n", pthread_self(), sig.sival_ptr);
}
void x_add_timer(void)
{
struct sigevent event;
struct itimerspec ts = {{0, 0}, {0, 10000}};
memset(&event, 0, sizeof(event));
event.sigev_notify = SIGEV_THREAD;
event.sigev_notify_function = timer_handle;
timer_create(CLOCK_MONOTONIC, &event, &timer);
timer_settime(timer, 0, &ts, NULL);
}
void x_del_timer(void)
{
timer_delete(timer);
}
int main()
{
int i;
struct timespec t = {0, 8000};
for (i = 0; i < 100; i++) {
x_add_timer();
nanosleep(&t, NULL);
x_del_timer();
}
return 0;
}
我是Linux编程的新手。我正在学习glibc计时器。但我遇到一个奇怪的问题。
我编写上面的代码并使用 mips64-octeon-linux-gnu-gcc 进行编译。
但在设备上运行时会发生分段故障有时
代码有什么问题吗?
非常感谢。
coredump是
Program terminated with signal 11, Segmentation fault.
[New process 16487]
[New process 16443]
[New process 16444]
#0 0x0000005558155568 in main_arena () from /lib64/libc.so.6
完全回溯是
Thread 3 (process 16444):
#0 0x00000055580c839c in clone () from /lib64/libc.so.6
No symbol table info available.
#1 0x0000005558176d38 in do_clone () from /lib64/libpthread.so.0
No symbol table info available.
#2 0x0000005558177260 in pthread_create@@GLIBC_2.2 ()
from /lib64/libpthread.so.0
No symbol table info available.
#3 0x0000005557fb8cdc in timer_helper_thread () from /lib64/librt.so.1
No symbol table info available.
#4 0x0000005558177cec in start_thread () from /lib64/libpthread.so.0
No symbol table info available.
#5 0x00000055580c83ec in __thread_start () from /lib64/libc.so.6
No symbol table info available.
Thread 2 (process 16443):
#0 0x000000555808f0e4 in nanosleep () from /lib64/libc.so.6
No symbol table info available.
#1 0x000000555808edfc in sleep () from /lib64/libc.so.6
No symbol table info available.
#2 0x0000000120000f54 in main () at hello.c:53
i = 100
t = {tv_sec = 0, tv_nsec = 8000}
Thread 1 (process 16487):
#0 0x0000005558155568 in main_arena () from /lib64/libc.so.6
No symbol table info available.
#1 0x0000005557fb8d5c in timer_sigev_thread () from /lib64/librt.so.1
No symbol table info available.
#2 0x0000005558177cec in start_thread () from /lib64/libpthread.so.0
No symbol table info available.
#3 0x00000055580c83ec in __thread_start () from /lib64/libc.so.6
No symbol table info available.
答案 0 :(得分:0)
那里你有一个明确的竞争条件。
速度更快(睡眠时间更短10us
),
nanosleep(&t, NULL);
其中t设置为timespec {0, 8000};
,在&#39;计时器之前返回&#39;火灾。
因此,x_del_timer()
和timer_handle()
发生的顺序错误。
增加nanosleep()时间以降低这种可能性。
如果glibc
对OCTEON/mips64
的支持与计时器API存在相同的问题,我不会感到惊讶。