Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <wait.h>
- #include <string.h>
- #include <errno.h>
- #include <time.h>
- #include <string.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <sys/time.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <sys/resource.h>
- int main(int argc, char *argv[]){
- int pid;
- if ((pid = fork()) < 0) return 1;
- else if (pid == 0){//son
- execlp("gcc", "gcc", "-g", "-Wall", "-pedantic", "util.c", "shell_pipe.c", "-o", "shell_pipe", NULL);
- printf("can't compile shell_pipe\n");
- return 1;
- //end son
- }else{
- int compile_status, pid_compile;
- int pid1, pid2, pid3, pid4, pid5;
- int fd[5][2];
- int i;
- time_t time1, time2;
- for (i = 0; i < 5; i++){
- pipe(fd[i]);
- }
- int status1, status2, status3, status4, status5;
- while ((pid_compile = wait(&compile_status)) == -1){
- if (errno != EINTR) return 1;
- }
- if (!WIFEXITED(compile_status)) return 1;
- else if (!WEXITSTATUS(compile_status)){
- printf("Compilation has been succeed\n");
- time1 = time(NULL);
- if ((pid1 = fork()) < 0){
- perror("fork1 ");
- exit(1);
- }else if (pid1 == 0){//SON 1
- char *str = "sleep 2 | sleep 1\n";
- int len = strlen(str);
- dup2(fd[0][0], 0);
- write(fd[0][1], str, len);
- for (i = 0; i < 5; i++){
- close(fd[i][1]);
- close(fd[i][0]);
- }
- execl("./shell_pipe", "./shell_pipe", NULL);
- printf("can't exec parallel test\n");
- return 1;
- }else{
- while((pid1 = wait(&status1)) == -1){
- if (errno != EINTR) return 1;
- }
- if (!WIFEXITED(status1)) return 1;
- else{
- time2 = time(NULL);
- if (time2 - time1 == 2){
- printf("Parallel executing\n");
- }
- }
- /*time1 = time(NULL);
- if ((pid2 = fork()) < 0){
- perror("fork2 ");
- exit(1);
- }else if (pid2 == 0){//SON 2
- char *str = "sleep 3 | ls -a | cat |sort\n";
- int len = strlen(str);
- dup2(fd[1][0], 0);
- write(fd[1][1], str, len);
- for (i = 0; i < 5; i++){
- close(fd[i][1]);
- close(fd[i][0]);
- }
- execl("./shell_pipe", "./shell_pipe", NULL);
- printf("can't test when the pipe ends\n");
- return 1;
- }else{
- while((pid2 = wait(&status2)) == -1){
- if (errno != EINTR) return 1;
- }
- if (!WIFEXITED(status2)) return 1;
- else{
- time2 = time(NULL);
- if (time2 - time1 == 3){
- printf("Pipe has been completed after the last program\n");
- }
- }
- }*/
- }
- for (i = 0; i < 5; i++){
- close(fd[i][1]);
- close(fd[i][0]);
- }
- return 0;
- }else{
- printf("Compillation hasn't been exited\n");
- return WEXITSTATUS(compile_status);
- }
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement