Repository navigation
Expand file tree
/
Copy pathfcfs.c
More file actions
103 lines (95 loc) · 1.72 KB
/
Copy pathfcfs.c
File metadata and controls
103 lines (95 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// this is a program to simulate the first-come-first-serve algorithm of process scheduling
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
struct process
{
int pid,cpu_burst;
struct process *next,*prev;
};
struct process *queue = NULL,*tail = NULL;
void addEnd(int p,int c)
{
if(queue==NULL)
{
queue = (struct process*)malloc(sizeof(struct process));
queue->pid = p;
queue->cpu_burst = c;
queue->prev = NULL;
queue->next = NULL;
tail = queue;
}
else
{
tail->next = (struct process*)malloc(sizeof(struct process));
tail->next->pid = p;
tail->next->cpu_burst = c;
tail->next->prev = tail;
tail->next->next = NULL;
tail = tail->next;
}
//sleep(1);
}
struct process* removeBegin()
{
if(queue==NULL)
{
return NULL;
}
else
{
struct process *rtn = queue;
queue = queue->next;
if(queue!=NULL)
queue->prev = NULL;
else
{
tail = NULL;
}
return rtn;
}
}
void* CreateProcess(void *n)
{
int k = *(int*)n,i,c;
struct process *t = NULL;
for(i=0;i<k;i++)
{
c = rand()%2+1;
printf("process id = %d cpu burst time = %d\n",i+1,c);
addEnd(i+1,c);
sleep(1);
}
printf("all processes created!\n");
}
void* shedule_process(void* k)
{
struct process *p;
int n = *(int*)k;
int i = 0;
do
{
p = removeBegin();
if(p!=NULL)
{
sleep(p->cpu_burst);
printf("process %d completed\n",p->pid);
i++;
}
}while(i!=n);
printf("scheduling process completed!\n");
}
int main()
{
pthread_t pth1,pth2;
srand(time(NULL));
printf("enter required number of processes\n");
int n;
scanf("%d",&n);
pthread_create(&pth1,NULL,CreateProcess,(void*)&n);
pthread_create(&pth2,NULL,shedule_process,(void*)&n);
pthread_join(pth2,NULL);
pthread_join(pth1,NULL);
return 0;
}