Пример #1
0
static void dqr1(struct l_queue *q, int n) {
    int total_sleep_us = 0;
    int sleep_limit = TIMELIMIT * 1000000 / 2;
    int max_per_sleep = sleep_limit / n;
    int success_ctr = 0;
    long val = 1, old_val = 0;
    
    while (success_ctr < n && total_sleep_us <= sleep_limit) {
        usleep(rand() % max_per_sleep + 1);
        if (!deq (q, (void **) &val)) {
            success_ctr += 1;
            if (old_val + 1 != val) {
                printf("FAIL: expected %ld, got %ld\n", old_val + 1, val);
                break;
            }
            old_val = val;
        }
    }

    if (success_ctr != n) {
        while (success_ctr < n) {
            if (!deq (q, (void **) &val)) {
                success_ctr += 1;
                if (old_val + 1 != val) {
                    printf("FAIL: expected %ld, got %ld\n", old_val + 1, val);
                    break;
                }
                old_val = val;
            }
        }
    }
    return;
}
Пример #2
0
int main(){
	queue_t *queue;
	int i = 0;
	int v = 0;
	queue = create_queue();
	if (queue == NULL){
		fprintf(stderr,"Error creating queue\n");
		return -EXIT_FAILURE;
	}
	assert (deq(&v,queue)==0);	/*deque an empty queue, print "Queue is empty"*/

	for (i = 0; i < 32; ++i)
		assert (enq(5,queue)==1);	/*fill the queue with 5*/

	for (i = 0; i < 32; ++i)
		assert (queue->value[i]==5);	/*check the entire queue*/

	assert (enq(5, queue)==0);	/*enque a full queue, print "Queue is full"*/

	assert (deq(&v, queue)==1);	/*deque the first element*/

	assert (enq(6, queue)==1);	/*enque 6*/

	for (i = 0; i < 32; i++)
		assert(deq(&v,queue)==1);
	assert (v==6);	/*the last element should be 6*/
	
	return EXIT_SUCCESS;
}
Пример #3
0
int main(void)
{
  int n,i,l,k,m,who,next,wlen,par,ismore;
  
  scanf("%d",&n);
  while(n--)
  {
    scanf("%s",&S);
    scanf("%s",&T);
    sl=strlen(S);
    tl=strlen(T);
    
    for (i=0;i<=tl;i++)
      fstart[0][i]=fstop[0][i]=fstart[1][i]=fstop[1][i]=0;
    
    for (l=0;l<(1<<tl);l++)
      vis[l]=-1;
    vis[0]=0;
    
    enq(0,0,0);
    ismore=1;
    par=0;
    while (ismore)
    {
      ismore=0;
      who=deq(par,&wlen);
      while (who>-1 && who!=((1<<tl)-1))
      {
        int alen=0;
        for (i=0;i<tl;i++)
        {
          int len,len2;
          len=issubst(T,tl,who,T,i,who);
          len2=issubst(S,sl,((1<<sl)-1),T,i,who);
          if (len2>len) len=len2;
          if (len>alen) // only append maximal substrings
          {
            next=who|((1<<(i+len))-(1<<i));
            if (vis[next]==-1)
            { 
              vis[next]=vis[who]+1;
              enq(1-par,next,wlen+len);
              ismore=1;
            } 
            alen=len;
          }
          alen--;
        }
        who=deq(par,&wlen);
      }
      if (who==((1<<tl)-1)) ismore=0;
      par=1-par;
    }
    
    if (vis[(1<<tl)-1]==-1) printf("impossible\n"); else printf("%d\n",vis[(1<<tl)-1]);
  }
  return 0;
}
Пример #4
0
/* glonass position and velocity by numerical integration --------------------*/
static void glorbit(double t, double *x, const double *acc)
{
    double k1[6],k2[6],k3[6],k4[6],w[6];
    int i;
    
    deq(x,k1,acc); for (i=0;i<6;i++) w[i]=x[i]+k1[i]*t/2.0;
    deq(w,k2,acc); for (i=0;i<6;i++) w[i]=x[i]+k2[i]*t/2.0;
    deq(w,k3,acc); for (i=0;i<6;i++) w[i]=x[i]+k3[i]*t;
    deq(w,k4,acc);
    for (i=0;i<6;i++) x[i]+=(k1[i]+2.0*k2[i]+2.0*k3[i]+k4[i])*t/6.0;
}
Пример #5
0
void print_longest(int rows, int grid[rows][rows])
{
  queue Q = queue_new();
  DFS(rows, grid, 0, 0, Q);

  while (!queue_empty(Q)) {
    int *r = deq(Q);
    int *c = deq(Q);
    printf("%d\n", grid[*r][*c]);
  }

  return;
}
Пример #6
0
/*------------------------------------------------------------------------
 *  ipfcons  -  construct a single packet from an IP fragment queue
 *------------------------------------------------------------------------
 */
struct ep *
ipfcons(struct ipfq *iq)
{
	struct	ep	*pep, *peptmp;
	struct	ip	*pip;
	int		off, seq;

	pep = (struct ep *)getbuf(Net.lrgpool);
	if (pep == (struct ep *)SYSERR) {
		while (peptmp = (struct ep *)deq(iq->ipf_q)) {
			IpReasmFails++;
			freebuf(peptmp);
		}
		freeq(iq->ipf_q);
		iq->ipf_state = IPFF_FREE;
		return 0;
	}
	/* copy the Ether and IP headers */

	peptmp = (struct ep *)deq(iq->ipf_q);
	pip = (struct ip *)peptmp->ep_data;
	off = IP_HLEN(pip);
	seq = 0;
	memcpy(pep, peptmp, EP_HLEN+off);

	/* copy the data */
	while (peptmp != 0) {
		int dlen, doff;

		pip = (struct ip *)peptmp->ep_data;
		doff = IP_HLEN(pip) + seq
			- ((pip->ip_fragoff&IP_FRAGOFF)<<3);
		dlen = pip->ip_len - doff;
		memcpy(pep->ep_data+off, peptmp->ep_data+doff, dlen);
		off += dlen;
		seq += dlen;
		freebuf(peptmp);
		peptmp = (struct ep *)deq(iq->ipf_q);
	}

	/* fix the large packet header */
	pip = (struct ip *)pep->ep_data;
	pip->ip_len = off;
	pip->ip_fragoff = 0;

	/* release resources */
	freeq(iq->ipf_q);
	iq->ipf_state = IPFF_FREE;
	IpReasmOKs++;
	return pep;
}
Пример #7
0
int main()
{
	queue_t q;
	init(&q);
	enq(&q, 1);
	deq(&q);
	enq(&q, 3);
	enq(&q, 2);
	deq(&q);
	deq(&q);
	deq(&q);
	deq(&q);
	enq(&q, 2);
}
Пример #8
0
void *consume(void *arg){
    thread_data_t *data = (thread_data_t*) arg;
    int tid = data->tid;
    int v = 0;
    while (1) {
        pthread_mutex_lock(&mutex);
        /*Dequeing testing strategy:
         *If multiple threads are in the critical section in the same time
         *there is a chance that some of the threads will dequeue
         *a ALREADY dequeued element, and these threads will get a 0 (instead of 1 for a
         *successful dequeing).
         */
        if (deq(&v, queue)==1){ /*if successfully dequeued*/
            cnt[tid]++;  /*increase the counter*/
            assert(v==1);   /*check for dequeued value:
                             *1 means correct dequeuing.
                             *0 means incorrect dequeing (the element is previously dequed).
                             **/
        }
        pthread_mutex_unlock(&mutex);
        if (is_stop){
            printf("Consumer %d consumed %d items.\n",tid,cnt[tid]);
            pthread_exit(NULL);
        }
    }
}
void Foam::diameterModels::ADD::correct()
{
    Deff();
    deq();
    tauRel();

    const volScalarField& rho = phase_.rho();
    const volScalarField& alpha = phase_;
    const surfaceScalarField& alphaRhoPhi = phase_.alphaRhoPhi();
    const volScalarField alphaRho = rho*alpha;
    
    volScalarField& d = d_;

    volScalarField& Deff = Deff_;
    volScalarField& deq = deq_;
    volScalarField& tauR = tauRel_;
    
    fvScalarMatrix dEqn
    (
        fvm::ddt(alpha, rho, d)
      + fvm::div(alphaRhoPhi, d) 
        ==
        fvm::laplacian(Deff, d)
      - fvm::Sp(alphaRho/tauR, d)
      + alphaRho/tauR*deq
    );
    
    dEqn.relax();
    dEqn.solve();
    
    d = max(dMin_,min(dMax_, d));
    
}
Пример #10
0
void * thread_func1(int idx, queue_t q, char * a,
		    int n_items, int n_enq_threads, int n_deq_threads) {
  if (idx < n_enq_threads) {
    /* 挿入するスレッド. n_enq_threads スレッドで分担して, 
       0, 1, 2, ..., n_items-1 を一度ずつ挿入する */
    int i;
    /* [my_items_beg,my_items_end) = 自分が挿入する範囲 */
    int my_items_beg = (n_items * idx)       / n_enq_threads;
    int my_items_end = (n_items * (idx + 1)) / n_enq_threads;
    /* 挿入(enq) */
    for (i = my_items_beg; i < my_items_end; i++) {
      enq(q, i);
    }
  } else {
    /* 削除(deq) */
    int deq_idx = idx - n_enq_threads;
    /* n_deq_threadsスレッドが分担して合計, n_items 回 deq を発効する.
       挿入した全要素が一度ずつとり出されるはずである */
    int my_items_beg = (n_items * deq_idx)       / n_deq_threads;
    int my_items_end = (n_items * (deq_idx + 1)) / n_deq_threads;
    int i;
    for (i = my_items_beg; i < my_items_end; i++) {
      int x = deq(q);
      assert(x != -1);	      /* (i) 空であってはいけない */
      assert(a[x] == 0); /* (ii) 同じ要素が2度とり出されてはいけない */
      a[x] = 1;	      /* xが取り出されたと記録 */
    }
  }
  return 0;
}
Пример #11
0
int main(void)
{
	int arr[] = {11,22,33,44,55,66,77,88};
	int i, j;
	int data;
	int ret;

	for (j = 0; j < 4; j++) {
		for (i = 0; i < sizeof(arr) / sizeof(*arr); i++) {
			ret = enq(arr[i]);
			if (ret == -1) {
				printf("queue is full.\n");
				break;
			}
		}

		for (i = 0; i < sizeof(arr) / sizeof(*arr); i++) {
			ret = deq(&data);
			if (ret == -1) {
				printf("queue is empty.\n");
				break;
			}
			printf("%d ", data);
		}
		printf("\n");
	}

	return 0;
}
Пример #12
0
/* 
 * Function tries to dequeue from the queue in an endless loop, 
 * stopping after two seconds (checking once every hundred iterations)
 * Prints "FAIL" and exits if it receives a non-sequential value
 */
