Advertisement
Guest User

Untitled

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