예제 #1
0
void MySemaphoreWait(MySemaphore sem) {
	if (debug) printf("***** MySemaphoreWait *****\n");
	if (debug) printf("***** Running   %i *****\n", running_thread->id);
	Semaphore * semaphore = (Semaphore *)sem;
	if (semaphore->initialValue > 0) {
		semaphore->initialValue--;
		if (debug) printf("***** Continuing   %i *****\n", running_thread->id);
	} else {
		semaphore->initialValue--;
		running_thread->state = 2;
		Enqueue(running_thread, semaphore->waitingQ, 3);
		Thread * blocked_thread = running_thread;

		running_thread = readyQ->front;
		Dequeue(readyQ, 0);
		if (debug) printf("***** Blocked on Sem   %i *****\n", blocked_thread->id);
		if (debug) printf("***** Running   %i *****\n", running_thread->id);

		if (queues) PrintQueue(readyQ, 0);
		if (queues) PrintQueue(blockedQ, 1);
		if (queues) PrintQueue(semaphore->waitingQ, 3);

		swapcontext(blocked_thread->context, running_thread->context);
	}
}
예제 #2
0
MyThread MyThreadCreate(void(*start_funct)(void *), void *args){
	if (debug) printf("***** MyThreadCreate *****\n");
	if (debug) printf("***** Running   %i   *****\n", running_thread->id);
	ucontext_t * new_context = malloc(sizeof(ucontext_t));
	getcontext(new_context);
	new_context->uc_link=0;
	new_context->uc_stack.ss_sp=malloc(THREAD_STACK_SIZE);
	new_context->uc_stack.ss_size=THREAD_STACK_SIZE;
	new_context->uc_flags=0;

	makecontext(new_context, (void *)start_funct, 1, args);

	Thread * thread = malloc(sizeof(Thread));
	thread->id 		 = THREAD_COUNTER++;
	thread->state	 = 0;
	thread->context  = new_context;
	thread->next_r 	 = NULL;
	thread->next_b	 = NULL;
	thread->next_s 	 = NULL;
	thread->next_sem = NULL;
	thread->join_all = false;
	thread->joined_by_parent= false;
	thread->children = NewQueue('c');
	thread->parent   = running_thread;

	Enqueue(thread, readyQ, 0);
	Enqueue(thread, running_thread->children, 2);

	if (debug) printf("***** Created   %i   *****\n", thread->id);
	if (queues) PrintQueue(readyQ, 0);
	if (queues) PrintQueue(blockedQ, 1);

	return (void*)thread;
}
예제 #3
0
int MyThreadJoin(MyThread thread) {
	if (debug) printf("***** MyThreadJoin *****\n");
	if (debug) printf("***** Running   %i *****\n", running_thread->id);

	Thread * child_thread = (Thread *)thread;

	if (child_thread->parent == running_thread) {
		running_thread->state = 2;
		Enqueue(running_thread, blockedQ, 1);

		child_thread->joined_by_parent = true;

		Thread * blocked_thread = running_thread;

		running_thread = readyQ->front;
		running_thread->state = 1;
		Dequeue(readyQ, 0);

		if (debug) printf("***** Blocked   %i   *****\n", blocked_thread->id);
		if (debug) printf("***** Running   %i   *****\n", running_thread->id);

		if (queues) PrintQueue(readyQ, 0);
		if (queues) PrintQueue(blockedQ, 1);

		swapcontext(blocked_thread->context, running_thread->context);
		return 0;
	} else {
		return -1;
	}
}
예제 #4
0
int main()
{
	//测试样例
	QElemType a[5]={2,3,4,5,6};
	QElemType result;
	LinkQueue queue;
	InitQueue(queue,1);
	printf_s("初始化队列...完成\n");
	PrintQueue(queue);
	//进出顺序:进三个,出两个,进两个,清空队列
	printf_s("队列进入三个元素...");
	EnQueue(queue,a[0]);
	EnQueue(queue,a[1]);
	EnQueue(queue,a[2]);
	printf_s("完成\n");
	PrintQueue(queue);
	printf_s("队列退出两个元素...");
	DeQueue(queue,result);
	DeQueue(queue,result);
	printf_s("完成\n");
	PrintQueue(queue);
	printf_s("队列进入两个元素...");
	EnQueue(queue,a[3]);
	EnQueue(queue,a[4]);
	printf_s("完成\n");
	PrintQueue(queue);
	printf_s("销毁队列...");
	DestroyQueue(queue);
	printf_s("完成\n");


}
예제 #5
0
int main()
{
	Queue TestQueue = CreateQueue(10);
	cout << "Begin test of queue " << endl;
	for (int i = 0, n = 1; i < 10; i++, n *= 2)
	{
		cout << i + 1 << "> Enqueue : " << n<<endl;
		Enqueue(n, TestQueue);
	}
	PrintQueue(TestQueue);
	cout << "Is Full ? " << IsFull(TestQueue)<<endl;

	for (int i = 0; i < 5; i++)
	{
		cout << i + 1 << " >Dequeue :" << Front(TestQueue) << endl;
		Dequeue(TestQueue);
	}
	PrintQueue(TestQueue);
	cout << "Front and dequeue: " << FrontAndDequeue(TestQueue)<<endl;
	cout << "Now add more data to test the cicular array..." << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << i + 1 << "> Enqueue : " << i << endl;
		Enqueue(i, TestQueue);
	}
	PrintQueue(TestQueue);
	cout << "Now make the queue empty...";
	MakeEmpty(TestQueue);
	cout << "Is Empty ? "<<IsEmpty(TestQueue)<<endl;
	cout << "Now dipose the queue!" << endl;
	DisposeQueue(TestQueue);
	cout << "Test Succeed!" << endl << "Good bye!"<<endl;
	getchar();
}
예제 #6
0
int main()
{
    Element data = 0;
    Ptrqueue  q = Queue_Creat();
    intoqueue(q, 5);
    intoqueue(q, 9);
    intoqueue(q, 6);
    PrintQueue(q);
    delqueue(q, &data);
    PrintQueue(q);
    printf("%d ",data);
    delqueue(q, &data);
    delqueue(q, &data);PrintQueue(q);
}
예제 #7
0
main () 
{   
  listelement listmember,*listpointer;   
  int     data,           choice;   
  listpointer = NULL;   
  do 
    {
      Menu (&choice);
      switch (choice) {   
      case 1: 
	printf ("Enter data item value to add  ");
	scanf ("%d", &data);
	listpointer = AddItem (listpointer, data);
	break;   
      case 2: 
	if (listpointer == NULL)   
	  printf ("Queue empty!\n");
	else   listpointer = RemoveItem (listpointer);
	break;   
      case 3: 
	PrintQueue (listpointer);
	break;   
      case 4: 
	break;   
      default: 
	printf ("Invalid menu choice - tryagain\n");
	break;
      }   
    } while (choice != 4);   
  ClearQueue (listpointer);
}				
예제 #8
0
void PrintQueue(QueueElement *queue) {
	if (queue) {
		printf("%d\t", queue->element);
		PrintQueue(queue->next);
	} else
		printf("\n");
}
예제 #9
0
파일: main08-3.c 프로젝트: HanaHosoe/c
int main(void) {
    Queue  que;
    char op[5];
                
    QueueInit(&que); /* キューの初期化 */
    //キューの中身を確認
    if(!QueueIsEmpty(&que)){
        printf("初期化失敗\n");
        return (-1);
    }

    while (1) {
        int  m, no;
        char name[NAMELEN];
        Node *data;

        printf("Operation:");
        printf("(1)データの追加(enqueue),(2)データの削除(dequeue), (0) 終了:");
        scanf("%s", op);

        m=atoi(op);

        if (m == 0)
            break;

        switch(m) {
            case 1: data = AllocNode();
                printf("追加するデータを入力してください。\n");
                printf("番号:");scanf("%d", &no);
                printf("名前:");scanf("%s", name);
                SetNode(data,no,name,NULL);
                QueueEnque(&que, data);

                //キューの中身を確認
                if(QueueIsEmpty(&que)){
                    printf("エンキュー失敗\n");
                    return (-1);
                }
                break;    
            case 2: if(data = QueueDeque(&que)) {
                        puts("デキューしたデータ:");
                        printf("番号:%d,名前:%s\n", data->no, data->name);
 //                           free(data);
                        }
                    else {
                        puts("デキューできません。");
                    }
                    break;
        }
        printf("キューの一覧<%d>:\n", QueueNo(&que));
        PrintQueue(&que);
    }

    QueueFree(&que);

    return(0);

}
예제 #10
0
int main() {
	SeQueue Q;
	int data = 3, value;

	//0. Init
	InitQueue(Q);
	PrintQueue(Q);

	//1. Enter Queue
	printf("\n");
	PrintQueue(Q);
	printf("EnQueue = %d\n", data);
	EnQueue(Q, data);
	PrintQueue(Q);

	//2. DeQueue
	printf("\n");
	PrintQueue(Q);
	value = Front(Q);
	DeQueue(Q);
	printf("DeQueue value = %d\n", value);
	PrintQueue(Q);

	//3. Clear
	printf("\n");
	PrintQueue(Q);
	printf("Clear Queue\n");
	ClearQueue(Q);
	PrintQueue(Q);

	return 0;
}
예제 #11
0
void MySemaphoreSignal(MySemaphore sem) {
	if (debug) printf("***** MySemaphoreSignal *****\n");
	if (debug) printf("***** Running   %i *****\n", running_thread->id);

	Semaphore * semaphore = (Semaphore *)sem;
	if (!isEmpty(semaphore->waitingQ)) {
		Thread * unblocked_thread = semaphore->waitingQ->front;
		Dequeue(semaphore->waitingQ, 3);
		unblocked_thread->state = 0;
		Enqueue(unblocked_thread, readyQ, 0);
		if (debug) printf("***** Unblocked on Sem   %i *****\n", unblocked_thread->id);
		if (queues) PrintQueue(readyQ, 0);
		if (queues) PrintQueue(blockedQ, 1);
		if (queues) PrintQueue(semaphore->waitingQ, 3);
		semaphore->initialValue++;
	} else {
		semaphore->initialValue++;
	}
}
예제 #12
0
파일: binqueue.c 프로젝트: buchenglei/cdsl
void main() {
   BinQueue H = InitCollection(3);
   H = Insert(12, H);
   //H = Insert(24, H);
   //H = Insert(21, H);
   //H = Insert(65, H);
   // TODO 执行到这一句的时候Segment Fault
   PrintQueue(H);
    
}
예제 #13
0
void MyThreadYield(void) {
	if (debug) printf("***** MyThreadYield *****\n");
	if (debug) printf("***** Running   %i *****\n", running_thread->id);
	if (!isEmpty(readyQ)) {
		running_thread->state = 0;
		Enqueue(running_thread, readyQ, 0);

		Thread * yielded_thread = running_thread;

		running_thread = readyQ->front;
		running_thread->state=1;
		Dequeue(readyQ, 0);

		if (debug) printf("***** Yielded   %i   *****\n", yielded_thread->id);
		if (debug) printf("***** Running   %i  *****\n", running_thread->id);

		if (queues) PrintQueue(readyQ, 0);
		if (queues) PrintQueue(blockedQ, 1);
		swapcontext(yielded_thread->context, running_thread->context);
	}
}
예제 #14
0
void NCursesFrontend::Update(int key)
{
	// Figure out how big the screen is
	CalcWindowSizes();

	if (m_dataUpdatePos <= 0)
	{
		FreeData();
		m_neededLogEntries = m_messagesWinClientHeight;
		if (!PrepareData())
		{
			return;
		}

		// recalculate frame sizes
		CalcWindowSizes();
	}

	if (m_inputMode == editQueue)
	{
		int queueSize = CalcQueueSize();
		if (queueSize == 0)
		{
			m_selectedQueueEntry = 0;
			m_inputMode = normal;
		}
	}

	//------------------------------------------
	// Print Current NZBInfoList
	//------------------------------------------
	if (m_queueWinHeight > 0)
	{
		PrintQueue();
	}

	//------------------------------------------
	// Print Messages
	//------------------------------------------
	if (m_messagesWinHeight > 0)
	{
		PrintMessages();
	}

	PrintStatus();

	PrintKeyInputBar();

	UpdateInput(key);

	RefreshScreen();
}
예제 #15
0
void MyThreadExit(void) {
	if (debug) printf("***** MyThreadExit *****\n");
	if (debug) printf("***** Running   %i *****\n", running_thread->id);

	Thread * exiting_thread = running_thread;

	exiting_thread->state = 3;

	if (exiting_thread->parent != NULL && exiting_thread->parent->join_all) {
		if (exiting_thread->parent->children->front == exiting_thread && exiting_thread->parent->children->rear == exiting_thread) {
			Thread * unblocked_node = exiting_thread->parent;
			unblocked_node->state = 0;
			DeleteFromQueue(unblocked_node, blockedQ, 1);
			Enqueue(unblocked_node, readyQ, 0);
		}
	} else if (exiting_thread->joined_by_parent) {
		Thread * unblocked_node = exiting_thread->parent;
		unblocked_node->state = 0;
		DeleteFromQueue(unblocked_node, blockedQ, 1);
		Enqueue(unblocked_node, readyQ, 0);
	}
	if (queues) PrintQueue(readyQ, 0);
	if (queues) PrintQueue(blockedQ, 1);
	if (debug) printf("***** Exited   %i   *****\n", exiting_thread->id);

	RelinquishThread(exiting_thread);

	if(!isEmpty(readyQ)) {
		Thread * next_thread = readyQ->front;
		next_thread->state = 1;
		running_thread = next_thread;
		Dequeue(readyQ, 0);
		if (debug) printf("***** Running   %i *****\n", running_thread->id);

		setcontext(running_thread->context);
	} else {
		setcontext(&main_context);
	}
}
예제 #16
0
void MyThreadJoinAll(void) {
	if (debug) printf("***** MyThreadJoinAll *****\n");
	if (debug) printf("***** Running   %i *****\n", running_thread->id);
	MyQueue * children = running_thread->children;
	if(!isEmpty(children)) {
		running_thread->state = 2;
		running_thread->join_all = true;
		Enqueue(running_thread, blockedQ, 1);

		Thread * blocked_thread = running_thread;

		running_thread = readyQ->front;
		Dequeue(readyQ, 0);
		running_thread->state = 1;

		if (debug) printf("***** Blocked   %i   *****\n", blocked_thread->id);
		if (debug) printf("***** Running   %i *****\n", running_thread->id);

		if (queues) PrintQueue(readyQ, 0);
		if (queues) PrintQueue(blockedQ, 1);
		swapcontext(blocked_thread->context, running_thread->context);
	}
}
예제 #17
0
int main(void)
{
	PQ q = CreateQueue(10);

	PushQueue(q, GetRand());
	PushQueue(q, GetRand());
	PushQueue(q, GetRand());
	PushQueue(q, GetRand());

	PrintQueue(q);


	int i = 0;
	for( ; i < 10; ++i)
	{
		PushQueue(q, GetRand());
		PopQueue(q);
		PrintQueue(q);
	}

	ReleaseQueue(q);
	
	return 0;
}
예제 #18
0
void main()
{
	Queue *queue = NULL;
	Element item;
	queue = CreateQueue();

	item.key = 1;
	printf("Add ist element into the queue.\n");
	AddQueue(queue, item);
	PrintQueue(queue);

	printf("Add 2st element into the queue.\n");
	item.key = 2;
	AddQueue(queue, item);
	PrintQueue(queue);

	printf("Add 2st element into the queue.\n");
	item.key = 3;
	AddQueue(queue, item);
	PrintQueue(queue);

	printf("Add 2st element into the queue.\n");
	item.key = 4;
	AddQueue(queue, item);
	PrintQueue(queue);

	printf("Delete the top element from the queue.\n");
	item = DeleteQueue(queue);
	printf("Delete %d\n", item.key);
	PrintQueue(queue);

	printf("Destroy the queue.\n");
	DestoryQueue(queue);

	PrintQueue(queue);
}
예제 #19
0
int main()
{
	/*kamus*/
	Queue Q;
	int n,n2,i;
	int t = 0; // waktu masukan
	int pret = 0; // waktu sebelum t. cek pret<=t
	int tclose; // waktu tutup
	int visitor = 0;
	float srvtime = 0; // total waktu pelayanan
	float que = 0; // total antrian*waktu
	char input[6];
	boolean close;
	/*algoritma*/
	printf("Ukuran Queue : ");
	scanf("%d",&n);
	CreateEmpty(&Q,n);
	close = false;
	do {
		printf("Masukkan perintah: ");
		scanf("%d%s",&t,input);
		if(t<pret) printf("Waktu yang dimasukkan tidak valid, seharusnya >= %d\n",pret);
		else {
			switch(input[0]) {
				case 'a' : 
							if(IsFull(Q)) printf("Queue penuh, tidak bisa menerima pengunjung baru\n");
							else {
								que+=NBElmt(Q)*(t-pret);
								Add(&Q,t);
								printf("Q = ");
								PrintQueue(Q);
							}
							break;
				case 'c' :
							close = true;
							tclose = t;
							if (IsEmpty(Q)) 
								printf("Bank tutup, tidak ada pengunjung tersisa\n");
							else printf("Bank tutup, masih ada %d pengunjung tersisa\n",NBElmt(Q));
							break;
				case 'd' :
							if(IsEmpty(Q))
								printf("Queue kosong, tidak ada pengunjung yang akan dilayani\n");
							else {
								visitor++;
								Del(&Q,&n);
								if(!close) que+=NBElmt(Q)*(t-pret);
								printf("waktu kedatangan = %d, waktu tunggu = %d, Q = ",n,t-n);
								PrintQueue(Q);
								srvtime += t-n;
							}
			}
			pret=t;
		}
	}while(!close || !IsEmpty(Q));

	printf("\nBank tutup pada t = %d, layanan selesai pada t = %d\n",tclose,t);
	if(visitor) {
		printf("Jumlah pengunjung dilayani: %d orang\n",visitor);
		printf("Waktu tunggu rata-rata: %.0f satuan waktu\n",srvtime/visitor);
		printf("Panjang antrian rata-rata: %.3f orang\n",que/tclose);
	} else printf("Tidak ada pengunjung yang dilayani\n");
	
	return 0;	
}
예제 #20
0
main() {

  int e = 0;
unsigned char my_mac[5] = {0xE7,0xD3,0xF0,0x35,0xAA};
  QueueRecord *Q;
  QueueRecord sa;
  Q = &sa;
 int *p;
  DTNMsg* mp;
  DTNMsg m; 
  int i;

  MakeEmpty(Q);
  printf("Enqueue 20 elements...\n");
  for (i=0; i<20; i++) {
   m.prob = rand()%100;
   Enqueue(m, Q);
 }

PrintQueue(Q);
mp = Front(Q);
mp->prob = 10;
SortQueue(Q);

PrintQueue(Q);




/* 
printf("\n Dequeing.......\n");
for (i=0; i<7; i++)
printf(" %d ",(FrontAndDequeue(Q)).prob);

printf("\n Enqueing.......\n");
for (i=0; i<5; i++) {
m.prob = rand()%100;
printf(" %d ",m.prob);
Enqueue(m, Q);
}


PrintQueue(Q); 
SortQueue(Q);
for (i=0; i<5; i++)
printf(" %d ",(FrontAndDequeue(Q)).prob);
*/

printf("\n\n");

unsigned char* mac;
mac = my_mac;
for(i=0;i<5;i++)
printf("%x",mac[i]);

unsigned int x;
x= 0x0000000F;
x=(x<<4)|0xF;
unsigned int xx = 0x00000000;
for(i = 0 ;i<=5;i++){
x = ((0x00000000 | 0x89B0)<<16) | xx++;

printf("\n\nx = %x\n",x);}













}