Rocker 发表于 2011-6-10 09:52:05

请教:嵌入式linux执行system函数程序崩溃

最近遇到个比较奇怪的问题,函数中执行system系统函数时程序就崩溃掉了。


但是,我单独写个测试函数,执行system函数没有问题,怀疑是不是交叉编译链接环境有影响?



有经验的大侠看过来!

Rocker 发表于 2011-6-16 22:41:43

还未解决,抽

Rocker 发表于 2011-6-27 09:13:26

问题解决了,在网上找到的如下:


原因好像是因为SIGCHLD的默认动作好像不是IGNORE。我写了个测试代码(在最后,a.c)。
gcc a.c
执行a.out DFL正常,执行a.out IGN就会返回-1,错误码解释为“No child processes”。
分别执行 strace -o DFL.log a.out DFL 和 strace -o IGN.log a.out IGN
比较两个文件,
前者为 rt_sigaction(SIGCHLD, {SIG_DFL}, {SIG_DFL}, 8) = 0
后者为 rt_sigaction(SIGCHLD, {SIG_IGN}, {SIG_DFL}, 8) = 0
这似乎是唯一区别。似乎能解释返回-1的原因。
但记得SIGCHLD的默认动作好像就是忽略。
并且,看不懂问题代码的那段。
rt_sigaction(SIGINT, {SIG_IGN}, {0xd92508, ~, SA_RESTART|SA_SIGINFO}, 8) = 0
rt_sigaction(SIGQUIT, {SIG_IGN}, {SIG_IGN}, 8) = 0


/* ************************************************ */
/*                                          test code a.c                                    */
/* ************************************************ */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>

int main(int argc, char * argv[])
{
      static struct sigaction act;
      static struct sigaction old_act;
      char command;
      int ret = -1;
      int i = 0;
      
      sigemptyset(&act.sa_mask);
      sigaddset(&act.sa_mask, SIGCHLD);
      act.sa_flags = SA_RESTART;
      
      if (argc == 2)
      {
                if (!memcmp(argv, "IGN", 3))
                {
                        act.sa_handler = SIG_IGN;
                }
                else if (!memcmp(argv, "DFL", 3))
                {
                        act.sa_handler = SIG_DFL;
                }
                else
                {
                        printf("useage: %s IGNDFL \n", argv);
                        return -1;
                }
      }
      else
      {
                printf("useage: %s IGN | DFL \n", argv);
                return -1;
      }
      
      if (sigaction(SIGCHLD, &act, &old_act) == -1)
      {
                perror("sigaction");
                return -1;
      }
      
      ret = sigismember(&act.sa_mask, SIGCHLD);
      printf("ret = %d\n",ret);
      memset(command, 0x00, sizeof(command));
      strcpy(command, "ls -al");
      ret = system(command);
      printf("ret = %d\n",ret);
      if (ret)
      {
                perror("system");
                return -1;
      }
      return 0;
}
/* ************************************************ */
页: [1]
查看完整版本: 请教:嵌入式linux执行system函数程序崩溃