implement function to obtain active thread of a process

This commit is contained in:
mykola2312 2024-07-20 23:53:50 +03:00
parent 0e018fc6c8
commit abe4af91e4
4 changed files with 63 additions and 11 deletions

View file

@ -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 // 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); 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 #endif

View file

@ -42,7 +42,9 @@ __attribute__((noreturn)) void* slave3_job(void*)
puts("[slave3] will do something each second but in a thread"); puts("[slave3] will do something each second but in a thread");
while (1) while (1)
{ {
__asm__("nop"); unsigned a = 3;
for (unsigned i = 0; i < 100000; i++)
a = a * 3 - a;
sleep(1); sleep(1);
} }
} }

View file

@ -1,3 +1,4 @@
#define _DEFAULT_SOURCE
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -36,21 +37,39 @@ int main(int argc, char** argv)
print_process(parent); print_process(parent);
// get threads // find active thread
puts("Looking for active thread..");
process_status_t* threads = NULL; process_status_t* threads = NULL;
size_t thread_count = 0; 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); if (process_get_threads(parent->pid, &threads, &thread_count))
free(list); {
return 1; 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:"); puts("Active thread:");
for (size_t i = 0; i < thread_count; i++) print_process(active);
print_process(&threads[i]);
free(list);
free(threads); free(threads);
free(list);
return 0; return 0;
} }

View file

@ -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) int processes_by_name(const char* name, process_status_t** list, size_t* count)
{ {
*list = NULL;
*count = 0;
DIR* proc = opendir("/proc"); DIR* proc = opendir("/proc");
if (!proc) return 1; 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) int process_get_threads(pid_t pid, process_status_t** list, size_t* count)
{ {
*list = NULL;
*count = 0;
char taskPath[256] = {0}; char taskPath[256] = {0};
snprintf(taskPath, sizeof(taskPath), "/proc/%d/task", pid); snprintf(taskPath, sizeof(taskPath), "/proc/%d/task", pid);
@ -186,4 +192,23 @@ int process_get_threads(pid_t pid, process_status_t** list, size_t* count)
closedir(taskDir); closedir(taskDir);
return 0; 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;
} }