Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <signal.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <sys/uio.h>
- #include <ctype.h>
- #define MAX_LENGTH 20
- #define CONSTRUCT_PROBLEM -1
- void reverseString(char *word);
- void exchangeString(char *word);
- void koiString(char *word);
- void upRegister(char *word);
- void lowRegister(char *word);
- void swapRegister(char *word);
- void sigHandlr(int signal);
- int conditionFlag = 0;
- int pid_1;
- int pipe_01[2];
- void reverseString(char *word) // Рнвертирование строки
- {
- char tempSymb;
- int lenString = strlen(word);
- for (int i = 0; i < lenString / 2; i++)
- {
- tempSymb = word[i];
- word[i] = word[lenString - i - 1];
- word[lenString - i -1] = tempSymb;
- }
- }
- void exchangeString(char *word) // Обмен соседних символов
- {
- char tempSymb;
- int lenString = strlen(word);
- int i = 0;
- while (i < lenString - 1)
- {
- tempSymb = word[i];
- word[i] = word[i + 1];
- word[i + 1] = tempSymb;
- i = i + 2;
- }
- }
- void koiString(char *word) // Перевод РІ РљРћР-8
- {
- for (int i = 0; i < strlen(word); i++)
- {
- word[i] = word[i] | 128;
- }
- }
- void upRegister(char *word) // Приведение к верхним регистрам
- {
- for (int i = 0; i < strlen(word); i++)
- {
- if (word[i] >= 'a' && word[i] <= 'z')
- word[i] = word[i] - ('a' - 'A');
- }
- }
- void lowRegister(char *word) // Приведение к нижним регистрам
- {
- for (int i = 0; i < strlen(word); i++)
- {
- if (word[i] >= 'A' && word[i] <= 'Z')
- word[i] = word[i] + ('a' - 'A');
- }
- }
- void swapRegister(char *word) // Смена регистров
- {
- for (int i = 0; i < strlen(word); i++)
- {
- if (word[i] >= 'A' && word[i] <= 'Z')
- {
- word[i] = word[i] + ('a' - 'A');
- }
- else if (word[i] >= 'a' && word[i] <= 'z')
- {
- word[i] = word[i] - ('a' - 'A');
- }
- }
- }
- void sigHandlr(int sign)
- {
- signal(SIGINT, sigHandlr); // Ненадежные сигналы
- printf("\n\t____________________________________\n");
- printf("\n\tEnter a string processing method.\n");
- printf("%s%s%s%s%s",
- "Commands for first processing method :\n\t",
- "def1 - simple broadcast;\n\t",
- "invert - invert the string;\n\t",
- "swap - exchange of adjacent characters in string;\n\t",
- "koi-8 - convertion to koi-8.\n"
- );
- printf("%s%s%s%s%s",
- "Commands for second processing method :\n\t",
- "def2 - string is not altered;\n\t",
- "upreg - make all characters in upper case;\n\t",
- "lowreg - make all characters in lower case\n\t" ,
- "swapreg - swap the register of each char;\n"
- );
- printf("%s%s",
- "Command to finish processing :\n\t",
- "out - finish work;\n"
- );
- printf("\n\t____________________________________\n");
- char cmd[10];
- while (1)
- {
- int i = 0;
- do {
- read(0, cmd + i, 1);
- i++;
- } while (cmd[i - 1] != '\n');
- cmd[i - 1] = '\0';
- if (!strcmp(cmd, "def1")) { // XY - X for first proc, Y for second proc
- conditionFlag = conditionFlag % 10;
- printf("\tDefault 1\n");
- return;
- }
- if (!strcmp(cmd, "invert")) {
- conditionFlag = conditionFlag % 10 + 10;
- printf("\tInvert string\n");
- return;
- }
- if (!strcmp(cmd, "swap")) {
- conditionFlag = conditionFlag % 10 + 20;
- printf("\tSwap Symbols\n");
- return;
- }
- if (!strcmp(cmd, "koi-8")) {
- conditionFlag = conditionFlag % 10 + 30;
- printf("\tKoi-8\n");
- return;
- }
- if (!strcmp(cmd, "def2")) {
- conditionFlag = (conditionFlag / 10) * 10;
- printf("\tDefault 2\n");
- return;
- }
- if (!strcmp(cmd, "upreg")) {
- conditionFlag = (conditionFlag / 10) * 10 + 1;
- printf("\tUp Regstrs\n");
- return;
- }
- if (!strcmp(cmd, "lowreg")) {
- conditionFlag = (conditionFlag / 10) * 10 + 2;
- printf("\tLow Regstrs\n");
- return;
- }
- if (!strcmp(cmd, "swapreg")) {
- conditionFlag = (conditionFlag / 10) * 10 + 3;
- printf("\tSwap Regstrs\n");
- return;
- }
- if (!strcmp(cmd, "out")) {
- conditionFlag = 44;
- printf("\tEnd of work\n");
- printf("\tShutting down....\n");
- write(pipe_01[1], &conditionFlag, sizeof(int));
- kill(pid_1, SIGCONT);
- while(wait(NULL) != -1);
- exit(0);
- }
- printf("Some problem occured: wrong command. Try again.\n");
- }
- }
- int main()
- {
- int pipe_12[2];
- int i = 0;
- int pid_0, pid_2;
- pid_0 = getpid(); // get process_0 pid
- if (pipe(pipe_01) == -1) //create 0-1 pipe system
- {
- perror("pipe_01 problem");
- exit(CONSTRUCT_PROBLEM);
- }
- if ( (pid_1 = fork()) == 0) //WE ARE IN PROCESS_1
- {
- if (pipe(pipe_12) == -1) //create 1-2 pipe system
- {
- perror("pipe_12 problem");
- exit(CONSTRUCT_PROBLEM);
- }
- if ( (pid_2 = fork()) == 0) //WE ARE IN WORKING PROCESS_2
- {
- close(pipe_12[1]);
- close(pipe_01[0]);
- close(pipe_01[1]);
- signal(SIGINT, SIG_IGN);
- while (1)
- {
- int flag1 = 0;
- char bufer_12[MAX_LENGTH];
- i = 0;
- kill(getpid(), SIGSTOP);
- read(pipe_12[0], &flag1, sizeof(flag1));
- read(pipe_12[0], bufer_12, MAX_LENGTH);
- switch (flag1 % 10)
- {
- case 1:
- upRegister(bufer_12);
- break;
- case 2:
- lowRegister(bufer_12);
- break;
- case 3:
- swapRegister(bufer_12);
- default:
- break;
- }
- write(1,"\tProcess 2 : ", 14);
- write(1, bufer_12, strlen(bufer_12));
- putchar('\n');
- printf("Enter the string to alt it in different ways:\n");
- }
- }
- else
- {
- if (pid_2 == -1) // Problem with creating proc_2
- {
- perror("proc_2 problem");
- exit(CONSTRUCT_PROBLEM);
- }
- else // WE ARE IN WORKING PROCESS_1
- {
- close(pipe_01[1]);
- close(pipe_12[0]);
- signal(SIGINT, SIG_IGN);
- while (1)
- {
- int i = 0;
- char symb;
- char bufer_01[MAX_LENGTH];
- int flag = 0;
- kill(getpid(), SIGSTOP);
- read(pipe_01[0], &flag, sizeof(flag));
- if (flag == 44)
- {
- kill(pid_2, SIGKILL);
- exit(0);
- }
- read(pipe_01[0], bufer_01, MAX_LENGTH);
- switch (flag / 10)
- {
- case 1:
- reverseString(bufer_01);
- break;
- case 2:
- exchangeString(bufer_01);
- break;
- case 3:
- koiString(bufer_01);
- break;
- default:
- break;
- }
- write(1,"\tProcess 1 : ", 14);
- write(1, bufer_01, strlen(bufer_01));
- putchar('\n');
- write(pipe_12[1], &flag, sizeof(int));
- write(pipe_12[1], bufer_01, strlen(bufer_01) + 1);
- kill(pid_2, SIGCONT);
- }
- }
- }
- }
- else //WE ARE IN PROCESS_0
- {
- if (pid_1 == -1) // Problem with creating proc_1
- {
- perror("proc_1 problem");
- exit(CONSTRUCT_PROBLEM);
- }
- else // WE ARE IN WORKING PROCESS_0
- {
- close(pipe_01[0]);
- signal(SIGINT, sigHandlr);
- conditionFlag = 11;
- printf("Enter the string to process it in different ways:\n");
- while (1)
- {
- char inputWord[MAX_LENGTH];
- int i = 0;
- do {
- read(0, inputWord + i, 1);
- i++;
- } while (inputWord[i - 1] != '\n');
- inputWord[i - 1] = '\0';
- write(1,"\tProcess 0 : ", 14);
- write(1, inputWord, strlen(inputWord));
- putchar('\n');
- write(pipe_01[1], &conditionFlag, sizeof(int));
- write(pipe_01[1], inputWord, strlen(inputWord) + 1);
- kill(pid_1, SIGCONT);
- }
- }
- while(wait(NULL) != -1);
- exit(0);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement