Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- enum clientState {waitCS, inputCS};
- struct processor
- {
- int ticTime, inTime;
- };
- struct clientNode
- {
- int id, priority, taskCount, inputWait;
- int *taskTime;
- bool isFinished;
- enum clientState state;
- struct clientNode *next;
- };
- struct clientQueue
- {
- int clientCount, downTime;
- float efficiency;
- struct clientNode *head, *tail;
- };
- struct clientNode *newNode(const int id, const int priority, const int taskCount, int *taskTime, enum clientState state)
- {
- struct clientNode *newClient = (struct clientNode*)malloc(sizeof(struct clientNode));
- newClient->id = id;
- newClient->priority = priority;
- newClient->taskCount = taskCount;
- newClient->taskTime = taskTime;
- newClient->isFinished = false;
- newClient->state = state;
- return newClient;
- }
- void pushClient(struct clientQueue *clients, struct clientNode *newNode)
- {
- if (!clients || !newNode)
- return;
- struct clientNode *node = malloc(sizeof(struct clientNode));
- if (!node)
- return;
- node->id = newNode->id;
- node->priority = newNode->priority;
- node->taskCount = newNode->taskCount;
- node->isFinished = false;
- node->state = waitCS;
- node->taskTime = malloc(sizeof(int) * newNode->taskCount);
- for (int i = 0; i < newNode->taskCount; i++)
- node->taskTime[i] = newNode->taskTime[i];
- node->next = nullptr;
- if (clients->head == NULL) // первая вставка
- {
- clients->head = node;
- clients->tail = node;
- clients->clientCount = 1;
- return;
- }
- // вставка по приоритету
- struct clientNode *prev = nullptr;
- struct clientNode *curr = clients->head;
- while (curr && curr->priority >= node->priority)
- {
- prev = curr;
- curr = curr->next;
- }
- if (prev == NULL) // вставляем в голову
- {
- node->next = clients->head;
- clients->head = node;
- }
- else // вставляем между prev и curr
- {
- prev->next = node;
- node->next = curr;
- }
- if (curr == NULL) // вставка в хвост
- clients->tail = node;
- clients->clientCount++;
- }
- bool isFinished(const struct clientQueue *clients)
- {
- const struct clientNode *temp = clients->head;
- while (temp != NULL)
- {
- if(!temp->isFinished)
- return false;
- temp = temp->next;
- }
- return true;
- }
- struct clientNode *getClient(const struct clientQueue *clients, const enum clientState state)
- {
- if (!clients || !clients->head)
- return nullptr;
- struct clientNode *temp = clients->head;
- while (temp != NULL)
- {
- if (temp->state == state && !temp->isFinished)
- return temp;
- temp = temp->next;
- }
- return NULL;
- }
- void isClientDone(struct clientNode *client)
- {
- for (int i = 0; i < client->taskCount; i++)
- if (client->taskTime[i] != 0)
- return;
- client->isFinished = true;
- client->taskCount = 0;
- }
- int findTaskIndex(struct clientNode *node)
- {
- if (!node)
- return -1;
- for (int i = 0; i < node->taskCount; i++)
- if (node->taskTime[i] != 0)
- return i;
- return -1;
- }
- int sumTaskTime(struct clientNode *node) {
- if (!node)
- return -1;
- int sum = 0;
- for (int i = 0; i <= node->taskCount; i++)
- sum += node->taskTime[i];
- return sum;
- }
- int sumAllTaskTime(struct clientQueue *clients) {
- if (!clients)
- return -1;
- int sum = 0;
- struct clientNode *temp = clients->head;
- for (int i = 0; i < clients->clientCount; i++) {
- sum += sumTaskTime(temp);
- temp = temp->next;
- }
- return sum;
- }
- void exchangeSamePriority(struct clientQueue *clients, struct clientNode *node)
- {
- if (!clients || !node)
- return;
- struct clientNode *prev = NULL;
- struct clientNode *curr = clients->head;
- // ищем node
- while (curr && curr != node)
- {
- prev = curr;
- curr = curr->next;
- }
- if (!curr)
- return;
- // если следующий другой приоритет → ничего
- if (!curr->next || curr->priority != curr->next->priority)
- return;
- // находим конец группы одинакового приоритета
- struct clientNode *groupEnd = curr->next;
- while (groupEnd->next && groupEnd->priority == curr->priority)
- groupEnd = groupEnd->next;
- // вырезаем curr
- if (prev) prev->next = curr->next;
- else clients->head = curr->next;
- // вставляем после groupEnd
- curr->next = groupEnd->next;
- groupEnd->next = curr;
- if (!curr->next)
- clients->tail = curr;
- }
- void startProcessorWork(const struct processor processor, struct clientQueue *clients)
- {
- int usedTime = 0;
- int neededTime = sumAllTaskTime(clients);
- while(!isFinished(clients))
- {
- struct clientNode *findClient = getClient(clients, waitCS);
- struct clientNode *temp = clients->head;
- if (findClient != NULL)
- {
- const int index = findTaskIndex(findClient);
- if(findClient->taskTime[index] <= processor.ticTime)
- {
- findClient->inputWait = (processor.inTime - (processor.ticTime - findClient->taskTime[index]) > 0) ? processor.inTime - (processor.ticTime - findClient->taskTime[index]) : 0;
- findClient->state = (findClient->inputWait == 0) ? waitCS : inputCS;
- clients->downTime+= processor.ticTime - findClient->taskTime[index];
- findClient->taskTime[index] = 0;
- isClientDone(findClient);
- }
- else
- {
- findClient->taskTime[index] = findClient->taskTime[index] - processor.ticTime > 0 ? findClient->taskTime[index] - processor.ticTime : 0;
- findClient->state = waitCS;
- }
- exchangeSamePriority(clients, findClient);
- }
- while (temp != NULL)
- {
- if(temp != findClient && temp->state == inputCS)
- {
- temp->state = (temp->inputWait > processor.ticTime) ? inputCS : waitCS;
- temp->inputWait = (temp->inputWait - processor.ticTime) > 0 ? temp->inputWait - processor.ticTime : 0;
- }
- temp = temp->next;
- }
- usedTime += processor.ticTime;
- if(!findClient)
- clients->downTime+= processor.ticTime;
- }
- clients->efficiency = neededTime / (float)usedTime;
- }
- int main(void)
- {
- struct processor proc;
- struct clientQueue *clients = malloc(sizeof(struct clientQueue));
- int task1[] = { 8, 3, 6, 4, 5, 1, 1, 7 };
- int task2[] = { 5, 4, 5 ,4 ,5, 4, 5, 4, 5, 4 };
- int task3[] = { 3, 2, 1, 4, 1, 1, 2, 3, 4, 5 };
- int task4[] = { 8, 7, 6, 7, 5, 3, 8, 4, 2, 1 };
- int task5[] = { 5, 4, 3, 5, 4, 2, 3, 1, 6, 3 };
- int task6[] = { 1, 2, 1, 3, 2, 3, 1, 3, 2, 1 };
- //int task1[] = { 4, 2, 3 };
- //int task2[] = { 2, 4, 2 };
- //int task3[] = { 2, 3, 3 };
- //int task4[] = { 2, 1, 3 };
- printf("Enter time of tic: ");
- scanf("%d", &proc.ticTime);
- printf("Enter time of input: ");
- scanf("%d", &proc.inTime);
- clients->efficiency = 0.0f;
- clients->downTime = 0;
- pushClient(clients, newNode(1,3,8,task1, waitCS));
- pushClient(clients, newNode(2,3,10,task2, waitCS));
- pushClient(clients, newNode(3,2,10,task3, waitCS));
- pushClient(clients, newNode(4,2,10,task4, waitCS));
- pushClient(clients, newNode(5,2,10,task5, waitCS));
- pushClient(clients, newNode(6,1,10,task6, waitCS));
- startProcessorWork(proc, clients);
- printf("Downtime = %d\n", clients->downTime);
- printf("Efficiency = %d%%", (int)(clients->efficiency * 100));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment