SICE Curriculum Portal for Thomas Heffron
Contents

Home
UMKC Curriculum
   FS '96
      CS 101
   SS '01
      CS 201
   FS '01
      CS 191
      CS 281
   WS '02
      CS 291
      EN 304wi
   SS '02
      CS 352
      CS 481
   FS '02
      CS 431
      CS 441
   WS '03
      CS 423
      CS 451
SICE Survival Guide
Personal Experience

contact me @umkc:
tehqnf@umkc.edu

CS441 - Homework #3

/****************************************
Name:     Thomas Heffron (tehqnf@umkc.edu)
Section:  CS431 NET
Program:  Program #3 - Part #1
Due Date: Oct. 15, 2002
Desc:     Write a program to create (fork/exec)
          a group of processes as read from
          the file sent as cmd argument.
Inputs:   Text file sent as cmd argument.
Outputs:  First a listing of each command in
          file and PID created to execute.
          Normal output from each command
          may be mixed as each proc gets
          the CPU.
*****************************************/

#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
extern char **environ;
extern int errno;
pid_t pid;

int main(int argc, char *argv[])
{
    /* Type declarations */
    FILE *in_file;
    char progname[128];
    char *args[] = { NULL };

    if (argc == 2) { /* Check for correct number of args */
        in_file = fopen(argv[1], "r");
        while (fscanf(in_file, "%s", progname) != EOF) { /* Read until EOF */
            pid = fork(); /* fork() for each line in file */
            if (pid == 0) { /* If child proc run execve() */
                if (execve(progname, args, environ) == -1) {
                    printf("ERROR IN EXECVE ");
                    perror("execve");
                }
                sleep(1); /* Sleep 1 second after each new proc */
            } else { /* If not child proc display info on new proc */
                printf("Created new process '%s' with ID = %d\n", progname, pid);
            }
        }
    } else { /* If not correct args display usage and exit */
        printf("Usage: %s input_file\n", argv[0]);
        exit(1);
    }
    sleep(10); /* Sleep 10 seconds after call to all procs */
    return 0;
}