static void *eternal_dqr(void *_q) {
    struct l_queue *q = (struct l_queue *) _q;
    struct timespec start, end;
    
    clock_gettime (CLOCK_MONOTONIC, &start);
    clock_gettime (CLOCK_MONOTONIC, &end);
    long val = 1;
    long old_val = 0;
    for (int i = 0;; i++) {
        if (!deq (q, (void **) &val)) {
            if (val == 0)
                break;
            if (old_val + 1 != val) {
                printf("FAIL: expected %ld, got %ld\n", old_val + 1, val);
                break;
            }
            old_val = val;
        }
        if (i % 100 == 0) {
            clock_gettime (CLOCK_MONOTONIC, &end);
            if (timediff (&start, &end) > 2000000) 
                break;
        }
    }
    return NULL;
}
Пример #13
0
int kb_getchar()
{
int c,code;
c=deq(&_q,&code);
if (c==-1) return -1;
return code;
};
Пример #14
0
/*------------------------------------------------------------------------
 *  lsu_in - handle a received link state update packet
 *------------------------------------------------------------------------
 */
void
lsu_in(struct ep *pep)
{
	struct ip	*pip = (struct ip *)pep->ep_data;
	struct ospf	*po;
	struct ospf_if	*pif = &ospf_if[pep->ep_ifn];
	struct ospf_lsu	*plsu;
	struct ospf_db	*pdb, *db_lookup();
	struct ospf_nb	*pnb;
	int		i;

	if (pif->if_state <= IFS_WAITING)
		return;
	pnb = &pif->if_nbtab[1];
	po = (struct ospf *)((char *)pip+IP_HLEN(pip));
	for (i=0; i<MAXNBR; ++i, ++pnb)
		if (pnb->nb_rid == po->ospf_rid)
			break;
	if (i == MAXNBR || pnb->nb_state < NBS_EXCHNG)
		return;
/* db update here */
	if (headq(pnb->nb_lsrl) && lsr_check(pnb))
		freebuf(deq(pnb->nb_lsrl));
/* flooding procedure */
}
Пример #15
0
void bfsMat(int startingNode, int** mat, int size)
{
    int* visited = (int*)malloc(sizeof(int)*size);
    int i;
    for(i = 0; i < size; i++)
        visited[i] = UNVISITED;

    visited[startingNode] = VISITED;
    printf("%d ", startingNode);
    node* head = NULL;
    node* tail = NULL;
    int temp;
    enq(&tail, startingNode, &temp);
    head = tail;

    while(head != NULL)
    {
        int v = head->data;
        deq(&head);
        for(i = 0; i < size; i++)
            if(mat[v][i] > 0 && visited[i] == UNVISITED)
            {
                visited[i] = VISITED;
                printf("%d ", i);
                enq(&tail, i, &temp);
                if(head == NULL)
                    head = tail;
            }
    }
    printf("\n");
}
Пример #16
0
int main()
{
	int i, level=0,j;
	qu *q=malloc(sizeof(qu)*100);
	memset(q, 0, sizeof(qu)*100);

	for(i=0;i<100;i++)
		for(j=0;j<100;j++)
			distance[i][j]=INFINITY;
	enq(q, 0, 0);
	distance[0][0] = 0;
	while(iE(q)) {
		int i, j, x_, y_;
		struct q_i *e = deq(q);
		printf("%d %d\n", e->i, e->j);
		x_ = e->i;y_=e->j;
		if(e->i == 7 && e->j == 7) {
			printf("found %d\n", distance[7][7]+1);
			return;	
		}
		for(i=0;i+1<sizeof(po)/sizeof(po[0]);i+=2) {
			int x, y;
			x=x_+po[i];
			y=y_+po[i+1];
			if(x >= 0 && x < 8 && y >= 0 && y < 8) {
				if(distance[x][y] == INFINITY) {
					distance[x][y]	= 1+ distance[x_][y_];
					enq(q, x, y);
				}
			}
		}
	}
	return 0;
}
Пример #17
0
void main()
{
     int x,c,ch,no;
     //int que[100];
     clrscr();
     do
     {
     printf("\n1.Enqueue\n2.Dequeue : ");
     scanf("%d",&x);
     if(x==1)
     {
	if(size==99)
	   printf("\nStack is full...") ;

	else
	{
	    printf("\nEnter the number : ");
	    scanf("%d",&no);
	    enq(no);
	}
       //	disp(que,size);
     }
     else
     {
	   deq();

     }
       disp();
    printf("\nDo u wnt to conti ? 1.yes 2.no");
    scanf("%d",&ch);
    }while(ch!=2);
     getch();
}
Пример #18
0
void
bfs (NODE *tptr)
{
	NODE 	*current = NULL;

	current = tptr;

	printf ("%d", current->data);

	while (current) {

			if (current->left)
				enq (&q, (long long) current->left);

			if (current->right)
				enq (&q, (long long) current->right);


		printf ("%d ", current->data);

		current = (NODE *) deq (&q);
	}


}
Пример #19
0
int main(void)
{
	int arr[] = {3,2,1,6,7,8,9,0,};
	int i, j;
	int data;
	int ret;

	for (j = 0; j < 5; j++) {
		for (i = 0; i < 3; i++) {
			ret = enq(arr[i] + j * 3);
			if (ret == -1) {
				printf("stack full, i = %d\n", i);
				break;
			}
		}

		for (i = 0; i < 7; i++) {
			ret = deq(&data);
			if (ret == -1) {
				break;
			}

			printf("%d ", data);
		}
		printf("\n");
	}

	return 0;
}
Пример #20
0
int getch() {
  int  data;
  do {
    while (empty(&kbd_q));
  } while (deq(&kbd_q, &data,NULL) != 0);
  return data;
}
Пример #21
0
void bfsLst(int startingNode, node** lst, int size)
{
    int* visited = (int*)malloc(sizeof(int)*size);
    int i;
    for(i = 0; i < size; i++)
        visited[i] = UNVISITED;

    visited[startingNode] = VISITED;
    printf("%d ", startingNode);
    node* head = NULL;
    node* tail = NULL;
    int temp;
    enq(&tail, startingNode, &temp);
    head = tail;

    while(head != NULL)
    {
        int v = head->data;
        deq(&head);
        node* aux = lst[v];
        while(aux != NULL)
        {
            if(visited[aux->data] == UNVISITED)
            {
                visited[aux->data] = VISITED;
                printf("%d ", aux->data);
                enq(&tail, aux->data, &temp);
                if(head == NULL)
                    head = tail;
            }
            aux = aux->next;
        }
    }
    printf("\n");
}
Пример #22
0
Файл: q.c Проект: chungae9ri/dsa
int main()
{
	int c, age;
	char name[16];
	struct qnode el;
	struct qnode *pel;

	c = getchar();
	while(c != 'q') {
		if(c=='p') {
			printf("input name : ");
			scanf("%s", el.name);
			printf("input age : ");
			scanf("%d", &el.age);
			enq(el);
		} else if(c=='d') {
			pel = deq();
			printf("dequeue name : %s\n", pel->name);
			printf("dequeue age : %d\n", pel->age);
			free(pel);
		}
		c=getchar();
	}
	xfree();
	return 0;
}
Пример #23
0
/**
 * wxThreadのテスト
 */
