/* In this example, the child process executes the command "head -n 15". Its input is redirected from this file (input-redirection.c)! The father process waits for the child to finish, before continuing execution. This is an example of how the "<" operator should work. Compilation: gcc input-redirection.c -o input-redirection -Wall */ #include #include #include #include #include #include #include #include #include int main() { int fd; char *argv[4]; pid_t pid = fork(); if (pid<0) { perror("fork() failed!"); return -1; } if (pid==0) { fd = open("input-redirection.c",O_RDONLY); close(0); dup(fd); close(fd); argv[0] = (char*) malloc(5*sizeof(char)); argv[1] = (char*) malloc(3*sizeof(char)); argv[2] = (char*) malloc(3*sizeof(char)); strcpy(argv[0],"head"); strcpy(argv[1],"-n"); strcpy(argv[2],"15"); argv[3] = NULL; printf("OK\n"); execvp(argv[0],argv); } waitpid(pid,NULL,0); printf("Father exitting...\n"); return 0; }