91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 2.3 KB
    文件類型: .rar
    金幣: 1
    下載: 0 次
    發布日期: 2024-10-09
  • 語言: 其他
  • 標簽: linux??C??電梯??練習??

資源簡介

這是我上班的時候無聊的時候寫的,我的基本思路是,在程序的初始化階段起兩個線程,一個是用戶輸出線程,另一個是電梯運行線程。這兩個線程共享一個電梯(結構體),用戶輸出的命令會根據自己定下的規則加入到命令鏈表中,電梯線程會根據鏈表的命令執行。

資源截圖

代碼片段和文件信息

#include?“lift.h“

ONE_LIFT_T?*lift?=?NULL;
pthread_t?lift_thread;
pthread_t?user_thread;

/*create?a?lift?struct*/
ONE_LIFT_T*?creatLift(){
ONE_LIFT_T?*lift;
lift?=?(ONE_LIFT_T*)malloc(sizeof(ONE_LIFT_T));
lift->status?=?STATUS_STOPPING;
lift->floor?=?1;
lift->commands?=?creatCmd(“1-“);?//default?floor?on?1
lift->direction?=?DIRECTION_NULL;

return?lift;
}

/*lift?running?thread?function*/
void?*liftThread(void*?p){
ONE_LIFT_T?*_lift?=?(ONE_LIFT_T*)p;
printf(“lift?cur?floor?is?%d\n“_lift->commands->floor);
while(1){
if(_lift->commands?!=?NULL){
if(_lift->commands->next?!=?NULL){
_lift->status?=?STATUS_RUNNING;?//set?lift?status
if(_lift->commands->floor?commands->next->floor){
_lift->commands->floor++;
_lift->commands->direction?=?DIRECTION_UP;?//set?lift?running?direction
}
else?if(_lift->commands->floor?>?_lift->commands->next->floor){
_lift->commands->floor--;
_lift->commands->direction?=?DIRECTION_DOWN;
}
else{?//_lift->commands->floor?==?_lift->commands->next->floor
delFirstCmd(&(_lift->commands));
printf(“lift?is?arrive?:?%d\n“_lift->commands->floor);
if(_lift->commands->next?!=?NULL){
_lift->status?=?STATUS_WAITTING;
}
else{
_lift->status?=?STATUS_STOPPING;
_lift->commands->direction?=?DIRECTION_NULL;
}

}
printf(“lift?cur?floor?is?%d\n“_lift->commands->floor);
}
}
sleep(2);?//arrive?next?floor?need?2?sec
}
}

/*user?operate?thread*/
void?*userThread(void*?p){
ONE_LIFT_T?*_lift?=?(ONE_LIFT_T*)p;
int?ret;
while(1){
char?cmd[2];
scanf(“%s“cmd);
//這里使用正則表達式最好
if((cmd[0]?>=?‘1‘?&&?cmd[0]?<=?‘9‘)?&&?
?(cmd[1]?==?‘-‘?||?cmd[1]?==?‘+‘?||?cmd[1]?==?‘*‘)?&&
?(strlen(cmd)?<=?2)
){
//if?user?input?correct?cmd??it?will?create?a?cmd?to?list
ret?=?addCmd(_lift->commandscreatCmd(cmd));
if(ret?!=?0)
printf(“Add?user?command?fail!\nIt?may?be?full!\n“);
}
else{
printf(“Usage:?nd?n(1~9)?d(-+)\n“);
}
}
}

/*lift?test?init*/
void?initThreads(){
lift?=?creatLift();
int?ret;
ret?=?pthread_create(&lift_threadNULLliftThread(void*)lift);
if(ret?!=?0){
printf(“Create?lift?thread?fail!\n“);
}

ret?=?pthread_create(&user_threadNULLuserThread(void*)lift);
if(ret?!=?0){
printf(“Create?user?thread?fail!\n“);
}
}

/*waitting?the?lift?thread?and?user?thread?for?stop*/
void?waitThreads(){
if(lift_thread?!=?0){
pthread_join(lift_threadNULL);
}
if(user_thread?!=?0){
pthread_join(user_threadNULL);
}
}


?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----

?????文件???????1903??2014-03-05?10:01??lift_cmd.c

?????文件????????971??2014-03-05?09:55??lift.h

?????文件???????2680??2014-03-05?09:55??lift.c

?????文件????????108??2014-03-05?09:55??Main.c

-----------?---------??----------?-----??----

?????????????????5662????????????????????4


評論

共有 條評論