Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char** argv)
- {
- int forkCount; // will come from argv[1]
- int pid = 0; // forked pid
- int mypid = 0; // forked pid
- int isChild = 0; // duh.
- int i; // counter
- if (argc < 1)
- {
- printf("ERROR: no amount of forks specified\n");
- return 1;
- }
- forkCount = atoi(argv[1]);
- if (forkCount == 0)
- {
- printf("ERROR: zero (or invalid) amount of forks specified\n");
- return 1;
- }
- printf("fork pids: ");
- for (i = 0; i < forkCount; i++)
- {
- pid = fork();
- if (pid == 0)
- {
- isChild = 1;
- break;
- }
- else
- {
- printf("%d:%d ", i, pid);
- }
- }
- if (isChild)
- {
- while (1)
- {
- sleep(1);
- }
- }
- else
- {
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement