Possibly working with a partner, write a single file
my_system.c
that defines themy_system
function specified below. You may include helper functions in the file, but it should not containmain
.In a separate file, write a
main
to test yourmy_system
function and make sure it works as intended.Show your working function to a TA or submit your
.c
files to the submission site.
1
int my_system(const char *command)
This is a simplified version of the system
standard
library function (see man 3 system
for details). You should handle all aspects of the function as described
in the manual page except
- you do not need to ignore or block any signals
- you do not need to check
if a shell is available
forNULL
commands
You must not use system
in your implementation. You
should use fork
; wait
or waitpid
;
and either execve
or one of its front-ends
(execl
, execlp
, execle
,
execv
, execvp
, or execvpe
).
In addition to manpages for the above functions, you may find it useful to refer to the lecture slides and/or the threads reading.
Note that the wait status
that my_system
should
return can be retrieved using wait
or
waitpid
.
Because my_system
needs to be thread-safe, do not use
any global variables.
The following main function
int main(int argc, const char *argv[]) {
int a1 = my_system("sleep 1; echo hi");
int a2 = my_system("echo bye");
int a3 = my_system("flibbertigibbet 23");
("%d %d %d\n",
printf(a1), WEXITSTATUS(a2), WEXITSTATUS(a3));
WEXITSTATUS}
should print
hi
bye
sh: 1: flibbertigibbet: not found
0 0 127
(note: the details of the not found
line will vary based on
the version of sh
installed on the computer)
The following main function
int main(int argc, const char *argv[]) {
("echo -n 'type something: ';"
my_system" read got;"
" echo Thanks for typing \\\"\"$got\"\\\"");
}
should prompt for user input, wait until it is provided, and then repeat what they typed, as e.g.
type something: this is a test
Thanks for typing "this is a test"