void HelloWorld::OnTestLockedQueue(wxCommandEvent& event) {

     // キューを作成 サイズは2
     wxMutex mutex;
     wxCondition enq(mutex), deq(mutex);
     wxLockedQueue<int> lq(2, &mutex, &enq, &deq);
     //WorkerThread<int>* thread = new WorkerThread<int>(lq);
}
Пример #24
0
// Updates 'memb'.
void update_memb() {
	size_t e;
	size_t h;
	size_t i;
	size_t j;
	size_t k;
	size_t a_card;
	double sums[clustc];
	double sumd;
	double val;
	for(i = 0; i < objc; ++i) {
		a_card = 0;
		for(k = 0; k < clustc; ++k) {
			sums[k] = 0.0;
			for(j = 0; j < dmatrixc; ++j) {
				sumd = 0.0;
				for(e = 0; e < medoids_card; ++e) {
					sumd += dmatrix[j][i][medoids[k][j][e]];
				}
				sums[k] += weights[k][j] * sumd;
			}
			if(deq(sums[k], 0.0)) {
				++a_card;
			}
		}
		if(a_card) {
			printf("Object %u has zero val.\n", i);
			val = 1.0 / ((double) a_card);
			for(k = 0; k < clustc; ++k) {
				if(deq(sums[k], 0.0)) {
					memb[i][k] = val;
				} else {
					memb[i][k] = 0.0;
				}
			}
		} else {
			for(k = 0; k < clustc; ++k) {
				memb[i][k] = 0.0;
				for(h = 0; h < clustc; ++h) {
					memb[i][k] += pow(sums[k] / sums[h], mfuzval);
				}
				memb[i][k] = 1.0 / memb[i][k];
			}
		}
	}
}
Пример #25
0
void main(int argc, char *argv[])
{
    int no, ch, e;

    /* Serena's seed */
    int sseed;
    if (argc == 2) {
      sseed = atol(argv[1]);
    }
    srand(sseed);

    int max_actions = 100;
    int nactions = rand() % max_actions;
    
    /* printf("\n 1 - Enque");
    printf("\n 2 - Deque");
    printf("\n 3 - Front element");
    printf("\n 4 - Empty");
    printf("\n 5 - Exit");
    printf("\n 6 - Display");
    printf("\n 7 - Queue size"); */
    create();
    for (int i = 0; i < nactions; i++)
    {
        printf("\n Enter choice : ");
        ch = rand() % 7 + 1;
        switch (ch)
        {
        case 1:
            printf("Enter data : ");
            no=rand() % max_actions;
            enq(no);
            break;
        case 2:
            deq();
            break;
        case 3:
            e = frontelement();
            if (e != 0)
                printf("Front element : %d", e);
            else
                printf("\n No front element in Queue as queue is empty");
            break;
        case 4:
            empty();
            break;
        case 6:
            display();
            break;
        case 7:
            queuesize();
            break;
        default:
            printf("Wrong choice, Please enter correct choice  ");
            break;
        }
    }
}
Пример #26
0
/* 
 * Terminate the calling thread (running) 
 * Run the next ready thread 
 */
