Objective: In this assignment, you will be writing a program that performs the f
ID: 3751964 • Letter: O
Question
Objective: In this assignment, you will be writing a program that performs the fork() and exec() system calls. You will demonstrate 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. Details: Your program will call the fork) system call at the very start. The parent process will display a message indicating it has started and will then wait for a specific child process ID to complete. Once the child process has completed, the parent process will display a message indicating that the child is complete and the parent will now terminate. The child process will display a message indicating that it has started. If one or more arguments were provided on the command line, the child process will perform the appropriate exec) system call to load the new executable into memory with the appropriate arguments if provided. If no argument:s were provided, the child process will display a message indicating that it will terminate.Explanation / Answer
#include<stdio.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
int pid;
int status;
pid=fork();
if(pid==0)
{
printf("Now Child started.");
if(argc<2)
printf("No arguments provided. Terminating child. ");
else if (argc<4)
{
printf("More than one argument provided. Calling execvp(), never to return (sniff) ");
execvp(argv[1],argv+1);
}
}
else
{
printf("PARENT started, now waiting for process ID#%d ",pid);
wait(&status);
printf("PARENT resumed. Child exit code of 0. Now terminating parent ");
}
}
/*
lenovo@lenovo-Vbox:~$ cc assn2.c -o assn2
lenovo@lenovo-Vbox:~$ ./assn2
PARENT started, now waiting for process ID#23178
Now Child started.No arguments provided. Terminating child.
PARENT resumed. Child exit code of 0. Now terminating parent
lenovo@lenovo-Vbox:~$ ./assn2 ls
PARENT started, now waiting for process ID#23180
Now Child started.More than one argument provided. Calling execvp(), never to return (sniff)
a.out assn2.c chegg Desktop Documents examples.desktop hist.txt Music Pictures Public t Templates
assn2 assn2.c~ c.txt detail.txt Downloads fedora_dir java.txt names.txt pl.txt story.txt t.c Videos
PARENT resumed. Child exit code of 0. Now terminating parent
lenovo@lenovo-Vbox:~$ ./assn2 ls -l
PARENT started, now waiting for process ID#23182
Now Child started.More than one argument provided. Calling execvp(), never to return (sniff)
total 128
-rwxrwxr-x 1 lenovo lenovo 8754 Sep 24 22:29 a.out
-rwxrwxr-x 1 lenovo lenovo 8755 Sep 24 23:26 assn2
-rw-rw-r-- 1 lenovo lenovo 641 Sep 24 23:25 assn2.c
-rw-rw-r-- 1 lenovo lenovo 639 Sep 24 23:18 assn2.c~
drwxrwxr-x 2 lenovo lenovo 4096 Sep 24 21:20 chegg
-rw-rw-r-- 1 lenovo lenovo 43 Sep 16 04:43 c.txt
drwxr-xr-x 2 lenovo lenovo 4096 Sep 8 22:04 Desktop
-rw-rw-r-- 1 lenovo lenovo 57 Sep 16 04:46 detail.txt
drwxr-xr-x 2 lenovo lenovo 4096 Sep 8 22:04 Documents
drwxr-xr-x 2 lenovo lenovo 4096 Sep 8 22:04 Downloads
-rw-r--r-- 1 lenovo lenovo 8980 Sep 8 21:32 examples.desktop
drwxrwxr-x 2 lenovo lenovo 4096 Sep 24 22:01 fedora_dir
-rw-rw-r-- 1 lenovo lenovo 40 Sep 16 04:45 hist.txt
-rw-rw-r-- 1 lenovo lenovo 24 Sep 16 04:44 java.txt
drwxr-xr-x 2 lenovo lenovo 4096 Sep 8 22:04 Music
-rw-rw-r-- 1 lenovo lenovo 25 Sep 16 04:44 names.txt
drwxr-xr-x 2 lenovo lenovo 4096 Sep 8 22:04 Pictures
-rw-rw-r-- 1 lenovo lenovo 34 Sep 16 04:43 pl.txt
drwxr-xr-x 2 lenovo lenovo 4096 Sep 8 22:04 Public
-rw-rw-r-- 1 lenovo lenovo 52 Sep 16 04:45 story.txt
-rwxrwxr-x 1 lenovo lenovo 8549 Sep 24 23:25 t
-rw-rw-r-- 1 lenovo lenovo 107 Sep 24 23:25 t.c
drwxr-xr-x 2 lenovo lenovo 4096 Sep 8 22:04 Templates
drwxr-xr-x 2 lenovo lenovo 4096 Sep 8 22:04 Videos
PARENT resumed. Child exit code of 0. Now terminating parent
lenovo@lenovo-Vbox:~$
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.