C 系统函数调用时的分段故障
当涉及系统功能时,我尝试生成信号^C时,试图找出分段故障错误的原因 此外,我试图理解为什么我只看到一个打印线程1被取消,而不是所有三个线程的3个打印?[更正了这一点的代码] 下面是testapp.c的代码。这是我为测试而制作的一种压力测试应用程序。实用程序不是我正在使用的实际程序,而是用来演示其中使用的信号量。它正在使用系统函数执行我的实用程序 下面列出了源代码和输出C 系统函数调用时的分段故障,c,gcc,segmentation-fault,pthreads,ubuntu-14.04,C,Gcc,Segmentation Fault,Pthreads,Ubuntu 14.04,当涉及系统功能时,我尝试生成信号^C时,试图找出分段故障错误的原因 此外,我试图理解为什么我只看到一个打印线程1被取消,而不是所有三个线程的3个打印?[更正了这一点的代码] 下面是testapp.c的代码。这是我为测试而制作的一种压力测试应用程序。实用程序不是我正在使用的实际程序,而是用来演示其中使用的信号量。它正在使用系统函数执行我的实用程序 下面列出了源代码和输出 /******* testapp.c *****************/ /* gcc testapp.c -o test
/******* 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;
}
这只是一个打字错误 更改:
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(&thread1, NULL, thread2_worker, (void*) NULL);
pthread_create(&thread1, NULL, thread3_worker, (void*) NULL);
进入:
根据注释更正代码会导致:
/******* 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
然后,由于我没有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函数被标记为可执行,并且位于该程序的同一目录中,否则它将失败
如果在同一目录中,将util更改为./util将起作用,而无需将其标记为可执行文件
seg故障事件很可能来自util函数,而不是发布的代码
应进一步考虑调用函数:signal,这在手册页中是不可靠的。手册页强烈建议改为使用:sigaction。根据注释更正代码会导致:
/******* 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
然后,由于我没有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函数被标记为可执行,并且位于该程序的同一目录中,否则它将失败
如果在同一目录中,将util更改为./util将起作用,而无需将其标记为可执行文件
seg故障事件很可能来自util函数,而不是发布的代码
应进一步考虑调用函数:signal,这在手册页中是不可靠的。手册页强烈建议改为使用:sigaction。OT:void*0应与NULL相同。@alk:感谢您的输入,它已经存在,但我想排除任何未预料到的问题,如分段错误。这可能也在我的util程序中。OT^2:您不能从信号处理程序中打印F。@OP执行一次并查找信号安全函数的列表。发布的代码编译不干净。因此,第一步是更正编译器输出的所有消息。提示:在main函数中,没有使用argc和argv参数,因此main的签名应该是:int main void。对于其他函数:如果未使用参数,则函数体中的第一条语句应类似于:voiddata;存在未使用的变量。设置了一些变量,但没有像ret.OT那样使用。void*0应该与NULL相同。@alk:谢谢您的输入,它已经存在了,但我想排除任何未预料到的问题,如分段错误。这可能也在我的util程序中。OT^2:您不能从信号处理程序中打印F。@OP执行一次并查找信号安全函数的列表。发布的代码编译不干净。因此,第一步是更正编译器输出的所有消息。提示:在main函数中,没有使用argc和argv参数,因此main的签名应该是:int main void。对于其他函数:如果未使用参数,则函数体中的第一条语句应类似于:voiddata;存在未使用的变量。设置了一些变量,但没有像ret.Ohhh那样使用。接得好。我忽略了它,专注于信号处理器。你解决了我提出这个问题的两个顾虑之一。谢谢。接得好。我忽略了它,专注于信号处理器。你解决了我提出这个问题的两个顾虑之一。谢谢,谢谢你的回复。实际上,util存储在device/usr/bin上,所以在上面的参考代码中是这样的。我想在这里提到的一点是,当您怀疑util可能存在实际问题时,util还处理与testapp相同数量的信号。我已经在我的原始问题中分享了util程序的链接。谢谢你的回答。实际上,util存储在device/usr/bin上,所以在上面的参考代码中是这样的。我想在这里提到的一点是,当您怀疑util可能存在实际问题时,util还处理与testapp相同数量的信号。在我的原始问题中,我分享了指向util程序的链接。