/* ** This short little program shows how the UNIX fork() mechanism works. ** When this program starts up, it has a stdin, stdout, and stderr that ** are associated with the terminal (and are opened by the C startup code ** before it calls the main() function). */ #define PARENT "parent" #define CHILD "child" int main(void) { int pid; char color[10]; char whoami[10]; char line[80]; write(1,"\n", 1); pid = fork(); if (pid == 0) { /* ** This is the child process. */ strcpy(whoami, CHILD); strcpy(color, "blue"); sprintf(line, "%s: Violets are %s.\n\n", whoami, color); write(1, line, strlen(line)); sprintf(line, "%s: I'm schizophrenic.\n\n", whoami); write(1, line, strlen(line)); } else { /* ** This is the parent process. */ strcpy(whoami, PARENT); strcpy(color, "red"); sprintf(line, "%s: Roses are %s,\n\n", whoami, color); write(1, line, strlen(line)); wait((int *) 0); /* wait for child to die */ sprintf(line, "%s: And so am I.\n\n", whoami); write(1, line, strlen(line)); } sprintf(line, "%s: (Which flower was %s?)\n\n", whoami, color); write(1, line, strlen(line)); exit(0); }