int checkStackPairwiseOrder(struct sNode *top) {
    struct Queue *q = createQueue();
    struct sNode *s = top;
    
    int pairwiseOrdered = 1;

    while (!isEmpty(s))
        enQueue(q, pop(&s));

    while (!queueIsEmpty(q))
        push(&s, deQueue(q)->key);

    while (!isEmpty(s)) {
        int n = pop(&s);
        enQueue(q, n);
        if (!isEmpty(s)) {
            int m  = pop(&s);
            enQueue(q, m);
            if(abs(n - m) != 1) {
                pairwiseOrdered = 0;
            }    
        }
    }    

    while (!queueIsEmpty(q))
        push(&s, deQueue(q)->key);
    return pairwiseOrdered;
}        
Example #2
0
void DoWait(UserContext *context) {
    if (BufferCheck(context->regs[0], INT_LENGTH) == ERROR || BufferWriteCheck(context->regs[0], INT_LENGTH) == ERROR) {
        TracePrintf(1, "DoWait: Pointer given not valid. Returning Error\n");
        context->regs[0] = ERROR;
        return;
    }


	if (queueIsEmpty(current_process->children) && queueIsEmpty(current_process->deadChildren)) {
		TracePrintf(2, "DoWait: PCB %d Has no children. Returning Error.\n", current_process->id);
		TracePrintf(2, "DoWait: Children queue count: %d, deadChildren count: %d\n", current_process->children->length, current_process->deadChildren->length);
		context->regs[0] = ERROR;
	} else {
		if (queueIsEmpty(current_process->deadChildren)) {
			current_process->status = WAITING;
			queuePush(wait_queue, current_process);
			LoadNextProc(context, BLOCK);
		}

        ZCB *child = queuePop(current_process->deadChildren);
        queueRemove(process_queue, child);
        *((int *)context->regs[0]) = child->status;
        context->regs[0] = child->id;
    }
}
Example #3
0
/** \brief Get an element from the queue
 *
 * This function gets an element from the front of a given queue passed as parameter.
 *
 * \param queue a given queue from where the element will be read.
 * \param msg pinter where the read element will be returned.
 * \param blocking determines if this function is or is not blocking. Possible values are:
 * 			BLOCKING_QUEUE, this function must return -1 if the queue is empty /
 * 			NO_BLOCKING_QUEUE, if the queue is empty this function must wait until the queue has a data.
 *
 * \returns 0 the data was gotten from queue successfully / -1 data was not gotten from the queue due to empty queue
 *
 */
uint8_t queueGet(queue_t * queue, queueElementT *msg, uint8_t blocking)
{
	if(queueIsEmpty(queue))
	{
		if(blocking == BLOCKING_QUEUE)
		{
			//the queue is empty and is blocking
			while (queueIsEmpty(queue))
			{

			}
		}
		else
		{
			return(-1);
		}
	}
	//get msg from the queue
	*msg = queue->contents[queue->front];
	queue->front++;
	if (queue->front >= MAX_QUEUE_SIZE)
	{
		queue->front = 0;
	}
	queue->count--;
	return(0);
}
Example #4
0
// TODO for cow
void FreePCB(PCB *pcb) {
    while (!queueIsEmpty(pcb->children)) {
        queuePop(pcb->children);
    }

    while(!queueIsEmpty(pcb->deadChildren)) {
        ZCB *zombie = queuePop(pcb->deadChildren);
        queueRemove(process_queue, zombie);
        free(zombie);
    }

    free(pcb->children);
    free(pcb->deadChildren);

    for (int i = 0; i < MAX_PT_LEN; i++) {
        if (pcb->cow.pageTable[i].valid == 1) {
            if (pcb->cow.refCount[i]) {
                if (*(pcb->cow.refCount[i]) == 1) {
                    addFrame(pcb->cow.pageTable[i].pfn);
                    free(pcb->cow.refCount[i]);
                } else {
                    *(pcb->cow.refCount[i])--;
                }
            } else {
                addFrame(pcb->cow.pageTable[i].pfn);
            }
        }
    }

    free(pcb);
}
Example #5
0
    void WarningsService::startService() {
        m_WarningsWorker = new WarningsCheckingWorker(&m_WarningsSettingsModel);

        QThread *thread = new QThread();
        m_WarningsWorker->moveToThread(thread);

        QObject::connect(thread, SIGNAL(started()), m_WarningsWorker, SLOT(process()));
        QObject::connect(m_WarningsWorker, SIGNAL(stopped()), thread, SLOT(quit()));

        QObject::connect(m_WarningsWorker, SIGNAL(stopped()), m_WarningsWorker, SLOT(deleteLater()));
        QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

        QObject::connect(m_WarningsWorker, SIGNAL(destroyed(QObject *)),
                         this, SLOT(workerDestoyed(QObject *)));

        QObject::connect(m_WarningsWorker, SIGNAL(stopped()),
                         this, SLOT(workerStopped()));

#ifdef INTEGRATION_TESTS
        QObject::connect(m_WarningsWorker, SIGNAL(queueIsEmpty()),
                         this, SIGNAL(queueIsEmpty()));
#endif

        LOG_INFO << "Starting worker";

        thread->start();
    }
Example #6
0
void CameraController::slotUploadFailed(const QString& folder, const QString& file, const QString& src)
{
    Q_UNUSED(folder);
    Q_UNUSED(src);

    sendLogMsg(i18n("Failed to upload <filename>%1</filename>", file), DHistoryView::ErrorEntry);

    if (!d->canceled)
    {
        if (queueIsEmpty())
        {
            KMessageBox::error(d->parent, i18n("Failed to upload file <filename>%1</filename>.", file));
        }
        else
        {
            const QString msg = i18n("Failed to upload file <filename>%1</filename>. Do you want to continue?", file);
            int result        = KMessageBox::warningContinueCancel(d->parent, msg);

            if (result != KMessageBox::Continue)
            {
                slotCancel();
            }
        }
    }
}
Example #7
0
    void SpellCheckerService::startChecking() {
        Q_ASSERT(!m_SpellCheckWorker->isRunning());
        qDebug() << "Starting spellchecker service...";

        QThread *thread = new QThread();
        m_SpellCheckWorker->moveToThread(thread);

        QObject::connect(thread, SIGNAL(started()), m_SpellCheckWorker, SLOT(process()));
        QObject::connect(m_SpellCheckWorker, SIGNAL(stopped()), thread, SLOT(quit()));

        QObject::connect(m_SpellCheckWorker, SIGNAL(stopped()), m_SpellCheckWorker, SLOT(deleteLater()));
        QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

        QObject::connect(this, SIGNAL(cancelSpellChecking()),
                         m_SpellCheckWorker, SLOT(cancel()));

        QObject::connect(m_SpellCheckWorker, SIGNAL(queueIsEmpty()),
                         this, SIGNAL(spellCheckQueueIsEmpty()));

        QObject::connect(m_SpellCheckWorker, SIGNAL(stopped()),
                         this, SLOT(workerFinished()));

        thread->start();
        m_WorkerIsAlive = true;
    }
