Starting from:
$35

$29

Homework 02 Solution

PART 1 (10 points):

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

Examine the code below. It forks a new process and loads xclock into the new process. Modify the code to fork five (5) copies of xclock. After your program has spawned 5 children it must prompt the user before terminating (to verify that they want to end the program). Once the user has agreed to terminate the program, the parent program must first terminate all of the children.




Once you get the 5 xclocks to work move on to part 2. Submit this program as hw2‐A‐yourname.c or hw2‐A‐yourname.cpp




PART 2 (10 points):

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

Modify the code to create a "fork bomb." A fork bomb is a program that creates a new program which in turn creates a new program etc, etc, etc. For part 2, each of your processes must create 2 children: xclock (if you have any masochistic tendencies, you may want to use netscape or mozilla in place of xclock) and a copy of the parent ‐ which does the "rampant spawning" (silly parent processes "rampant spawning" is for rabbits!).




Submit this program as hw2‐B‐yourname.c or hw2‐B‐yourname.cpp




 
Note that you may bring the machine to its knees and may not be able to log off (press "reset").




 
Note that you cannot do this simply with an infinite loop, that approach will earn you 0 points for this section.




***********************************************************************

* DO NOT RUN THIS ON
SHELL!!!!!!!!!!!!!!!!!!!!!!!
*
*
Only run this on a
machine that you can push the "reset" button!!!! *
*




*
* IF YOU BRING ANY SERVER DOWN SCC WILL LOCK YOUR ACCOUNT! and I will *

* not assist in your getting your account back. *




***********************************************************************




REQUIREMENTS:

‐‐‐‐‐‐‐‐‐‐‐‐‐

 
Your program must run in Streibel 109.




 
Your full name must appear as a comment at the beginning of your program.




 
Your source code must be named hw2‐yourname.c




3. Email your source (subject hw2‐yourname) to rmarsh@cs.und.edu.










Code:

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

#include <stdio.h

#include <stdlib.h




http://rmarsh.cs.und.edu/CLASS/CS451/HOMEWORK/hw2.txt 1/2
rmarsh.cs.und.edu/CLASS/CS451/HOMEWORK/hw2.txt




#include <ctype.h

#include <string.h

#include <sys/types.h

#include <unistd.h

#include <sys/wait.h




#include <signal.h




int main(void) {

int i, j;

int ExitCode1 = 1;

pid_t PID1;




/* Read execv man pages about the following 3 lines. */


char
*path1
= "/usr/X11R6/bin/xclock";


char
*arg1_0
= "xclock";


char
*arg1_1
= NULL;


printf("Spawning a child.\n");


printf("‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐\n");


PID1 = fork();


/*
The child's code.
*/
if
(PID1 ==
0) {




printf("CHILD: Hello from child.\n");




printf("CHILD: Starting XCLOCK..\n");




/* Start
XCLOCK in the forked process.
*/


execlp(path1, arg1_0, arg1_1);




/* The 2 lines below will terminate a user‐written
*/


/* child
process. But, will not work when we execute
*/


/* XCLOCK. Why?
*/
 
printf("CHILD 1: Exiting with code: %d\n", ExitCode1);

 
exit(ExitCode);




}




/* The parent's code. */

if (PID1 != 0) {

/* You need to add a prompt here or something that */

/* will let you decide when to kill off the processes. */




/* Sends kill signal and wiats for child to die off. */ printf("PARENT: Child exited with code: %d\n", kill(PID1, 9)); waitpid(PID1, &ExitCode1, 0);




}




return 0;




}









































More products