pseudo_emulator.c 865 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. static int execute(char* argv[])
  7. {
  8. pid_t my_pid;
  9. int status, timeout;
  10. struct timespec duration;
  11. duration.tv_sec = 0;
  12. duration.tv_nsec = 100000000;
  13. if (0 == (my_pid = fork())) {
  14. if (-1 == execve(argv[0], (char**)argv, NULL)) {
  15. perror("child process execve failed");
  16. return -1;
  17. }
  18. }
  19. timeout = 100;
  20. while (0 == waitpid(my_pid, &status, WNOHANG)) {
  21. if (--timeout < 0) {
  22. perror(argv[0]);
  23. return -1;
  24. }
  25. nanosleep(&duration, NULL);
  26. }
  27. if (1 != WIFEXITED(status) || 0 != WEXITSTATUS(status)) {
  28. return -1;
  29. }
  30. return 0;
  31. }
  32. int main(int argc, char* argv[])
  33. {
  34. if (argc < 2) {
  35. fprintf(stderr, "%s: require at least one argument.\n", argv[0]);
  36. return -1;
  37. }
  38. return execute(&argv[1]);
  39. }