Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <time.h>
  6.  
  7.  
  8. typedef struct process {
  9.     char name[21];
  10.     int execution_time;
  11. } PROCESS;
  12.  
  13.  
  14. typedef struct node {
  15.     PROCESS process;
  16.     struct node *next;
  17. } NODE;
  18.  
  19.  
  20. void addProcesses(NODE **, NODE **);
  21. void addProcess(NODE **, NODE **, PROCESS *);
  22. void execute(NODE *);
  23. int delete(NODE **, NODE **, PROCESS *);
  24. void deleteRow(NODE **, NODE **);
  25.  
  26. int main()
  27. {
  28.     NODE *f = 0, *r = 0;
  29.     char choice[6];
  30.  
  31.     do
  32.     {
  33.         printf("[1] Enter new processes\n");
  34.         printf("[2] Exit\n");
  35.         printf("[START] Start process execution\n");
  36.         printf(">>");
  37.  
  38.         fscanf(stdin, "%s", choice);
  39.  
  40.         if (strcmp(choice, "1") == 0)
  41.         {
  42.             addProcesses(&f, &r);
  43.         }
  44.         else if (strcmp(choice, "START") == 0)
  45.         {
  46.             execute(f);
  47.         }
  48.         printf("\n");
  49.     } while(strcmp(choice, "2") != 0);
  50.  
  51.     deleteRow(&f, &r);
  52.     return 0;
  53. }
  54.  
  55.  
  56. void addProcesses(NODE **pf, NODE **pr)
  57. {
  58.     int n;
  59.     char id[3];
  60.     PROCESS temp;
  61.  
  62.     printf("Enter the number of processes that you wish to add: ");
  63.     do
  64.     {
  65.         fscanf(stdin, "%d", &n);
  66.     } while(n < 0);
  67.  
  68.     srand(time(NULL));
  69.  
  70.     for (int i = 0; i < n; i++)
  71.     {
  72.         sprintf(id, "%d", i + 1);
  73.         strcpy(temp.name, "process");
  74.         strcat(temp.name, id);
  75.  
  76.         temp.execution_time = rand() % 5 + 1;
  77.  
  78.         addProcess(pf, pr, &temp);
  79.     }
  80. }
  81.  
  82.  
  83. void addProcess(NODE **pf, NODE **pr, PROCESS *process)
  84. {
  85.     NODE *new_node = calloc(1, sizeof(NODE));
  86.  
  87.     new_node->process = *process;
  88.     new_node->next = 0;
  89.  
  90.     if (*pf == 0)
  91.     {
  92.         *pf = *pr = new_node;
  93.     }
  94.     else
  95.     {
  96.         (*pr)->next = new_node;
  97.         *pr = new_node;
  98.     }
  99. }
  100.  
  101.  
  102. void execute(NODE *f)
  103. {
  104.     while(f)
  105.     {
  106.         printf("process_name=%s; execution_time=%d;\n", f->process.name, f->process.execution_time);
  107.         sleep(f->process.execution_time);
  108.         f = f->next;
  109.     }
  110. }
  111.  
  112.  
  113. int delete(NODE **pf, NODE **pr, PROCESS *process)
  114. {
  115.     if (*pf == 0)
  116.     {
  117.         return 0;
  118.     }
  119.  
  120.     NODE *temp = *pf;
  121.     *process = temp->process;
  122.  
  123.     if (*pf == *pr)
  124.     {
  125.         *pf = *pr = 0;
  126.     }
  127.     else
  128.     {
  129.         *pf = temp->next;
  130.     }
  131.  
  132.     free(temp);
  133.  
  134.     return 1;
  135. }
  136.  
  137.  
  138. void deleteRow(NODE **pf, NODE **pr)
  139. {
  140.     NODE temp;
  141.  
  142.     while(delete(pf, pr, &temp));
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement