²Ù×÷ϵͳʵÑé
µÚÒ»ÆÚ ÏîÄ¿¿ª·¢ÊµÏÖ
ʵÑéÃû³Æ EXP.1 CPU Scheduling
Exp.2 Allocation & Reclaim
ʵÑéÄÚÈÝ
Ò»£¬Ñ¡ÔñÒ»¸öµ÷¶ÈËã·¨£¬ÊµÏÖ´¦Àí»úµ÷¶È£»
¶þ£¬´¦Àí»úµ÷¶È¹ý³ÌÖУ¬Ö÷´æ´¢Æ÷¿Õ¼äµÄ·ÖÅäºÍ»ØÊÕ£»
ʵÑéÄ¿µÄ
Ò»,¶àµÀϵͳÖУ¬µ±¾ÍÐ÷½ø³ÌÊý´óÓÚ´¦Àí»úÊýʱ£¬Ðë°´ÕÕijÖÖ²ßÂÔ¾ö¶¨ÄÄЩ½ø³ÌÓÅÏÈÕ¼Óô¦Àí»ú¡£±¾ÊµÑéÄ£ÄâʵÏÖ´¦Àí»úµ÷¶È£¬ÒÔ¼ÓÉîÁ˽⴦Àí»úµ÷¶ÈµÄ¹¤×÷£»
¶þ£¬°ïÖúÁ˽âÔÚ²»Í¬µÄ´æ´¢¹ÜÀí·½Ê½Ï£¬Ó¦ÔõÑùʵÏÖÖ÷´æ¿Õ¼äµÄ·ÖÅäºÍ»ØÊÕ£»
ʵÑéÌâÄ¿
Ò»£¬£¨1£©Éè¼ÆÒ»¸ö°´ÕÕÓÅÏÈȨµ÷¶ÈË㷨ʵÏÖ´¦Àí»úµ÷¶ÈµÄ³ÌÐò£» £¨2£©Éè¼Æ°´Ê±¼äƬÂÖתʵÏÖ´¦Àí»úµ÷¶ÈµÄ³ÌÐò£»
¶þ£¬Ôڿɱä·ÖÇø¹ÜÀí·½Ê½Ï£¬²ÉÓÃ×îÏÈÊÊÓ¦Ë㷨ʵÏÖÖ÷´æ¿Õ¼äµÄ·ÖÅäºÍ»ØÊÕ£»
ʵÑéÒªÇó
Ò»£¬£¨a£©£¬PCBÄÚÈÝ£º ½ø³ÌÃû/PID£» ÒªÇóÔËÐÐʱ¼ä£¨µ¥Î»Ê±¼ä£©£» ÓÅÏÈȨ£» ״̬£» PCBÖ¸Õ룻 ¡ª¡ª(Òò¿Î³ÌÄÚÈÝÔÒò£¬Õâ¸öÖ¸ÕëÔÚÉè¼ÆÖÐûÓÃ)
1£¬¿ÉËæ»úÊäÈëÈô¸É½ø³Ì£¬²¢°´ÓÅÏÈȨÅÅÐò£»
2£¬´Ó¾ÍÐ÷¶ÓÁÐÊ×Ñ¡½ø³ÌÔËÐУºÓÅÏÈȨ-1/ ÒªÇóÔËÐÐʱ¼ä-1£»ÒªÇóÔËÐÐʱ¼ä=0ʱ£¬
³·Ïú¸Ã½ø³Ì£»
3£¬ÖØÐÂÅÅÐò£¬½øÐÐÏÂÒ»ÂÖµ÷¶È£» £¨b£©£¬×îºÃ²ÉÓÃͼÐνçÃæ£»
£¨c£©,¿ÉËæÊ±Ôö¼Ó½ø³Ì£»
£¨d£©£¬¹æ¶¨µÀÊý£¬ÉèÖú󱸶ÓÁÐºÍ¹ÒÆð״̬¡£ÈôÄÚ´æÖнø³ÌÊýÉÙÓڹ涨µÀÊý£¬¿É×Ô¶¯´Óºó±¸¶ÓÁе÷¶ÈÒ»×÷Òµ½øÈë¡£±»¹ÒÆð½ø³ÌÈç=Èë¹ÒÆð¶ÓÁУ¬ÉèÖýâ¹Ò¹¦ÄÜÓÃÓÚ½«Ö¸¶¨¹ÒÆð½ø³Ì½â¹ÒÈë¾ÍÐ÷¶ÓÁУ»
£¨e£©£¬Ã¿´Îµ÷¶Èºó£¬ÏÔʾ¸÷½ø³Ì״̬£»
¶þ£¬£¨a£©£¬×ÔÐмÙÉèÖ÷´æ¿Õ¼ä´óС£¬Ô¤Éè²Ù×÷ϵͳËùÕ¼´óС²¢¹¹Ôìδ·Ö·ÖÇø±í£» ±íÄ¿ÄÚÈÝ£ºÆðÖ·¡¢³¤¶È¡¢×´Ì¬£¨Î´·Ö/¿Õ±íÄ¿£©
£¨b£©£¬½áºÏʵÑéÒ»£¬PCBÔö¼ÓΪ£º£ûPID£¬ÒªÇóÔËÐÐʱ¼ä£¬ÓÅÏÈȨ£¬×´Ì¬£¬ËùÐèÄÚ´æ´ó
С£¬Ö÷´æÆðʼλÖã¬PCBÖ¸Õ루ʧЧ£©£ý£» £¨C£©²ÉÓÃ×îÏÈÊÊÓ¦Ëã·¨·ÖÅäÖ÷´æ¿Õ¼ä£»
£¨D£©£¬½ø³ÌÍê³Éºó£¬»ØÊÕÖ÷´æ£¬²¢ÓëÏàÁÚ¿ÕÏзÖÇøºÏ²¢£»
ʵÑé¹ý³Ì¼°·ÖÎö
1£¬
³õ²½Éè¼Æ£º
2£¬ ÏêϸÉè¼Æ£º
£¨1£©×÷Òµ½øÈëÄÚ´æÖУ¬ÓÉCPU·ÖÅä²úÉúPCBÊôÐÔ£¬²¢Í¨¹ýPCB¼Ç¼½ø³Ì״̬£¬ÊµÑé¼´ÒÔPCB´ú±í½ø³ÌÄ£Äâµ÷¶È¹ý³Ì£»
£¨2£©ÔÚ¶àµÀϵͳÖУ¬¶àµÀϵͳÖУ¬µ±¾ÍÐ÷½ø³ÌÊý´óÓÚ´¦Àí»úÊýʱ£¬Ðë°´ÕÕijÖÖ²ßÂÔ¾ö¶¨ÄÄЩ½ø³ÌÓÅÏÈÕ¼Óô¦Àí»ú£¬±¾ÊµÑé²ÉÓÃÓÅÏȼ¶£»
£¨3£©£¬½ø³Ìµ÷¶Èʱ£¬¹æ¶¨Èô¾ÍÐ÷¶ÓÁнø³ÌÊýÉÙÓÚ6¸ö£¬Ôò×Ô¶¯´Óºó±¸¶ÓÁе÷ÈëÒ»¸ö×÷Òµ£»
£¨4£©£¬ÏµÍ³»á½«Õ¼Óн϶à×ÊÔ´¡¢Ô¤ÆÚ½á¹û²»·ûºÏÒªÇóµÄ½ø³Ì×Ô¶¯¹ÒÆð£¬²¢»ØÊÕËùÕ¼×ÊÔ´£¬¶ø±¾ÊµÑéÉèÖÃΪÊÖ¶¯¹ÒÆð£»
£¨5£©£¬ÔÚÊÊÒËÌõ¼þÏ£¬ÏµÍ³»á½«¹ÒÆðµÄ½ø³Ì×Ô¶¯½â¹Ò£¬¶øÇÒÖ»½â¹Òµ½¾ÍÐ÷¶ÓÁУ»±¾ÊµÑéΪ¼ò»¯²Ù×÷£¬ÉèÖÃΪÊÖ¶¯½â¹Ò£¬Èô½â¹ÒÌõ¼þºÏÊÊ£¨¼´CPU¸÷ÖÖ×ÊÔ´¿ÉÓã©£¬Ôò½â¹Òµ½¾ÍÐ÷¶ÓÁУ¬²¢·ÖÅäÄڴ棻Èô½â¹ÒÌõ¼þ²»ÊÊÒË£¬Ôò½â¹ÒÖÁºó±¸¶ÓÁУ¬µ«²»·ÖÅäÄڴ棨ʵ¼ÊÉÏÕâÊDz»¶ÔµÄ£¬ÒòΪ×÷Òµ½øÈëÄڴ棬ÓÉCPU±ê¼ÇPCBºó£¬²»Äܳ·ÏúPCBÔÙ·µ»ØÄڴ棬³ý·Ç¸Ã½ø³ÌÖ´ÐнáÊø£¬µ«±¾³ÌÐòΪÌåÏÖ½â¹ÒµÄÒâ˼£¬»¹ÊÇ´íÎóµØÉè¼ÆÎª¿ÉÒÔ½â¹Òµ½ºó±¸¶ÓÁУ¬¶ÁÕßÐè×¢Ò⣬Õâ¸ö¹¦ÄÜ¿ÉÒÔÔÚ´úÂëÖÐ×¢Ïú£¬ÁíÍâҲϣÍûÓиßÊÖ¿ÉÒԸĽø£©£»
£¨b£©£¬ÊµÑé³ÌÐòÉè¼Æ£º
£¨1£©£¬±¾ÊµÑé²ÉÓÃjavaÓïÑÔ±à³Ì£¬²¢ÊµÏÖGUI½çÃæÏÔʾ£»
£¨a£©£¬²Ù×÷ϵͳ֪ʶ»Ø¹Ë£º
£¨2£©£¬ÎªÌåÏÖjavaÓïÑÔÃæ¶Ô¶ÔÏó³ÌÐòÉè¼ÆµÄÌØµã£¬ÊµÑéÉè¼ÆÎªProcessPCB¡¢MemoryItemÀà·â×°PCBºÍËù·ÖÅäµÄÄÚ´æ¸÷×ÔµÄÊôÐÔÓë·½·¨£»ÓÃProcessRecords¡¢MemoryRecordsÀà·â×°Êý×é·½·¨£»ÓÃSingleCPUSchedulingʵÏÖGUI½çÃæÏÔʾ£» £¨3£©£¬ProcessPCBÀàÖУ¬¶¨ÒåPCBµÄ½ø³ÌÃû¡¢ÒªÇóÔËÐÐʱ¼ä¡¢ÓÅÏȼ¶¡¢×´Ì¬¡¢Ö÷´æÆðʼλÖá¢ËùÐèÄÚ´æ´óСÕâ6¸öÊôÐÔ£¬²¢¶¨Òå¸÷ÊôÐÔµÄgetºÍset·½·¨£¬¶¨Òåequals·½·¨ÓÃÓÚ¶Ô±ÈÀàµÄÊôÐÔ£¬¶¨ÒåtoString·½·¨µÃµ½ÀàÊôÐÔµÄ×Ö·û´®£¬¶¨Òårun·½·¨·â×°ÓÅÏÈȨ-1/ ÒªÇóÔËÐÐʱ¼ä-1µÄ¹ý³Ì£»MemoryItemÀàÖУ¬¶¨Òå¿É·Ö·ÖÇø±íÿһ¿É·Ö¼Ç¼µÄÖ÷´æÆðʼλÖá¢ÄÚ´æ´óС¼°ÆägetºÍset·½·¨£¬¶¨ÒåtoString·½·¨µÃµ½¿ÉÔÚ½çÃæÏÔʾµÄ×Ö·û´®£»
£¨4£©£¬ProcessRecords·â×°PCBÊý×éµÄÌí¼ÓÔªËØaddItemºÍɾ³ýÔªËØremoveItem·½·¨£¬²¢¹¹Ô캯ÊýgetItemͨ¹ý²ÎÊýProcessPCBºÍString²éÕÒÊý×éÔªËØ£¬¶¨ÒågetNumberOfItemsÈ¡Êý×é´óС£¬¶¨ÒågetItemsPriorities·½·¨È¡ËùÓÐÊý×éÔªËØµÄtoString
·½·¨ÓÃÓÚ½çÃæÏÔʾ£¬¶¨Òåiterator·½·¨È¡µÃÊý×éµÄµü´úÆ÷£»
MemoryItemÀàÖУ¬¶¨ÒåMemoryµÄÄÚ´æÆðʼλÖúÍËùÐèÄÚ´æ´óС¡¢¡¢¡¢¡¢¡¢¡¢
£¨5£©£¬MemoryRecordsÓÃͬÑùµÄÉè¼ÆË¼Ïë·â×°ÒÔMemoryItemΪÊý×éÔªËØµÄ¸÷ÊôÐԺͷ½·¨£»
£¨6£©SingleCPUSchedulingÀà¼Ì³ÐJFrameÀ࣬ʵÏÖ½çÃæ»¯ÏÔʾ£»ÓëÉÏÃæÏà¶ÔÓ¦£¬ÊµÀý»¯ProcessRecords£¨3´Î£©ºÍMemoryRecords£¨1´Î£©×÷Ϊ˽ÓбäÁ¿£¬·Ö±ð×÷Ϊºó±¸¶ÓÁС¢¾ÍÐ÷¶ÓÁС¢¹ÒÆð¶ÓÁкÍÄÚ´æ¿É·Ö·ÖÇø±í£»ÔÚ½çÃæÉè¼ÆÖУ¬Éè¼Æºó±¸¶ÓÁС¢¹ÒÆð¶ÓÁУ¨¸½´ø½â¹Òumount°´Å¥£©¡¢¾ÍÐ÷¶ÓÁУ¨¸½´ø¹ÒÆðsuspend°´Å¥£©¿É·Ö·ÖÇø±íÁбíÏÔʾ¿ò£¬ÉèÖÃPCBÌí¼Ó¿ò£¬¸½´øÌí¼ÓÖÁºó±¸¶ÓÁУ¨addToBackup£©¡¢Ìí¼ÓÖÁ¾ÍÐ÷¶ÓÁУ¨addToReady£©°´Å¥£¬ÒÔ¼°CPUµ±Ç°Ö´ÐÐ״̬ÏÔʾ¿ò¡¢ÏµÍ³ÈÕÖ¾ÏÔʾ¿ò£¬ºÍ¿ªÊ¼µ÷¶È£¨systemStart£©°´Å¥£¬ÓÅÏȼ¶ºÍʱ¼äƬµ¥Ñ¡°´Å¥£¬ÒÔ¼°Ê±¼äƬÏÔʾ±êÇ©ºÍÎı¾±à¼¿ò£» £¨7£©½çÃæÉè¼ÆÏê½â£»ºó±¸¶ÓÁÐÏÔʾ¿òÓÃÓÚÏÔʾÒÑÌí¼ÓÖÁºó±¸¶ÓÁеÄProcessRecordsÊôÐÔÐÅÏ¢£¬ÆäÖÐÖ÷´æÆðʼλÖÃĬÈÏΪ-1£¬±íʾδ·ÖÅ䣻¹ÒÆð¶ÓÁÐÏÔʾ¿òÓÃÓÚÏÔʾ´Ó¾ÍÐ÷¶ÓÁÐ¹ÒÆðµÄPCB£¬ÆäÖÐÊôÐÔ¡°Ö÷´æÆðʼλÖá±£¨MemoryBase£©½«ÓɷǸºÊý±äΪ-1£¬±íʾ¹ÒÆðºóÊÕ»ØÄڴ棻¾ÍÐ÷¶ÓÁÐÏÔʾ¿òÖÐÏÔʾ¾ÍÐ÷¶ÓÁÐÊôÐÔ£¬ÆäÖС°Ö÷´æÆðʼλÖá±¾ùΪ·Ç¸º£¬±íʾһ·ÖÅäÄڴ棻PCBÐÅÏ¢Ìí¼Ó¿ò·ÖÁÐPCB6¸öÊôÐÔÏÔʾ±êÇ©ºÍ¿É±à¼Îı¾¿ò£¬ºÍÌí¼Ó°´Å¥£¬ÓÃÓÚÌí¼ÓPCB£»ÏµÍ³ÈÕÖ¾ÏÔʾ¿ò¸½Êôʱ¼äƬÏÔʾ±êÇ©ºÍ¿É±à¼Îı¾±à¼¿ò£¬¿ÉÓÉÓû§¾ö¶¨Ê±¼äƬ´óС£»
¶ÔÓÚʵÑéÒ»£¬½çÃæÈçÏ£º
ÒÔÉÏÓÉSingleCPUScheduling001.java£¨ÁíÐèProcessPCB.javaºÍPCBRdecords.java£© £¨8£©¸½Êô¹¦ÄÜÌí¼ÓÍêÉÆ£»×îÖØÒªµÄÊÇΪ³ÌÐòÌí¼ÓỊ̈߳¬Ê¹³ÌÐòÄÜÒÔÍ£¶ÙÒ»¶Îʱ¼äµÄƵÂÊ×Ô¶¯ÔËÐУ»ºó±¸¶ÓÁС¢¹ÒÆð¶ÓÁÐÌí¼ÓtotalÏÔʾ±êÇ©ºÍ²»¿É±à¼Îı¾ÏÔʾ¿ò£¬ÓÃÓÚÏÔʾ¸÷×ÔÊý×éÖÐÔªËØÊýÄ¿£¬¹ÒÆð¶ÓÁи½Êôɾ³ý£¨remove£©°´Å¥£¬¿Éɾ³ý¹ÒÆð¶ÓÁÐÖеÄÔªËØ£»ºó±¸¡¢¹ÒÆð¡¢¾ÍÐ÷¶ÓÁоùÌí¼Ó¼àÌýÆ÷£¬ÓÃÓÚÏìÓ¦Óû§µ¥»÷²Ù×÷£¬¿ÉÒÔÔÚPCBÐÅÏ¢Ìí¼Ó¿òÏÔʾÓû§µ¥»÷µÄÄÇÒ»ÌõPCBµÄÐÅÏ¢£»PCBÐÅÏ¢Ìí¼Ó¿ò¸½Êôreset°´Å¥£¬ÓÃÓÚÒ»¼üÇå¿ÕÐÅÏ¢¿òÖÐÐÅÏ¢£¬·½±ãÊäÈ룻ϵͳÈÕÖ¾Ãæ°å¸½ÊôϵͳÔÝÍ££¨systemPause£©ºÍÏµÍ³ÖØÖã¨systemReset£©°´Å¥£¬·Ö±ðÓÃÓÚÔÝÍ£ÔËÐУ¨·½±ãÓû§¹Û²ìµ±Ç°ÔËÐнá¹û£©ºÍÖØÖÃϵͳ£¨·½±ãÓû§Öظ´Ê¹ÓóÌÐò£¬ÃâÈ¥¹Ø±ÕºóÖØÆô±¾³ÌÐòµÄÂé·³£©£»×îÖÕ½çÃæÈçͼ£º
ʵÑé½á¹û±¨¸æ¼¶·ÖÎö
1£¬ 2£¬
³ÌÐòÍê³ÉÁËʵÑéËùÓеĻù±¾ÒªÇó£»
±¾³ÌÐò»¹´æÔÚһЩ¼¼ÊõÉϵÄÎÊÌ⣬ʹµÃ³ÌÐò²»Äܾ¡Éƾ¡ÃÀ£»È磬PCBÐÅÏ¢Ìí¼Ó¿òûÓС°Ëæ»úÖÃÈë¾ÍÐ÷¶ÓÁС±¹¦ÄÜ£¬Ìí¼ÓPCBÐÅÏ¢ÈÔÏԵ÷±Ëö£»¾ÍÐ÷¶ÓÁÐµÄ¹ÒÆð¹¦ÄÜÔÚ³ÌÐò×Ô¶¯ÔËÐÐʱ£¬´æÔÚ·´Ó¦Òì³££¨·´Ó¦ÑÓ³Ù»òÖ±½ÓÎÞ·´Ó³£©£»¿É·Ö·ÖÇø±íÖ»ÏÔʾÁ˵±Ç°¿É·ÖµÄÄڴ棬ûÓÐÏÔʾÒÑ·ÖµÄPCB¼°Æä¶ÔÓ¦ÄÚ´æÊ¹ÓÃÇé¿ö£¬ÇÒûÓÐÀûÓÃͼÐκͷḻµÄÑÕÉ«À´¸üºÃµÄչʾ£»Ê±¼äƬÉè¼Æ»¹ÐèÒª¸Ä½ø£¬Ê¹ÓÃЧÂʲ»¸ß£»ÏµÍ³ÖØÖù¦ÄÜ´æÔÚÏìÓ¦ÑÓ³ÙµÄÎÊÌ⣻ÁíÍ⣬½çÃæ²»¹»ÃÀ¹Û£»»¹ÐèÒª²»¶Ï¸Ä½ø£» ʵÑé¸ÐÏë
ͨ¹ýÕâ´ÎʵÑ飬ÎÒ¶Ô²Ù×÷ϵͳµÄ½ø³Ìµ÷¶ÈºÍÄÚ´æ·ÖÅä¹ÜÀíÓÐÁ˸ü¼ÓÉîÈëµÄÁ˽⣬¶Ô²Ù×÷ϵͳÄÚ²¿µÄ¹¤×÷ÔÀíÓÐÁ˽øÒ»²½µÄÈÏʶ£»
ͨ¹ý±à³Ì£¬Ò²¹®¹ÌÁËÎҵijÌÐòÉè¼ÆºÍ´úÂë±àдµÄÄÜÁ¦£¬ÊµÑé¹ý³ÌÖÐÓöµ½µÄ¸÷ÖÖÎÊÌâÒÔ¼°½â¾öÎÊÌâµÄ¹ý³ÌÓë·½·¨£¬¶¼ÊÇÎÒ»ñÒæ·Ëdz£»
ͬʱ£¬³ÌÐòµÄ²»ÍêÉÆ£¬Ò²½«´ÙʹÎÒÔڿγÌÖ®ºó£¬¼ÌÐøÑ§Ï°¡¢Àí½â¿Î³ÌÄÚÈÝ£¬²¢¾¡Ò»ÇÐŬÁ¦²»¶ÏÍêÉÆ³ÌÐò£¬×öµ½¾¡Éƾ¡ÃÀ£»
³ÌÐò´úÂë
ÍêÕû°æ£¨³õѧjava£¬²ËÄñ¼¶±ð£¬µ±È»Êǽ«ËùѧµÄÈ«²¿ÕÕ°ìÕÕ³£¬ÊµÎªÓ·Ö×£¬¿ÉΪ³õѧÕßÒýÒÔΪ½ä£¬×¢Òâ´úÂëÖÊÁ¿£¡£©
ÕâÀï½÷Ìù³öÊ®·ÖÓ·Ö׵ĴúÂ룬½ö¹©³õѧÕß½»Á÷¾Ñ飬֨ÔÚ¿ª·¢µÄ˼Ï룬Á˽⿪·¢µÄÁ÷³Ì£¬¶ø01°æ£¨¾«¼ò°æ£©´úÂëÔÚºóÃæ£» ProcessPCB.java
package src;
public class ProcessPCB {
// backupBAK ºó±¸ ready ¾ÍÐ÷ suspend ¹ÒÆð memoryÄÚ´æ private String PID; private int RequiredTime; // private String Priority; private int Priority; private String Status;
private int MwmoryBase = 0000; private int MemoryLimit;
// private String PCBPointer;
public ProcessPCB(String initpID, int initRTime, int initpriority, String status, int initBase, int initLimit) {
this.PID = initpID;
this.RequiredTime = initRTime; this.Priority = initpriority; this.Status = status; this.MwmoryBase = initBase; this.MemoryLimit = initLimit;
}
public String getPID() { if(this.PID == null)
return \ else
return this.PID;
}
public void setPID(String pid) { if(pid == null)
this.PID = \ else
this.PID = pid;
}
public int getRequiredTime() { return this.RequiredTime; }
public void setRequiredTime(int time) { this.RequiredTime = time; }
public int getPriority() { return this.Priority; }
public void setPriority(int priority) { this.Priority = priority; }
public String getStatus() { }
public void setStatues(String statues) { }
public int getMemoryBase() { }
public void setMemoryBase(int base) { }
public int getMemoryLimit() { }
public void setMemoryLimit(int limit) { }
public boolean equals(ProcessPCB pcb) { }
public String toString() {
return this.getPID() + \ + this.getRequiredTime() + \ + this.getPriority() + \ + this.getStatus() + \ + this.getMemoryBase() + \ + this.getMemoryLimit()
if(pcb.getPID() == this.getPID()) { }
else return false;
return true;
this.MemoryLimit = limit; return this.MemoryLimit; this.MwmoryBase = base; return this.MwmoryBase; if(statues == null)
this.Status = \; if(this.Status == null)
return \;
else
return this.Status;
else
this.Status = statues;
+ \; }
public void run() { } }
this.RequiredTime = this.RequiredTime-1; this.Priority = this.Priority-1;
MemoryItem.java
package src;
public class MemoryItem { private int memoryBase=0; private int memoryLimit=0; private int availableStatus=0;
public MemoryItem(int initMemoryBase, int initMemoryLimit) { }
public int getMemoryBase() { }
public void setMemoryBase(int base) { }
public int getMemoryLimit() { }
public void setMemoryLimit(int limit) { }
public int getStatus() { }
public void setStatus(int status) { }
public String toString() {
return this.getMemoryBase() + \ + this.getMemoryLimit() + \; this.memoryBase = status; return this.availableStatus; this.memoryLimit = limit; return this.memoryLimit; this.memoryBase = base; return this.memoryBase;
this.memoryBase = initMemoryBase; this.memoryLimit = initMemoryLimit;
} }
PCBRecords.java package src;
import java.util.ArrayList; import java.util.Iterator;
public class PCBRecords implements Iterable
private ArrayList
public ArrayList
public PCBRecords() { this.PCBItems = new ArrayList
public void addItem(ProcessPCB PcbItem) { this.PCBItems.add(PcbItem); }
public void removeItem(ProcessPCB PCbItem) { this.PCBItems.remove(PCbItem); }
public ProcessPCB getItem(ProcessPCB processPCB) { for (ProcessPCB pCbItem : this.PCBItems) { if (pCbItem.equals(processPCB)) { return pCbItem; } } return null; }
public ProcessPCB getItem(String pid) { for (ProcessPCB pcBItem : this.PCBItems) { if (pcBItem.getPID().equals(pid)) { return pcBItem; } } return null; }
public int getNumberOfItems() { return this.PCBItems.size(); }
public String[] getItemsProperties() { String itemsProperties[] = new String[getNumberOfItems()]; int i = 0;
for(Iterator iterator1 = PCBItems.iterator(); iterator1.hasNext();)
{
ProcessPCB stu_Item = (ProcessPCB)iterator1.next(); itemsProperties[i++] = stu_Item.toString(); }
return itemsProperties; }
public Iterator
MemoryRecords.java package src;
import java.util.ArrayList; import java.util.Iterator;
public class MemoryRecords implements Iterable
public Iterator
public ArrayList
public MemoryRecords() {
this.memoryItems = new ArrayList
public void addItem(MemoryItem newMemoryItem) { this.memoryItems.add(newMemoryItem); }
public void removeItem(MemoryItem momoryItem) { this.memoryItems.remove(momoryItem); }
public MemoryItem getMomoryItem(MemoryItem item) { for(MemoryItem mItem : this.memoryItems) { if(mItem.equals(item)) { return mItem; } }
return null; }
public MemoryItem getMemoryItem(int base) { for(MemoryItem mItem : this.memoryItems) { if(mItem.getMemoryBase() == base) { return mItem; } }
return null;
}
public int getNumberOfItems() { return this.memoryItems.size(); }
public String[] getItemsProperties() {
String itemsProperties[] = new String[getNumberOfItems()]; int i=0;
for(Iterator iterator1 = this.memoryItems.iterator(); iterator1.hasNext(); ) { MemoryItem mmItem = (MemoryItem) iterator1.next(); itemsProperties[i++] = mmItem.toString();//???? }
//System.out.println(itemsProperties + \ if(itemsProperties == null) { itemsProperties[0] = \ \ }
return itemsProperties; } }
SingleCPUSchedulingGUI001.Java
import java.util.*; import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import src.SingleCPUSchedulingGUI001.AddToBAKListener; import src.SingleCPUSchedulingGUI001.AddToReadyListener; import src.SingleCPUSchedulingGUI001.RemoveListener; import src.SingleCPUSchedulingGUI001.ResetListener;
import src.SingleCPUSchedulingGUI001.ResetSystemListener; import src.SingleCPUSchedulingGUI001.StartSystemListener; import src.SingleCPUSchedulingGUI001.SuspendListener; import src.SingleCPUSchedulingGUI001.SystemPauseListener; import src.SingleCPUSchedulingGUI001.UmountListener; import src.SingleCPUSchedulingGUI001.priotiryListener; import src.SingleCPUSchedulingGUI001.timeslicListener;
import java.io.*; import java.text.*;
public class SingleCPUSchedulingGUI001 extends JFrame {
private int systemStatues; //×¢Òâstatic ÊôÐÔ
/*define 0--system prepare status--system reset and re-prepare 1--system start 2--system pause 3--system stop*/
/* Standar error stream */
static private PrintWriter stdErr = new PrintWriter(System.err, true);
static private int WIDTH = 600, HEIGHT = 700; // the size of the Frame Ö÷Ãæ°å
/* ¸÷Áбí¶ÔÓ¦µÄÃæ°å¹æ¸ñ*/
/* ¶ÔÓ¦¸÷Ãû´ÊÊÍÒå backupBAK ºó±¸ ready ¾ÍÐ÷ suspend ¹ÒÆð memoryÄÚ´æ */ static private int BackupBAK_CELL_SIZE = 250, BackupBAK_LIST_ROWS = 10; //ºó±¸¶ÓÁÐ static private int Suspend_CELL_SIZE = 250, Suspend_LIST_ROWS = 10; //¹ÒÆð¶ÓÁÐ
static private int Ready_CELL_SIZE = 200, Ready_LIST_ROWS = 6; //¾ÍÐ÷¶ÓÁÐ static private int CPU_ROWS = 10, CPU_COLS = 22; //CPUÃæ°å static private int STATUS_ROWS = 8, STATUS_COLS = 30; //ÏµÍ³×´Ì¬Ãæ°å private int timeslice = 1; //ÉèÖÃʱ¼äƬ´óС
private int systemStatus=0; //ÉèÖÃϵͳ״̬ 0¡ª¡ªÏµÍ³Ô¤±¸×´Ì¬£¬µÈ´ý¿ªÊ¼£¬1¡ª¡ªÏµÍ³ÔËÐÐ״̬£¬2¡ª¡ªÏµÍ³ÔÝͣ״̬
static private int TOTAL__TEXTFIELD_SIZE = 10; // Size total text field ¼Ç¼¸÷¶ÓÁÐÔªËØ¸öÊý
private JList backupList, suspendList, readyList; //¸÷¶ÓÁÐÏà¶ÔÓ¦µÄÊý×éÁбí // ½ø³ÌÌí¼Ó¿òÖеÄ\Ìí¼ÓÖÁºó±¸¶ÓÁÐ\£¬\Ìí¼ÓÖÁ¾ÍÐ÷¶ÓÁÐ\£¬\ÖØÖÃ\ private JButton addToBAKButton, addToReadyButton, resetButton; //¾ÍÐ÷¶ÓÁпòÖеÄ\¹ÒÆð\£¬¹ÒÆð¶ÓÁпòÖеÄ\½â¹Ò\£¬\ɾ³ý\ private JButton suspendButton, umountButton, removeButton; //StatusÃæ°åÖеÄ\Æô¶¯ÏµÍ³\£¬\ÖØÖÃϵͳ\
private JButton startButton, pauseButton, resetSyatemButton; //ÓÅÏȼ¶ºÍʱ¼äƬµ¥Ñ¡Å¥¼°Ê±¼äƬÏÔʾ¿ò private JRadioButton priorityJRB, timesliceJRB; private JLabel timesliceSizeLabel; private JTextField timesliceJtf;
//ºó±¸Ãæ°å¡¢½ø³ÌÌí¼ÓÃæ°å¡¢¹ÒÆðÃæ°å¡¢ÄÚ´æÃæ°å
private JPanel backupBAKPanel, PCBItemPanel, suspendedPanel; //ºó±¸¶ÓÁС¢¹ÒÆð¶ÓÁÐÔªËØ×ÜÊý±êÇ© private JLabel backupTotalLabel, suspendTotalLabel;
//½ø³ÌÐÅÏ¢±êÇ© ½ø³Ì±àºÅPID,ËùÐèÔËÐÐʱ¼ärequiredTime,ÓÅÏȼ¶priority,µ±Ç°×´Ì¬statues,ÄÚ´æÖеĻùÖ·base,ËùÐèÄÚ´æ´óСlimit
private JLabel PIDLabel, requiredTimeLabel, priorityLabel, statuesLabel; //ºó±¸¶ÓÁС¢¹ÒÆð¶ÓÁÐÔªËØ×ÜÊýÎı¾¿ò£¨²»¿É±à¼£© private JTextField backupTotalTextField, suspendTotalTextField;
//½ø³ÌÐÅÏ¢Îı¾¿ò PID(¿É±à¼),requiredTime(¿É±à¼),priority(¿É±à¼),status(²»¿É±à¼),base(²»¿É±à¼),limit(¿É±à¼)
private JTextField PIDTextField, requiredTimeTextField, priorityTextField, statusTextField;
//CPU״̬ÏÔʾÎı¾Óò(²»¿É±à¼)£¬statusÐÅÏ¢Îı¾Óò(ÓÃÓÚÏÖʵ³ÌÐòÿһ²½µÄ²Ù×÷ºÍÓ°Ï죬²»¿É±à¼)
private JTextArea CPUTextArea, statuesTextArea;
//ºó±¸¶ÓÁÐPCBÊý×é,¾ÍÐ÷¡¢¹ÒÆð£¬¡ª¡ªÄڴ棨¿É·Ö·ÖÇø±í£© PCBRecords backupPCB, readyPCB, suspendedPCB;
public static void main(String[] args) throws IOException { // TODO Auto-generated method stub new SingleCPUSchedulingGUI001().initFrame(); }
public void initFrame() { backupList = new JList(); backupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); backupList.setVisibleRowCount(BackupBAK_LIST_ROWS); backupList.setFixedCellWidth(BackupBAK_CELL_SIZE); suspendList = new JList(); suspendList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); suspendList.setVisibleRowCount(Suspend_LIST_ROWS); suspendList.setFixedCellWidth(Suspend_CELL_SIZE); readyList = new JList(); readyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); readyList.setVisibleRowCount(Ready_LIST_ROWS); readyList.setFixedCellWidth(Ready_CELL_SIZE); suspendButton = new JButton(\
addToBAKButton = new JButton(\ addToReadyButton = new JButton(\ resetButton = new JButton(\ umountButton = new JButton(\ removeButton = new JButton(\ startButton = new JButton(\ pauseButton = new JButton(\ resetSyatemButton = new JButton(\ priorityJRB = new JRadioButton(\ timesliceJRB = new JRadioButton(\ backupTotalLabel = new JLabel(\ backupTotalTextField = new JTextField(\ backupTotalTextField.setEditable(false); suspendTotalLabel = new JLabel(\ suspendTotalTextField = new JTextField(\ suspendTotalTextField.setEditable(false); timesliceSizeLabel = new JLabel(\ timesliceJtf = new JTextField(\ timesliceJtf.setEditable(true); CPUTextArea = new JTextArea(CPU_ROWS, CPU_COLS); CPUTextArea.setEditable(false); statuesTextArea = new JTextArea(STATUS_ROWS, STATUS_COLS); statuesTextArea.setEditable(false);
/* north panel*/
// JPanel northPanel = new JPanel(new BorderLayout()); JPanel northPanel = new JPanel(new GridLayout(1, 3)); // JPanel north = new JPanel(new BorderLayout());
// ProcessPCB item information Panel PCBItemPanel = new JPanel(new BorderLayout()); PCBItemPanel.setBorder( BorderFactory.createTitledBorder(\ JPanel PCBItemButtonJPanel = new JPanel(new GridLayout(3, 1)); PCBItemButtonJPanel.add(addToBAKButton); PCBItemButtonJPanel.add(addToReadyButton); PCBItemButtonJPanel.add(resetButton); PCBItemPanel.add(this.initPCBItemPanel(), BorderLayout.CENTER); PCBItemPanel.add(PCBItemButtonJPanel, BorderLayout.SOUTH);
//backupBAKList Panel backupBAKPanel = new JPanel(new BorderLayout()); backupBAKPanel.setBorder(BorderFactory.createTitledBorder(\
JPanel backupTotalPAnel = new JPanel(); backupTotalPAnel.add(backupTotalLabel); backupTotalPAnel.add(backupTotalTextField); backupBAKPanel.add ( new JScrollPane(backupList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
backupBAKPanel.add(backupTotalPAnel, BorderLayout.SOUTH);
// north.add(backupBAKPanel, BorderLayout.WEST); // north.add(PCBItemPanel, BorderLayout.CENTER);
// SuspendList Panel
suspendedPanel = new JPanel(new BorderLayout());
suspendedPanel.setBorder(BorderFactory.createTitledBorder(\
JPanel suspendedTotalPAnel = new JPanel(); suspendedTotalPAnel.add(suspendTotalLabel); suspendedTotalPAnel.add(suspendTotalTextField);
JPanel suspendComponentPanel = new JPanel(new GridLayout(1, 2)); suspendComponentPanel.add(umountButton); suspendComponentPanel.add(removeButton);
suspendedPanel.add (suspendedTotalPAnel, BorderLayout.NORTH); suspendedPanel.add ( new JScrollPane(suspendList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
suspendedPanel.add(suspendComponentPanel, BorderLayout.SOUTH);
// northPanel.add(north, BorderLayout.CENTER);
// northPanel.add(suspendedPanel, BorderLayout.EAST); northPanel.add(backupBAKPanel); northPanel.add(PCBItemPanel); northPanel.add(suspendedPanel); /* center Panel*/
JPanel centrelPanel = new JPanel(new BorderLayout()); // JPanel centrelPanel = new JPanel(new GridLayout(1, 3));
// readyList panel
JPanel readyListPanel = new JPanel(new BorderLayout());
readyListPanel.setBorder(BorderFactory.createTitledBorder(\ readyListPanel.add ( new JScrollPane(readyList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));//, BorderLayout.CENTER
readyListPanel.add (suspendButton, BorderLayout.SOUTH); // CPU panel
JPanel CPUPanel = new JPanel();
CPUPanel.setBorder(BorderFactory.createTitledBorder(\ CPUPanel.add (new JScrollPane(CPUTextArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
centrelPanel.add(readyListPanel, BorderLayout.WEST); centrelPanel.add(CPUPanel, BorderLayout.CENTER);
/*statues panel*/
JPanel southPanel = new JPanel(new BorderLayout());
JPanel statuesPanel = new JPanel();
statuesPanel.setBorder(BorderFactory.createTitledBorder(\ statuesPanel.add (new JScrollPane(statuesTextArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)); //, BorderLayout.CENTER //statuesPanel.setSize(400, 100);
//statuesPanel.setPreferredSize(new Dimension(400, 10));
JPanel systemContralButtonPanel = new JPanel(new GridLayout(6, 1));
systemContralButtonPanel.setBorder(BorderFactory.createTitledBorder(\ ButtonGroup group = new ButtonGroup(); group.add(priorityJRB); group.add(timesliceJRB);
JPanel porityPanel = new JPanel(new GridLayout(1, 2)); porityPanel.add(timesliceSizeLabel); porityPanel.add(timesliceJtf);
systemContralButtonPanel.add(priorityJRB); systemContralButtonPanel.add(timesliceJRB); systemContralButtonPanel.add(porityPanel); systemContralButtonPanel.add(startButton); systemContralButtonPanel.add(pauseButton);
systemContralButtonPanel.add(resetSyatemButton);
southPanel.add(statuesPanel, BorderLayout.CENTER);
southPanel.add(systemContralButtonPanel, BorderLayout.EAST); // arrange panels in window setLayout(new BorderLayout()); add(northPanel, BorderLayout.NORTH); add(centrelPanel, BorderLayout.CENTER); add(southPanel, BorderLayout.SOUTH); //statuesPanel // start listening for list and buttons events addToBAKButton.addActionListener(new AddToBAKListener()); addToReadyButton.addActionListener(new AddToReadyListener()); resetButton.addActionListener(new ResetListener()); suspendButton.addActionListener(new SuspendListener()); umountButton.addActionListener(new UmountListener()); removeButton.addActionListener(new RemoveListener()); startButton.addActionListener(new StartSystemListener()); pauseButton.addActionListener(new SystemPauseListener()); resetSyatemButton.addActionListener(new ResetSystemListener()); priorityJRB.addActionListener(new priotiryListener()); timesliceJRB.addActionListener(new timeslicListener()); backupPCB = new PCBRecords(); readyPCB = new PCBRecords(); suspendedPCB = new PCBRecords(); backupList.setListData(backupPCB.getItemsProperties()); readyList.setListData(readyPCB.getItemsProperties()); suspendList.setListData(suspendedPCB.getItemsProperties()); this.setTitle(\ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(WIDTH, HEIGHT); this.setResizable(true); this.setVisible(true); this.setLocation(200, 10); }
public JPanel initPCBItemPanel() { JPanel iniPCBItemJPanel = new JPanel(new BorderLayout()); JPanel iniNamePanel = new JPanel(new GridLayout(4, 1)); JPanel iniValuePanel = new JPanel(new GridLayout(4, 1)); PIDLabel = new JLabel(\ requiredTimeLabel = new JLabel(\ priorityLabel = new JLabel(\ statuesLabel = new JLabel(\ iniNamePanel.add(PIDLabel); iniNamePanel.add(requiredTimeLabel); iniNamePanel.add(priorityLabel); iniNamePanel.add(statuesLabel); PIDTextField = new JTextField(\ PIDTextField.setEditable(true); requiredTimeTextField = new JTextField(\ requiredTimeTextField.setEditable(true); priorityTextField = new JTextField(\ priorityTextField.setEditable(true); statusTextField = new JTextField(\ statusTextField.setEditable(false); iniValuePanel.add(PIDTextField); iniValuePanel.add(requiredTimeTextField); iniValuePanel.add(priorityTextField); iniValuePanel.add(statusTextField); iniPCBItemJPanel.add(iniNamePanel, BorderLayout.WEST); iniPCBItemJPanel.add(iniValuePanel, BorderLayout.CENTER); return iniPCBItemJPanel; }
public int readInteger(String s) { int num = -1; try { num = Integer.parseInt(s); } catch(NumberFormatException numberformatexception) { statuesTextArea.append(\ num = -999; } return num; }
public void addToBackupList(String newID, int s1, int s2, String s) { ProcessPCB item = backupPCB.getItem(newID); if(item != null) { statuesTextArea.append(\ + \ while(item != null) { newID = s + newID; item = backupPCB.getItem(newID); } }
ProcessPCB newPCB = new ProcessPCB(newID, s1, s2, \ backupPCB.addItem(newPCB);
backupList.setListData(backupPCB.getItemsProperties());
backupTotalTextField.setText(Integer.toString(backupPCB.getNumberOfItems()));
}
public void addToReadyList(String nowID, int s1, int s2, String s) { ProcessPCB item = readyPCB.getItem(nowID); if(item != null) { statuesTextArea.append(\ + \ while(item != null) { nowID = s + nowID; item = backupPCB.getItem(nowID); } }
ProcessPCB newPCB = new ProcessPCB(nowID, s1, s2, \ readyPCB.addItem(newPCB); sortReadyPCB();
readyList.setListData(readyPCB.getItemsProperties()); }
class AddToBAKListener implements ActionListener { public void actionPerformed(ActionEvent event) { String newID = PIDTextField.getText(); String newTime = requiredTimeTextField.getText(); String newPriority = priorityTextField.getText(); int s1 = 0, s2 = 0, tag1=-1, tag2=-1; if(newTime != null) { s1 = readInteger(newTime); if(s1 > 0.0) tag1 = 1;
else statuesTextArea.append(\ } if(newPriority != null) { s2 = readInteger(newPriority); if(s1 != -999) tag2 = 1;
else statuesTextArea.append(\ } if(tag1 ==1 && tag2 == 1 ) { if(newID == null) { statuesTextArea.append(\ } else { addToBackupList(newID, s1, s2, \
statuesTextArea.append(\ reset(); } } } }
class AddToReadyListener implements ActionListener { public void actionPerformed(ActionEvent event) { String nowID = PIDTextField.getText(); String time = requiredTimeTextField.getText(); String priority = priorityTextField.getText(); int s1 = 0, s2 = 0; int tag1 = -1, tag2 = -1; if(time != null)
{ s1 = readInteger(time); if(s1 > 0.0) tag1=1;
else statuesTextArea.append(\ } if(priority != null) { s2 = readInteger(priority); if(s2 != -999) tag2=1;
else statuesTextArea.append(\ }
if(tag1 ==1 && tag2 == 1) { if(nowID == null) { statuesTextArea.append(\ } else { if(readyPCB.getNumberOfItems() < 6) { addToReadyList(nowID, s1, s2, \ statuesTextArea.append(\student record item has been added to ReadyList!\\n\ } else { statuesTextArea.append(\added to BackupList!\\n\ addToBackupList(nowID, s1, s2, \ statuesTextArea.append(\student record item has been added to BackupList!\\n\ } reset(); } } } }
public void reset() { PIDTextField.setText(\ requiredTimeTextField.setText(\ priorityTextField.setText(\ statusTextField.setText(\ } /**
* This inner class processes resetButton
events. */
class ResetListener implements ActionListener { public void actionPerformed(ActionEvent event) { reset(); } } /**
* This inner class processes suspendButton
events. */
//×¢£ºÔÚ¹ÒÆðʱ£¬²»»á´¥·¢½ø³Ìµ÷¶È£¬¶øÊÇÔÚµã»÷\ʱ²Å»á³ö·¢½ø³Ìµ÷¶È class SuspendListener implements ActionListener { /** * Removes an order item from the current order. * * @param event the event object.
*/ public void actionPerformed(ActionEvent event) { String selectedReadyItem = null; String pid = \ if(readyPCB.getNumberOfItems() == 0) { statuesTextArea.append(\ } else {
selectedReadyItem = (String) readyList.getSelectedValue(); if(selectedReadyItem == null) {
statuesTextArea.append(\ } else {
StringTokenizer stringtokenizer = new StringTokenizer(selectedReadyItem, \ pid = stringtokenizer.nextToken(); ProcessPCB selectedItem = readyPCB.getItem(pid); if(selectedItem == null) { statuesTextArea.append(\student recorditem of Num \+ pid + \is founded!\\n\
} else { ProcessPCB boolTtem = suspendedPCB.getItem(pid); if(boolTtem != null) { statuesTextArea.append(\PCB \+ pid + \has existed in Suspend List!\\n\ + \ while(boolTtem != null) { pid = \ boolTtem = suspendedPCB.getItem(pid); } } ProcessPCB newPcb = new ProcessPCB(pid, selectedItem.getRequiredTime(), selectedItem.getPriority(),\ suspendedPCB.addItem(newPcb); readyPCB.removeItem(selectedItem); sortReadyPCB(); //×¢Ò⿼ÂÇһϠsuspendList.setListData(suspendedPCB.getItemsProperties()); readyList.setListData(readyPCB.getItemsProperties());
statuesTextArea.append(\
suspendTotalTextField.setText(Integer.toString(suspendedPCB.getNumberOfItems())); }
} } } }
public void sortReadyPCB() { PCBRecords currentReadyPCB = new PCBRecords(); int num = readyPCB.getNumberOfItems(); if(num > 0) { for(int i=num; i>=1; i--) { Iterator readyIterator = readyPCB.iterator(); ProcessPCB currentItem = (ProcessPCB) readyIterator.next(); for( ; readyIterator.hasNext(); ) { ProcessPCB nowItem = (ProcessPCB) readyIterator.next(); if(currentItem.getPriority() < nowItem.getPriority()) { currentItem = null;
currentItem = nowItem; } } currentReadyPCB.addItem(currentItem); readyPCB.removeItem(currentItem); } readyPCB = null; readyPCB = currentReadyPCB; } } /**
* This inner class processes umountButton
events. */
class UmountListener implements ActionListener { /** * Removes an order item from the current order. * * @param event the event object. */ public void actionPerformed(ActionEvent event) { String selectedSuspendedItem = null; String id = \ if(suspendedPCB.getNumberOfItems() == 0) { statuesTextArea.append(\ } else {
selectedSuspendedItem = (String) suspendList.getSelectedValue(); if(selectedSuspendedItem == null) {
statuesTextArea.append(\ } else {
StringTokenizer stringtokenizer = new StringTokenizer(selectedSuspendedItem, \ id = stringtokenizer.nextToken(); ProcessPCB selectedItem = suspendedPCB.getItem(id); if(selectedItem == null) { statuesTextArea.append(\student recorditem of Num \+ id + \is founded!\\n\
} else { if(readyPCB.getNumberOfItems() < 6) { ProcessPCB selectedToItem = readyPCB.getItem(id); if(selectedToItem != null) { statuesTextArea.append(\The PID will be modify!\\n\ while(selectedToItem != null) { id = id+\ selectedToItem = readyPCB.getItem(id); } } ProcessPCB newToItemPCB = new ProcessPCB(id, selectedItem.getRequiredTime(), selectedItem.getPriority(), \ readyPCB.addItem(newToItemPCB); suspendedPCB.removeItem(selectedItem); suspendList.setListData(suspendedPCB.getItemsProperties());
suspendTotalTextField.setText(Integer.toString(suspendedPCB.getNumberOfItems())); sortReadyPCB(); //×¢Ò⿼ÂÇ readyList.setListData(readyPCB.getItemsProperties()); statuesTextArea.append(\product has been Umounted to Ready List!\\n\
} else { statuesTextArea.append(\The new ProcessPCB will be Umounted to BackupList!\\n\ ProcessPCB selectedToItem = backupPCB.getItem(id); if(selectedToItem != null) { statuesTextArea.append(\The PID will be modify!\\n\ while(selectedToItem != null) { id = id+\ selectedToItem = backupPCB.getItem(id); } } ProcessPCB newToItemPCB = new ProcessPCB(id, selectedItem.getRequiredTime(), selectedItem.getPriority(), \ backupPCB.addItem(newToItemPCB); suspendedPCB.removeItem(selectedItem); suspendList.setListData(suspendedPCB.getItemsProperties()); backupList.setListData(backupPCB.getItemsProperties());
suspendTotalTextField.setText(Integer.toString(suspendedPCB.getNumberOfItems())); statuesTextArea.append(\product has been Umounted to Backup List!\\n\
}
} } } } } /**
* This inner class processes removeButton
events. */
class RemoveListener implements ActionListener { public void actionPerformed(ActionEvent event) { String selectedRemovsItem = null; String id = \ if(suspendedPCB.getNumberOfItems() == 0) { statuesTextArea.append(\ } else {
selectedRemovsItem = (String) suspendList.getSelectedValue(); if(selectedRemovsItem == null) {
statuesTextArea.append(\ } else {
StringTokenizer stringtokenizer = new StringTokenizer(selectedRemovsItem, \ id = stringtokenizer.nextToken(); ProcessPCB suspendedItem = suspendedPCB.getItem(id); if(suspendedItem == null) { statuesTextArea.append(\student recorditem of Num \+ id + \is founded!\\n\
} else { suspendedPCB.removeItem(suspendedItem); suspendList.setListData(suspendedPCB.getItemsProperties()); statuesTextArea.append(\
suspendTotalTextField.setText(Integer.toString(suspendedPCB.getNumberOfItems())); } } } } }
public void PCBSchudling() { while(readyPCB.getNumberOfItems() < 6) { if(backupPCB.getNumberOfItems() > 0) { Iterator bwackupPCBIterator = backupPCB.iterator(); ProcessPCB newItem = (ProcessPCB) bwackupPCBIterator.next(); readyPCB.addItem(newItem); backupPCB.removeItem(newItem); //×¢Ò⿼ÂÇÒª²»ÒªÅÅÐò sortReadyPCB(); readyList.setListData(readyPCB.getItemsProperties()); backupList.setListData(backupPCB.getItemsProperties()); backupTotalTextField.setText(Integer.toString(backupPCB.getNumberOfItems())); } else break; } }
public void run() { PCBSchudling(); if(readyPCB.getNumberOfItems() > 0) { Iterator readyIterator = readyPCB.iterator(); ProcessPCB runningItem = (ProcessPCB) readyIterator.next(); if(runningItem.getRequiredTime()>0) { runningItem.run(); CPUTextArea.append(\ } if(runningItem.getRequiredTime() <= 0) { statuesTextArea.append(\the memory!\\n\ readyPCB.removeItem(runningItem); runningItem = null; //×¢Òâ PCBSchudling(); } sortReadyPCB(); readyList.setListData(readyPCB.getItemsProperties()); } else { this.systemStatues = 0; System.exit(0); } }
boolean getSystemStatues() { if(this.systemStatues == 1) return true;
else return false; }
class StartSystemListener implements ActionListener { public void actionPerformed(ActionEvent event) { systemStatues = 1; run(); } }
class SystemPauseListener implements ActionListener { public void actionPerformed(ActionEvent event) { systemStatues = 2; } }
class StopSystemListener implements ActionListener { public void actionPerformed(ActionEvent event) { systemStatues = 3; } }
class ResetSystemListener implements ActionListener { public void actionPerformed(ActionEvent event) { systemStatues = 0; } }
class priotiryListener implements ActionListener { public void actionPerformed(ActionEvent event) { timeslice = 1; } }
class timeslicListener implements ActionListener { public void actionPerformed(ActionEvent event) { timeslice = getTimesliceText(); if(timeslice <= 0) { statuesTextArea.append(\ timesliceJtf.setText(\ timeslice = 1; } } }
public int getTimesliceText() { int n; try { n = Integer.parseInt(timesliceJtf.getText()); } catch(NumberFormatException numberformatexception) { statuesTextArea.append(\ n = -999; } return n; } }
01°æ£¨¾«¼ò°æ£© MemoryItem.java package src;
public class MemoryItem { private int memoryBase=0; private int memoryLimit=0; private int availableStatus=0;
public MemoryItem(int initMemoryBase, int initMemoryLimit) { this.memoryBase = initMemoryBase; this.memoryLimit = initMemoryLimit; }
public int getMemoryBase() { return this.memoryBase; }
public int getMemoryLimit() { return this.memoryLimit; }
public int getStatus() { return this.availableStatus; }
public String toString() {
return this.getMemoryBase() + \ } }
ProcessPCB.java package src;
public class ProcessPCB {
// backupBAK ºó±¸ ready ¾ÍÐ÷ suspend ¹ÒÆð memoryÄÚ´æ private String PID;
private int RequiredTime; private int Priority; private String Status;
private int MwmoryBase = 0000; private int MemoryLimit; // private String PCBPointer;
public ProcessPCB(String initpID, int initRTime, int initpriority, String status, int initBase, int initLimit) { this.PID = initpID; this.RequiredTime = initRTime; this.Priority = initpriority; this.Status = status; this.MwmoryBase = initBase; this.MemoryLimit = initLimit; }
public String getPID() { if(this.PID == null) return \ else return this.PID; }
public int getRequiredTime() { return this.RequiredTime; }
public int getPriority() { return this.Priority; }
public String getStatus() { if(this.Status == null) return \ else return this.Status; }
public int getMemoryBase() { return this.MwmoryBase; }
public int getMemoryLimit() { return this.MemoryLimit; }
public boolean equals(ProcessPCB pcb) { if(pcb.getPID() == this.getPID()) { return true; } else return false; }
public String toString() { return this.getPID() + \ + this.getStatus() + \ }
public void run() { this.RequiredTime = this.RequiredTime-1; this.Priority = this.Priority-1; } }
PCBRecords.java package src;
import java.util.ArrayList; import java.util.Iterator;
public class PCBRecords implements Iterable
private ArrayList
public ArrayList
public PCBRecords() { this.PCBItems = new ArrayList
public void addItem(ProcessPCB PcbItem) { this.PCBItems.add(PcbItem); }
public void removeItem(ProcessPCB PCbItem) { this.PCBItems.remove(PCbItem); }
public ProcessPCB getItem(ProcessPCB processPCB) { for (ProcessPCB pCbItem : this.PCBItems) { if (pCbItem.equals(processPCB)) { return pCbItem; } } return null; }
public ProcessPCB getItem(String pid) { for (ProcessPCB pcBItem : this.PCBItems) { if (pcBItem.getPID().equals(pid)) { return pcBItem; } } return null; }
public int getNumberOfItems() { return this.PCBItems.size(); }
public String[] getItemsProperties() { String itemsProperties[] = new String[getNumberOfItems()]; int i = 0;
for(Iterator iterator1 = PCBItems.iterator(); iterator1.hasNext();) {
ProcessPCB stu_Item = (ProcessPCB)iterator1.next(); itemsProperties[i++] = stu_Item.toString(); }
return itemsProperties; }
public Iterator
MemoryRecords.java package src;
import java.util.ArrayList; import java.util.Iterator;
public class MemoryRecords implements Iterable
public Iterator
public ArrayList
public MemoryRecords() {
this.memoryItems = new ArrayList
public void addItem(MemoryItem newMemoryItem) { this.memoryItems.add(newMemoryItem); }
public void removeItem(MemoryItem momoryItem) { this.memoryItems.remove(momoryItem); }
public MemoryItem getMomoryItem(MemoryItem item) { for(MemoryItem mItem : this.memoryItems) { if(mItem.equals(item)) { return mItem; } }
return null; }
public MemoryItem getMemoryItem(int base) { for(MemoryItem mItem : this.memoryItems) { if(mItem.getMemoryBase() == base) { return mItem; } }
return null; }
public int getNumberOfItems() { return this.memoryItems.size(); }
public String[] getItemsProperties() {
String itemsProperties[] = new String[getNumberOfItems()]; int i=0;
for(Iterator iterator1 = this.memoryItems.iterator(); iterator1.hasNext(); ) { MemoryItem mmItem = (MemoryItem) iterator1.next(); itemsProperties[i++] = mmItem.toString(); }
if(itemsProperties == null) { itemsProperties[0] = \ \ }
return itemsProperties; } }
SingleCPUSchedulingGUI03.java package src;
import java.util.*; import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import src.SingleCPUSchedulingGUI03.AddToBAKListener; import src.SingleCPUSchedulingGUI03.AddToReadyListener;
import src.SingleCPUSchedulingGUI03.DisplayBackupProcessListener; import src.SingleCPUSchedulingGUI03.DisplaySuspendedProcessListener; import src.SingleCPUSchedulingGUI03.DisplayreadyProcessListener; import src.SingleCPUSchedulingGUI03.RemoveListener; import src.SingleCPUSchedulingGUI03.ResetListener;
import src.SingleCPUSchedulingGUI03.ResetSystemListener; import src.SingleCPUSchedulingGUI03.StartSystemListener; import src.SingleCPUSchedulingGUI03.SuspendListener; import src.SingleCPUSchedulingGUI03.UmountListener; //import src.myframe.MyWindowMonitor;
import java.io.*; import java.text.*;
public class SingleCPUSchedulingGUI03 extends JFrame { /* Standar error stream */
static private PrintWriter stdErr = new PrintWriter(System.err, true);
static private int WIDTH = 800, HEIGHT = 750; // the size of the Frame Ö÷Ãæ°å
/* ¸÷Áбí¶ÔÓ¦µÄÃæ°å¹æ¸ñ*/
/* ¶ÔÓ¦¸÷Ãû´ÊÊÍÒå backupBAK ºó±¸ ready ¾ÍÐ÷ suspend ¹ÒÆð memoryÄÚ´æ */ static private int BackupBAK_CELL_SIZE = 250, BackupBAK_LIST_ROWS = 10; //ºó±¸¶ÓÁÐ static private int Suspend_CELL_SIZE = 250, Suspend_LIST_ROWS = 10; //¹ÒÆð¶ÓÁÐ static private int Ready_CELL_SIZE = 200, Ready_LIST_ROWS = 6; //¾ÍÐ÷¶ÓÁÐ static private int Memory_CELL_SIZE = 200, Memory_LIST_ROWS = 10; //ÄÚ´æ¶ÓÁÐ static private int CPU_ROWS = 12, CPU_COLS = 22; //CPUÃæ°å static private int STATUS_ROWS = 8, STATUS_COLS = 30; //ÏµÍ³×´Ì¬Ãæ°å private int timeslice = 1; //ÉèÖÃʱ¼äƬ´óС private int systemStatus=0; //ÉèÖÃϵͳ״̬ 0¡ª¡ªÏµÍ³Ô¤±¸×´Ì¬£¬µÈ´ý¿ªÊ¼£¬1¡ª¡ªÏµÍ³ÔËÐÐ״̬£¬2¡ª¡ªÏµÍ³ÔÝͣ״̬
static private int TOTAL__TEXTFIELD_SIZE = 10; // Size total text field ¼Ç¼¸÷¶ÓÁÐÔªËØ¸öÊý
private JList backupList, suspendList, readyList, memoryList; //¸÷¶ÓÁÐÏà¶ÔÓ¦µÄÊý×éÁбí // ½ø³ÌÌí¼Ó¿òÖеÄ\Ìí¼ÓÖÁºó±¸¶ÓÁÐ\£¬\Ìí¼ÓÖÁ¾ÍÐ÷¶ÓÁÐ\£¬\ÖØÖÃ\ private JButton addToBAKButton, addToReadyButton, resetButton; //¾ÍÐ÷¶ÓÁпòÖеÄ\¹ÒÆð\£¬¹ÒÆð¶ÓÁпòÖеÄ\½â¹Ò\£¬\ɾ³ý\ private JButton suspendButton, umountButton, removeButton; //StatusÃæ°åÖеÄ\Æô¶¯ÏµÍ³\£¬\ÖØÖÃϵͳ\
private JButton startButton, pauseButton, resetSyatemButton; //ÓÅÏȼ¶ºÍʱ¼äƬµ¥Ñ¡Å¥¼°Ê±¼äƬÏÔʾ¿ò private JRadioButton priorityJRB, timesliceJRB; private JLabel timesliceSizeLabel; private JTextField timesliceJtf;
//ºó±¸Ãæ°å¡¢½ø³ÌÌí¼ÓÃæ°å¡¢¹ÒÆðÃæ°å¡¢ÄÚ´æÃæ°å
private JPanel backupBAKPanel, PCBItemPanel, suspendedPanel, memoryPanel; //ºó±¸¶ÓÁС¢¹ÒÆð¶ÓÁÐÔªËØ×ÜÊý±êÇ© private JLabel backupTotalLabel, suspendTotalLabel;
//½ø³ÌÐÅÏ¢±êÇ© ½ø³Ì±àºÅPID,ËùÐèÔËÐÐʱ¼ärequiredTime,ÓÅÏȼ¶priority,µ±Ç°×´Ì¬statues,ÄÚ´æÖеĻùÖ·base,ËùÐèÄÚ´æ´óСlimit
private JLabel PIDLabel, requiredTimeLabel, priorityLabel, statuesLabel, baseLabel, limitLabel; //ºó±¸¶ÓÁС¢¹ÒÆð¶ÓÁÐÔªËØ×ÜÊýÎı¾¿ò£¨²»¿É±à¼£© private JTextField backupTotalTextField, suspendTotalTextField;
//½ø³ÌÐÅÏ¢Îı¾¿ò PID(¿É±à¼),requiredTime(¿É±à¼),priority(¿É±à¼),status(²»¿É±à¼),base(²»¿É±à¼),limit(¿É±à¼)
private JTextField PIDTextField, requiredTimeTextField, priorityTextField, statusTextField, baseTextField, limitTextField;
//CPU״̬ÏÔʾÎı¾Óò(²»¿É±à¼)£¬statusÐÅÏ¢Îı¾Óò(ÓÃÓÚÏÖʵ³ÌÐòÿһ²½µÄ²Ù×÷ºÍÓ°Ï죬²»¿É±à¼)
private JTextArea CPUTextArea, statuesTextArea;
//ºó±¸¶ÓÁÐPCBÊý×é,¾ÍÐ÷¡¢¹ÒÆð£¬¡ª¡ªÄڴ棨¿É·Ö·ÖÇø±í£© PCBRecords backupPCB, readyPCB, suspendedPCB; private MemoryRecords memoryItems;
private boolean flag = false; //mainº¯Êý
public static void main(String[] args) throws IOException { new SingleCPUSchedulingGUI03().initFrame(); }
//³õʼ»¯Frame
public void initFrame() { backupList = new JList(); backupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); backupList.setVisibleRowCount(BackupBAK_LIST_ROWS); backupList.setFixedCellWidth(BackupBAK_CELL_SIZE); suspendList = new JList(); suspendList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); suspendList.setVisibleRowCount(Suspend_LIST_ROWS); suspendList.setFixedCellWidth(Suspend_CELL_SIZE); readyList = new JList(); readyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); readyList.setVisibleRowCount(Ready_LIST_ROWS); readyList.setFixedCellWidth(Ready_CELL_SIZE); memoryList = new JList(); memoryList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); memoryList.setVisibleRowCount(Memory_LIST_ROWS); memoryList.setFixedCellWidth(Memory_CELL_SIZE); suspendButton = new JButton(\ addToBAKButton = new JButton(\ addToReadyButton = new JButton(\ resetButton = new JButton(\ umountButton = new JButton(\ removeButton = new JButton(\ startButton = new JButton(\ pauseButton = new JButton(\
resetSyatemButton = new JButton(\ priorityJRB = new JRadioButton(\ timesliceJRB = new JRadioButton(\ backupTotalLabel = new JLabel(\ backupTotalTextField = new JTextField(\ backupTotalTextField.setEditable(false); suspendTotalLabel = new JLabel(\ suspendTotalTextField = new JTextField(\ suspendTotalTextField.setEditable(false); timesliceSizeLabel = new JLabel(\ timesliceJtf = new JTextField(\ timesliceJtf.setEditable(true); CPUTextArea = new JTextArea(CPU_ROWS, CPU_COLS); CPUTextArea.setEditable(false); statuesTextArea = new JTextArea(STATUS_ROWS, STATUS_COLS); statuesTextArea.setEditable(false);
/* north panel*/ JPanel northPanel = new JPanel(new GridLayout(1, 3)); // ProcessPCB item information Panel PCBItemPanel = new JPanel(new BorderLayout()); PCBItemPanel.setBorder( BorderFactory.createTitledBorder(\ JPanel PCBItemButtonJPanel = new JPanel(new GridLayout(3, 1)); PCBItemButtonJPanel.add(addToBAKButton); PCBItemButtonJPanel.add(addToReadyButton); PCBItemButtonJPanel.add(resetButton); PCBItemPanel.add(this.initPCBItemPanel(), BorderLayout.CENTER); PCBItemPanel.add(PCBItemButtonJPanel, BorderLayout.SOUTH);
//backupBAKList Panel backupBAKPanel = new JPanel(new BorderLayout()); backupBAKPanel.setBorder(BorderFactory.createTitledBorder(\
JPanel backupTotalPAnel = new JPanel(); backupTotalPAnel.add(backupTotalLabel); backupTotalPAnel.add(backupTotalTextField); backupBAKPanel.add ( new JScrollPane(backupList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER); backupBAKPanel.add(backupTotalPAnel, BorderLayout.SOUTH);
// SuspendList Panel
suspendedPanel = new JPanel(new BorderLayout());
suspendedPanel.setBorder(BorderFactory.createTitledBorder(\
JPanel suspendedTotalPAnel = new JPanel(); suspendedTotalPAnel.add(suspendTotalLabel); suspendedTotalPAnel.add(suspendTotalTextField);
JPanel suspendComponentPanel = new JPanel(new GridLayout(1, 2)); suspendComponentPanel.add(umountButton); suspendComponentPanel.add(removeButton);
suspendedPanel.add (suspendedTotalPAnel, BorderLayout.NORTH); suspendedPanel.add ( new JScrollPane(suspendList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER); suspendedPanel.add(suspendComponentPanel, BorderLayout.SOUTH);
northPanel.add(backupBAKPanel); northPanel.add(PCBItemPanel); northPanel.add(suspendedPanel); /* center Panel*/
JPanel centrelPanel = new JPanel(new GridLayout(1, 3));
// readyList panel
JPanel readyListPanel = new JPanel(new BorderLayout());
readyListPanel.setBorder(BorderFactory.createTitledBorder(\ readyListPanel.add ( new JScrollPane(readyList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)); readyListPanel.add (suspendButton, BorderLayout.SOUTH); // CPU panel
JPanel CPUPanel = new JPanel();
CPUPanel.setBorder(BorderFactory.createTitledBorder(\ CPUPanel.add (new JScrollPane(CPUTextArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
memoryPanel = new JPanel(new BorderLayout());
memoryPanel.setBorder(BorderFactory.createTitledBorder(\ memoryPanel.add ( new JScrollPane(memoryList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
centrelPanel.add(readyListPanel); centrelPanel.add(CPUPanel);
centrelPanel.add(memoryPanel); /*statues panel*/
JPanel southPanel = new JPanel(new BorderLayout());
JPanel statuesPanel = new JPanel();
statuesPanel.setBorder(BorderFactory.createTitledBorder(\ statuesPanel.add (new JScrollPane(statuesTextArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
JPanel systemContralButtonPanel = new JPanel(new GridLayout(6, 1));
systemContralButtonPanel.setBorder(BorderFactory.createTitledBorder(\ ButtonGroup group = new ButtonGroup(); group.add(priorityJRB); group.add(timesliceJRB);
JPanel porityPanel = new JPanel(new GridLayout(1, 2)); porityPanel.add(timesliceSizeLabel); porityPanel.add(timesliceJtf);
systemContralButtonPanel.add(priorityJRB);
systemContralButtonPanel.add(timesliceJRB); systemContralButtonPanel.add(porityPanel); systemContralButtonPanel.add(startButton); systemContralButtonPanel.add(pauseButton);
systemContralButtonPanel.add(resetSyatemButton);
southPanel.add(statuesPanel, BorderLayout.CENTER);
southPanel.add(systemContralButtonPanel, BorderLayout.EAST); // arrange panels in window setLayout(new BorderLayout()); add(northPanel, BorderLayout.NORTH); add(centrelPanel, BorderLayout.CENTER); add(southPanel, BorderLayout.SOUTH); //statuesPanel // start listening for list and buttons events backupList.addListSelectionListener(new DisplayBackupProcessListener()); suspendList.addListSelectionListener(new DisplaySuspendedProcessListener()); readyList.addListSelectionListener(new DisplayreadyProcessListener()); addToBAKButton.addActionListener(new AddToBAKListener()); addToReadyButton.addActionListener(new AddToReadyListener()); resetButton.addActionListener(new ResetListener()); suspendButton.addActionListener(new SuspendListener()); umountButton.addActionListener(new UmountListener()); removeButton.addActionListener(new RemoveListener()); startButton.addActionListener(new StartSystemListener()); pauseButton.addActionListener(new SystemPauseListener()); resetSyatemButton.addActionListener(new ResetSystemListener()); priorityJRB.addActionListener(new priotiryListener()); timesliceJRB.addActionListener(new timeslicListener()); backupPCB = new PCBRecords(); readyPCB = new PCBRecords(); suspendedPCB = new PCBRecords(); memoryItems = new MemoryRecords(); MemoryItem initMemoryItem = new MemoryItem(0,3000); memoryItems.addItem(initMemoryItem); backupList.setListData(backupPCB.getItemsProperties()); readyList.setListData(readyPCB.getItemsProperties()); suspendList.setListData(suspendedPCB.getItemsProperties()); memoryList.setListData(memoryItems.getItemsProperties()); this.setTitle(\ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(WIDTH, HEIGHT); this.setResizable(true); this.setVisible(true); this.setLocation(200, 10); this.addWindowListener(new MyWindowMonitor()); }
public JPanel initPCBItemPanel() { JPanel iniPCBItemJPanel = new JPanel(new BorderLayout()); JPanel iniNamePanel = new JPanel(new GridLayout(6, 1));
JPanel iniValuePanel = new JPanel(new GridLayout(6, 1)); PIDLabel = new JLabel(\ requiredTimeLabel = new JLabel(\ priorityLabel = new JLabel(\ statuesLabel = new JLabel(\ baseLabel = new JLabel(\ limitLabel = new JLabel(\ iniNamePanel.add(PIDLabel); iniNamePanel.add(requiredTimeLabel); iniNamePanel.add(priorityLabel); iniNamePanel.add(statuesLabel); iniNamePanel.add(baseLabel); iniNamePanel.add(limitLabel); PIDTextField = new JTextField(\ PIDTextField.setEditable(true); requiredTimeTextField = new JTextField(\ requiredTimeTextField.setEditable(true); priorityTextField = new JTextField(\ priorityTextField.setEditable(true); statusTextField = new JTextField(\ statusTextField.setEditable(false); baseTextField = new JTextField(\ baseTextField.setEditable(false); limitTextField = new JTextField(\ limitTextField.setEditable(true); iniValuePanel.add(PIDTextField); iniValuePanel.add(requiredTimeTextField); iniValuePanel.add(priorityTextField); iniValuePanel.add(statusTextField); iniValuePanel.add(baseTextField); iniValuePanel.add(limitTextField); iniPCBItemJPanel.add(iniNamePanel, BorderLayout.WEST); iniPCBItemJPanel.add(iniValuePanel, BorderLayout.CENTER); return iniPCBItemJPanel; }
class MyWindowMonitor extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); setVisible(false); } }
public void displayInformation(ProcessPCB pcb) { PIDTextField.setText(pcb.getPID()); requiredTimeTextField.setText(Integer.toString(pcb.getRequiredTime())); priorityTextField.setText(Integer.toString(pcb.getPriority())); statusTextField.setText(pcb.getStatus()); baseTextField.setText(Integer.toString(pcb.getMemoryBase())); limitTextField.setText(Integer.toString(pcb.getMemoryLimit())); }
//Displays the information of the selected PCBItem
class DisplayBackupProcessListener implements ListSelectionListener {
//
public void valueChanged(ListSelectionEvent event) { if (! backupList.getValueIsAdjusting()) { String id = null, selectedBAKItem = null; selectedBAKItem = (String) backupList.getSelectedValue(); if(selectedBAKItem != null) { StringTokenizer stringtokenizer = new StringTokenizer(selectedBAKItem, \ id = stringtokenizer.nextToken(); ProcessPCB backupItem = backupPCB.getItem(id); if(backupItem != null) { displayInformation(backupItem); } statuesTextArea.append(backupItem.toString() + \ + \ } } } }
class DisplaySuspendedProcessListener implements ListSelectionListener { }
public void valueChanged(ListSelectionEvent event) { if (! suspendList.getValueIsAdjusting()) { stdErr.flush(); String id = \ String str = (String) suspendList.getSelectedValue(); if(str != null) { StringTokenizer strtokenizer = new StringTokenizer(str, \ id = strtokenizer.nextToken(); ProcessPCB selectedSuspendedItem = suspendedPCB.getItem(id); if(selectedSuspendedItem != null) { displayInformation(selectedSuspendedItem); } statuesTextArea.append(selectedSuspendedItem.toString() + \ + \ } } }
class DisplayreadyProcessListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent event) { if (! readyList.getValueIsAdjusting()) { String id = null, s = null; s = (String) readyList.getSelectedValue(); if(s != null) { StringTokenizer sttokenizer = new StringTokenizer(s, \ id = sttokenizer.nextToken(); ProcessPCB readyItem = readyPCB.getItem(id); if(readyItem != null) { displayInformation(readyItem); } statuesTextArea.append(readyItem.toString() + \ + \
} } } }
//ÄÚ´æ¿É·Ö·ÖÇø±íÅÅÐò£¬°´¿ÉÓÃÄÚ´æ´óС½µÐòÅÅÁÐ public void sortMemoryList() { MemoryRecords currentListItems = new MemoryRecords(); int num = memoryItems.getNumberOfItems(); if(num>0) { for(int i=num; i>=1; i--) { Iterator memoryIterator = memoryItems.iterator(); MemoryItem currentItem = (MemoryItem) memoryIterator.next(); for( ; memoryIterator.hasNext(); ) { MemoryItem nowItem = (MemoryItem) memoryIterator.next(); if(currentItem.getMemoryLimit() < nowItem.getMemoryLimit()) { currentItem = null; currentItem = nowItem; } } currentListItems.addItem(currentItem); memoryItems.removeItem(currentItem); } memoryItems = null; memoryItems = currentListItems; } memoryList.setListData(memoryItems.getItemsProperties()); }
//ÅжÏÄÜ·ñΪ½ø³Ì·ÖÅäÄÚ´æ
public boolean boolMemoryAllocation(int neededLimit) { if(memoryItems.getNumberOfItems()>0) { Iterator memoryListIteartor = memoryItems.iterator(); MemoryItem lagerestItem = (MemoryItem) memoryListIteartor.next(); if(lagerestItem.getMemoryLimit()>= neededLimit) return true; else { return false; } } else { return false; } }
//ÄÚ´æ·ÖÅ䣬°´×îÏÈÊÊÓ¦Ëã·¨ÔÔò
public int MemoryAllocation(int neededLimit) { int currentBase=-1, currentLimit=-1; Iterator memoryListIteartor = memoryItems.iterator(); MemoryItem lagerestItem = (MemoryItem) memoryListIteartor.next(); currentBase = lagerestItem.getMemoryBase()+neededLimit; currentLimit = lagerestItem.getMemoryLimit()-neededLimit; memoryItems.removeItem(lagerestItem); if(currentLimit>0) { memoryItems.addItem(new MemoryItem(currentBase, currentLimit)); } sortMemoryList(); return lagerestItem.getMemoryBase(); }
//ÄÚ´æ»ØÊÕ
public void memoryRecall(int nowBase, int nowLimit) { MemoryItem recallItem = null;
int recalledBase = nowBase; int recalledLimit = nowLimit; if(memoryItems.getNumberOfItems()>0) { Iterator memoryListIteartor = memoryItems.iterator(); for( ;memoryListIteartor.hasNext(); ) { MemoryItem existedItem = (MemoryItem) memoryListIteartor.next(); if(existedItem.getMemoryBase() == (nowBase+nowLimit)) { recalledLimit = recalledLimit+existedItem.getMemoryLimit(); memoryItems.removeItem(existedItem); memoryListIteartor = memoryItems.iterator(); } else if(nowBase == (existedItem.getMemoryBase()+existedItem.getMemoryLimit())) { recalledBase = existedItem.getMemoryBase(); recalledLimit = recalledLimit+existedItem.getMemoryLimit(); memoryItems.removeItem(existedItem); memoryListIteartor = memoryItems.iterator(); } //memoryListIteartor = memoryItems.iterator(); //×¢Òâ´Ë´¦´úÂëÔøÒý·¢Òì³£ } //recallItem = new MemoryItem(recalledBase, recalledLimit); //Ôø²úÉúÒì³£ } recallItem = new MemoryItem(recalledBase, recalledLimit); memoryItems.addItem(recallItem); sortMemoryList(); }
public int readInteger(String s) { int num = -1; try { num = Integer.parseInt(s); } catch(NumberFormatException numberformatexception) { statuesTextArea.append(\ num = -999; } return num; }
public void addToBackupList(String newID, int s1, int s2, int s3, String s) { ProcessPCB item = backupPCB.getItem(newID); if(item != null) { statuesTextArea.append(\ + \ while(item != null) { newID = s + newID; item = backupPCB.getItem(newID); } }
ProcessPCB newPCB = new ProcessPCB(newID, s1, s2, \ backupPCB.addItem(newPCB);
backupList.setListData(backupPCB.getItemsProperties());
backupTotalTextField.setText(Integer.toString(backupPCB.getNumberOfItems())); }
public void addToReadyList(String nowID, int s1, int s2, int s3, String s) { ProcessPCB item = readyPCB.getItem(nowID); if(item != null) { statuesTextArea.append(\
+ \ while(item != null) {
nowID = s + nowID; item = backupPCB.getItem(nowID); } }
int s4=MemoryAllocation(s3); sortMemoryList();
ProcessPCB newPCB = new ProcessPCB(nowID, s1, s2, \ readyPCB.addItem(newPCB); sortReadyPCB(); }
class AddToBAKListener implements ActionListener { public void actionPerformed(ActionEvent event) { String newID = PIDTextField.getText(); String newTime = requiredTimeTextField.getText(); String newPriority = priorityTextField.getText(); String newLimit = limitTextField.getText(); int s1 = 0, s2 = 0, s3 = 0, tag1=-1, tag2=-1, tag3=-1; if(newTime != null) { s1 = readInteger(newTime); if(s1 > 0.0) tag1 = 1;
else statuesTextArea.append(\ } if(newPriority != null) { s2 = readInteger(newPriority); if(s1 != -999) tag2 = 1;
else statuesTextArea.append(\ } if(newLimit != null) { s3 = readInteger(newLimit); if(s3 > 0.0) tag3 = 1;
else statuesTextArea.append(\ } if(tag1 ==1 && tag2 == 1 && tag3 == 1) { if(newID == null) { statuesTextArea.append(\ } else { addToBackupList(newID, s1, s2, s3, \
statuesTextArea.append(\ reset(); } } } }
class AddToReadyListener implements ActionListener { public void actionPerformed(ActionEvent event) { String nowID = PIDTextField.getText(); String time = requiredTimeTextField.getText(); String priority = priorityTextField.getText(); String limit = limitTextField.getText(); int s1 = 0, s2 = 0, s3 =0; int tag1 = -1, tag2 = -1, tag3 = -1;
if(time != null) { s1 = readInteger(time); if(s1 > 0.0) tag1=1;
else statuesTextArea.append(\ } if(priority != null) { s2 = readInteger(priority); if(s2 != -999) tag2=1;
else statuesTextArea.append(\ } if(limit != null) { s3 = readInteger(limit); if(s3 > 0.0) tag3=1;
else statuesTextArea.append(\ }
if(tag1 ==1 && tag2 == 1 && tag3 ==1) { if(nowID == null) { statuesTextArea.append(\ } else {
// PCBItem item = null; if(readyPCB.getNumberOfItems() < 6) { if(boolMemoryAllocation(s3)) { addToReadyList(nowID, s1, s2, s3, \ statuesTextArea.append(\student recorditem has been added to ReadyList!\\n\ } else { statuesTextArea.append(\memory available! The PCB will be added to backupList!\\n\ addToBackupList(nowID, s1, s2, s3, \ statuesTextArea.append(\student recorditem has been added to BackupList!\\n\ } } else { statuesTextArea.append(\ReadyList was full! The new ProcessPCB will be added to BackupList!\\n\ addToBackupList(nowID, s1, s2, s3, \ statuesTextArea.append(\ } reset(); } } } }
public void reset() { PIDTextField.setText(\ requiredTimeTextField.setText(\ priorityTextField.setText(\ statusTextField.setText(\ baseTextField.setText(\ limitTextField.setText(\ }
// This inner class processes resetButton events. class ResetListener implements ActionListener {
public void actionPerformed(ActionEvent event) { reset(); } }
// This inner class processes suspendButton events.
//×¢£ºÔÚ¹ÒÆðʱ£¬²»»á´¥·¢½ø³Ìµ÷¶È£¬¶øÊÇÔÚµã»÷\ʱ²Å»á³ö·¢½ø³Ìµ÷¶È class SuspendListener implements ActionListener { public void actionPerformed(ActionEvent event) { String selectedReadyItem = null; String pid = \ if(readyPCB.getNumberOfItems() == 0) { statuesTextArea.append(\ } else { try {
selectedReadyItem = (String) readyList.getSelectedValue(); if(selectedReadyItem == null) {
statuesTextArea.append(\ } else {
StringTokenizer stringtokenizer = new StringTokenizer(selectedReadyItem, \ pid = stringtokenizer.nextToken(); ProcessPCB selectedItem = readyPCB.getItem(pid); if(selectedItem == null) { statuesTextArea.append(\student recorditem of Num \+ pid + \is founded!\\n\
} else { ProcessPCB boolItem = suspendedPCB.getItem(pid); if(boolItem != null) { statuesTextArea.append(\ + \ while(boolItem != null) { pid = \ boolItem = suspendedPCB.getItem(pid); } } //×¢ÒâÏÂÒ»²½´æÔÚÎÊÌ⣡ ProcessPCB newPcb = new ProcessPCB(pid, selectedItem.getRequiredTime(), selectedItem.getPriority(),\-1, selectedItem.getMemoryLimit()); memoryRecall(selectedItem.getMemoryBase(), selectedItem.getMemoryLimit()); suspendedPCB.addItem(newPcb); readyPCB.removeItem(selectedItem); // if(systemStatues == 1) { // PCBSchudling(); // } sortReadyPCB(); suspendList.setListData(suspendedPCB.getItemsProperties()); statuesTextArea.append(\
suspendTotalTextField.setText(Integer.toString(suspendedPCB.getNumberOfItems())); } }
} catch (RuntimeException e1) { // TODO Auto-generated catch block } } }
}
//¶ÔReady¶ÓÁнøÐÐÓÅÏȼ¶ÅÅÐò public void sortReadyPCB() { PCBRecords currentReadyPCB = new PCBRecords(); int num = readyPCB.getNumberOfItems(); if(num > 0) { for(int i=num; i>=1; i--) { Iterator readyIterator = readyPCB.iterator(); ProcessPCB currentItem = (ProcessPCB) readyIterator.next(); for( ; readyIterator.hasNext(); ) { ProcessPCB nowItem = (ProcessPCB) readyIterator.next(); if(currentItem.getPriority() < nowItem.getPriority()) { currentItem = null; currentItem = nowItem; } } currentReadyPCB.addItem(currentItem); readyPCB.removeItem(currentItem); } readyPCB = null; readyPCB = currentReadyPCB; } readyList.setListData(readyPCB.getItemsProperties()); }
// This inner class processes umountButton events. class UmountListener implements ActionListener { public void actionPerformed(ActionEvent event) { String selectedSuspendedItem = null; String id = \ if(suspendedPCB.getNumberOfItems() == 0) { statuesTextArea.append(\ } else { try { selectedSuspendedItem = (String) suspendList.getSelectedValue(); if(selectedSuspendedItem == null) {
statuesTextArea.append(\ } else {
StringTokenizer stringtokenizer = new StringTokenizer(selectedSuspendedItem, \ id = stringtokenizer.nextToken(); ProcessPCB selectedItem = suspendedPCB.getItem(id); if(selectedItem == null) { statuesTextArea.append(\ } else { int s3 = selectedItem.getMemoryLimit(); if(readyPCB.getNumberOfItems() < 6) {
if(boolMemoryAllocation(s3)) { addToReadyList(id, selectedItem.getRequiredTime(), selectedItem.getRequiredTime(), s3, \ statuesTextArea.append(\product has been Umounted to Ready List!\\n\
} else { statuesTextArea.append(\memory available! The PCB will be Umounted to BackupList!\\n\ addToBackupList(id, selectedItem.getRequiredTime(), selectedItem.getRequiredTime(), s3, \
statuesTextArea.append(\product has been Umounted to Backup List!\\n\
} } else { statuesTextArea.append(\be Umounted to BackupList!\\n\ addToBackupList(id, selectedItem.getRequiredTime(), selectedItem.getRequiredTime(), s3, \ statuesTextArea.append(\product has been Umounted to Backup List!\\n\
} suspendedPCB.removeItem(selectedItem); suspendList.setListData(suspendedPCB.getItemsProperties());
suspendTotalTextField.setText(Integer.toString(suspendedPCB.getNumberOfItems())); } }
} catch (RuntimeException e1) { // TODO Auto-generated catch block } } } } /**
* This inner class processes removeButton
events. */
public int getTimeslice() { return timeslice; }
class RemoveListener implements ActionListener { public void actionPerformed(ActionEvent event) { String selectedRemovsItem = null; String id = \ if(suspendedPCB.getNumberOfItems() == 0) { statuesTextArea.append(\ } else {
selectedRemovsItem = (String) suspendList.getSelectedValue(); if(selectedRemovsItem == null) {
statuesTextArea.append(\ } else {
StringTokenizer stringtokenizer = new StringTokenizer(selectedRemovsItem, \ id = stringtokenizer.nextToken(); ProcessPCB suspendedItem = suspendedPCB.getItem(id); if(suspendedItem == null) { statuesTextArea.append(\ } else { suspendedPCB.removeItem(suspendedItem); suspendList.setListData(suspendedPCB.getItemsProperties()); statuesTextArea.append(\ }
suspendTotalTextField.setText(Integer.toString(suspendedPCB.getNumberOfItems())); } } } }
//ÄÚ´æ½ø³Ìµ÷¶È public void PCBSchudling() { while(readyPCB.getNumberOfItems() < 6) { if(backupPCB.getNumberOfItems() > 0) { Iterator bwackupPCBIterator = backupPCB.iterator(); ProcessPCB newItem = (ProcessPCB) bwackupPCBIterator.next(); readyPCB.addItem(newItem); backupPCB.removeItem(newItem); sortReadyPCB(); readyList.setListData(readyPCB.getItemsProperties()); backupList.setListData(backupPCB.getItemsProperties()); backupTotalTextField.setText(Integer.toString(backupPCB.getNumberOfItems())); } else break; } } /*
public void run() { PCBSchudling(); if(readyPCB.getNumberOfItems() > 0) { Iterator readyIterator = readyPCB.iterator(); PCBItem runningItem = (PCBItem) readyIterator.next(); if(runningItem.getPCB().getRequiredTime()>0) { runningItem.run(); } if(runningItem.getPCB().getRequiredTime() <= 0) { statuesTextArea.append(\work has been finished and the PCB will be moved out from the memory!\\n\ readyPCB.removeItem(runningItem); runningItem = null; //×¢Òâ PCBSchudling(); } sortReadyPCB(); readyList.setListData(readyPCB.getItemsProperties()); } else { statuesTextArea.append(\ // System.exit(0); } } */
class RunThread extends Thread { int t, TimeSlice; boolean Flag = true; public void run(){ while(Flag) { //Warning!!!!!!!!!!!! if(getSystemStatus() == 0) { Flag = false; break; } else if(getSystemStatus() == 2) { while(getSystemStatus() == 2) { try {
Thread.sleep(1000); } catch (Exception ee) { ee.printStackTrace(); } } } if(readyPCB.getNumberOfItems() < 6) { if(backupPCB.getNumberOfItems() > 0) { while(readyPCB.getNumberOfItems() < 6) { if(backupPCB.getNumberOfItems() > 0) { Iterator bwackupPCBIterator = backupPCB.iterator(); ProcessPCB newItem = (ProcessPCB) bwackupPCBIterator.next(); while(bwackupPCBIterator.hasNext() ) { int s = newItem.getMemoryLimit(); if (boolMemoryAllocation(s)) { break; } else newItem = (ProcessPCB) bwackupPCBIterator.next(); if(newItem == null) break; } if(newItem != null) { int bbase=0; int llimit = newItem.getMemoryLimit(); bbase = MemoryAllocation(llimit); ProcessPCB nowItem = new ProcessPCB(newItem.getPID(), newItem.getRequiredTime(), newItem.getPriority(), \bbase, llimit); readyPCB.addItem(nowItem); backupPCB.removeItem(newItem); backupList.setListData(backupPCB.getItemsProperties());
backupTotalTextField.setText(Integer.toString(backupPCB.getNumberOfItems())); } else { statuesTextArea.append(\ break; } } else break; } } } sortReadyPCB(); backupList.setListData(backupPCB.getItemsProperties()); backupTotalTextField.setText(Integer.toString(backupPCB.getNumberOfItems())); Iterator readyIterator = readyPCB.iterator(); ProcessPCB runningItem = null; if(readyIterator.hasNext()) { runningItem = (ProcessPCB) readyIterator.next(); } //PCBItem runningItem = (PCBItem) readyIterator.next(); //readyPCB.getNumberOfItems() > 0 //×¢Òâ´Ë´¦ÓÐbug½á¹û¾Í³öÏÖÁËjava.util.NoSuchElementExceptionµÄ´íÎóÌáʾ £¬¾ÍÊÇ»ÆÉ«µÄÄÇÒ»¾ä£¬¶àÁËÒ»¸ö µü´úÆ÷µÄnext£¨£©¡£ if(runningItem != null) { t = runningItem.getRequiredTime(); TimeSlice = getTimeslice(); System.out.println(TimeSlice);
for(int i=TimeSlice; i>=0; i--) { if(t >= 0) { runningItem.run(); t--; sortReadyPCB(); if(getFlag() == true) CPUTextArea.append(\PCB \+ runningItem.getPID() + \is running!\\n\ try { Thread.sleep(990); } catch (Exception ee) { ee.printStackTrace(); } } } if(t < 0) { Flag = false; statuesTextArea.append(\from the memory!\\n\ memoryRecall(runningItem.getMemoryBase(), runningItem.getMemoryLimit()); readyPCB.removeItem(runningItem); runningItem = null; sortReadyPCB(); } if(readyPCB.getNumberOfItems() > 0) Flag = getFlag(); else Flag = false; } else Flag = false; //Warning!!!!!!!!!!!!!!!! if(getSystemStatus() == 0) { Flag = false; break; } else { if(getSystemStatus() == 2) { while(getSystemStatus() == 2) { try { Thread.sleep(1000); } catch (Exception ee) { ee.printStackTrace(); } } } if(getSystemStatus() == 1) Flag = true; } } } }
public boolean getFlag() { return this.flag; }
public int getSystemStatus() { return systemStatus; }
class StartSystemListener implements ActionListener {
public void actionPerformed(ActionEvent event) { if(readyPCB.getNumberOfItems() > 0) { if(systemStatus == 0) { Thread runThread = new RunThread(); flag = true; runThread.start(); systemStatus = 1; statuesTextArea.append(\ } else { statuesTextArea.append(\ } } else statuesTextArea.append(\ } }
class SystemPauseListener implements ActionListener { public void actionPerformed(ActionEvent event) { if(systemStatus == 1) { flag = false; systemStatus = 2; pauseButton.setText(\ statuesTextArea.append(\ } else if(systemStatus == 2) { flag = true; systemStatus = 1; pauseButton.setText(\ statuesTextArea.append(\ } else { statuesTextArea.append(\ } } }
class ResetSystemListener implements ActionListener { public void actionPerformed(ActionEvent event) { if(systemStatus != 0) { flag = false;
systemStatus = 0;
backupPCB = null; readyPCB = null; suspendedPCB = null; memoryItems = null;
backupPCB = new PCBRecords(); readyPCB = new PCBRecords(); suspendedPCB = new PCBRecords(); backupList.setListData(backupPCB.getItemsProperties()); readyList.setListData(readyPCB.getItemsProperties()); suspendList.setListData(suspendedPCB.getItemsProperties()); backupTotalTextField.setText(\ suspendTotalTextField.setText(\ CPUTextArea.setText(\ statuesTextArea.setText(\ priorityJRB.doClick(); timeslice = 1;
timesliceJtf.setText(\
memoryItems = new MemoryRecords(); memoryList.removeAll(); MemoryItem newMemoryItem = new MemoryItem(0,3000); memoryItems.addItem(newMemoryItem);
memoryList.setListData(memoryItems.getItemsProperties()); statuesTextArea.append(\ } else { statuesTextArea.append(\ } } }
class priotiryListener implements ActionListener { public void actionPerformed(ActionEvent event) { timeslice = 1; } }
class timeslicListener implements ActionListener { public void actionPerformed(ActionEvent event) { timeslice = getTimesliceText(); if(timeslice <= 0) { statuesTextArea.append(\ timesliceJtf.setText(\ timeslice = 1; } } }
public int getTimesliceText() { int n; try { n = Integer.parseInt(timesliceJtf.getText()); } catch(NumberFormatException numberformatexception) { statuesTextArea.append(\ n = -999; } return n; } }