fork download
  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <sys/wait.h>
  5. #include <string.h>
  6.  
  7. #define MAXLINE 1024
  8. void getCmd(char*);
  9. void fork4aCmd(pid_t*, char*);
  10. void wait4Child(pid_t*);
  11.  
  12. int main(void)
  13. {
  14. char buf[MAXLINE];
  15. pid_t pid;
  16.  
  17. printf("%% ");
  18. while(fgets(buf, MAXLINE, stdin) != NULL) {
  19. getCmd(buf);
  20. fork4aCmd(buf);
  21. wait4child(pid);
  22. printf("%%");
  23. }
  24. exit(0);
  25. }
  26. void getCmd(char *buf)
  27. {
  28. if(buf[strlen(buf)-1] == '\n')
  29. buf[strlen(buf)-1] = 0;
  30. return;
  31. }
  32.  
  33. void fork4aCmd(pid_t *pid, char *buf)
  34. {
  35. if((*pid = fork()) < 0) {
  36. fprintf(stderr, "fork error!\n");
  37. exit(1);
  38. } else {
  39. execlp(buf, buf, (char*) 0);
  40. fprintf(stderr, "exec error!\n");
  41. exit(2);
  42. }
  43. return;
  44. }
  45.  
  46. void wait4Child(pid_t *pid)
  47. {
  48. int status;
  49. if((*pid = waitpid(*pid, &status, 0)) < 0){
  50. fprintf(stderr, "waitpid error!\n");
  51. exit(3);
  52. }
  53. return;
  54. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:9:16: error: unknown type name ‘pid_t’
 void fork4aCmd(pid_t*, char*);
                ^~~~~
prog.c:10:17: error: unknown type name ‘pid_t’
 void wait4Child(pid_t*);
                 ^~~~~
prog.c: In function ‘main’:
prog.c:15:2: error: unknown type name ‘pid_t’
  pid_t pid;
  ^~~~~
prog.c:20:3: warning: implicit declaration of function ‘fork4aCmd’ [-Wimplicit-function-declaration]
   fork4aCmd(buf);
   ^~~~~~~~~
prog.c:21:3: warning: implicit declaration of function ‘wait4child’ [-Wimplicit-function-declaration]
   wait4child(pid);
   ^~~~~~~~~~
prog.c: At top level:
prog.c:33:16: error: unknown type name ‘pid_t’
 void fork4aCmd(pid_t *pid, char *buf)
                ^~~~~
prog.c:46:17: error: unknown type name ‘pid_t’
 void wait4Child(pid_t *pid)
                 ^~~~~
stdout
Standard output is empty