negtab

Bird

Sep 11th, 2025
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. enum clientState {waitCS, inputCS};
  5.  
  6. struct processor
  7. {
  8.     int ticTime, inTime;
  9. };
  10.  
  11. struct clientNode
  12. {
  13.     int id, priority, taskCount, inputWait;
  14.     int *taskTime;
  15.     bool isFinished;
  16.     enum clientState state;
  17.  
  18.     struct clientNode *next;
  19. };
  20.  
  21. struct clientQueue
  22. {
  23.     int clientCount, downTime;
  24.     float efficiency;
  25.     struct clientNode  *head, *tail;
  26. };
  27.  
  28. struct clientNode *newNode(const int id, const int priority, const int taskCount, int *taskTime, enum clientState state)
  29. {
  30.     struct clientNode *newClient = (struct clientNode*)malloc(sizeof(struct clientNode));
  31.     newClient->id = id;
  32.     newClient->priority = priority;
  33.     newClient->taskCount = taskCount;
  34.     newClient->taskTime = taskTime;
  35.     newClient->isFinished = false;
  36.     newClient->state = state;
  37.     return newClient;
  38. }
  39.  
  40. void pushClient(struct clientQueue *clients, struct clientNode *newNode)
  41. {
  42.     if (!clients || !newNode)
  43.         return;
  44.  
  45.     struct clientNode *node = malloc(sizeof(struct clientNode));
  46.     if (!node)
  47.         return;
  48.  
  49.     node->id = newNode->id;
  50.     node->priority = newNode->priority;
  51.     node->taskCount = newNode->taskCount;
  52.     node->isFinished = false;
  53.     node->state = waitCS;
  54.  
  55.     node->taskTime = malloc(sizeof(int) * newNode->taskCount);
  56.     for (int i = 0; i < newNode->taskCount; i++)
  57.         node->taskTime[i] = newNode->taskTime[i];
  58.     node->next = nullptr;
  59.  
  60.     if (clients->head == NULL) // первая вставка
  61.     {
  62.         clients->head = node;
  63.         clients->tail = node;
  64.         clients->clientCount = 1;
  65.         return;
  66.     }
  67.  
  68.     // вставка по приоритету
  69.     struct clientNode *prev = nullptr;
  70.     struct clientNode *curr = clients->head;
  71.     while (curr && curr->priority >= node->priority)
  72.     {
  73.         prev = curr;
  74.         curr = curr->next;
  75.     }
  76.  
  77.     if (prev == NULL) // вставляем в голову
  78.     {
  79.         node->next = clients->head;
  80.         clients->head = node;
  81.     }
  82.     else // вставляем между prev и curr
  83.     {
  84.         prev->next = node;
  85.         node->next = curr;
  86.     }
  87.  
  88.     if (curr == NULL) // вставка в хвост
  89.         clients->tail = node;
  90.  
  91.     clients->clientCount++;
  92. }
  93.  
  94. bool isFinished(const struct clientQueue *clients)
  95. {
  96.     const struct clientNode *temp = clients->head;
  97.     while (temp != NULL)
  98.     {
  99.         if(!temp->isFinished)
  100.             return false;
  101.         temp = temp->next;
  102.     }
  103.     return true;
  104. }
  105.  
  106. struct clientNode *getClient(const struct clientQueue *clients, const enum clientState state)
  107. {
  108.     if (!clients || !clients->head)
  109.         return nullptr;
  110.  
  111.     struct clientNode *temp = clients->head;
  112.     while (temp != NULL)
  113.     {
  114.         if (temp->state == state && !temp->isFinished)
  115.             return temp;
  116.         temp = temp->next;
  117.     }
  118.     return NULL;
  119. }
  120.  
  121. void isClientDone(struct clientNode *client)
  122. {
  123.     for (int i = 0; i < client->taskCount; i++)
  124.         if (client->taskTime[i] != 0)
  125.             return;
  126.     client->isFinished = true;
  127.     client->taskCount = 0;
  128. }
  129.  
  130. int findTaskIndex(struct clientNode *node)
  131. {
  132.     if (!node)
  133.         return -1;
  134.     for (int i = 0; i < node->taskCount; i++)
  135.         if (node->taskTime[i] != 0)
  136.             return i;
  137.     return -1;
  138. }
  139.  
  140. int sumTaskTime(struct clientNode *node) {
  141.     if (!node)
  142.         return -1;
  143.     int sum = 0;
  144.     for (int i = 0; i <= node->taskCount; i++)
  145.         sum += node->taskTime[i];
  146.     return sum;
  147. }
  148.  
  149. int sumAllTaskTime(struct clientQueue *clients) {
  150.     if (!clients)
  151.         return -1;
  152.     int sum = 0;
  153.     struct clientNode *temp = clients->head;
  154.     for (int i = 0; i < clients->clientCount; i++) {
  155.         sum += sumTaskTime(temp);
  156.         temp = temp->next;
  157.     }
  158.     return sum;
  159. }
  160.  
  161. void exchangeSamePriority(struct clientQueue *clients, struct clientNode *node)
  162. {
  163.     if (!clients || !node)
  164.         return;
  165.  
  166.     struct clientNode *prev = NULL;
  167.     struct clientNode *curr = clients->head;
  168.  
  169.     // ищем node
  170.     while (curr && curr != node)
  171.     {
  172.         prev = curr;
  173.         curr = curr->next;
  174.     }
  175.  
  176.     if (!curr)
  177.         return;
  178.  
  179.     // если следующий другой приоритет → ничего
  180.     if (!curr->next || curr->priority != curr->next->priority)
  181.         return;
  182.  
  183.     // находим конец группы одинакового приоритета
  184.     struct clientNode *groupEnd = curr->next;
  185.     while (groupEnd->next && groupEnd->priority == curr->priority)
  186.         groupEnd = groupEnd->next;
  187.  
  188.     // вырезаем curr
  189.     if (prev) prev->next = curr->next;
  190.     else clients->head = curr->next;
  191.  
  192.     // вставляем после groupEnd
  193.     curr->next = groupEnd->next;
  194.     groupEnd->next = curr;
  195.  
  196.     if (!curr->next)
  197.         clients->tail = curr;
  198. }
  199.  
  200.  
  201. void startProcessorWork(const struct processor processor, struct clientQueue *clients)
  202. {
  203.     int usedTime = 0;
  204.     int neededTime = sumAllTaskTime(clients);
  205.     while(!isFinished(clients))
  206.     {
  207.         struct clientNode *findClient = getClient(clients, waitCS);
  208.         struct clientNode *temp = clients->head;
  209.         if (findClient != NULL)
  210.         {
  211.             const int index = findTaskIndex(findClient);
  212.             if(findClient->taskTime[index] <= processor.ticTime)
  213.             {
  214.                 findClient->inputWait = (processor.inTime - (processor.ticTime - findClient->taskTime[index]) > 0) ? processor.inTime - (processor.ticTime - findClient->taskTime[index]) : 0;
  215.  
  216.                 findClient->state = (findClient->inputWait == 0) ? waitCS : inputCS;
  217.  
  218.                 clients->downTime+= processor.ticTime - findClient->taskTime[index];
  219.                 findClient->taskTime[index] = 0;
  220.                 isClientDone(findClient);
  221.             }
  222.             else
  223.             {
  224.                 findClient->taskTime[index] = findClient->taskTime[index] - processor.ticTime > 0 ? findClient->taskTime[index] - processor.ticTime : 0;
  225.                 findClient->state = waitCS;
  226.             }
  227.             exchangeSamePriority(clients, findClient);
  228.         }
  229.         while (temp != NULL)
  230.         {
  231.             if(temp != findClient && temp->state == inputCS)
  232.             {
  233.                 temp->state = (temp->inputWait > processor.ticTime) ? inputCS : waitCS;
  234.                 temp->inputWait = (temp->inputWait - processor.ticTime) > 0 ? temp->inputWait - processor.ticTime : 0;
  235.             }
  236.             temp = temp->next;
  237.         }
  238.         usedTime += processor.ticTime;
  239.         if(!findClient)
  240.             clients->downTime+= processor.ticTime;
  241.     }
  242.     clients->efficiency = neededTime / (float)usedTime;
  243. }
  244.  
  245. int main(void)
  246. {
  247.     struct processor proc;
  248.     struct clientQueue *clients = malloc(sizeof(struct clientQueue));
  249.  
  250.     int task1[] = { 8, 3, 6, 4, 5, 1, 1, 7 };
  251.     int task2[] = { 5, 4, 5 ,4 ,5, 4, 5, 4, 5, 4 };
  252.     int task3[] = { 3, 2, 1, 4, 1, 1, 2, 3, 4, 5 };
  253.     int task4[] = { 8, 7, 6, 7, 5, 3, 8, 4, 2, 1 };
  254.     int task5[] = { 5, 4, 3, 5, 4, 2, 3, 1, 6, 3 };
  255.     int task6[] = { 1, 2, 1, 3, 2, 3, 1, 3, 2, 1 };
  256.     //int task1[] = { 4, 2, 3 };
  257.     //int task2[] = { 2, 4, 2 };
  258.     //int task3[] = { 2, 3, 3 };
  259.     //int task4[] = { 2, 1, 3 };
  260.  
  261.     printf("Enter time of tic: ");
  262.     scanf("%d", &proc.ticTime);
  263.  
  264.     printf("Enter time of input: ");
  265.     scanf("%d", &proc.inTime);
  266.  
  267.     clients->efficiency = 0.0f;
  268.     clients->downTime = 0;
  269.  
  270.     pushClient(clients, newNode(1,3,8,task1, waitCS));
  271.     pushClient(clients, newNode(2,3,10,task2, waitCS));
  272.     pushClient(clients, newNode(3,2,10,task3, waitCS));
  273.     pushClient(clients, newNode(4,2,10,task4, waitCS));
  274.     pushClient(clients, newNode(5,2,10,task5, waitCS));
  275.     pushClient(clients, newNode(6,1,10,task6, waitCS));
  276.  
  277.     startProcessorWork(proc, clients);
  278.  
  279.     printf("Downtime = %d\n", clients->downTime);
  280.     printf("Efficiency = %d%%", (int)(clients->efficiency * 100));
  281.  
  282.     return 0;
  283. }
  284.  
Advertisement
Add Comment
Please, Sign In to add comment