Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <signal.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include <sys/wait.h>
  9. #include <sys/types.h>
  10. #include <sys/uio.h>
  11. #include <ctype.h>
  12.  
  13.  
  14. #define MAX_LENGTH 20
  15. #define CONSTRUCT_PROBLEM -1
  16.  
  17. void reverseString(char *word);
  18. void exchangeString(char *word);
  19. void koiString(char *word);
  20. void upRegister(char *word);
  21. void lowRegister(char *word);
  22. void swapRegister(char *word);
  23. void sigHandlr(int signal);
  24.  
  25. int conditionFlag = 0;
  26. int pid_1;
  27. int pipe_01[2];
  28.  
  29. void reverseString(char *word) // Инвертирование строки
  30. {
  31.     char tempSymb;
  32.     int lenString = strlen(word);
  33.  
  34.     for (int i = 0; i < lenString / 2; i++)
  35.     {
  36.         tempSymb = word[i];
  37.         word[i] = word[lenString - i - 1];
  38.         word[lenString - i -1] = tempSymb;
  39.     }
  40. }
  41.  
  42. void exchangeString(char *word) // Обмен соседних символов
  43. {
  44.     char tempSymb;
  45.     int lenString = strlen(word);
  46.     int i = 0;
  47.     while (i < lenString - 1)
  48.     {
  49.         tempSymb = word[i];
  50.         word[i] = word[i + 1];
  51.         word[i + 1] = tempSymb;
  52.         i = i + 2;
  53.     }
  54. }
  55.  
  56.  
  57. void koiString(char *word) // Перевод в КОИ-8
  58. {
  59.     for (int i = 0; i < strlen(word); i++)
  60.     {
  61.         word[i] = word[i] | 128;
  62.     }
  63. }
  64.  
  65. void upRegister(char *word) // Приведение к верхним регистрам
  66. {
  67.     for (int i = 0; i < strlen(word); i++)
  68.     {
  69.         if (word[i] >= 'a' && word[i] <= 'z')
  70.             word[i] = word[i] - ('a' - 'A');
  71.     }
  72. }
  73.  
  74. void lowRegister(char *word) // Приведение к нижним регистрам
  75. {
  76.     for (int i = 0; i < strlen(word); i++)
  77.     {
  78.         if (word[i] >= 'A' && word[i] <= 'Z')
  79.             word[i] = word[i] + ('a' - 'A');
  80.     }
  81. }
  82.  
  83. void swapRegister(char *word) // Смена регистров
  84. {
  85.     for (int i = 0; i < strlen(word); i++)
  86.     {
  87.         if (word[i] >= 'A' && word[i] <= 'Z')
  88.         {
  89.                 word[i] = word[i] + ('a' - 'A');
  90.         }
  91.         else if (word[i] >= 'a' && word[i] <= 'z')
  92.         {
  93.             word[i] = word[i] - ('a' - 'A');
  94.         }
  95.     }
  96. }
  97.  
  98.  
  99.  
  100.  
  101. void sigHandlr(int sign)
  102. {
  103.     signal(SIGINT, sigHandlr); // Ненадежные сигналы
  104.  
  105.     printf("\n\t____________________________________\n");
  106.  
  107.     printf("\n\tEnter a string processing method.\n");
  108.     printf("%s%s%s%s%s",
  109.             "Commands for first processing method :\n\t",
  110.             "def1 - simple broadcast;\n\t",
  111.             "invert - invert the string;\n\t",
  112.             "swap - exchange of adjacent characters in string;\n\t",
  113.             "koi-8 - convertion to koi-8.\n"
  114.             );
  115.     printf("%s%s%s%s%s",
  116.             "Commands for second processing method :\n\t",
  117.             "def2 - string is not altered;\n\t",
  118.             "upreg - make all characters in upper case;\n\t",
  119.             "lowreg - make all characters in lower case\n\t" ,
  120.             "swapreg - swap the register of each char;\n"
  121.             );
  122.     printf("%s%s",
  123.             "Command to finish processing :\n\t",
  124.             "out - finish work;\n"
  125.             );
  126.     printf("\n\t____________________________________\n");
  127.  
  128.     char cmd[10];
  129.  
  130.  
  131.     while (1)
  132.     {
  133.         int i = 0;
  134.  
  135.         do {
  136.                 read(0, cmd + i, 1);
  137.                 i++;
  138.             } while (cmd[i - 1] != '\n');
  139.         cmd[i - 1] = '\0';
  140.  
  141.         if (!strcmp(cmd, "def1")) {       // XY - X for first proc, Y for second proc
  142.             conditionFlag = conditionFlag % 10;
  143.             printf("\tDefault 1\n");
  144.             return;
  145.         }
  146.      
  147.         if (!strcmp(cmd, "invert")) {
  148.             conditionFlag = conditionFlag % 10 + 10;
  149.             printf("\tInvert string\n");
  150.             return;
  151.         }
  152.      
  153.         if (!strcmp(cmd, "swap")) {
  154.             conditionFlag = conditionFlag % 10 + 20;
  155.             printf("\tSwap Symbols\n");
  156.             return;
  157.         }
  158.      
  159.         if (!strcmp(cmd, "koi-8")) {
  160.             conditionFlag = conditionFlag % 10 + 30;
  161.             printf("\tKoi-8\n");
  162.  
  163.             return;
  164.         }
  165.      
  166.  
  167.  
  168.  
  169.  
  170.  
  171.         if (!strcmp(cmd, "def2")) {
  172.             conditionFlag = (conditionFlag / 10) * 10;
  173.             printf("\tDefault 2\n");
  174.             return;
  175.         }
  176.      
  177.         if (!strcmp(cmd, "upreg")) {
  178.             conditionFlag = (conditionFlag / 10) * 10 + 1;
  179.             printf("\tUp Regstrs\n");
  180.             return;
  181.         }
  182.      
  183.         if (!strcmp(cmd, "lowreg")) {
  184.             conditionFlag = (conditionFlag / 10) * 10 + 2;
  185.             printf("\tLow Regstrs\n");
  186.             return;
  187.         }
  188.      
  189.         if (!strcmp(cmd, "swapreg")) {
  190.             conditionFlag = (conditionFlag / 10) * 10 + 3;
  191.             printf("\tSwap Regstrs\n");
  192.             return;
  193.         }
  194.      
  195.         if (!strcmp(cmd, "out")) {
  196.             conditionFlag = 44;
  197.             printf("\tEnd of work\n");
  198.             printf("\tShutting down....\n");
  199.             write(pipe_01[1], &conditionFlag, sizeof(int));
  200.             kill(pid_1, SIGCONT);
  201.             while(wait(NULL) != -1);
  202.             exit(0);
  203.         }
  204.         printf("Some problem occured: wrong command. Try again.\n");
  205.     }
  206. }
  207.  
  208.  
  209.  
  210. int main()
  211. {
  212.  
  213.     int pipe_12[2];
  214.     int i = 0;
  215.  
  216.     int pid_0, pid_2;
  217.  
  218.     pid_0 = getpid(); // get process_0 pid
  219.  
  220.     if (pipe(pipe_01) == -1) //create 0-1 pipe system
  221.     {
  222.         perror("pipe_01 problem");
  223.         exit(CONSTRUCT_PROBLEM);
  224.     }
  225.  
  226.  
  227.     if ( (pid_1 = fork()) == 0)             //WE ARE IN PROCESS_1
  228.     {
  229.         if (pipe(pipe_12) == -1) //create 1-2 pipe system
  230.             {
  231.                 perror("pipe_12 problem");
  232.                 exit(CONSTRUCT_PROBLEM);
  233.             }
  234.  
  235.         if ( (pid_2 = fork()) == 0) //WE ARE IN WORKING PROCESS_2
  236.         {
  237.             close(pipe_12[1]);
  238.             close(pipe_01[0]);
  239.             close(pipe_01[1]);
  240.             signal(SIGINT, SIG_IGN);
  241.             while (1)
  242.             {
  243.                 int flag1 = 0;
  244.                 char bufer_12[MAX_LENGTH];
  245.                 i = 0;
  246.                 kill(getpid(), SIGSTOP);
  247.                 read(pipe_12[0], &flag1, sizeof(flag1));
  248.                 read(pipe_12[0], bufer_12, MAX_LENGTH);
  249.  
  250.                 switch (flag1 % 10)
  251.                 {
  252.                     case 1:
  253.                         upRegister(bufer_12);
  254.                         break;
  255.                     case 2:
  256.                         lowRegister(bufer_12);
  257.                         break;
  258.                     case 3:
  259.                         swapRegister(bufer_12);
  260.                     default:
  261.                         break;
  262.                 }
  263.                     write(1,"\tProcess 2 : ", 14);
  264.                     write(1, bufer_12, strlen(bufer_12));
  265.                     putchar('\n');
  266.                     printf("Enter the string to alt it in different ways:\n");
  267.             }
  268.         }
  269.         else
  270.         {
  271.             if (pid_2 == -1) // Problem with creating proc_2
  272.             {
  273.                 perror("proc_2 problem");
  274.                 exit(CONSTRUCT_PROBLEM);
  275.             }
  276.             else // WE ARE IN WORKING PROCESS_1
  277.             {
  278.                 close(pipe_01[1]);
  279.                 close(pipe_12[0]);
  280.                 signal(SIGINT, SIG_IGN);
  281.                 while (1)
  282.                 {
  283.                     int i = 0;
  284.                     char symb;
  285.                     char bufer_01[MAX_LENGTH];
  286.                     int flag = 0;
  287.                     kill(getpid(), SIGSTOP);
  288.                     read(pipe_01[0], &flag, sizeof(flag));
  289.                     if (flag == 44)
  290.                     {
  291.  
  292.                         kill(pid_2, SIGKILL);
  293.                         exit(0);
  294.                     }
  295.                     read(pipe_01[0], bufer_01, MAX_LENGTH);
  296.                     switch (flag / 10)
  297.                     {
  298.                         case 1:
  299.                             reverseString(bufer_01);
  300.                             break;
  301.                         case 2:
  302.                             exchangeString(bufer_01);
  303.                             break;
  304.                         case 3:
  305.                             koiString(bufer_01);
  306.                             break;
  307.                         default:
  308.                             break;
  309.                     }
  310.                     write(1,"\tProcess 1 : ", 14);
  311.                     write(1, bufer_01, strlen(bufer_01));
  312.                     putchar('\n');
  313.                     write(pipe_12[1], &flag, sizeof(int));
  314.                     write(pipe_12[1], bufer_01, strlen(bufer_01) + 1);
  315.                     kill(pid_2, SIGCONT);
  316.                 }
  317.             }
  318.         }
  319.     }
  320.  
  321.     else                          //WE ARE IN PROCESS_0
  322.     {
  323.  
  324.         if (pid_1 == -1) // Problem with creating proc_1
  325.         {
  326.             perror("proc_1 problem");
  327.             exit(CONSTRUCT_PROBLEM);
  328.         }
  329.  
  330.         else // WE ARE IN WORKING PROCESS_0
  331.         {
  332.             close(pipe_01[0]);
  333.             signal(SIGINT, sigHandlr);
  334.             conditionFlag = 11;
  335.             printf("Enter the string to process it in different ways:\n");
  336.             while (1)
  337.             {
  338.                 char inputWord[MAX_LENGTH];
  339.                 int i = 0;
  340.                 do {
  341.                 read(0, inputWord + i, 1);
  342.                 i++;
  343.             } while (inputWord[i - 1] != '\n');
  344.             inputWord[i - 1] = '\0';
  345.  
  346.                 write(1,"\tProcess 0 : ", 14);
  347.                 write(1, inputWord, strlen(inputWord));
  348.                 putchar('\n');
  349.                 write(pipe_01[1], &conditionFlag, sizeof(int));
  350.                 write(pipe_01[1], inputWord, strlen(inputWord) + 1);
  351.                 kill(pid_1, SIGCONT);
  352.             }
  353.         }
  354.         while(wait(NULL) != -1);
  355.         exit(0);
  356.     }
  357.     return 0;
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement