#include <sys/errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

	
int main()
{	
	char *file="text_file";
	char content[30];	/* To store what it has read */
	int open_ret, read_ret, fork_pid;	/* Return values variables */

	/* 
	 * Open the file, check for errors and display 
	 * the filename and the decriptor associated with it
	 */
	open_ret=open(file, O_RDONLY);
	printf("opened %s as fd: %d \n", file, open_ret);

	/*
	 * Create a child process
	 * and store the fork return value for next operation
	 */
	fork_pid=fork();

	/* 
	 * Fork returns a 0 in child's and the pid of 
	 * child in parent so we can sync both by that value
	 * So if it is not the child then put it to sleep
	 * for enough time for the child to finish.
	 */
	if(fork_pid==0)
		sleep(3);
	/*
	 * Just to make sure we want to see which executes
	 */
	printf("\nfork ret value: %d\n", fork_pid);
	
	/*
	 * Read from the file, display number of bytes 
	 * read and display what was read
	 */
	read_ret=read(open_ret, content, 10);
	printf("total bytes read: %d \n", read_ret);
	printf("content read:\n%s \n", content);
	
	close(open_ret);
/*	return(0);*/
}	

/*
###### OUTPUT ######

adimitro@alef:/student/adimitro/cwork> ./a.out
opened text_file as fd: 3

fork ret value: 16137
total bytes read: 10
content read:
line 1
lin
adimitro@alef:/student/adimitro/cwork>
fork ret value: 0
total bytes read: 10
content read:
e 2
line 3

adimitro@alef:/student/adimitro/cwork>
*/
