Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <time.h>
- typedef struct process {
- char name[21];
- int execution_time;
- } PROCESS;
- typedef struct node {
- PROCESS process;
- struct node *next;
- } NODE;
- void addProcesses(NODE **, NODE **);
- void addProcess(NODE **, NODE **, PROCESS *);
- void execute(NODE *);
- int delete(NODE **, NODE **, PROCESS *);
- void deleteRow(NODE **, NODE **);
- int main()
- {
- NODE *f = 0, *r = 0;
- char choice[6];
- do
- {
- printf("[1] Enter new processes\n");
- printf("[2] Exit\n");
- printf("[START] Start process execution\n");
- printf(">>");
- fscanf(stdin, "%s", choice);
- if (strcmp(choice, "1") == 0)
- {
- addProcesses(&f, &r);
- }
- else if (strcmp(choice, "START") == 0)
- {
- execute(f);
- }
- printf("\n");
- } while(strcmp(choice, "2") != 0);
- deleteRow(&f, &r);
- return 0;
- }
- void addProcesses(NODE **pf, NODE **pr)
- {
- int n;
- char id[3];
- PROCESS temp;
- printf("Enter the number of processes that you wish to add: ");
- do
- {
- fscanf(stdin, "%d", &n);
- } while(n < 0);
- srand(time(NULL));
- for (int i = 0; i < n; i++)
- {
- sprintf(id, "%d", i + 1);
- strcpy(temp.name, "process");
- strcat(temp.name, id);
- temp.execution_time = rand() % 5 + 1;
- addProcess(pf, pr, &temp);
- }
- }
- void addProcess(NODE **pf, NODE **pr, PROCESS *process)
- {
- NODE *new_node = calloc(1, sizeof(NODE));
- new_node->process = *process;
- new_node->next = 0;
- if (*pf == 0)
- {
- *pf = *pr = new_node;
- }
- else
- {
- (*pr)->next = new_node;
- *pr = new_node;
- }
- }
- void execute(NODE *f)
- {
- while(f)
- {
- printf("process_name=%s; execution_time=%d;\n", f->process.name, f->process.execution_time);
- sleep(f->process.execution_time);
- f = f->next;
- }
- }
- int delete(NODE **pf, NODE **pr, PROCESS *process)
- {
- if (*pf == 0)
- {
- return 0;
- }
- NODE *temp = *pf;
- *process = temp->process;
- if (*pf == *pr)
- {
- *pf = *pr = 0;
- }
- else
- {
- *pf = temp->next;
- }
- free(temp);
- return 1;
- }
- void deleteRow(NODE **pf, NODE **pr)
- {
- NODE temp;
- while(delete(pf, pr, &temp));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement