Commit 386acea7 authored by Zhaozhong Liu's avatar Zhaozhong Liu
Browse files

Lab 1, problem 3 completed

parent 89c6aef2
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
extern char **environ;
int main(int argc, char* argv[]) {
print_environment();
char *str = argv[1];
int pid1, pid2;
int *status;
pid1 = Fork();
if (pid1 == 0) {
execlp("/bin/cat", "/bin/cat", str, (char *)NULL);
exit(0);
}
else {
// parent process
wait(status);//
pid2 = Fork();
if (pid2 == 0){
execlp("/usr/bin/wc", "/usr/bin/wc", str, (char *)NULL);
exit(0);
}
else{
wait(&status); //the wait for child2
}
exit(0);
}
return 0;
}
void print_environment(void){
char **p;
for (p = environ; *p != NULL; p++){
char *element = *p;
printf("%s\n", element);
}
return 0;
}
pid_t Fork(void){
int pid = fork();
if (-1 == pid){
perror("fork() Failure\n");
exit(-1);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment