系统函数调用时的分段错误

时间:2016-07-06 18:49:04

标签: c gcc segmentation-fault pthreads ubuntu-14.04

我试图找到当我在涉及系统功能时尝试生成Signal(^ C)时发生的分段错误错误的原因。

另外,我试图理解为什么我只看到一个打印Thread1 cancelled而不是所有三个线程的3个打印件? [Corrected the code for this point]

以下是testapp.c的代码。这是我为测试my utility program而做的一种压力测试应用程序。该实用程序不是我正在使用的实际程序,而是演示其中使用的信号量。它正在使用system()函数执行我的实用程序。

下面列出了源代码和输出,

/*******  testapp.c  *****************/
/* gcc testapp.c -o testapp -pthread */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <errno.h>

#define SLEEP1  250
#define SLEEP2  250
#define SLEEP3  250
pthread_t   thread1;
pthread_t   thread2;
pthread_t   thread3;
static volatile sig_atomic_t isRunning = 1;

void signal_handler(int signal)
{
    int rc = 0;
    switch (signal)
    {
        case SIGINT:
        case SIGTERM:
        case SIGQUIT:
            printf("Signal generated, cancelling threads...");
            // Graceful shutdown
            //isRunning = 0; //commented to see the segmentation fault issue
            rc = pthread_cancel(thread1);
            if(rc != 0){
                printf("signal_handler:pthread_cancel-1 failed - %d (%m)\n", errno);
            }
            printf("Thread1 cancelled\n");
            rc = pthread_cancel(thread2);
            if(rc != 0){
                printf("signal_handler:pthread_cancel-2 failed - %d (%m)\n", errno);
            }
            printf("Thread2 cancelled\n");
            rc = pthread_cancel(thread3);
            if(rc != 0){
                printf("signal_handler:pthread_cancel-3 failed - %d (%m)\n", errno);
            }
            printf("Thread3 cancelled\n");
            break;
        default:
            break;
    }
}

void* thread1_worker(void* data)
{
    struct timeval timeout;
    int ret = 0;
    do
    {
        printf("Requesting cmd1\n");
        ret = system("util -c:cmd1 -v 2>> /tmp/stderr.log");

        timeout.tv_sec  = 0;
        timeout.tv_usec = SLEEP1 * 1000;  //milliseconds
        ret = select(0, NULL, NULL, NULL, &timeout);
    }while(isRunning);

    printf("Exiting thread1...");
    pthread_exit((void*)0);
}

void* thread2_worker(void* data)
{
    struct timeval timeout;
    int ret = 0;
    do
    {
        printf("Requesting cmd2\n");
        ret = system("util -c:cmd2 -v 2>> /tmp/stderr.log");

        timeout.tv_sec  = 0;
        timeout.tv_usec = SLEEP2 * 1000;  //milliseconds
        int ret = select(0, NULL, NULL, NULL, &timeout);
    }while(isRunning);

    printf("Exiting thread2...");
    pthread_exit((void*)0);
}

void* thread3_worker(void* data)
{
    struct timeval timeout;
    int ret = 0;
    do
    {
        printf("Requesting cmd3\n");
        ret = system("util -c:cmd3 -v 2>> /tmp/stderr.log");

        timeout.tv_sec  = 0;
        timeout.tv_usec = SLEEP3 * 1000;  //milliseconds
        int ret = select(0, NULL, NULL, NULL, &timeout);
    }while(isRunning);

    printf("Exiting thread3...");
    pthread_exit((void*)0);
}

int main(int argc, char **argv){
    signal(SIGTERM, signal_handler);
    signal(SIGINT, signal_handler);
    signal(SIGQUIT, signal_handler);

    printf("Starting threads...\n");
    pthread_create(&thread1, NULL, thread1_worker, (void*) NULL);
    pthread_create(&thread2, NULL, thread2_worker, (void*) NULL);
    pthread_create(&thread3, NULL, thread3_worker, (void*) NULL);

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);
    return 0;
}

输出如下,方括号中的打印件是程序的实际输出而不是上面的util程序,

root@mydevice:~$ testapp 
Starting threads...
Requesting cmd1
Requesting cmd2
Requesting cmd3
cmd2:[05 05]
cmd1:[0]
cmd3:[00 00 00 00 00 ]
Requesting cmd2
Requesting cmd1
cmd2:[05 05]
Requesting cmd3
cmd1:[1]
cmd3:[00 00 00 00 00 ]
Requesting cmd2
Requesting cmd1
cmd2:[05 05]
Requesting cmd3
cmd1:[2]
cmd3:[00 00 00 00 00 ]
Requesting cmd2
Requesting cmd1
cmd2:[05 05]
Requesting cmd3
cmd1:[3]
cmd3:[00 00 00 00 00 ]
Requesting cmd2
Requesting cmd1
cmd2:[05 05]
Requesting cmd3
^Ccmd1:[4]
sh: line 1:   964 Segmentation fault      util -c:cmd3 -v 2>> /tmp/stderr.log
Requesting cmd2
cmd2:[05 05]
Requesting cmd1
Requesting cmd3
cmd1:[5]
cmd3:[00 00 00 00 00 ]
Requesting cmd2
cmd2:[05 05]
Requesting cmd1
Requesting cmd3
cmd3:[00 00 00 00 00 ]
Requesting cmd2
cmd1:[84213760]
Requesting cmd3
^Ccmd2:[00 00]  -----------------------------------------------> Signal
cmd3:[00 00 00 00 00 ]
Requesting cmd1
cmd1:[7]
Requesting cmd2
Requesting cmd3
cmd2:[05 05]
cmd3:[00 00 00 00 00 ]
Requesting cmd1
cmd1:[8]
^CSignal generated, cancelling threads...Thread1 cancelled ---->Signal
Segmentation fault
root@mydevice:~$ 