void t_terminate(){
	if(ready_0 == NULL && ready_1 == NULL){
		t_shutdown();
	}else{
		remove_running();
		if(deq())
			setcontext(running->thread_context);
	}
}
Пример #27
0
void keyboardflush()
 {
  char c,code;
  do
   {
    c=deq(&_q,&code);
   }
   while (c!=-1);
 };
Пример #28
0
/****************************************************
******************* Radix Sort **********************
****************************************************/
void resetList(List* lstv)
{
    int i;
    for(i = 0; i < 10; i++)
    {
        while(lstv[i].head != NULL)
            deq(&lstv[i]);
    }
}
Пример #29
0
/*------------------------------------------------------------------------
 *  nb_mismatch - handle sequence # mismatch event
 *------------------------------------------------------------------------
 */
int
nb_mismatch(struct ospf_if *pif, struct ospf_nb *pnb)
{
	struct ep	*pep;

	pnb->nb_state = NBS_EXSTART;
	
	/* empty lists */
	while (pep = (struct ep *)deq(pnb->nb_lsal))
		freebuf(pep);
	while (pep = (struct ep *)deq(pnb->nb_dsl))
		freebuf(pep);
	while (pep = (struct ep *)deq(pnb->nb_lsrl))
		freebuf(pep);
	pnb->nb_seq++;
	dd_queue(pif, pnb);
	return 0;
}
Пример #30
0
int kb_dequeue(int *val)
{
int retval;
if (getprocessid()!=fg_getkeyboardowner())
          retval= -1;
                else
          retval= deq(&_q,val);
return retval;
};