Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/wait.h>
- int main(int argc, char *args[]) {
- if(argc < 3) {
- fprintf(stderr, "Usage: %s <number> <file1> <file2> ...\n", args[0]);
- exit(1);
- }
- int pid;
- int i = 0;
- if((pid=fork()) < 0)
- {
- perror("ERROR");
- exit(1);
- }
- if(pid==0)
- {
- for(i = 0; i <= atoi(args[1]); i++) {
- printf("%d: %d\n", getpid(), i);
- }
- exit(0);
- }
- for(i = 2; i <= argc-1; i++) {
- if((pid=fork()) < 0)
- {
- perror("ERROR");
- exit(1);
- }
- if(pid==0)
- {
- char *argv[] = { "wc", args[i], "-l", 0 };
- execvp(argv[0], argv);
- } else {
- int status;
- wait(&status);
- if (WIFEXITED(status)) {
- printf("PID: %d exited, status=%d\n", pid, WEXITSTATUS(status));
- } else if (WIFSIGNALED(status)) {
- printf("PID: %d killed by signal %d\n", pid, WTERMSIG(status));
- } else if (WIFSTOPPED(status)) {
- printf("PID: %d stopped by signal %d\n", pid, WSTOPSIG(status));
- } else if (WIFCONTINUED(status)) {
- printf("PID: %d continued\n", pid);
- }
- }
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement