Sunteți pe pagina 1din 4

*Thread Scheduling

Multiple threads will enter into the user function and all threads that come after the first one will wait
until it either runs for the time quantum or it finishes its burst.
This will continue until all of the bursts are completed.
Analysis of the Algorithm:
This algorithm works. It runs for the correct amount of time and produces the correct output.
Description:
This program creates a scheduler for user-level threads using a Round Robin (RR) scheduling
algorithm. A controller creates as many threads as input by the user,
then uses a timer that interrupts the scheduler at predetermined intervals, also set by the user, and
switches the currently running thread to the thread in the front of the waiting queue.
The user input for this program is entered in the form of ./scheduling <number of threads> <length of
time quantum in seconds>
Input:
@x:~$ ./scheduling 3 5
Burst time for thread 0: 8
Burst time for thread 1: 9
Burst time for thread 2: 12
Output:
Thread 0 is now being executed
1
2
3
4
5
Thread 1 is now being executed
6
7
8
9
10
Thread 2 is now being executed
11
12
13
14
15

Thread 0 is now being executed


16
17
18
Thread 0 finished its job. It will be terminated
Thread 1 is now being executed
19
20
21
22
Thread 1 finished its job. It will be terminated
Thread 2 is now being executed
23
24
25
26
27
Thread 2 is now being executed
28
29
Thread 2 finished its job. It will be terminated
@x:~/$ make
C CODE:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
int numThreads=0;
int quantum=0;
int sum=1;
int *bur=NULL;
int *curBur=NULL;
int *curThr=NULL;
int count=0;
int currThread=0;
int assignID=0;
void *user(void *u); //The functions that the threads run
pthread_mutex_t lock,l2; //create locks
int main(int argc, char *argv[])
{
pthread_mutex_init(&lock,NULL); //initialize lock
numThreads=(intptr_t) atoi((void *) argv[1]);//initialize & declare vars
quantum = (intptr_t) atoi((void *) argv[2]);

int num = (numThreads);


int i, burst[numThreads],tracker[numThreads];
pthread_t tid[num];
pthread_attr_t attr;
curThr=malloc(sizeof(int)*numThreads);
for(i=0;i<numThreads;i++)//fill burst times
{
printf("Burst time for thread %d: ", i);
scanf("%d", &burst[i]);
tracker[i]=0;
curThr[i]=0;
}
bur = burst; //assign pointer lockations
curBur=tracker;
for(i=0; i < num; i++) //create & join threads
pthread_create(&tid[i], NULL,user,(void *)(intptr_t)(numThreads-i-1));
for(i=0; i < num; i++)
pthread_join(tid[i],NULL);
return 0;
}
void *user(void *u) //User- funcion for threads to run
{
int i,j, id=0;
while(count < numThreads)//1(curBur[id] < bur[id]) //go while burst times arent equal
{
pthread_mutex_lock(&lock);//lock and access critical section
id=assignID;
assignID++;
if(assignID==numThreads)
assignID=0;
if(curThr[id] ==1)
{
assignID++;
pthread_mutex_unlock(&lock);//lock and access critical section
continue;
}
printf("Thread %d is now being executed\n",id); //print which is being executed
for(j=0;j < quantum; j++)//for loop to substact from time quantum
{
sleep(1);
if(curBur[id] < bur[id])
{
printf("%d\n",sum);
sum+=1;
*(curBur+id)+=1;
}
if(curBur[id]==bur[id])

{
printf("Thread %d finished its job. It will be terminated\n",id);//finish
curThr[id]=1;
count++;
break;
}
}
pthread_mutex_unlock(&lock);//lock and access critical section
}
}

S-ar putea să vă placă și