Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <unistd.h>
- void childsWork(int ID);
- int main(int argc, char **argv) {
- int numOfChildProcesses;
- // Receive an integer numeric command line argument representing the number
- // of child processes to create represented by numOfChildProcesses
- char c;
- if(sscanf(argv[1], "%d %c", &numOfChildProcesses, &c) != 1) {
- printf("Must include integer numeric command line argument");
- return -1;
- }
- // Create a number of child processes equal to numOfChildProcesses
- for(int i = 0; i < numOfChildProcesses; i++) {
- int pid = fork();
- // Determine whether current process is a child process (i.e. PID = 0)
- // If true, execute childs work and exit gracefully
- // If false, continue
- if(pid == 0) {
- childsWork(i);
- exit(0);
- }
- }
- return 0;
- }
- void childsWork(int ID) {
- // Arbitrarily simulate a variable amount of time that a processes
- // task could take to complete
- srand(time(NULL));
- int n = rand() % 10000000;
- for(int i = 0; i < n; i++);
- printf("Child: %d, PID: %d\n", ID, getpid());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement