os161-1.99
 All Data Structures
forkbomb.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * forkbomb - apply malthus to an operating system ;-)
00032  *
00033  * DO NOT RUN THIS ON A REAL SYSTEM - IT WILL GRIND TO A HALT AND
00034  * PEOPLE WILL COME AFTER YOU WIELDING BASEBALL BATS OR THE AD
00035  * BOARD(*). WE WARNED YOU.
00036  *
00037  * We don't expect your system to withstand this without grinding to
00038  * a halt, but once your basic system calls are complete it shouldn't
00039  * crash. Likewise for after your virtual memory system is complete.
00040  *
00041  * (...at least in an ideal world. However, it can be difficult to
00042  * handle all the loose ends involved. Heroic measures are not
00043  * expected. If in doubt, talk to the course staff.)
00044  *
00045  *
00046  * (*) The Administrative Board of Harvard College handles formal
00047  * disciplinary action.
00048  */
00049 
00050 #include <unistd.h>
00051 #include <err.h>
00052 
00053 static volatile int pid;
00054 
00055 int
00056 main()
00057 {
00058         int i;
00059 
00060         while (1) {
00061                 fork();
00062 
00063                 pid = getpid();
00064 
00065                 /* Make sure each fork has its own address space. */
00066                 for (i=0; i<300; i++) {
00067                         volatile int seenpid;
00068                         seenpid = pid;
00069                         if (seenpid != getpid()) {
00070                                 errx(1, "pid mismatch (%d, should be %d) "
00071                                      "- your vm is broken!", 
00072                                      seenpid, getpid());
00073                         }
00074                 }
00075         }
00076 }
 All Data Structures