示例#1
0
int main(){
	int n, s, m;
	ptrqnode circle;
	ptrqnode select, q;
	
	//输入
	printf("Please input people number:\t");
	scanf("%d", &n);
	printf("Please input s(s>0):\t");
	scanf("%d", &s);
	printf("Please input period\t");
	scanf("%d", &m);
	
	//构造圈
	circle = (ptrqnode)malloc(sizeof(qnode));
	circle->tag = 1;
	circle->next = circle;

	InitLink(circle, n);

	//得到出圈序列
	select = Out_of_circle(circle, s, m);
	q = select->next;
	while(q->next){
		q = q->next;
	}
	print(q);
	system("pause");
	return 0;
}
/* Must be called before any of the below functions. Failure to do so may
 * result in undefined behavior. 'period' is the scheduling quantum (interval)
 * in microseconds (i.e., 1/1000000 sec.). */
void gtthread_init(long period){

	struct sigaction sa;
	struct itimerval timer;
	GLOBAL_PERIOD = period;

	/*Allocate memory for a link*/
	dlink_t *main_dlink;
	main_dlink = (dlink_t*)malloc(sizeof(dlink_t));
	gtthread_t *main_thread;
	main_thread = (gtthread_t*)malloc(sizeof(gtthread_t));

	//Initialize main link
	if(InitLink(main_dlink,main_thread,0,-1)==-1){
		fprintf(stderr,"ERRROR: InitLink() on main did not work\n");
	}
	main_dlink->thread_block->runnning = 1;

	//Initialize the dqueue
	schedule_queue = (queue_t*)malloc(sizeof(queue_t));
	schedule_queue->begin = main_dlink;
	schedule_queue->end = main_dlink;
	schedule_queue->running = main_dlink;
	schedule_queue->length = 1;
	schedule_queue->main_exited = 0;

	//Get main context
	if(getcontext(&(main_dlink->thread_block->thread_context))==-1){
			fprintf(stderr,"ERRROR: getcontext() on main did not work\n");
	}

	//Initialize return list
	head_return_list = NULL;

	/*Install timer handler as the signal handler for SIFVTALARM*/
	memset(&sa, 0, sizeof(sa));
	sa.sa_sigaction = timer_handler;
	sa.sa_flags = 0;
	sigaction (SIGVTALRM, &sa, NULL);

	/* Configure the timer to expire after period usec... */
	sigemptyset (&signal_info);
	sigaddset(&signal_info, SIGVTALRM);
	timer.it_value.tv_sec = 0;
	timer.it_value.tv_usec = (suseconds_t)period;
	timer.it_interval.tv_sec = 0;
	timer.it_interval.tv_usec = (suseconds_t)period;

	/* Start a virtual timer. It counts down whenever this process is
	   executing. */
	setitimer (ITIMER_VIRTUAL, &timer, NULL);
}
/* see man pthread_create(3); the attr parameter is omitted, and this should
 * behave as if attr was NULL (i.e., default attributes) */
