简体   繁体   English

系统+ usleep +多线程= SEGV / SIGILL

[英]system + usleep + multi-thread = SEGV / SIGILL

I am using kernel version 2.6.10 and "plain" C. 我正在使用内核版本2.6.10和“普通”C。

I have narrowed-down to two threads on why our program crashes. 关于程序为什么崩溃,我将范围缩小到两个线程。

TIMER THREAD 定时器线

void TimerThread(void)
{
    while (exec) {
        usleep(10000);

        RSLTCD r = SUCCESS;
        // r = LockMutex(Mtx);
        do {
              // some process
        } while(1);     
        // r = UnlockMutex(Mtx);
    }
    return ;        // Not Reached
}

SYSTEM THREAD 系统线程

void SystemThread(void)
{
    CreateThread(TimerThread, OALTHRD_DEFAULT_STACKSIZE, THREADPRI_NORMAL, 0, 0);
    for(;;){
        system("echo this is a SYSTEM CALL 1");
        system("echo this is a SYSTEM CALL 2");
    }    
}

I have remove all the rest of the code and even with just running this two "simple" threads, the program will crash after a few loops(50-100). 我已经删除了所有其余代码,即使只运行了这两个“简单”线程,该程序也会在几次循环(50-100)之后崩溃。 I have also read that system() uses fork() and there are sometimes trouble with fork()ing and mutexes. 我还读过system()使用fork(),有时在fork()和互斥锁方面会遇到麻烦。 So I removed the mutex part and still it crashes. 因此,我删除了互斥锁部分,但仍然崩溃。

I tried making my own "system call" function using fork()+execv() combo and also posix_spawn(). 我尝试使用fork()+ execv()组合以及posix_spawn()进行自己的“系统调用”功能。 Both cases resulted with the same result. 两种情况的结果均相同。

Also, I have tried using other sleep functions than usleep.(nano_sleep, etc) Still the programs crashes 另外,我尝试使用usleep以外的其他睡眠功能。(nano_sleep等)程序仍然崩溃

Is there trouble with using system() and usleep() in multi-thread processes? 在多线程进程中使用system()和usleep()是否有麻烦? What other alternatives do I have other than using these functions? 除了使用这些功能,我还有什么其他选择?

Basically it is not easy to mix multi-threading and "fork()" and a lot more details about your SW would be needed to provide useful help. 基本上,将多线程和“ fork()”混用起来并不容易,并且需要大量有关您的软件的详细信息才能提供有用的帮助。 Please read http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them for description of the problems when mixing threads and fork(). 请阅读http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them ,以了解混合线程和fork()时遇到的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM