fork download
  1. Consider the following example program:
  2.  
  3. #include <stdio.h>
  4. #include <unistd.h>
  5.  
  6. int main(int argc, char **argv)
  7. {
  8. printf("--beginning of program\n");
  9.  
  10. int counter = 0;
  11. pid_t pid = fork();
  12.  
  13. if (pid == 0)
  14. {
  15. // child process
  16. int i = 0;
  17. for (; i < 5; ++i)
  18. {
  19. printf("child process: counter=%d\n", ++counter);
  20. }
  21. }
  22. else if (pid > 0)
  23. {
  24. // parent process
  25. int j = 0;
  26. for (; j < 5; ++j)
  27. {
  28. printf("parent process: counter=%d\n", ++counter);
  29. }
  30. }
  31. else
  32. {
  33. // fork failed
  34. printf("fork() failed!\n");
  35. return 1;
  36. }
  37.  
  38. printf("--end of program--\n");
  39.  
  40. return 0;
  41. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:1: error: 'Consider' does not name a type
 Consider the following example program:
 ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:232:9: error: '__gid_t' does not name a type
 typedef __gid_t gid_t;
         ^
/usr/include/unistd.h:237:9: error: '__uid_t' does not name a type
 typedef __uid_t uid_t;
         ^
/usr/include/unistd.h:255:9: error: '__useconds_t' does not name a type
 typedef __useconds_t useconds_t;
         ^
/usr/include/unistd.h:260:9: error: '__pid_t' does not name a type
 typedef __pid_t pid_t;
         ^
/usr/include/unistd.h:267:9: error: '__intptr_t' does not name a type
 typedef __intptr_t intptr_t;
         ^
/usr/include/unistd.h:274:9: error: '__socklen_t' does not name a type
 typedef __socklen_t socklen_t;
         ^
/usr/include/unistd.h:334:8: error: '__off_t' does not name a type
 extern __off_t lseek (int __fd, __off_t __offset, int __whence) __THROW;
        ^
/usr/include/unistd.h:345:8: error: '__off64_t' does not name a type
 extern __off64_t lseek64 (int __fd, __off64_t __offset, int __whence)
        ^
/usr/include/unistd.h:360:8: error: 'ssize_t' does not name a type
 extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur;
        ^
/usr/include/unistd.h:366:8: error: 'ssize_t' does not name a type
 extern ssize_t write (int __fd, const void *__buf, size_t __n) __wur;
        ^
/usr/include/unistd.h:376:8: error: 'ssize_t' does not name a type
 extern ssize_t pread (int __fd, void *__buf, size_t __nbytes,
        ^
/usr/include/unistd.h:384:8: error: 'ssize_t' does not name a type
 extern ssize_t pwrite (int __fd, const void *__buf, size_t __n,
        ^
/usr/include/unistd.h:404:8: error: 'ssize_t' does not name a type
 extern ssize_t pread64 (int __fd, void *__buf, size_t __nbytes,
        ^
/usr/include/unistd.h:408:8: error: 'ssize_t' does not name a type
 extern ssize_t pwrite64 (int __fd, const void *__buf, size_t __n,
        ^
/usr/include/unistd.h:452:8: error: '__useconds_t' does not name a type
 extern __useconds_t ualarm (__useconds_t __value, __useconds_t __interval)
        ^
/usr/include/unistd.h:460:20: error: '__useconds_t' was not declared in this scope
 extern int usleep (__useconds_t __useconds);
                    ^
/usr/include/unistd.h:473:39: error: '__uid_t' has not been declared
 extern int chown (const char *__file, __uid_t __owner, __gid_t __group)
                                       ^
/usr/include/unistd.h:473:56: error: '__gid_t' has not been declared
 extern int chown (const char *__file, __uid_t __owner, __gid_t __group)
                                                        ^
/usr/include/unistd.h:478:30: error: '__uid_t' has not been declared
 extern int fchown (int __fd, __uid_t __owner, __gid_t __group) __THROW __wur;
                              ^
/usr/include/unistd.h:478:47: error: '__gid_t' has not been declared
 extern int fchown (int __fd, __uid_t __owner, __gid_t __group) __THROW __wur;
                                               ^
/usr/include/unistd.h:483:40: error: '__uid_t' has not been declared
 extern int lchown (const char *__file, __uid_t __owner, __gid_t __group)
                                        ^
/usr/include/unistd.h:483:57: error: '__gid_t' has not been declared
 extern int lchown (const char *__file, __uid_t __owner, __gid_t __group)
                                                         ^
/usr/include/unistd.h:491:52: error: '__uid_t' has not been declared
 extern int fchownat (int __fd, const char *__file, __uid_t __owner,
                                                    ^
/usr/include/unistd.h:492:8: error: '__gid_t' has not been declared
        __gid_t __group, int __flag)
        ^
/usr/include/unistd.h:511:35: error: 'size_t' has not been declared
 extern char *getcwd (char *__buf, size_t __size) __THROW __wur;
                                   ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:623:8: error: 'size_t' does not name a type
 extern size_t confstr (int __name, char *__buf, size_t __len) __THROW;
        ^
/usr/include/unistd.h:628:8: error: '__pid_t' does not name a type
 extern __pid_t getpid (void) __THROW;
        ^
/usr/include/unistd.h:631:8: error: '__pid_t' does not name a type
 extern __pid_t getppid (void) __THROW;
        ^
/usr/include/unistd.h:634:8: error: '__pid_t' does not name a type
 extern __pid_t getpgrp (void) __THROW;
        ^
/usr/include/unistd.h:637:8: error: '__pid_t' does not name a type
 extern __pid_t __getpgid (__pid_t __pid) __THROW;
        ^
/usr/include/unistd.h:639:8: error: '__pid_t' does not name a type
 extern __pid_t getpgid (__pid_t __pid) __THROW;
        ^
/usr/include/unistd.h:646:21: error: '__pid_t' was not declared in this scope
 extern int setpgid (__pid_t __pid, __pid_t __pgid) __THROW;
                     ^
/usr/include/unistd.h:646:36: error: '__pid_t' was not declared in this scope
 extern int setpgid (__pid_t __pid, __pid_t __pgid) __THROW;
                                    ^
/usr/include/unistd.h:646:50: error: expression list treated as compound expression in initializer [-fpermissive]
 extern int setpgid (__pid_t __pid, __pid_t __pgid) __THROW;
                                                  ^
In file included from /usr/include/features.h:374:0,
                 from /usr/include/stdio.h:27,
                 from prog.cpp:3:
/usr/include/unistd.h:646:52: error: expected ',' or ';' before 'throw'
 extern int setpgid (__pid_t __pid, __pid_t __pgid) __THROW;
                                                    ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:667:8: error: '__pid_t' does not name a type
 extern __pid_t setsid (void) __THROW;
        ^
/usr/include/unistd.h:671:8: error: '__pid_t' does not name a type
 extern __pid_t getsid (__pid_t __pid) __THROW;
        ^
/usr/include/unistd.h:675:8: error: '__uid_t' does not name a type
 extern __uid_t getuid (void) __THROW;
        ^
/usr/include/unistd.h:678:8: error: '__uid_t' does not name a type
 extern __uid_t geteuid (void) __THROW;
        ^
/usr/include/unistd.h:681:8: error: '__gid_t' does not name a type
 extern __gid_t getgid (void) __THROW;
        ^
/usr/include/unistd.h:684:8: error: '__gid_t' does not name a type
 extern __gid_t getegid (void) __THROW;
        ^
/usr/include/unistd.h:689:35: error: '__gid_t' has not been declared
 extern int getgroups (int __size, __gid_t __list[]) __THROW __wur;
                                   ^
/usr/include/unistd.h:693:26: error: '__gid_t' was not declared in this scope
 extern int group_member (__gid_t __gid) __THROW;
                          ^
In file included from /usr/include/features.h:374:0,
                 from /usr/include/stdio.h:27,
                 from prog.cpp:3:
/usr/include/unistd.h:693:41: error: expected ',' or ';' before 'throw'
 extern int group_member (__gid_t __gid) __THROW;
                                         ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:700:20: error: '__uid_t' was not declared in this scope
 extern int setuid (__uid_t __uid) __THROW __wur;
                    ^
In file included from /usr/include/features.h:374:0,
                 from /usr/include/stdio.h:27,
                 from prog.cpp:3:
/usr/include/unistd.h:700:35: error: expected ',' or ';' before 'throw'
 extern int setuid (__uid_t __uid) __THROW __wur;
                                   ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:705:22: error: '__uid_t' was not declared in this scope
 extern int setreuid (__uid_t __ruid, __uid_t __euid) __THROW __wur;
                      ^
/usr/include/unistd.h:705:38: error: '__uid_t' was not declared in this scope
 extern int setreuid (__uid_t __ruid, __uid_t __euid) __THROW __wur;
                                      ^
/usr/include/unistd.h:705:52: error: expression list treated as compound expression in initializer [-fpermissive]
 extern int setreuid (__uid_t __ruid, __uid_t __euid) __THROW __wur;
                                                    ^
In file included from /usr/include/features.h:374:0,
                 from /usr/include/stdio.h:27,
                 from prog.cpp:3:
/usr/include/unistd.h:705:54: error: expected ',' or ';' before 'throw'
 extern int setreuid (__uid_t __ruid, __uid_t __euid) __THROW __wur;
                                                      ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:710:21: error: '__uid_t' was not declared in this scope
 extern int seteuid (__uid_t __uid) __THROW __wur;
                     ^
In file included from /usr/include/features.h:374:0,
                 from /usr/include/stdio.h:27,
                 from prog.cpp:3:
/usr/include/unistd.h:710:36: error: expected ',' or ';' before 'throw'
 extern int seteuid (__uid_t __uid) __THROW __wur;
                                    ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:717:20: error: '__gid_t' was not declared in this scope
 extern int setgid (__gid_t __gid) __THROW __wur;
                    ^
In file included from /usr/include/features.h:374:0,
                 from /usr/include/stdio.h:27,
                 from prog.cpp:3:
/usr/include/unistd.h:717:35: error: expected ',' or ';' before 'throw'
 extern int setgid (__gid_t __gid) __THROW __wur;
                                   ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:722:22: error: '__gid_t' was not declared in this scope
 extern int setregid (__gid_t __rgid, __gid_t __egid) __THROW __wur;
                      ^
/usr/include/unistd.h:722:38: error: '__gid_t' was not declared in this scope
 extern int setregid (__gid_t __rgid, __gid_t __egid) __THROW __wur;
                                      ^
/usr/include/unistd.h:722:52: error: expression list treated as compound expression in initializer [-fpermissive]
 extern int setregid (__gid_t __rgid, __gid_t __egid) __THROW __wur;
                                                    ^
In file included from /usr/include/features.h:374:0,
                 from /usr/include/stdio.h:27,
                 from prog.cpp:3:
/usr/include/unistd.h:722:54: error: expected ',' or ';' before 'throw'
 extern int setregid (__gid_t __rgid, __gid_t __egid) __THROW __wur;
                                                      ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:727:21: error: '__gid_t' was not declared in this scope
 extern int setegid (__gid_t __gid) __THROW __wur;
                     ^
In file included from /usr/include/features.h:374:0,
                 from /usr/include/stdio.h:27,
                 from prog.cpp:3:
/usr/include/unistd.h:727:36: error: expected ',' or ';' before 'throw'
 extern int setegid (__gid_t __gid) __THROW __wur;
                                    ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:733:23: error: '__uid_t' was not declared in this scope
 extern int getresuid (__uid_t *__ruid, __uid_t *__euid, __uid_t *__suid)
                       ^
/usr/include/unistd.h:733:32: error: '__ruid' was not declared in this scope
 extern int getresuid (__uid_t *__ruid, __uid_t *__euid, __uid_t *__suid)
                                ^
/usr/include/unistd.h:733:40: error: '__uid_t' was not declared in this scope
 extern int getresuid (__uid_t *__ruid, __uid_t *__euid, __uid_t *__suid)
                                        ^
/usr/include/unistd.h:733:49: error: '__euid' was not declared in this scope
 extern int getresuid (__uid_t *__ruid, __uid_t *__euid, __uid_t *__suid)
                                                 ^
/usr/include/unistd.h:733:57: error: '__uid_t' was not declared in this scope
 extern int getresuid (__uid_t *__ruid, __uid_t *__euid, __uid_t *__suid)
                                                         ^
/usr/include/unistd.h:733:66: error: '__suid' was not declared in this scope
 extern int getresuid (__uid_t *__ruid, __uid_t *__euid, __uid_t *__suid)
                                                                  ^
/usr/include/unistd.h:733:72: error: expression list treated as compound expression in initializer [-fpermissive]
 extern int getresuid (__uid_t *__ruid, __uid_t *__euid, __uid_t *__suid)
                                                                        ^
In file included from /usr/include/features.h:374:0,
                 from /usr/include/stdio.h:27,
                 from prog.cpp:3:
/usr/include/unistd.h:734:6: error: expected ',' or ';' before 'throw'
      __THROW;
      ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:738:23: error: '__gid_t' was not declared in this scope
 extern int getresgid (__gid_t *__rgid, __gid_t *__egid, __gid_t *__sgid)
                       ^
/usr/include/unistd.h:738:32: error: '__rgid' was not declared in this scope
 extern int getresgid (__gid_t *__rgid, __gid_t *__egid, __gid_t *__sgid)
                                ^
/usr/include/unistd.h:738:40: error: '__gid_t' was not declared in this scope
 extern int getresgid (__gid_t *__rgid, __gid_t *__egid, __gid_t *__sgid)
                                        ^
/usr/include/unistd.h:738:49: error: '__egid' was not declared in this scope
 extern int getresgid (__gid_t *__rgid, __gid_t *__egid, __gid_t *__sgid)
                                                 ^
/usr/include/unistd.h:738:57: error: '__gid_t' was not declared in this scope
 extern int getresgid (__gid_t *__rgid, __gid_t *__egid, __gid_t *__sgid)
                                                         ^
/usr/include/unistd.h:738:66: error: '__sgid' was not declared in this scope
 extern int getresgid (__gid_t *__rgid, __gid_t *__egid, __gid_t *__sgid)
                                                                  ^
/usr/include/unistd.h:738:72: error: expression list treated as compound expression in initializer [-fpermissive]
 extern int getresgid (__gid_t *__rgid, __gid_t *__egid, __gid_t *__sgid)
                                                                        ^
In file included from /usr/include/features.h:374:0,
                 from /usr/include/stdio.h:27,
                 from prog.cpp:3:
/usr/include/unistd.h:739:6: error: expected ',' or ';' before 'throw'
      __THROW;
      ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:743:23: error: '__uid_t' was not declared in this scope
 extern int setresuid (__uid_t __ruid, __uid_t __euid, __uid_t __suid)
                       ^
/usr/include/unistd.h:743:39: error: '__uid_t' was not declared in this scope
 extern int setresuid (__uid_t __ruid, __uid_t __euid, __uid_t __suid)
                                       ^
/usr/include/unistd.h:743:55: error: '__uid_t' was not declared in this scope
 extern int setresuid (__uid_t __ruid, __uid_t __euid, __uid_t __suid)
                                                       ^
/usr/include/unistd.h:743:69: error: expression list treated as compound expression in initializer [-fpermissive]
 extern int setresuid (__uid_t __ruid, __uid_t __euid, __uid_t __suid)
                                                                     ^
In file included from /usr/include/features.h:374:0,
                 from /usr/include/stdio.h:27,
                 from prog.cpp:3:
/usr/include/unistd.h:744:6: error: expected ',' or ';' before 'throw'
      __THROW __wur;
      ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:748:23: error: '__gid_t' was not declared in this scope
 extern int setresgid (__gid_t __rgid, __gid_t __egid, __gid_t __sgid)
                       ^
/usr/include/unistd.h:748:39: error: '__gid_t' was not declared in this scope
 extern int setresgid (__gid_t __rgid, __gid_t __egid, __gid_t __sgid)
                                       ^
/usr/include/unistd.h:748:55: error: '__gid_t' was not declared in this scope
 extern int setresgid (__gid_t __rgid, __gid_t __egid, __gid_t __sgid)
                                                       ^
/usr/include/unistd.h:748:69: error: expression list treated as compound expression in initializer [-fpermissive]
 extern int setresgid (__gid_t __rgid, __gid_t __egid, __gid_t __sgid)
                                                                     ^
In file included from /usr/include/features.h:374:0,
                 from /usr/include/stdio.h:27,
                 from prog.cpp:3:
/usr/include/unistd.h:749:6: error: expected ',' or ';' before 'throw'
      __THROW __wur;
      ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:756:8: error: '__pid_t' does not name a type
 extern __pid_t fork (void) __THROWNL;
        ^
/usr/include/unistd.h:764:8: error: '__pid_t' does not name a type
 extern __pid_t vfork (void) __THROW;
        ^
/usr/include/unistd.h:774:46: error: 'size_t' has not been declared
 extern int ttyname_r (int __fd, char *__buf, size_t __buflen)
                                              ^
/usr/include/unistd.h:809:8: error: 'ssize_t' does not name a type
 extern ssize_t readlink (const char *__restrict __path,
        ^
/usr/include/unistd.h:820:8: error: 'ssize_t' does not name a type
 extern ssize_t readlinkat (int __fd, const char *__restrict __path,
        ^
/usr/include/unistd.h:839:8: error: '__pid_t' does not name a type
 extern __pid_t tcgetpgrp (int __fd) __THROW;
        ^
/usr/include/unistd.h:842:33: error: '__pid_t' has not been declared
 extern int tcsetpgrp (int __fd, __pid_t __pgrp_id) __THROW;
                                 ^
/usr/include/unistd.h:857:38: error: 'size_t' has not been declared
 extern int getlogin_r (char *__name, size_t __name_len) __nonnull ((1));
                                      ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:879:39: error: 'size_t' has not been declared
 extern int gethostname (char *__name, size_t __len) __THROW __nonnull ((1));
                                       ^
/usr/include/unistd.h:886:45: error: 'size_t' has not been declared
 extern int sethostname (const char *__name, size_t __len)
                                             ^
/usr/include/unistd.h:897:41: error: 'size_t' has not been declared
 extern int getdomainname (char *__name, size_t __len)
                                         ^
/usr/include/unistd.h:899:47: error: 'size_t' has not been declared
 extern int setdomainname (const char *__name, size_t __len)
                                               ^
/usr/include/unistd.h:917:57: error: 'size_t' has not been declared
 extern int profil (unsigned short int *__sample_buffer, size_t __size,
                                                         ^
/usr/include/unistd.h:918:6: error: 'size_t' has not been declared
      size_t __offset, unsigned int __scale)
      ^
/usr/include/unistd.h:993:42: error: '__off_t' has not been declared
 extern int truncate (const char *__file, __off_t __length)
                                          ^
/usr/include/unistd.h:1005:44: error: '__off64_t' has not been declared
 extern int truncate64 (const char *__file, __off64_t __length)
                                            ^
/usr/include/unistd.h:1016:33: error: '__off_t' has not been declared
 extern int ftruncate (int __fd, __off_t __length) __THROW __wur;
                                 ^
/usr/include/unistd.h:1026:35: error: '__off64_t' has not been declared
 extern int ftruncate64 (int __fd, __off64_t __length) __THROW __wur;
                                   ^
/usr/include/unistd.h:1043:20: error: 'intptr_t' was not declared in this scope
 extern void *sbrk (intptr_t __delta) __THROW;
                    ^
In file included from /usr/include/features.h:374:0,
                 from /usr/include/stdio.h:27,
                 from prog.cpp:3:
/usr/include/unistd.h:1043:38: error: expected ',' or ';' before 'throw'
 extern void *sbrk (intptr_t __delta) __THROW;
                                      ^
In file included from prog.cpp:4:0:
/usr/include/unistd.h:1081:40: error: '__off_t' has not been declared
 extern int lockf (int __fd, int __cmd, __off_t __len) __wur;
                                        ^
/usr/include/unistd.h:1091:42: error: '__off64_t' has not been declared
 extern int lockf64 (int __fd, int __cmd, __off64_t __len) __wur;
                                          ^
/usr/include/unistd.h:1134:5: error: 'ssize_t' has not been declared
     ssize_t __n) __THROW __nonnull ((1, 2));
     ^
prog.cpp: In function 'int main(int, char**)':
prog.cpp:8:38: error: 'printf' was not declared in this scope
     printf("--beginning of program\n");
                                      ^
prog.cpp:11:5: error: 'pid_t' was not declared in this scope
     pid_t pid = fork();
     ^
prog.cpp:13:9: error: 'pid' was not declared in this scope
     if (pid == 0)
         ^
stdout
Standard output is empty