Question
Write a program that performs the fork() and exec() system calls, which demonstrates that you understand how to properly interpret the output of the fork()
Write a program that performs the fork() and exec() system calls, which demonstrates that you understand how to properly interpret the output of the fork() system call as well as the proper place to use the exec() system call. Set up your main() function so that it accepts 1 argument from the command line (default shell).
In main(), your program needs to create a new process by using fork() system call. If there is an error of calling fork() handle folk error and print this message,
"Fork failure"
If the fork() is successful indicate that the child process has started in the main process and will wait for the child process to complete by printing this message
“PARENT started, now waiting for process ID# ZZZ.”
where ZZZ is the actual PID the child process is assigned by your OS.
In the child process, display a message indicating that the child
process has started. “CHILD started.”
And also print out this message, “Student Username: XXX” “Finish time: YYY”
Where XXX is your username at K200 server and should be retrieved by calling geteuid() in pwd.h. YYY is the current time and should be retrieved by using any method in time.h.
Then, sleep this child process for a random time (but no more than 10 seconds). After sleep, print
“CHILD done sleeping.”
Once the child process is done, parent process prints this message,
“PARENT resumed. Child exits. Now PARENT executing...”
The parent process will perform the appropriate exec() system call to load the new executable into memory based on the argument on the command line sent to the parent process.
Compile your source code and run your executable with Linux command “ls” (list files) as argument.
Tips:
exec() system call can be used with argument from the shell in this way, execlp(argv[1], argv[1], NULL);
Step by Step Solution
3.42 Rating (155 Votes )
There are 3 Steps involved in it
Step: 1
Fork System Call Fork system call is used for creating a new process which is called child process This System call can be used by any process to crea...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started