2015-07-10 59 views
0

我正在用C/C++编写一个多线程程序,其目标是一个Linux机器。是否有可能在Linux [暂停]中检测到线程已进行上下文切换?

是否有可能检测到其中一个线程获取上下文切换(即暂停)?请注意,我不知道线程是否还活着,我想知道它是否正在运行。

+2

你如何考虑使用这些信息?你想从另一个线程中找到相同的可执行文件,或从不同的进程? – rici

+0

从不同的线程。我想从线程0检查线程1 ... N是否正在运行。我正在实施一个工作窃取算法。 – JohnTortugo

+4

这些信息在收到时会过时。 –

回答

0

能够当所述线程中的一个被切换上下文(即,暂停),以检测可以找到?

至于你的问题是否可能 - 它认为这是可能的。至少SystemTap(https://sourceware.org/systemtap/)可以做到这一点。

SystemTap的有一个脚本,让用户查看特定PID(https://sourceware.org/systemtap/examples/profiling/sched_switch.stp)上下文切换:

probe scheduler.ctxswitch 
{ 
    if (target_pid != 0 
     && next_pid != target_pid 
     && prev_pid != target_pid) 
      next 

    if (target_name != "" 
     && prev_task_name != target_name 
     && next_task_name != target_name) 
      next 

    printf("%-16s%5d %5d %5d:%5d:%s ==> %5d:%5d:%s %-16s\n",prev_task_name, 
     task_cpu(prev_task),gettimeofday_ns(),prev_pid,prev_priority,state_calc(prevtsk_state),next_pid, 
     next_priority,state_calc(nexttsk_state),next_task_name) 
} 

据我知道的SystemTap编译STP文件,把那么在Linux内核,并显示您的信息。

我不知道如何在用户空间和C++程序中获取这些信息。但它似乎也有可能(http://netsplit.com/tracing-on-linux):

其他跟踪事件可用于用户空间!我们并不需要 写内核模块可以挂接到他们,但显然我们 只能读取数据这样

因此,这将是巨大的,如果你能发现它,稍后回答自己题。