fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <string.h>
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10. int fd;
  11. pid_t process_id = 0;
  12. pid_t sid = 0;
  13.  
  14. process_id = fork();
  15.  
  16. if (process_id < 0)
  17. {
  18. printf("fork failed!\n");
  19. exit(1);
  20. }
  21.  
  22. if (process_id > 0)
  23. {
  24. printf("process_id of child process %d \n", process_id);
  25. exit(0);
  26. }
  27.  
  28. umask(0);
  29. sid = setsid();
  30. if(sid < 0)
  31. {
  32. exit(1);
  33. }
  34.  
  35. chdir("/");
  36.  
  37. close(STDIN_FILENO);
  38. close(STDOUT_FILENO);
  39. close(STDERR_FILENO);
  40.  
  41. while(1) {
  42. fd = open("/dev/mydev", O_RDONLY);
  43. read(fd, &buf, 7);
  44. close(fd);
  45. }
  46. return EXIT_SUCCESS;
  47. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘main’:
prog.c:42:10: warning: implicit declaration of function ‘open’; did you mean ‘popen’? [-Wimplicit-function-declaration]
     fd = open("/dev/mydev", O_RDONLY);
          ^~~~
          popen
prog.c:42:29: error: ‘O_RDONLY’ undeclared (first use in this function)
     fd = open("/dev/mydev", O_RDONLY);
                             ^~~~~~~~
prog.c:42:29: note: each undeclared identifier is reported only once for each function it appears in
prog.c:43:15: error: ‘buf’ undeclared (first use in this function)
     read(fd, &buf, 7);
               ^~~
prog.c:35:3: warning: ignoring return value of ‘chdir’, declared with attribute warn_unused_result [-Wunused-result]
   chdir("/");
   ^~~~~~~~~~
stdout
Standard output is empty