Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <signal.h>
- #include <sys/wait.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- int pfd[2][2];
- int pid1, pid2, pid3;
- int letters, lowercase;
- int count = 0;
- void fiu(int p){
- printf("KKK\n");
- close(pfd[p][1]);
- int letters = 0;
- int rd, size = 1;
- char buffer[size];
- while((rd = read(pfd[p][0], buffer, size)) != 0){
- printf("%s \n", buffer);
- letters++;
- }
- sleep(4);
- close(pfd[p][0]);
- printf("letters: %d \n", letters);
- exit(letters);
- }
- void fiu3(char *file){
- close(pfd[0][0]);
- close(pfd[0][1]);
- close(pfd[1][0]);
- close(pfd[1][1]);
- sleep(2);
- execlp("wc", "wc", "-w", file, (char*)0);
- printf("Error at exec \n");
- exit(1);
- }
- void fiu_ends(int sig){
- int status, pid;
- printf("LLL\n");
- if ((pid = wait(&status)) < 0) {
- printf("error wait \n");
- exit(1);
- }
- printf("Fiul cu pid=%d exitcode=%d \n", pid, WEXITSTATUS(status));
- if (pid1 == pid){
- letters = WEXITSTATUS(status);
- }else if (pid2 == pid){
- lowercase = WEXITSTATUS(status);
- }
- count++;
- if (count == 3){
- printf("Toti fiii s-au terminat\n");
- printf("GATA \n ");
- exit(0);
- }
- }
- void parent(char *file){
- close(pfd[0][0]);
- close(pfd[1][0]);
- struct sigaction act;
- act.sa_handler = fiu_ends;
- if (sigaction(SIGCHLD, &act, NULL) < 0){
- printf("error sigaction \n");
- exit(1);
- }
- int fd;
- if ((fd = open(file, O_RDONLY)) < 0){
- printf("Cannot open file \n");
- exit(1);
- }
- int rd, size = 512;
- char buffer[size];
- while((rd = read(fd, &buffer, size)) != 0){
- for (int i = 0; i < rd; i++){
- printf("buff : %d \n", buffer[i]);
- if ((buffer[i] >= 'A' && buffer[i] <= 'Z') || (buffer[i] >='a' && buffer[i] <= 'z')) {
- write(pfd[0][1], buffer[i], 1);
- }
- if (buffer[i] >='a' && buffer[i] <= 'z') {
- write(pfd[1][1], buffer[i], 1);
- }
- }
- }
- close(pfd[0][1]);
- close(pfd[1][1]);
- }
- int main(int argc, char *argv[]){
- if (argc < 2){
- printf("Usage: %s file\n", argv[0]);
- exit(1);
- }
- if (pipe(pfd[0]) < 0){
- printf("Error pipe 1\n");
- exit(1);
- }
- if (pipe(pfd[1]) < 0){
- printf("Error pipe 2\n");
- exit(1);
- }
- if ((pid1 = fork()) < 0){
- printf("Error fork 1\n");
- exit(1);
- }else if (pid1 == 0){
- fiu(0);
- }
- if ((pid2 = fork()) < 0){
- printf("Error fork 2\n");
- exit(1);
- }else if (pid2 == 0){
- fiu(1);
- }
- if ((pid3 = fork()) < 0){
- printf("Error fork 3\n");
- exit(1);
- }else if (pid3 == 0){
- fiu3(argv[1]);
- }
- parent(argv[1]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement