#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>

int main(void) {
int outfd, count;
char *str1;
pid_t chpid;

if ((outfd = open("testfile.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) {
	perror("testfile.txt"); exit(1); }
if ((chpid = fork()) == -1) {
	perror("creating child"); exit(1); }
else if (chpid == 0) {
	sleep(1);
	for (count = 0; count < 5; count++) {
		printf("CHILD: file position = %ld\n", tell(outfd));
		write(outfd, "CHILD PROCESS\n", 14);
		sleep(2);
	}
	close(outfd); return 0;
}
for (count = 0; count < 5; count++) {
	printf("PARENT: file position = %ld\n", tell(outfd));
	write(outfd, "PARENT PROCESS\n", 15);
	sleep(2);
}
close(outfd); 
wait(NULL);
return 0;
}
