implement function to obtain active thread of a process
This commit is contained in:
parent
0e018fc6c8
commit
abe4af91e4
4 changed files with 63 additions and 11 deletions
|
|
@ -46,4 +46,10 @@ int determine_parent_process(process_status_t* list, size_t count, process_statu
|
|||
// get all process threads. for list and count same rules applies as for processes_by_name
|
||||
int process_get_threads(pid_t pid, process_status_t** list, size_t* count);
|
||||
|
||||
// returns 1 if state considered active for a process/thread
|
||||
int is_considered_active(process_state_t state);
|
||||
|
||||
// find any active (running) thread and returns 0 and success, otherwise non zero
|
||||
int find_active_thread(process_status_t* list, size_t count, process_status_t** thread);
|
||||
|
||||
#endif
|
||||
|
|
@ -42,7 +42,9 @@ __attribute__((noreturn)) void* slave3_job(void*)
|
|||
puts("[slave3] will do something each second but in a thread");
|
||||
while (1)
|
||||
{
|
||||
__asm__("nop");
|
||||
unsigned a = 3;
|
||||
for (unsigned i = 0; i < 100000; i++)
|
||||
a = a * 3 - a;
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
37
src/main.c
37
src/main.c
|
|
@ -1,3 +1,4 @@
|
|||
#define _DEFAULT_SOURCE
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
|
@ -36,21 +37,39 @@ int main(int argc, char** argv)
|
|||
|
||||
print_process(parent);
|
||||
|
||||
// get threads
|
||||
// find active thread
|
||||
puts("Looking for active thread..");
|
||||
|
||||
process_status_t* threads = NULL;
|
||||
size_t thread_count = 0;
|
||||
if (process_get_threads(parent->pid, &threads, &thread_count))
|
||||
|
||||
process_status_t* active;
|
||||
while (1)
|
||||
{
|
||||
fputs("failed to obtain process threads\n", stderr);
|
||||
free(list);
|
||||
return 1;
|
||||
if (process_get_threads(parent->pid, &threads, &thread_count))
|
||||
{
|
||||
fputs("failed to obtain process threads\n", stderr);
|
||||
free(list);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (find_active_thread(threads, thread_count, &active))
|
||||
{
|
||||
// no active threads - free list and continue
|
||||
free(threads);
|
||||
usleep(500*1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
// we got active thread!
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
puts("Threads:");
|
||||
for (size_t i = 0; i < thread_count; i++)
|
||||
print_process(&threads[i]);
|
||||
puts("Active thread:");
|
||||
print_process(active);
|
||||
|
||||
free(list);
|
||||
free(threads);
|
||||
free(list);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -67,6 +67,9 @@ static int is_numeric(const char* str)
|
|||
|
||||
int processes_by_name(const char* name, process_status_t** list, size_t* count)
|
||||
{
|
||||
*list = NULL;
|
||||
*count = 0;
|
||||
|
||||
DIR* proc = opendir("/proc");
|
||||
if (!proc) return 1;
|
||||
|
||||
|
|
@ -140,6 +143,9 @@ int determine_parent_process(process_status_t* list, size_t count, process_statu
|
|||
|
||||
int process_get_threads(pid_t pid, process_status_t** list, size_t* count)
|
||||
{
|
||||
*list = NULL;
|
||||
*count = 0;
|
||||
|
||||
char taskPath[256] = {0};
|
||||
snprintf(taskPath, sizeof(taskPath), "/proc/%d/task", pid);
|
||||
|
||||
|
|
@ -187,3 +193,22 @@ int process_get_threads(pid_t pid, process_status_t** list, size_t* count)
|
|||
closedir(taskDir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int is_considered_active(process_state_t state)
|
||||
{
|
||||
return state == INTERRUPTIBLE_SLEEP || state == RUNNING;
|
||||
}
|
||||
|
||||
int find_active_thread(process_status_t* list, size_t count, process_status_t** thread)
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
TRACE("task %d state %d\n", list[i].pid, list[i].state);
|
||||
if (is_considered_active(list[i].state))
|
||||
{
|
||||
*thread = &list[i];
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue