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;
}