You are on page 1of 25

NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

MICRO PROJECT & TERM PAPER

TOPIC:APPLICATION OF QUEUE DATA STRUCTURE

QUEUE IN DATA STRUCTURE

Queueing theory is the mathematical study of waiting lines, or queues A


queueing model is constructed so that queue lengths and waiting time can be
predicted.Queueing theory is generally considered a branch of operations
research because the results are often used when making business decisions
about the resources needed to provide a service.

In programming terms, A queue is basically a linear data structure to store


and manipulate the data elements. It follows the order of First In First Out
(FIFO). In queues, the first element entered into the array is the first element to
be removed from the array. A queue is open in both the ends.

FLOWCHART FOR INSERTION (ENQUEUE) AND DELETION


(DEQUEUE) FUNCTION

Page | 1
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

APPLICATION OF QUEUE

There are various applications of queue in real life one of which is


Call Center phone systems .Call queuing is a concept used in
inbound call centers. When calling a phone system that uses call
queues, callers usually hear a welcome message and an IVR menu
and are then sent into a call queue. The normal distribution
approach in call queues is first in, first out. Call centers use an
Automatic Call Distributor (ACD) to distribute incoming calls to
specific resources (agents) in the center. ACDs hold queued calls
until agents become available .Call queues make it easier for callers
to access telephony services, while the call center and service staff
is relieved due to ACD Queues .There are agents whose phones are
constantly connected to the call queue. Some call queues also allow
dynamic agent login. This allows additional staff to login their
phones into queues in times of high call loads to support the other
agents and ensure smooth service for the callers. Decreasing the
call load, the additional agents can logout the queue and go back to
other tasks .Also there is an option for the customers to file a
complaint so that it reaches to the executive and the problem can
be solved.

Page | 2
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

VISUAL REPRESENTATION OF A CALL CENTER QUEUE

Page | 3
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

ALGORITHM FOR CALL QUEUE PROGRAM:

INSERT ALGORITHM:

Step 1: IF REAR = MAX-1

Write OVERFLOW

Goto step 4

[END OF IF]

Step 2: IF FRONT=-1 and REAR=-1

SET FRONT = REAR = 0

ELSE

SET REAR = REAR+1

[END OF IF]

Step 3: SET QUEUE[REAR] = NUM

Step 4: EXIT

DELETE ALGORITHM:

Algorithm DEQUEUE_ARRAY(QUEUE, FRONT, REAR, ITEM)

Step 1 [If underflow, Exit algorithm. Deletion not possible]

IF FRONT==-1 Then Exit

Step 2 [Store deleted element in ITEM argument]

Page | 4
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

ITEM=QUEUE[FRONT]

Step 2 [Generate position for next first element of the queue]

FRONT=FRONT+1

Step 4 Exit

EXECUTIVE ALGORITHM:

Algorithm executive()

Step 1 Take input for choice

Step 2 Repeat step 3 while choice<3

Step 3 If choice=1 do Insert

If choice=2 do delete

Step 4 Exit

COMPLAIN ALGORITHM:

Algorithm complain()

Step 1 Take input from user for phone number

Step 2 Take input user name

Step 3 Take input of complaint from user

Step 4 Print inconvenience message

Step 5 Exit

Page | 5
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

DELAY ALGORITHM:

Algorithm delay(int number_of_seconds)

Step 1 Set milli_second = 1000* number_of_seconds

Step 2 Set clock_t start_time= clock()

Step 3 Repeat step 1 and 2 while clock()<start_time + milli_seconds

Step 4 Exit

EXPENSIVE_CALL ALGORITHM:

Algorithm *expensive_call(void *data)

Step 1 set int oldtype

Step 2 pthread_setcanceltype(PTH READ_CANCEL_ASYNCHRONUS, &oldtype)

[Allow the thred to be killed at any time]

Step 3 pthrad_setcanceltype(PTH READ_CANCEL_ASYNCHRONUS, &oldtype)

[calculation and expensive io for example]

[infinite loop]

Step 4 pthread_cond_signal(&done)

Step 5 return NULL

Step 6 Exit

DO_OR_TIMEOUT ALGORITHM:

Page | 6
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

int do_or_timeout(struct timespec *max_wait)

Step 1 pthread_mutex_lock(&calculating)

[pthread cond_timedwait expects an absolute time to wait until]

Step 2 clock_gettime(CLOCK_REALTIME, &abs_time)

Step 3 Set abs_time.tv_sec +=max_wait->tv_sec

Set abs_time.tv_nsec += max_wait-> tv_nsec

Step 4 pthread_create(&tid,NULL,expensive_call,NULL)

[pthread_cond_timedwait can return spuriously: this should be in a loop for

production code]

Step 4 Set err= pthread_cond_timedwaid(&done, &calculating, &abs_time)

Step 5 if err= ETIMEDOUT print func

if !err

pthread_mutex_unlock(&calculating)

Step 6 Return err

Step 7 Exit

Page | 7
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

CODE FOR CALL QUEUE:

#include <stdio.h>

#include<stdlib.h>

#include <time.h>

#include <conio.h>

#include <pthread.h>

#include <errno.h>

#include <string.h>

#define MAX 20

pthread_mutex_t calculating = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t done = PTHREAD_COND_INITIALIZER;

void executive();

void insert();

void delete();

void complain ();

void returnprev();

void delay(int );

void *expensive_call(void *data);

Page | 8
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

int do_or_timeout(struct timespec *max_wait);

int store[MAX];

int rear = - 1;

int front = - 1;

main()

int choice,lang,i;

struct timespec max_wait;

while (1)

printf("***********WELCOME TO CUSTOMER SERVICE HELPLINE************* \n");

printf("Please select the language:\n ");

printf("Press 1 for English,2 for Hindi,3 for other languages:\n");

scanf("%d",&lang);

printf("Your call is being recorded for feedback purpose!!\n");

printf("Select an Option to continue:\n");

printf("Press 1 to talk to our Executive,2 for Complaints\n");

printf("Press 3 to return to the previous menu,5 to Quit:\n");

Page | 9
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

scanf("%d", &choice);

if(choice==NULL)

for (i = 0; i < 10; i++)

delay(1);

printf("%d seconds have passed\n", i + 1);

if(i+1>10)

printf("*****TIME OUT*****TRY AGAIN*****\n");

switch (choice)

case 1:

executive();

break;

Page | 10
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

case 2:

complain();

break;

case 3:

returnprev();

break;

case 4:

memset(&max_wait, 0, sizeof(max_wait));

/* wait at most 20 seconds */

max_wait.tv_sec = 20;

do_or_timeout(&max_wait);

case 5:

exit(1);

default:

printf("Wrong choice!!!Try Again!!! \n");

Page | 11
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

void executive()

int choice;

do

printf("\n Enter choice:");

scanf("%d",&choice);

switch(choice)

case 1:

insert();

break;

case 2:

delete();

break;

}while(choice<3);

Page | 12
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

void insert()

int num;

printf("Redirecting the calls to Executive in a queue :\n ");

printf("Order of Calls in Waiting List:\n");

scanf("%d",&num);

if (rear == MAX - 1)

printf("Call Overflow \n");

printf("Try After Sometime\n");

else if(front==-1 && rear==-1)

front=rear=0;

else

rear = rear + 1;

Page | 13
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

store[rear] = num;

void delete()

if (front == - 1 || front > rear)

printf("Call Underflow \n");

return ;

else

printf("Call Disconnected from queue is : %d-TH\n", store[front]);

printf("Ready To Take New Calls!!\n");

front = front + 1;

if(front>rear)

front=rear=-1;

Page | 14
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

void complain ()

char number[10];char name[30];char c[500];

printf("\n ENTER YOUR PHONE NUMBER");

scanf("%s",number);

printf("\n ENTER USER NAME");

scanf("%s", name);

printf("\n ENTER YOUR COMPLAINT HERE ");

scanf("%s",c);

printf("\n WE ARE SORRY FOR THE INCONVINIENCE.WE WILL TRY TO SOLVE YOUR PROBLEM!");

printf("\n *****THANK YOU FOR CALLING CUSTOMER SERVICE*****\n");

void returnprev()

int i;

Page | 15
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

printf("\n Enter Choice:\n");

scanf("%d",&i);

if(i==1)

insert();

else if(i==2)

complain();

else

printf("INVALID!!");

void delay(int number_of_seconds)

int milli_seconds = 1000 * number_of_seconds;

Page | 16
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

clock_t start_time = clock();

while (clock() < start_time + milli_seconds);

void *expensive_call(void *data)

int oldtype;

/* allow the thread to be killed at any time */

pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);

/* ... calculations and expensive io here, for example:

* infinitely loop

*/

for (;;) {}

/* wake up the caller if we've completed in time */

pthread_cond_signal(&done);

return NULL;

/* note: this is not thread safe as it uses a global condition/mutex */

int do_or_timeout(struct timespec *max_wait)

Page | 17
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

struct timespec abs_time;

pthread_t tid;

int err;

pthread_mutex_lock(&calculating);

/* pthread cond_timedwait expects an absolute time to wait until */

clock_gettime(CLOCK_REALTIME, &abs_time);

abs_time.tv_sec += max_wait->tv_sec;

abs_time.tv_nsec += max_wait->tv_nsec;

pthread_create(&tid, NULL, expensive_call, NULL);

/* pthread_cond_timedwait can return spuriously: this should

* be in a loop for production code

*/

err = pthread_cond_timedwait(&done, &calculating, &abs_time);

if (err == ETIMEDOUT)

fprintf(stderr, "%s: Configured Timed Out\n", __func__);

if (!err)

pthread_mutex_unlock(&calculating);

Page | 18
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

return err;

EXECUTION SEQUENCE:

OUTPUT:

***********WELCOME TO CUSTOMER SERVICE HELPLINE*************

Please select the language:

Press 1 for English,2 for Hindi,3 for other languages:

Your call is being recorded for feedback purpose!!

Select an Option to continue:

Press 1 to talk to our Executive,2 for Complaints

Press 3 to return to the previous menu,5 to Quit:

1 seconds have passed

2 seconds have passed

3 seconds have passed

4 seconds have passed

5 seconds have passed

Page | 19
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

6 seconds have passed

7 seconds have passed

8 seconds have passed

9 seconds have passed

10 seconds have passed

*****TIME OUT*****TRY AGAIN*****

Wrong choice!!!Try Again!!!

***********WELCOME TO CUSTOMER SERVICE HELPLINE*************

Please select the language:

Press 1 for English,2 for Hindi,3 for other languages:

Your call is being recorded for feedback purpose!!

Select an Option to continue:

Press 1 to talk to our Executive,2 for Complaints

Press 3 to return to the previous menu,5 to Quit:

Enter choice:1

Redirecting the calls to Executive in a queue :

Page | 20
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

Order of Calls in Waiting List:

Enter choice:1

Redirecting the calls to Executive in a queue :

Order of Calls in Waiting List:

Enter choice:1

Redirecting the calls to Executive in a queue :

Order of Calls in Waiting List:

Enter choice:1

Redirecting the calls to Executive in a queue :

Order of Calls in Waiting List:

Enter choice:1

Redirecting the calls to Executive in a queue :

Order of Calls in Waiting List:

Page | 21
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

Enter choice:2

Call Disconnected from queue is : 1-TH

Ready To Take New Calls!!

Enter choice:2

Call Disconnected from queue is : 2-TH

Ready To Take New Calls!!

Enter choice:3

***********WELCOME TO CUSTOMER SERVICE HELPLINE*************

Please select the language:

Press 1 for English,2 for Hindi,3 for other languages:

Your call is being recorded for feedback purpose!!

Select an Option to continue:

Press 1 to talk to our Executive,2 for Complaints

Press 3 to return to the previous menu,5 to Quit:

ENTER YOUR PHONE NUMBER3248750126

ENTER USER NAMEXYZ

Page | 22
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

ENTER YOUR COMPLAINT HERE SAMPLE

WE ARE SORRY FOR THE INCONVINIENCE.WE WILL TRY TO SOLVE YOUR PROBLEM!

*****THANK YOU FOR CALLING CUSTOMER SERVICE*****

Enter choice:3

***********WELCOME TO CUSTOMER SERVICE HELPLINE*************

Please select the language:

Press 1 for English,2 for Hindi,3 for other languages:

***********WELCOME TO CUSTOMER SERVICE HELPLINE*************

Please select the language:

Press 1 for English,2 for Hindi,3 for other languages:

Your call is being recorded for feedback purpose!!

Select an Option to continue:

Press 1 to talk to our Executive,2 for Complaints

Press 3 to return to the previous menu,5 to Quit:

do_or_timeout: Configured Timed Out

Process returned 1 (0x1) execution time : 70.735 s

Page | 23
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

Press any key to continue.

VISUAL REPRESENTATION:

Page | 24
NAME:SOMOJIT DAS DEPT:ECE(SEM 3) ROLL NO:14

Page | 25

You might also like