2 个答案:

答案 0 :(得分:3)

这只是一个“错字”错误。

变化:

    pthread_create(&thread1, NULL, thread1_worker, (void*) NULL);
    pthread_create(&thread1, NULL, thread2_worker, (void*) NULL);
    pthread_create(&thread1, NULL, thread3_worker, (void*) NULL);

分为:

    pthread_create(&thread1, NULL, thread1_worker, (void*) NULL);
    pthread_create(&thread2, NULL, thread2_worker, (void*) NULL);
    pthread_create(&thread3, NULL, thread3_worker, (void*) NULL);

答案 1 :(得分:1)

根据评论结果纠正代码:

/*******  testapp.c  *****************/
/* gcc testapp.c -o testapp -pthread */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <errno.h>

#define SLEEP1  250
#define SLEEP2  250
#define SLEEP3  250

pthread_t   thread1;
pthread_t   thread2;
pthread_t   thread3;

static volatile sig_atomic_t isRunning = 1;

void signal_handler(int signal)
{
    int rc = 0;
    char buffer[1024];

    switch (signal)
    {
        case SIGINT:
        case SIGTERM:
        case SIGQUIT:
            write( 1, "Signal generated, cancelling threads...\n");
            // Graceful shutdown
            //isRunning = 0; //commented to see the segmentation fault issue
            rc = pthread_cancel(thread1);
            if( rc )
            {
                write(1, "signal_handler:pthread_cancel-1 failed\n" ):
            }

            write( 1, "Thread1 cancelled\n");


            rc = pthread_cancel(thread2);
            if( rc )
            {
                write( 1, "signal_handler:pthread_cancel-2 failed\n");
            }

            write( 1, "Thread2 cancelled\n");


            rc = pthread_cancel(thread3);
            if( rc )
            {
                write( 1, "signal_handler:pthread_cancel-3 failed\n");
            }

            "Thread3 cancelled\n");
            break;

        default:
            break;
    } // end switch
} // end function: signal_handler


void* thread1_worker(void* data)
{
    struct timeval timeout;
    int ret = 0;

    (void)data;

    do
    {
        printf("Requesting cmd1\n");
        ret = system("util -c:cmd1 -v 2>> /tmp/stderr.log");
        if( ret )
        {
            perror( "system-util result" );
        }

        timeout.tv_sec  = 0;
        timeout.tv_usec = SLEEP1 * 1000;  //milliseconds
        select(0, NULL, NULL, NULL, &timeout);
    } while(isRunning);

    printf("Exiting thread1...\n");
    pthread_exit( NULL );
} // end function: thread1_worker


void* thread2_worker(void* data)
{
    struct timeval timeout;
    int ret = 0;

    (void)data;

    do
    {
        printf("Requesting cmd2\n");
        ret = system("util -c:cmd2 -v 2>> /tmp/stderr.log");
        if( ret )
        {
            perror( "system-util result" );
        }

        timeout.tv_sec  = 0;
        timeout.tv_usec = SLEEP2 * 1000;  //milliseconds
        select(0, NULL, NULL, NULL, &timeout);
    }while(isRunning);

    printf("Exiting thread2...\n");
    pthread_exit((void*)0);
} // end function: thread2_worker


void* thread3_worker(void* data)
{
    struct timeval timeout;
    int ret = 0;

    (void)data;

    do
    {
        printf("Requesting cmd3\n");
        ret = system("util -c:cmd3 -v 2>> /tmp/stderr.log");
        if( ret )
        {
            perror( "system-util result" );
        }

        timeout.tv_sec  = 0;
        timeout.tv_usec = SLEEP3 * 1000;  //milliseconds
        select(0, NULL, NULL, NULL, &timeout);
    }while(isRunning);

    printf("Exiting thread3...\n");
    pthread_exit((void*)0);
} // end function: thread3_worker


int main( void )
{
    signal(SIGTERM, signal_handler);
    signal(SIGINT, signal_handler);
    signal(SIGQUIT, signal_handler);

    printf("Starting threads...\n");
    pthread_create(&thread1, NULL, thread1_worker, (void*) NULL);
    pthread_create(&thread2, NULL, thread2_worker, (void*) NULL);
    pthread_create(&thread3, NULL, thread3_worker, (void*) NULL);

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);
    return 0;
} // end function: main

运行代码时,以下输出是典型的:(继续重复,语句顺序略有不同:

system-util result: Success
system-util result: Success
system-util result: Success
Requesting cmd3
Requesting cmd2
Requesting cmd1

然后,由于我没有util函数,/tmp/stderr.log包含以下重复语句:

sh: 1: util: not found
sh: 1: util: not found
sh: 1: util: not found
sh: 1: util: not found
sh: 1: util: not found
sh: 1: util: not found
sh: 1: util: not found
sh: 1: util: not found

并且,除非util函数标记为executable并且在此程序中位于同一目录中,否则它将失败。

util更改为./util,如果在同一目录中,则无需将其标记为executable

seg fault事件很可能来自util函数,而不是来自发布的代码。

应该进一步考虑调用函数:signal(),每个手册页都不可靠。该手册页强烈建议使用:sigaction()代替。