#include <unistd.h> // for close, fork
#include <stdio.h>
#include <fcntl.h> // for open
int main()
{
char *argv[2];
argv[0] = "cat";
argv[1] = 0;
if (fork() == 0)
{
close(0);
open("input.txt", O_RDONLY);
execvp("cat", argv);
}
}
input.txt
is:
Hello
I
am
Sam
But, when I run this code, the output is
Hello
I
am
Thus, not printing the last line of input.txt
Can someone explain the reason for this I am quite new to OS.