Example #8
0
btree_node * front(queue *q) {
    btree_node *r = NULL;
    if (!queueIsEmpty(q)) {
        r = q->els[q->first];
    }
    return r;
}
Example #9
0
void DoSpoon(UserContext *context) {
    // Get an available process id.                                                                                       
    int newPid = pidCount++;
    PCB *child = (PCB *)malloc(sizeof(PCB));

    // If for any reason we can't fork, return ERROR to calling process.                                                  
    if (!newPid || !child) {
        context->regs[0] = ERROR;
        return;
    }

    PCB_Init(child);
    child->id = newPid;
    child->parent = current_process;
    queuePush(child->parent->children, child);
    if (queueIsEmpty(child->parent->children))
        TracePrintf(3, "DoSpoon: parent queue empty.\n");


    child->status = RUNNING;

    // Return 0 to child and arm the child for execution.                                                                 
    child->user_context = *context;
    queuePush(ready_queue, child);
    queuePush(process_queue, child);
    KernelContextSwitch(SpoonKernel, current_process, child);

    // Return child's pid to parent and resume execution of the parent.                                                   
    if (current_process->id == newPid) {
        *context = current_process->user_context;
        context->regs[0] = 0;
    } else {
        context->regs[0] = newPid;
    }
}
Example #10
0
void LoadNextProc(UserContext *context, int block) {
    DelayPop();
    if (!queueIsEmpty(ready_queue)) {
        if (current_process)  {
            current_process->user_context = *context;
            if (block == NO_BLOCK) {
                queuePush(ready_queue, current_process);
            }
        }

        PCB *next = queuePop(ready_queue);
        TracePrintf(1, "LoadNextProc: Next Process Id: %d\n", next->id);
        if(next->id == 2) 
            TracePrintf(3, "LoadNextProc: PCB has %p child\n", next->parent->children->head);
        WriteRegister(REG_PTBR1, (unsigned int) &(next->cow.pageTable)); 
        WriteRegister(REG_TLB_FLUSH, TLB_FLUSH_1);


        KernelContextSwitch(MyKCS, current_process, next);
        TracePrintf(1, "LoadNextProc: Got past MyKCS\n");
        *context = current_process->user_context;

        TracePrintf(3, "LoadNextProc: current user context pc is %p\n", context->pc);
    }
}
int qExit(Queue *q, productList *products)
{
    int code;
    clearScreen();
    printf("\n\n\n\tThank you for using SUPER MARKET COUNTER!\n");
    printf("\n\tPress any key to exit!");
    
    while(queueIsEmpty(q)==0)
                             freeCustomer(queueRemove(q));
    
    queueFree(q);
    
    code = freeProductList(products);
    if(code==0)
    {
                                    getch();
                                    exit(code);
    }
    else
    {
        printf("Error: Freeing memory failed!");
        wait();
        exit(code);
    }
}
Example #12
0
int stackPop(struct node **n,struct node **m){
	int x,i,prev,swap;
	
	swap = queueIsEmpty(n); //get which queue is empty
	
	for(i=0;i<count;i++){	
		if(swap){
			x = dequeue(m); //pop value in queue
			prev = x;
			if(i+1==count){
				count--; //to get no of elements
				 return prev;	//return top value					 
			}
			enqueue(n,x); //push value to queue
			
		}else{
			x = dequeue(n); //pop value in queue
			prev = x;
			if(i+1==count){
				count--;
				 return prev; //return top value				
			}
			enqueue(m,x);//push value to queue
		}
	}
	
}
Example #13
0
static void *
listen_thread(void *data)
{
	long length;
	DNS_query *query;

	while (running) {
		if (queueIsEmpty(&queries_unused)) {
			if ((query = malloc(sizeof (*query))) == NULL) {
				syslog(LOG_ERR, "out of memory");
				sleep(2);
				continue;
			}
		} else {
			query = queueDequeue(&queries_unused)->data;
		}

		do {
			length = socketReadFrom(service, (unsigned char *) &query->packet.header, UDP_PACKET_SIZE, &query->client);
			if (0 < debug && 0 < length) {
				char *from = socketAddressToString(&query->client);
				syslog(LOG_DEBUG, "from=%s length=%ld", from, length);
				free(from);
			}
		} while (length <= 0);

		query->node.data = query;
		query->packet.length = length;
		queueEnqueue(&queries_waiting, &query->node);
	}

	return NULL;
}
Example #14
0
btree_node * dequeue(queue *q) {
    btree_node *r = NULL;
    if (!queueIsEmpty(q)) {
        r = q->els[q->first];
        q->first = (q->first + 1) % q->size;
        q->lastAdded = 0;
    }
    return r;
}
Example #15
0
void* priorityDequeue(Queue *queue){
        void* temp;
        void* deletedElement = malloc(queue->elementSize);
        if(queueIsEmpty(queue))
                return NULL;
        temp = queue->base+(queue->front*queue->elementSize);
        memcpy(deletedElement,temp, queue->elementSize);
        queue->front++;
        return deletedElement;
}
Example #16
0
int queueFirst(Queue *queue)
{
	if(queueIsEmpty(queue)) return -1;
	int firstItem = queue->items[queue->front];
	queue->front++;
	queue->count--;
	if(queue->front >= MAX_QUEUE_MEMBERS)
			queue->front = 0;
	return firstItem;
}
void queueStack(struct Queue *q)
{

    /*
    Do
    Delete an item from Q
    Push the item to S
    While (! empty Q); 
    
    Do
    Pop an item from S
    Insert the item to Q
    While (! empty S); 
    
    Do
    Delete an item from Q
    Push the item to S
    While (! empty Q); 
    */
    
    // init a stack
    struct sNode *s = NULL;
    int n;
    struct QNode *temp = NULL;;
    
    do {
        temp = deQueue(q);
        push(&s, temp->key);
    }while (!queueIsEmpty(q));
	
    do {
        n = pop(&s);
        enQueue(q, n);
    }while (!isEmpty(s));    
    
    do {
        temp = deQueue(q);
        push(&s, temp->key);
    }while (!queueIsEmpty(q));
    
	printf("\n Final Stack");
    print(s);    
} 
Example #18
0
int dequeue(queueT *queue, queueElementT *element)
{
    if(queueIsEmpty(queue)) {
        printf("queue is empty\n");
        return -1;
    }

    *element = queue->contents[queue->head % queue->size];
    queue->head = (queue->head + 1) % queue->size;

    return 0;
}
Example #19
0
File: queue.c Project: valeth/games
void* queueGet(Queue queue)
{
	void *ret = NULL;
	
	if (!queueIsEmpty(queue)) {
		queue->full = false;
		queue->start = (queue->start + 1) % queue->max;
		ret = listShift(queue->queue);
	}
	
	return ret;
}
Example #20
0
void* dequeue(Queue *queue){
	void* deletedElement=malloc(queue->elementSize);
	void *copyFrom, *copyTo;
	size_t noOfBytesToCopy;
	if(queueIsEmpty(queue))
		return NULL;
	memcpy(deletedElement, queue->base, queue->elementSize);
	copyFrom = queue->base+queue->elementSize;
	copyTo = queue->base;
	noOfBytesToCopy = (queue->length-1)*queue->elementSize;
	memmove(copyTo, copyFrom, noOfBytesToCopy);
	return deletedElement;
}
Example #21
0
int ReclaimLock(Lock *lock) {
    // Return error if some proc is holding on to this lock
    // or some proc is waiting to acquire this lock
    // or if there have been calls to CvarWait associated with this lock
    if (lock->owner || !queueIsEmpty(lock->waiting) || lock->cvars) {
        return ERROR;
    } else {
        queueRemove(locks, lock);
        free(lock->waiting);
        free(lock);
        return SUCCESS;
    }
}
int main ( void )
{
    fun_queue = queueInit(FUN_Q_LEN);
    rx_pay_queue = pqInit(12); //replace 12 with a #define const later
    test_function tf;

    /* Initialization */
    SetupClock();
    SwitchClocks();
    SetupPorts();

    SetupInterrupts();
    SetupI2C();
    SetupADC();
    SetupTimer1();
    SetupPWM();
    SetupTimer2();
    gyroSetup();
    xlSetup();
    dfmemSetup();

    WordVal pan_id    = {RADIO_PAN_ID};
    WordVal src_addr  = {RADIO_SRC_ADDR};
    WordVal dest_addr = {RADIO_DEST_ADDR};

    radioInit(src_addr, pan_id, RADIO_RXPQ_MAX_SIZE, RADIO_TXPQ_MAX_SIZE);
    radioSetDestAddr(dest_addr);
    radioSetChannel(RADIO_MY_CHAN);

    char j;
    for(j=0; j<3; j++){
        LED_2 = ON;
        delay_ms(500);
        LED_2 = OFF;
        delay_ms(500);
    }

    LED_2 = ON;

    EnableIntT2;
    while(1){
        while(!queueIsEmpty(fun_queue))
        {
            rx_payload = pqPop(rx_pay_queue);
            tf = (test_function)queuePop(fun_queue);
            (*tf)(payGetType(rx_payload), payGetStatus(rx_payload), payGetDataLength(rx_payload), payGetData(rx_payload));
            payDelete(rx_payload);
        }
    }
    return 0;
}
Example #23
0
struct node *removeFromFront(struct Queue *queue)
{
    if (queueIsEmpty(queue))
        return NULL;
    struct node *temp = queue->array[queue->front];
    if (queueHasOnlyOneItem(queue))
    {
        queue->front = -1;
        queue->rear = -1;
    }
    else
        ++queue->front;
    return temp;
}
Example #24
0
File: queue.c Project: Anemone0/vim
#include <errno.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef struct Node * pNode;
typedef struct Queue * pQueue;
typedef struct ElementType
{
    int num;
}ElementType;
typedef struct Node
{
    ElementType element;
    pNode next;
}Node;

typedef struct Queue
{
    pNode front;
    pNode rear;
}Queue;

int queueCreate ( pQueue pQue );
int queueIsEmpty ( pQueue pQue );
int queueEnter ( pQueue pQue,ElementType newElement );
int queueDelete ( pQueue pQue,ElementType * delElement );
int queueGetLen ( pQueue pQue );
int queueClear ( pQueue pQue );
pNode getHead ( pQueue pQue );
int queueWalk ( pQueue pQue,int (*walkWay)(ElementType ele) );
int printElement ( ElementType ele );

int main()
{
    return 0;
}

int queueCreate ( pQueue pQue )
{
    pQue->front=pQue->rear=(pNode)malloc(sizeof(Node));
    pQue->rear->next=NULL;
    return 0;
}		/* -----  end of function queueCreate  ----- */
int queueClear ( pQueue pQue )
{
    ElementType tmpCell;
    while(pQue->front!=pQue->rear)
    {
        queueDelete(pQue,&tmpCell);
    }
    return 0;
}		/* -----  end of function queueClear  ----- */
int queueIsEmpty ( pQueue pQue )
{
    return pQue->front==pQue->rear;
}		/* -----  end of function queueIsEmpty  ----- */
int queueGetLen ( pQueue pQue )
{
    int n=0;
    pNode start=pQue->front->next;
    while(start!=NULL)
    {
        n++;
        start=start->next;
    }
    return n;
}		/* -----  end of function queueLength  ----- */
pNode getHead ( pQueue pQue )
{
    return pQue->front;
}		/* -----  end of function getHead  ----- */
int queueEnter ( pQueue pQue,ElementType newElement )
{
    pNode pTmpCell;
    pTmpCell=(pNode)malloc(sizeof(Node));
    pTmpCell->element=newElement;
    pTmpCell->next=NULL;
    pQue->rear->next=pTmpCell;
    pQue->rear=pTmpCell;
    return 0;
}		/* -----  end of function queueAdd  ----- */
int queueDelete ( pQueue pQue,ElementType * delElement )
{
    if(queueIsEmpty(pQue))
    {
        return 1;
    }
    pNode pTmpCell;
    pTmpCell=pQue->front->next;
    pQue->front->next=pTmpCell->next;
    if(pQue->rear==pTmpCell)
    {
        pQue->rear=pQue->front;
    }
    *delElement=pTmpCell->element;
    free(pTmpCell);
    return 0;
}		/* -----  end of function queueDelete  ----- */
int queueWalk ( pQueue pQue,int (*walkWay)(ElementType ele) )
{
    if(queueIsEmpty(pQue))
    {
        return 1;
    }
    pNode start=pQue->front->next;
    while(start!=NULL)
    {
        (*walkWay)(start->element);
        start=start->next;
    }
    return 0;
}		/* -----  end of function queueWalk  ----- */
Example #25
0
/*
 * Adiciona um elemento (thread) na fila.
 * Para adicionar, NÃO é feita uma cópia da thread.
 * Simplismente é adicionado o ponteiro.
 * O campo 'next' da thread é alterado.
 */
bool queueAppend(s_queue *queue, s_tcb *thread)
{
	if(queueIsEmpty(queue)) {
		thread->prev = NULL;
		thread->next = NULL;
		queue->first = thread;
		queue->last  = thread;
	} else {
		thread->prev = queue->last;
		queue->last->next = thread;
		queue->last = thread;
		thread->next = NULL;
	}
	return true; // ??? OK
}
Example #26
0
void queuePush(Queue *q, char content)
{
	QueueNode *n = (QueueNode *) calloc(1, sizeof(QueueNode));
	n->content = content;

	n->prev = NULL;
	n->next = q->last;
	q->last = n;

	if(queueIsEmpty(q)) {
		q->first = n;
	} else {
		n->next->prev = n;
	}
}
Example #27
0
/*
 * Função para debug.
 * Imprime as TID dos elementos de uma fila.
 */
void queuePrintReverse(s_queue *queue, const char *nomeFila)
{
	s_tcb *aux;
	
	if(queueIsEmpty(queue)) {
		printf("%s: vazio\n", nomeFila);
		return;
	}
	
	PRINT("%s: [last]->", nomeFila);
	for(aux = queue->last; aux != NULL; aux = aux->prev)
	{
		printf(" %d", aux->tid);
	}
	printf(" <-[first]\n");
}
Example #28
0
char queuePop(Queue *q)
{
	char content;

	QueueNode *popped = q->first;
	q->first = popped->prev;
	content = popped->content;

	if(!queueIsEmpty(q)) {
		popped->prev->next = NULL;
	}

	free(popped);

	return content;
}
Example #29
0
// Standard level-order traversal to test
void levelOrder(struct node *root)
{
    struct Queue *queue = createQueue(SIZE);
    enqueue(root, queue);
    while (!queueIsEmpty(queue))
    {
        struct node *temp = removeFromFront(queue);
        printf("%d\n", temp->data);
        if (temp->left)
            //printf("adding temp->left to queue\n");
            enqueue(temp->left, queue);
        if (temp->right)
            //printf("adding temp->right to queue\n");
            enqueue(temp->right, queue);
    }
}
Example #30
0
    std::shared_ptr<Artworks::ArtworkMetadata> SpellCheckWorker::processWorkItem(WorkItem &workItem) {
        std::shared_ptr<Artworks::ArtworkMetadata> result;
        if (workItem.isSeparator()) {
            emit queueIsEmpty();
        } else {
            this->processSpellingQuery(workItem.m_Item);

            if (workItem.isMilestone()) {
                QThread::msleep(SPELLCHECK_WORKER_SLEEP_DELAY);
            }

            result = std::dynamic_pointer_cast<Artworks::ArtworkMetadata>(workItem.m_Item->getBasicModelSource());
        }

        return result;
    }