Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program for the following problems. It is recommended that the programs

ID: 3862176 • Letter: W

Question

Write a program for the following problems. It is recommended that the programs should be written and run on Ubuntu environment using C/C++. However, students can also use other tools to accomplish the following tasks, such as using Java on Win32 API, etc. a) Create a (child) process that may or may not copy the parent address space and then make the child as a zombie process. Use ps utility to demonstrate that the zombie has been created. Please note that the zombie process will be depicted as . b) Use the procedure followed in 1(a) to make the child process as orphan. Using ps utility to show that the orphan process has been handed over to the init process who's pid is 1. c) Create a child process that executes the following commands from the command line. You can use the exec () family of system calls. a. Is (with switches/parameters, such as > > Is -l) b. echo (with a string parameter, such as > echo this is OS class and echo SPATH) c. pwd

Explanation / Answer

Problem 1)
-> First shell window

-> make file zombie.c and copy below code:

$ vim zombie.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
// creates a new process by duplicating the calling process
pid_t child_pid = fork();

// check for Parent process
if (child_pid > 0)
{
printf("The child pid is greater than 0 ");
printf("pid returned is :%d ", child_pid);
sleep(60);
}

// Child process
else
exit(0);

return 0;
}
-> compile file by running below command:              
                                  
$ gcc -o zombie zombie.c

-> To run executable named zombie, run with below command (Note: you might need to give executable mode permission to your process )
$ chmod 777 zombie

-> Run by below command

$ ./zombie

-> You will see some output like below:

The child pid is greater than 0
pid returned is :24344


-> Now open other shell window, without closing this existing window and run following command:

$ ps -e -o pid,ppid,stat,cmd | grep defunct

-> You will see snapshot like below:

24344 24343 Z+ [zombie] <defunct>
24365 11870 Z [sh] <defunct>
24386 17508 S+ grep --color=auto defunct


The pid returned by above process "24344" is shown as Z+, means zombie.
The child process is marked as <defunct>, and its status code is Z, for zombie.

Problem 2)

-> First shell window

-> make file orphan.c and copy below code:

$ vim orphan.c

#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
// This fork create a child process
int pid = fork();

if (pid > 0)
{
printf("the process pid is: %d ", pid);
}

// Note that pid is 0 in child process and is negative if fork() fails
else if (pid == 0)
{
sleep(30);
printf("in child process");
}

return 0;
}


-> compile and run the program

$ gcc -o orphan orphan.c
$ ./orphan
the process pid is: 31968

-> open new shell without closing this and run below command to find our process "orphan" state

$ ps -e -o pid,ppid,stat,cmd | grep orphan
31968 1 S ./orphan
31995 17508 S+ grep --color=auto orphan


Since in our command, the second paramter is PPID, means parent process id, so it is now "1"
Which signifies that the orphan process has been adpoted by init.

Problem 3)

Open new file and copy below code

$ vim child_exec_commands.c
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
// various commands arguments
char *args1[] = {"/bin/ls", "-r", "-t", "-l", (char *) 0 };
char *args2[] = {"/bin/pwd", (char *) 0 };
//char *args3[] = {"/bin/echo", "$PATH", (char *) 0 };
char *args3[] = {"/bin/echo", getenv("PATH") };
pid_t child_pid = fork();

// check for Parent process
if (child_pid > 0)
{
printf("Inside parent process ");
}

// Child process
else
{
printf("Inside child process ");
printf("Argument is: %s ", argv[1]);
if((strcmp(argv[1],"ls")==0))
{
printf("Executing ls command .... : ");
execv("/bin/ls", args1);
}
else if((strcmp(argv[1],"pwd")==0))
{
printf("Executing pwd command .... : ");
execv("/bin/pwd", args2);
}
else if((strcmp(argv[1],"echo")==0))
{
printf("Executing echo command .... : ");
execv("/bin/echo", args3);
}
exit(0);
}
return 0;
}

-> execute the program with following commands:

$ gcc -o child_exec_commands child_exec_commands.c

-> pass any argument along with the executable to run particular type of shell command
like follows

$ ./child_exec_commands ls

Inside parent process
Inside child process
Argument is: ls
Executing ls command .... :
$ total 60
-rwxrwxr-x 1 aaaaa aaaa 8703 Mar 16 10:34 a.out
-rw-rw-r-- 1 aaaaa aaaa 445 Mar 16 10:37 zombie.c
-rwxrwxr-x 1 aaaaa aaaaa 8755 Mar 16 10:37 zombie
-rw-rw-r-- 1 aaaaa aaaaa 404 Mar 16 10:54 orphan.c
-rwxrwxr-x 1 aaaaa aaaaa 8655 Mar 16 11:00 orphan
-rw-rw-r-- 1 aaaaa aaaaa 1063 Mar 16 11:43 child_exec_commands.c
-rwxrwxr-x 1 aaaaa aaaaa 8872 Mar 16 11:43 child_exec_commands


$ ./child_exec_commands pwd

Inside parent process
Inside child process
Argument is: pwd
Executing pwd command .... :
$ /home/aaaaa/child_process


$ ./child_exec_commands echo

Inside parent process
Inside child process
Argument is: echo
Executing echo command .... :
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/aaaaa/bin:/usr/java/jdk1.8.0_73/bin:/usr/local/p4v/bin:/usr/local/jdk1.4.2/bin/ /bin/ls -r -t -l
$

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote