#include #include #include #include #include /** * This program shows what can happen when multiple processes * try to indiscrimately share a file descriptor, in this * stdout. * For best results, pipe the output of the file into more, * * ./sharedOut | more * * What determines how many characters are output by each process * before the next process takes over? */ int main() { pid_t pid; char s[] = "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"; char *p = NULL; pid = fork(); /* fork another process */ if (pid < 0) /* Forking error! */ { exit(-1); } else if (0 == pid) /* Child process */ { // Child replaces string of |'s with *'s. p = s; while (*p) *(p++) = '*'; // Go into infinite loop printing out string. while (1) { p = s; while(*p) putchar(*(p++)); putchar('\n'); } exit(0); } else /* pid > 0: Parent process */ { // Go into infinite loop printing out string. while (1) { p = s; while(*p) putchar(*(p++)); putchar('\n'); } } }