MIT6.S081/6.828 瀹為獙1锛歀ab Unix Utilities
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MIT6.S081/6.828 瀹為獙1锛歀ab Unix Utilities相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e8%be%93%e5%87%ba' title='杈撳嚭'>杈撳嚭
open() lin 娴佺▼ 鎼缓 鐜鎼缓 -- 鎬濊矾 鏍规嵁
Mit6.828/6.S081 fall 2019鐨凩ab1鏄疷nix utilities锛屼富瑕佸唴瀹逛负鍒╃敤xv6鐨勭郴缁熻皟鐢ㄥ疄鐜皊leep銆乸ingpong銆乸rimes銆乫ind鍜寈args绛夊伐鍏枫€傛湰鏂囧鍚勭▼搴忕殑瀹炵幇鎬濊矾鍙妜v6鐨勭郴缁熻皟鐢ㄦ祦绋嬭繘琛岃缁嗕粙缁嶃€?/p>
鍓嶈█
鍦ㄥ疄楠屼箣鍓嶏紝鎺ㄨ崘闃呰涓€涓?a href="https://pdos.csail.mit.edu/6.828/2019/schedule.html">瀹樼綉
LEC1涓彁渚涚殑璧勬枡銆傚叾涓璉ntroduction鏄璇ヨ绋嬬殑鐨勬杩帮紝examples鍒欐槸鍑犱釜绯荤粺缂栫▼鐨勬牱渚嬶紝杩欎袱閮ㄥ垎蹇€熸祻瑙堜竴閬嶅嵆鍙€傚浜?a href="https://pdos.csail.mit.edu/6.828/2019/xv6/book-riscv-rev0.pdf">xv6 book
鐨勭涓€绔狅紝鍒欏缓璁◢寰粏鑷村湴闃呰涓€閬嶏紝鐗瑰埆鏄fork()銆乪xec()銆乸ipe()銆乨up()杩欏嚑涓郴缁熻皟鐢ㄧ殑浠嬬粛锛屼細鍦ㄥ悗闈㈠疄楠屼腑鐢ㄥ埌銆?/p>
瀹為獙鐜鎼缓鍙傝€?a href="https://zhayujie.com/mit6828-env.html">涓婁竴绡囨枃绔?/a>銆傝繘鍏v6-riscv-fall19椤圭洰鍚庡彲浠ョ湅鍒颁袱涓瘮杈冮噸瑕佺殑鐩綍锛歬ernel涓簒v6鍐呮牳婧愮爜锛岄噷闈㈤櫎浜唎s宸ヤ綔鐨勬牳蹇冧唬鐮侊紙濡傝繘绋嬭皟搴︼級锛岃繕鏈夊悜澶栨彁渚涚殑鎺ュ彛锛坰ystem call锛夛紱user涓垯鏄敤鎴风▼搴忥紝濡傛垜浠啛鎮夌殑ls锛宔cho鍛戒护绛夈€傛湰娆″疄楠岀殑鐩殑灏辨槸鍦╱ser涓鍔犵敤鎴风▼搴忥紝鍊熷姪kernel涓彁渚涚殑system call鏉ュ疄鐜版墍闇€鐨勫姛鑳姐€?/p>
瀹為獙鎬濊矾
姣忎竴涓狶ab闇€瑕佸湪瀵瑰簲鐨勫垎鏀紪鍐欎唬鐮侊紝杩涘叆xv6-riscv-fall19鐩綍涓嬶紝浣跨敤git checkout util
鍒囨崲鍒皍til鍒嗘敮锛屽嵆鍙紑濮嬬紪鍐欐垜浠殑绋嬪簭銆備笅闈富瑕佹彁渚涘疄鐜版€濊矾锛屽叿浣撳疄楠屼唬鐮佽鍙傝€?a href="https://github.com/zhayujie/xv6-riscv-fall19">Github銆?/p>
瀹為獙瀹屾垚鍚庝娇鐢?code>make grade鍙互鎵ц鍗曞厓娴嬭瘯杩涜璇勫垎锛屼細浠db-server妯″紡鍚姩qemu锛屽苟鍦╣radelib.py涓ā鎷焔db-client瀵规垜浠殑绋嬪簭杩涜娴嬭瘯銆傚鏋滃湪make grade鏃舵姤閿?code>Timeout! Failed to connect to QEMU锛屽彲浠ュ皢gradelib.py鐨?25琛屾敼涓?code>self.sock.connect(("127.0.0.1", port))銆?/p>
sleep
sleep鍔熻兘涓轰娇杩涚▼鐫$湢鑻ュ共涓椂閽熷懆鏈燂紙xv6涓竴涓猼ick涓?00ms锛夛紝棣栧厛鍒涘缓user/sleep.c婧愭枃浠讹紝寮曞叆user.h澶存枃浠讹紝绯荤粺璋冪敤鍜屽伐鍏峰嚱鏁伴兘瀹氫箟鍦ㄨ鏂囦欢閲屻€傛牳蹇冧唬鐮佸涓嬶細
sleep(atoi(argv[1]));
瀹屾垚缂栧啓鍚庯紝鍦∕akefile鐨刄PROGS涓拷鍔犱竴琛?code>$U/_sleep銆傝緭鍏?code>make qemu杩涜缂栬瘧锛屾垚鍔熷悗杩涘叆shell锛岃緭鍏?code>sleep 10锛屽鏋滆繘绋嬬潯鐪犱簡澶х害1s锛屽垯琛ㄧず绋嬪簭缂栧啓姝g‘銆?/p>
pingpong
鍔熻兘鏄埗杩涚▼閫氳繃绠¢亾鍚戝瓙杩涚▼鍙戦€?瀛楄妭锛屽瓙杩涚▼鏀跺埌鍚庡悜鐖惰繘绋嬪洖澶?瀛楄妭銆?/p>
鐢变簬绠¢亾鏄崟鍚戞祦鍔ㄧ殑锛屾墍浠ヤ袱娆¤皟鐢?code>pipe()鍒涘缓涓や釜绠¢亾锛屽垎鍒搴斾袱涓柟鍚戙€備娇鐢?code>fork()鍒涘缓瀛愯繘绋嬶紝鍦ㄥ瓙杩涚▼涓厛浠庣閬?read()
鍐嶅悜绠¢亾2write()
锛岀埗杩涚▼涓垯涓庝箣鐩稿弽銆?/p>
primes
primes鐨勫姛鑳芥槸杈撳嚭2~35涔嬮棿鐨勭礌鏁帮紝瀹炵幇鏂瑰紡鏄€掑綊fork杩涚▼骞朵娇鐢ㄧ閬撻摼鎺ワ紝褰㈡垚涓€鏉ipeline鏉ュ绱犳暟杩涜杩囨护銆?/p>
姣忎釜杩涚▼鏀跺埌鐨勭涓€涓暟p涓€瀹氭槸绱犳暟锛屽悗缁殑鏁板鏋滆兘琚玴鏁撮櫎鍒欎箣闂翠涪寮冿紝濡傛灉涓嶈兘鍒欒緭鍑哄埌涓嬩竴涓繘绋嬶紝璇︾粏浠嬬粛鍙弬鑰?a href="https://swtch.com/~rsc/thread/">鏂囨。銆備吉浠g爜濡備笅锛?/p>
void primes() {
p = read from left // 浠庡乏杈规帴鏀跺埌鐨勭涓€涓暟涓€瀹氭槸绱犳暟
if (fork() == 0):
primes() // 瀛愯繘绋嬶紝杩涘叆閫掑綊
else:
loop:
n = read from left // 鐖惰繘绋嬶紝寰幆鎺ユ敹宸﹁竟鐨勮緭鍏?
if (p % n != 0):
write n to right // 涓嶈兘琚玴鏁撮櫎鍒欏悜鍙宠緭鍑?
}
杩橀渶瑕佹敞鎰忎袱鐐癸細
-
鏂囦欢鎻忚堪绗︽孩鍑猴細 xv6闄愬埗fd鐨勮寖鍥翠负0~15锛岃€屾瘡娆ipe()閮戒細鍒涘缓涓や釜鏂扮殑fd锛屽鏋滀笉鍙婃椂鍏抽棴涓嶉渶瑕佺殑fd锛屼細瀵艰嚧鏂囦欢鎻忚堪绗﹁祫婧愮敤灏姐€傝繖閲屼娇鐢ㄩ噸瀹氬悜鍒版爣鍑咺/O鐨勬柟寮忔潵閬垮厤鐢熸垚鏂扮殑fd锛岄鍏坈lose()鍏抽棴鏍囧噯I/O鐨刦d锛岀劧鍚庝娇鐢╠up()澶嶅埗鎵€闇€鐨勭閬揻d锛堜細鑷姩澶嶅埗鍒板簭鍙锋渶灏忕殑fd锛屽嵆鍏抽棴鐨勬爣鍑咺/O锛夛紝闅忓悗瀵筽ipe涓や晶fd杩涜鍏抽棴锛堟鏃跺彧浼氱Щ闄ゆ弿杩扮锛屼笉浼氬叧闂疄闄呯殑file瀵硅薄锛夈€?/p>
-
pipeline鍏抽棴锛?/strong> 鍦ㄥ畬鎴愮礌鏁拌緭鍑哄悗锛岄渶瑕佷緷娆¢€€鍑簆ipeline涓婄殑鎵€鏈夎繘绋嬨€傚湪閫€鍑虹埗杩涚▼鍓嶅叧闂叾鏍囧噯杈撳叆fd锛屾鏃秗ead()灏嗚鍙栧埌eof锛堝€间负0锛夛紝姝ゆ椂鍚屾牱鍏抽棴瀛愯繘绋嬬殑鏍囧噯杈撳叆fd锛岄€€鍑鸿繘绋嬶紝杩欐牱杩涚▼閾句笂鐨勬墍鏈夎繘绋嬪氨鍙互閫€鍑恒€?/p>
find
find鍔熻兘鏄湪鐩綍涓尮閰嶆枃浠跺悕锛屽疄鐜版€濊矾鏄€掑綊鎼滅储鏁翠釜鐩綍鏍戙€?/p>
浣跨敤open()鎵撳紑褰撳墠fd锛岀敤fstat()
鍒ゆ柇fd鐨則ype锛屽鏋滄槸鏂囦欢锛屽垯涓庤鎵剧殑鏂囦欢鍚嶈繘琛屽尮閰嶏紱濡傛灉鏄洰褰曪紝鍒欏惊鐜痳ead()鍒癲irent缁撴瀯锛屽緱鍒板叾瀛愭枃浠?鐩綍鍚嶏紝鎷兼帴寰楀埌褰撳墠璺緞鍚庤繘鍏ラ€掑綊璋冪敤銆傛敞鎰忓浜庡瓙鐩綍涓殑.
鍜?code>..涓嶈杩涜閫掑綊銆?/p>
xargs
xargs鐨勫姛鑳芥槸灏嗘爣鍑嗚緭鍏ヨ浆涓虹▼搴忕殑鍛戒护琛屽弬鏁般€傚彲閰嶅悎绠¢亾浣跨敤锛岃鍘熸湰鏃犳硶鎺ユ敹鏍囧噯杈撳叆鐨勫懡浠ゅ彲浠ヤ娇鐢ㄦ爣鍑嗚緭鍏ヤ綔涓哄弬鏁般€?/p>
鏍规嵁lab涓殑浣跨敤渚嬪瓙鍙互鐪嬪嚭锛寈v6鐨剎args姣忔鍥炶溅閮戒細鎵ц涓€娆″懡浠ゅ苟杈撳嚭缁撴灉锛岀洿鍒癱trl+d鏃剁粨鏉燂紱鑰宭inux涓殑瀹炵幇鍒欐槸涓€鐩存帴鏀惰緭鍏ワ紝鏀跺埌ctrl+d鏃舵墠鎵ц鍛戒护骞惰緭鍑虹粨鏋溿€?/p>
鎬濊矾鏄娇鐢ㄤ袱灞傚惊鐜鍙栨爣鍑嗚緭鍏ワ細
- 鍐呭眰寰幆渚濇璇诲彇姣忎竴涓瓧绗︼紝鏍规嵁绌烘牸杩涜鍙傛暟鍒嗗壊锛屽皢鍙傛暟瀛楃涓插瓨鍏ヤ簩缁存暟缁勪腑锛屽綋璇诲彇鍒扳€榎n鈥樻椂锛岄€€鍑哄綋鍓嶅惊鐜紱褰撴帴鏀跺埌ctrl+d锛坮ead杩斿洖鐨勯暱搴?lt;0锛夋椂閫€鍑虹▼搴忋€?/li>
- 澶栧眰寰幆瀵规瘡涓€琛岃緭鍏?code>fork()鍑哄瓙杩涚▼锛岃皟鐢?code>exec()鎵ц鍛戒护銆傛敞鎰廵xec鎺ユ敹鐨勪簩缁村弬鏁版暟缁刟rgv锛岀涓€涓弬鏁癮rgv[0]蹇呴』鏄鍛戒护鏈韩锛屾渶鍚庝竴涓弬鏁癮rgv[size-1]蹇呴』涓?锛屽惁鍒欏皢鎵ц澶辫触銆?/li>
xv6绯荤粺璋冪敤娴佺▼
Lab涓system call鐨勪娇鐢ㄥ緢绠€鍗曪紝鐪嬭捣鏉ュ拰鏅€氬嚱鏁拌皟鐢ㄥ苟娌℃湁浠€涔堝尯鍒紝浣嗗疄闄呬笂鐨勮皟鐢ㄦ祦绋嬫槸杈冧负澶嶆潅鐨勩€傛垜浠緢瀹规槗浜х敓涓€浜涚枒闂細绯荤粺璋冪敤鐨勬暣涓敓鍛藉懆鏈熷叿浣撴槸浠€涔堟牱鐨勶紵鐢ㄦ埛杩涚▼鍜屽唴鏍歌繘绋嬩箣闂存槸濡備綍鍒囨崲涓婁笅鏂囩殑锛熺郴缁熻皟鐢ㄧ殑鍑芥暟鍚嶃€佸弬鏁板拰杩斿洖鍊兼槸濡備綍鍦ㄧ敤鎴疯繘绋嬪拰鍐呮牳杩涚▼涔嬮棿浼犻€掔殑锛?/p>
1.鐢ㄦ埛鎬佽皟鐢?/strong>
鍦ㄧ敤鎴风┖闂达紝鎵€鏈塻ystem call鐨勫嚱鏁板0鏄庡啓鍦?code>user.h涓紝璋冪敤鍚庝細杩涘叆usys.S
鎵ц姹囩紪鎸囦护锛氬皢瀵瑰簲鐨勭郴缁熻皟鐢ㄥ彿锛坰ystem call number锛夌疆浜庡瘎瀛樺櫒a7涓紝骞舵墽琛宔call鎸囦护杩涜绯荤粺璋冪敤锛屽叾涓嚱鏁板弬鏁板瓨鍦╝0~a5杩?涓瘎瀛樺櫒涓€俥call鎸囦护灏嗚Е鍙戣蒋涓柇锛宑pu浼氭殏鍋滃鐢ㄦ埛绋嬪簭鐨勬墽琛岋紝杞€屾墽琛屽唴鏍哥殑涓柇澶勭悊閫昏緫锛岄櫡鍏ワ紙trap锛夊唴鏍告€併€?/p>
2.涓婁笅鏂囧垏鎹?/strong>
涓柇澶勭悊鍦?code>kernel/trampoline.S涓紝棣栧厛杩涜涓婁笅鏂囩殑鍒囨崲锛屽皢user杩涚▼鍦ㄥ瘎瀛樺櫒涓殑鏁版嵁save鍒板唴瀛樹腑锛堜繚鎶ょ幇鍦猴級锛屽苟restore锛堟仮澶嶏級kernel鐨勫瘎瀛樺櫒鏁版嵁銆傚唴鏍镐腑浼氱淮鎶や竴涓繘绋嬫暟缁勶紙鏈€澶氬绾?4涓繘绋嬶級锛屽瓨鍌ㄦ瘡涓繘绋嬬殑鐘舵€佷俊鎭紝proc缁撴瀯浣撳畾涔夊湪proc.h锛岃繖涔熸槸xv6瀵筆CB锛圥rocess Control Block锛夌殑瀹炵幇銆傜敤鎴风▼搴忕殑瀵勫瓨鍣ㄦ暟鎹皢琚殏鏃朵繚瀛樺埌proc->trapframe
缁撴瀯涓€?/p>
3.鍐呮牳鎬佹墽琛?/strong>
瀹屾垚杩涚▼鍒囨崲鍚庯紝璋冪敤trap.c/usertrap()
锛屾帴鐫€杩涘叆syscall.c/syscall()
锛屽湪璇ユ柟娉曚腑鏍规嵁system call number鎷垮埌鏁扮粍涓殑鍑芥暟鎸囬拡锛屾墽琛岀郴缁熻皟鐢ㄥ嚱鏁般€傚嚱鏁板弬鏁颁粠鐢ㄦ埛杩涚▼鐨?code>trapframe缁撴瀯涓幏鍙?a0~a5)锛屽嚱鏁版墽琛岀殑缁撴灉鍒欏瓨鍌ㄤ簬trapframe鐨刟0瀛楁涓€傚畬鎴愯皟鐢ㄥ悗鍚屾牱闇€瑕佽繘绋嬪垏鎹紝鍏坰ave鍐呮牳瀵勫瓨鍣ㄥ埌trapframe->kernel_*
锛屽啀灏唗rapframe涓殏瀛樼殑user杩涚▼鏁版嵁restore鍒板瘎瀛樺櫒锛岄噸鏂板洖鍒扮敤鎴风┖闂达紝cpu浠庝腑鏂缁х画鎵ц锛屼粠瀵勫瓨鍣╝0涓嬁鍒板嚱鏁拌繑鍥炲€笺€?/p>
鑷虫锛岀郴缁熻皟鐢ㄥ畬鎴愶紝鍏辩粡鍘嗕簡涓ゆ杩涚▼涓婁笅鏂囧垏鎹細鐢ㄦ埛杩涚▼ -> 鍐呮牳杩涚▼ -> 鐢ㄦ埛杩涚▼锛屽悓鏃朵即闅忕潃涓ゆCPU宸ヤ綔鐘舵€佺殑鍒囨崲锛氱敤鎴锋€?-> 鍐呮牳鎬?-> 鐢ㄦ埛鎬併€?/p>
瀹為獙浠g爜锛?a href="https://github.com/zhayujie/xv6-riscv-fall19">https://github.com/zhayujie/xv6-riscv-fall19
鍘熸枃閾炬帴锛?a href="https://zhayujie.com/mit6828-lab-util.html">https://zhayujie.com/mit6828-lab-util.html
!--more-->以上是关于MIT6.S081/6.828 瀹為獙1锛歀ab Unix Utilities的主要内容,如果未能解决你的问题,请参考以下文章
201771010142 寮犵嚂& 鏉ㄨ搲搴? 瀹為獙鍗佷竴 闆嗗悎