gpt4 book ai didi

c - 系统函数调用时出现段错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:19:34 25 4
gpt4 key购买 nike

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

另外,我试图理解为什么我只看到一个打印 Thread1 cancelled 而不是所有三个线程的 3 个打印? [更正了这一点的代码]

下面是 testapp.c 的代码。这是我做的一种压力测试app,用来测试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:~$

最佳答案

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

改变:

    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);

关于c - 系统函数调用时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38231616/

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