/* In this example, the child process executes the command "ls -l" and redirects the ls output to file "example-output.txt". The father process waits for the child to finish, before continuing execution. This is an example of how the ">" operator should work. Compilation: gcc output-redirection.c -o output-redirection -Wall */ #include #include #include #include #include #include #include #include #include int main() { int fd; char *argv[5]; pid_t pid = fork(); if (pid<0) { perror("fork() failed!"); return -1; } if (pid==0) { fd = open("example-output.txt", O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR); close(1); dup(fd); close(fd); argv[0] = (char*) malloc(5*sizeof(char)); argv[1] = (char*) malloc(5*sizeof(char)); strcpy(argv[0],"ls"); strcpy(argv[1],"-l"); argv[2] = NULL; execvp(argv[0],argv); } waitpid(pid,NULL,WNOHANG); printf("Father exitting...\n"); return 0; }