Write a program that receives one command line argument: the name of a text file. Your program will work as follows:
Start out by creating a child process.
The child process calls exec to run cat command with command line argument that the parent received.
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
pid_t pid;
if ((pid = fork()) < 0) {
fprintf(stderr, "Error in fork\n");
}
else if (pid == 0) {
execlp("cat", "cat", argv[1], NULL);
}
else {
waitpid(pid, NULL, 0);
}
return 0;
}
Comments
Leave a comment