os161-1.99
 All Data Structures
farm.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  * farm.c
00032  *
00033  *      Run a bunch of cpu pigs and one cat.
00034  *      The file it cats is "catfile". This should be created in advance.
00035  *
00036  * This test should itself run correctly when the basic system calls
00037  * are complete. It may be helpful for scheduler performance analysis.
00038  */
00039 
00040 #include <unistd.h>
00041 #include <err.h>
00042 
00043 static char *hargv[2] = { (char *)"hog", NULL };
00044 static char *cargv[3] = { (char *)"cat", (char *)"catfile", NULL };
00045 
00046 #define MAXPROCS  6
00047 static int pids[MAXPROCS], npids;
00048 
00049 static
00050 void
00051 spawnv(const char *prog, char **argv)
00052 {
00053         int pid = fork();
00054         switch (pid) {
00055             case -1:
00056                 err(1, "fork");
00057             case 0:
00058                 /* child */
00059                 execv(prog, argv);
00060                 err(1, "%s", prog);
00061             default:
00062                 /* parent */
00063                 pids[npids++] = pid;
00064                 break;
00065         }
00066 }
00067 
00068 static
00069 void
00070 waitall(void)
00071 {
00072         int i, status;
00073         for (i=0; i<npids; i++) {
00074                 if (waitpid(pids[i], &status, 0)<0) {
00075                         warn("waitpid for %d", pids[i]);
00076                 }
00077                 else if (WIFSIGNALED(status)) {
00078                         warnx("pid %d: signal %d", pids[i], WTERMSIG(status));
00079                 }
00080                 else if (WEXITSTATUS(status) != 0) {
00081                         warnx("pid %d: exit %d", pids[i], WEXITSTATUS(status));
00082                 }
00083         }
00084 }
00085 
00086 static
00087 void
00088 hog(void)
00089 {
00090         spawnv("/testbin/hog", hargv);
00091 }
00092 
00093 static
00094 void
00095 cat(void)
00096 {
00097         spawnv("/bin/cat", cargv);
00098 }
00099 
00100 int
00101 main()
00102 {
00103         hog();
00104         hog();
00105         hog();
00106         cat();
00107 
00108         waitall();
00109 
00110         return 0;
00111 }
 All Data Structures