Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Zhaozhong Liu
csci315
Commits
386acea7
Commit
386acea7
authored
Sep 07, 2021
by
Zhaozhong Liu
Browse files
Lab 1, problem 3 completed
parent
89c6aef2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Labs/Lab1/catcount.c
0 → 100644
View file @
386acea7
#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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment