【问题标题】:Fork() and Execl() in C++, Execl() is failingC++ 中的 Fork() 和 Execl(),Execl() 失败
【发布时间】:2013-04-01 19:14:34
【问题描述】:

我正在尝试在 C++ 中运行 execl(),但它失败了,我不确定为什么。

  string fileName = "test.txt";
  string computerName = "jssoile@palmetto.school.edu";
  pid_t pid;
  int    status;

  if ((pid = fork()) < 0) {     /* fork a child process           */
     printf("*** ERROR: forking child process failed\n");
     exit(1);
  }
  else if (pid == 0) {          /* for the child process:         */
     if (execl("scp", fileName.c_str(), computerName.c_str(), NULL) < 0) {     /* execute the command  */
        printf("*** ERROR: exec failed\n");
        exit(1);
     }
  }
  else {                                /* for the parent:      */
     while (wait(&status) != pid)       /* wait for completion  */
     ;
  }

当我运行它时,我的 execl() 调用失败并打印出来

*** ERROR: exec failed

【问题讨论】:

  • 您的帖子不包含问题;请编辑您的帖子。无论如何,请将您的 printf("*** ERROR: exec failed\n") 替换为 perror("scp")

标签: c++ string character exec fork


【解决方案1】:

您正在使用需要完整路径作为第一个参数的exec 变体。

替换:

execl( "scp", ... )

与任一

execl("/usr/bin/scp", ...)

execlp("scp", ... )

参考:http://linux.die.net/man/3/execl

【讨论】:

    【解决方案2】:

    您的 execl 语法不正确。您需要指定 scp(包括 scp)的完整路径作为第一个参数,然后是参数列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多