training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

ex-8.3.c (536B)


      1 #include	<sys/types.h>
      2 #include	"ourhdr.h"
      3 
      4 static void charatatime(char *);
      5 
      6 int
      7 main(void)
      8 {
      9 	pid_t	pid;
     10 
     11 	TELL_WAIT();
     12 
     13 	if ( (pid = fork()) < 0)
     14 		err_sys("fork error");
     15 	else if (pid == 0) {
     16 		WAIT_PARENT();
     17 		charatatime("output from child\n");
     18 		TELL_PARENT(getppid());
     19 	} else {
     20 		charatatime("output from parent\n");
     21 		TELL_CHILD(pid);
     22 		WAIT_CHILD();
     23 	}
     24 	exit(0);
     25 }
     26 
     27 static void
     28 charatatime(char *str)
     29 {
     30 	char	*ptr;
     31 	int		c;
     32 
     33 	setbuf(stdout, NULL);			/* set unbuffered */
     34 	for (ptr = str; (c = *ptr++); )
     35 		putc(c, stdout);
     36 }