c - 如何运行这个程序?

标签 c unix gcc fork

我可以编译提供给我的这个程序,但我必须进一步开发。我对此有一些疑问:

#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#define TIMEOUT (20)

int main(int argc, char *argv[])
{
  pid_t pid;
  if(argc > 1 && strncmp(argv[1], "-help", strlen(argv[1])) == 0)
    {
      fprintf(stderr, "Usage: RunSafe Prog [CommandLineArgs]\n\nRunSafe takes as arguments:\nthe program to be run (Prog) and its command line arguments (CommandLineArgs) (if any)\n\nRunSafe will execute Prog with its command line arguments and\nterminate it and any remaining childprocesses after %d seconds\n", TIMEOUT);
      exit(0);
    }

  if((pid = fork()) == 0)        /* Fork off child */
    {
      execvp(argv[1], argv+1);
      fprintf(stderr,"RunSafe failed to execute: %s\n",argv[1]);
      perror("Reason");
      kill(getppid(),SIGKILL);   /* kill waiting parent */
      exit(errno);               /* execvp failed, no child - exit immediately */
    }
  else if(pid != -1)
    {
      sleep(TIMEOUT);
      if(kill(0,0) == 0)         /* are there processes left? */
    {
      fprintf(stderr,"\nRunSafe: Attempting to kill remaining (child) processes\n");
      kill(0, SIGKILL);      /* send SIGKILL to all child processes */
    }
    }
  else
    {
      fprintf(stderr,"RunSafe failed to fork off child process\n");
      perror("Reason");
        }

}

编译时我的警告是什么意思?

$ gcc -o RunSafe RunSafe.c -lm 
RunSafe.c: In function ‘main’:
RunSafe.c:30:44: warning: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default]

为什么我不能执行文件?

$ file RunSafe
RunSafe: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x0a128c8d71e16bfde4dbc316bdc329e4860a195f, not stripped
ubuntu@ubuntu:/media/Lexar$ sudo chmod 777 RunSafe
ubuntu@ubuntu:/media/Lexar$ ./RunSafe
bash: ./RunSafe: Permission denied
ubuntu@ubuntu:/media/Lexar$ sudo ./RunSafe
sudo: ./RunSafe: command not found

最佳答案

首先,您需要#include <string.h>摆脱那个警告。

其次,操作系统可能会阻止您在 /media/Lexar 上执行程序文件系统,无论它们的权限位是什么。如果您键入 mount你可能会看到 noexec /media/Lexar 的选项.

关于c - 如何运行这个程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10908376/

相关文章:

python - 多个 Python 进程缓慢

c - 编译时设置共享库前缀

c++ - 从另一个应用程序访问 lua_State

c - "file.c"和 "file.h"和有什么区别?

linux - FreeBSD 上的每个挂载都需要 10% "root space"吗?

linux - 在名称中带有空格的文件夹中进行 Grep

c++ - 跨平台键盘/鼠标输入推荐

您可以对 C_INCLUDE_PATH 使用通配符吗

c++ - 如何在 gcc 中链接到我自己的库?

c - 总线错误 - 这段代码有什么问题?