gpt4 book ai didi

c - 警报处理程序中 swapcontext 后的段错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:35:50 26 4
gpt4 key购买 nike

基本上,我正在尝试做的是在具有上下文切换的单个线程上模拟多线程。我每 10 微秒设置一个警报,然后将上下文从一个线程切换到另一个线程。问题是大约五分之一的运行在警报完成 swapcontext 后立即以段错误结束,至少这是我用 gdb 跟踪它的地方。

这是我的源文件主程序

    #include "umt.h"

void f()
{
int x = 10;
printf("starting thread\n");
while(x)
{
printf("thread %d\n", x);
sleep(1);
x--;
}

}

int main()
{
int x = 0, y, z;
umt_init();
y = umt_thread_create(f);
printf("starting main\n");
if(y == 0)
{
printf("Problems with creating thread\n");
return;
}
x = 10;
z = 1;
while(x)
{
printf("main\n");
x--;
}
umt_thread_join(y);
printf("done waiting\n");
return 0;
}

UMT.h

    #include <sys/time.h>
#include <stdio.h>
#include <signal.h>
#include <ucontext.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0

typedef struct _umt_thread
{
int thread_id;
ucontext_t context;
void (*handler)(void);
int hasFinished;
}umt_thread, *pumt_thread;

void umt_init();

int umt_thread_create(void (*handler)(void));

void umt_thread_join(int thr);

和 umt.c

    #include "umt.h"

#define MAIN_CONTEXT 0
#define STACK_SIZE 1638400

int currentThread;
char threadpool[15];

pumt_thread threads;

void signal_thread_finish();

void thread_handler()
{
threads[currentThread].handler();
signal_thread_finish();
}

void thread_scheduler();

void signal_thread_finish()
{
threads[currentThread].hasFinished = TRUE;
threadpool[currentThread] = 0;
thread_scheduler();
}

void thread_scheduler()
{
int nextThread = 0, curThread = 0;
int x = 0;
ucontext_t *con1, *con2;

nextThread = currentThread + 1;
while(1)
{
if(nextThread == 15)
nextThread = 0;
if(nextThread == currentThread)
break;
if(threadpool[nextThread] == 1)
break;
nextThread++;
}

if(nextThread == currentThread)
return;
curThread = currentThread;
currentThread = nextThread;
con1 = &(threads[curThread].context);
con2 = &(threads[nextThread].context);
x = swapcontext(con1, con2);
}

void umt_init()
{
ucontext_t context;
struct itimerval mytimer;
int i;
stack_t new_stack;

getcontext(&context);

threads = (pumt_thread)malloc(sizeof(umt_thread) * 15);
threads[MAIN_CONTEXT].thread_id = MAIN_CONTEXT;
threads[MAIN_CONTEXT].context = context;

threadpool[MAIN_CONTEXT] = 1;
for(i = 1;i<15;i++)
{
threadpool[i] = 0;
}

currentThread = 0;

new_stack.ss_sp = (char*)malloc(STACK_SIZE);
new_stack.ss_size = STACK_SIZE;
new_stack.ss_flags = 0;
i = sigaltstack(&new_stack, NULL);
if(i != 0)
{
printf("problems assigning new stack for signaling\n");
}

signal(SIGALRM, thread_scheduler);
mytimer.it_interval.tv_sec = 0;
mytimer.it_interval.tv_usec = 10;
mytimer.it_value.tv_sec = 0;
mytimer.it_value.tv_usec = 5;
setitimer(ITIMER_REAL, &mytimer, 0);
}

int umt_thread_create(void (*handler)(void))
{
ucontext_t context;
int i, pos;

for(i = 1;i<15;i++)
{
if(threadpool[i] == 0)
{
pos = i;
break;
}
}
if(i == 15)
{
printf("No empty space in the threadpool\n");
return -1;
}

if(getcontext(&context) == -1)
{
printf("Problems getting context\n");
return 0;
}
context.uc_link = 0;//&(threads[MAIN_CONTEXT].context);
context.uc_stack.ss_sp = (char*)malloc(STACK_SIZE);
if(context.uc_stack.ss_sp == NULL)
{
printf("Problems with allocating stack\n");
}
context.uc_stack.ss_size = STACK_SIZE;
context.uc_stack.ss_flags = 0;
makecontext(&context, thread_handler, 0);

threads[pos].thread_id = pos;
threads[pos].context = context;
threads[pos].handler = handler;
threads[pos].hasFinished = FALSE;

threadpool[pos] = 1;

printf("Created thread on pos %d\n", pos);

return pos;
}

void umt_thread_join(int tid)
{
while(!threads[tid].hasFinished)
{
}
}

我尝试了很多组合并尝试通过指令进行跟踪,但无法就可能导致此段错误的原因得出结论或想法。谢谢

最佳答案

我看到的问题很少(有些与段错误有关 + 一些其他评论)

  1. 您的调度程序 (thread_scheduler) 应该位于临界区,例如您应该阻止任何警报信号(或忽略它们),以便以不会破坏它的方式处理线程池。您可以使用 sigprocmask 或一个 volatile bool 变量来使警报静音(请注意,这与用户线程互斥量不同,只是与您的调度逻辑的内部同步)

  2. 恕我直言,您的时钟走得太快了,这是以微秒为单位,而不是毫秒,因此 tv_usec 的 1000 微秒对于测试目的可能更有意义。

  3. 较小的堆栈大小也可能导致段错误,但看起来您的堆栈足够大。

附注有一个更好的方法来处理连接,你目前在它上面浪费了很多 CPU 周期,为什么不简单地避免切换到调用连接的线程,直到它正在等待的线程终止?

关于c - 警报处理程序中 swapcontext 后的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19213101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com