int  gtthread_create(gtthread_t *thread,
                     void *(*start_routine)(void *),
                     void *arg){
	struct itimerval* timer;
	gtid_t parent_id;
	dlink_t* thread_dlink;
	THREAD_COUNT++;

	//Allocate memory for a link
	thread_dlink = (dlink_t*)malloc(sizeof(dlink_t));
	if(thread_dlink == NULL){
		return -1;
	}


	//Stop Timer
	BlockSignals();
	parent_id = schedule_queue->running->thread_block->threadid;
	schedule_queue->running->thread_block->child_count++;
	UnblockSignals();

	//Initialize the link
	if(InitLink(thread_dlink,thread,THREAD_COUNT,parent_id)==-1){
		return -1;
	}

	//Make Thread Context
	if(getcontext(&(thread->thread_context))==-1){
		return -1;
	}
	thread->thread_context.uc_stack.ss_sp = (char*)malloc(sizeof(char)*20*MB);
	thread->thread_context.uc_stack.ss_size = sizeof(char)*20*MB;
	if(getcontext(thread->thread_context.uc_link)==-1){
		return -1;
	}
	makecontext(&(thread->thread_context),WrapperFunction, 2, start_routine,arg);

	//Add Link to Queue
	return(PushBack_Queue(thread_dlink,schedule_queue));
}
示例#4
0
int main()
{
	int n = -1;
	int flag = 0;
	int flag1;
	int save;
	struct Node * head;
	char fileName[20] = "0";

	flag = SignIn();           //登陆

	if(flag == 1)
	{
		head = InitLink();      //初始化
	}
	
	for(;;)  //各种功能 一直循环
	{
		fflush(stdin);				
		flag = 1;
		ReturnDesk();	

		system("clear");
		ShowDesk();
		fflush(stdin);
		scanf("%d", &n);
		fflush(stdin);	

		if(n == 0)
		{
			system("clear");
			FreeAllNodes(head);
			printf("\n\n\n\n\n\n               谢谢使用, ----made by Mr.Du 丶Lun\n\n\n\n\n\n");
			getch();
			exit(0);
		}//if

		if(n == 1)	
		{
			system("clear");
			Print(head);
		}//if
		if(n == 4)
		{
			system("clear");
			head = Sort(head);
			printf("sort over,new info:\n\n\n");
			Print(head);
		}
		if(n == 5)
		{
			system("clear");
			Print(head);
			Insert(head);
			system("clear");
			printf("insert over\n");
			Print(head);			
		}
		if(n == 6)
		{
			system("clear");
			Print(head);
			Remove(&head);
			system("clear");
			printf("remove over new info:\n");
			Print(head);
		}
		if(n == 8)
		{
			system("clear");
			Print(head);
			head =AddNodeBefore(head);
		}
		if(n == 9)
		{
			system("clear");
			Print(head);
			AddNodeAfter(head);
		}
		if(n == 10)
		{
			system("clear");
			printf("倒序排列为:\n");	
			head = Reverse(head);
			Print(head);
		}	
		if(n == 11)
		{
			system("clear");
			if(flag1 == -1)
			{
				Save_now_info(head, fileName);
				save = 1;
			}
			else
			{
				//printf("%d", flag);
				save = Save_info(head);
			}
			if(save == 1)
				printf("保存成功");
		}
		if(n == 12)
		{
			system("clear");
			head = Read_info(fileName);
			flag1 = -1;
			printf("\n");
			Print(head);
		}
		if(n == 2)
		{
			system("clear");
			Print(head);
			continueCreat(head);
		}
		if(n == 13)
		{
			system("clear");
			ChangePasswd();
			
		}
		if(n == 3)
		{
			system("clear");
			flag1 = 1;
			head = creatNewList();
		}
		if(n == 7)
		{
			system("clear");
			ChangeScore(head);
		}

	}//死循环
	
	getch();
}//main
void LinkOptions::OnOk()
{
    static const int length = 256;
    int timeout;
    CString timeoutStr;
    CString host;
    CString title;
    CString addressMessage;

    UpdateData(TRUE);

    // Close any previous link
    CloseLink();

    m_serverip.GetWindowText(host);
    m_timeout.GetWindowText(timeoutStr);
    sscanf(timeoutStr, "%d", &timeout);
    SetLinkTimeout(timeout);

    LinkMode newMode = (LinkMode)m_type;

    if (newMode == LINK_DISCONNECTED) {
        linkTimeout = timeout;
        linkMode = LINK_DISCONNECTED;
        theApp.linkHostAddr = host;
        CDialog::OnOK();
        return;
    }

    bool needsServerHost = newMode == LINK_GAMECUBE_DOLPHIN || (newMode == LINK_CABLE_SOCKET && !m_server) || (newMode == LINK_RFU_SOCKET && !m_server);

    if (needsServerHost) {
        bool valid = SetLinkServerHost(host);
        if (!valid) {
            AfxMessageBox("You must enter a valid host name", MB_OK | MB_ICONSTOP);
            return;
        }
    }

    EnableSpeedHacks(linkHacks);
    EnableLinkServer(m_server, linkNumPlayers + 1);

    if (m_server) {
        char localhost[length];
        GetLinkServerHost(localhost, length);

        title = "Waiting for clients...";
        addressMessage.Format("Server IP address is: %s\n", localhost);
    } else {
        title = "Waiting for connection...";
        addressMessage.Format("Connecting to %s\n", host);
    }

    // Init link
    ConnectionState state = InitLink(newMode);

    // Display a progress dialog while the connection is establishing
    if (state == LINK_NEEDS_UPDATE) {
        ServerWait* dlg = new ServerWait();
        dlg->Create(ServerWait::IDD, this);
        dlg->m_plconn[1] = title;
        dlg->m_serveraddress = addressMessage;
        dlg->ShowWindow(SW_SHOW);

        while (state == LINK_NEEDS_UPDATE) {
            // Ask the core for updates
            char message[length];
            state = ConnectLinkUpdate(message, length);

            // Update the wait message
            if (strlen(message)) {
                dlg->m_plconn[1] = message;
            }

            // Step the progress bar and update dialog data
            dlg->m_prgctrl.StepIt();
            dlg->UpdateData(false);

            // Process Windows messages
            MSG msg;
            while (PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE)) {
                AfxGetApp()->PumpMessage();
            }

            // Check whether the user has aborted
            if (dlg->m_userAborted) {
                state = LINK_ABORT;
            }
        }

        delete dlg;
    }

    // The user canceled the connection attempt
    if (state == LINK_ABORT) {
        CloseLink();
        return;
    }

    // Something failed during init
    if (state == LINK_ERROR) {
        AfxMessageBox("Error occurred.\nPlease try again.", MB_OK | MB_ICONSTOP);
        return;
    }

    linkTimeout = timeout;
    linkMode = GetLinkMode();
    theApp.linkHostAddr = host;

    CDialog::OnOK();
    return;
}