” 写作CIS 657编程、 辅导C/C++程序Syracuse University CIS 657: Principles of Operating Systems Spring 2021PA-5: Nachos System Calls for File I/O andMulti-programmingTotal points: 100 pts Due on Apr May 8, 2021 at 11:59 PMInstructor: Endadul HoqueImportant RemindersAcademic Integrity: Review the academic integrity policy (from the syllabus) and the course honor pledge; yousubmitted a signed pledge at the Beginning of the semester. For this programming assignment and the future ones,we will use moss ( httpss://theory.stanford.edu/~aiken/moss/), a system for detecting software similarity,extensively to detect plagiarism among this semesters submissions as well as previous years submissions (ifapplicable). Remember that violating academic integrity policy will significantly jeopardize your grade as you agreedon the honor pledge.The first step for success: As with every lab/PA handout/description, I strongly urge you to read every word verycarefully. You may be able to find answers to many questions about each task by carefully reading this document.FThe late submission policy is not applicable for this PA.Logistics1. This lab must be done individually, not in groups.2. This PA expects that you are already familiar with the Nachos code base and know how to build and runnachos and execute test programs. All these aspects were covered in the lab on Nachos.3. For this programming assignment (PA-5), you have to download an archive called student-pa5.tar.gz fromBlackboard. Upon extraction, you will see a directory named student/, which contains nachos/code/ directory.You have to add to and/or edit the existing Nachos source code.4. For all the following tasks (Task-1, …), you have to use the Linux server (lcs-vc-cis486.syr.edu) dedicatedfor this course. nachos will not compile and run on a different machine.6. Unless stated otherwise, you MUST NOT modify/edit/rename/move other existing files under the student/directory.7. Unless stated otherwise, you MUST NOT use any additional C/C++ library (e.g., math library) that requiresmodification to the compilation Commands included in the existing Makefiles.8. For the following task, you have to use the Linux server (lcs-vc-cis486.syr.edu) dedicated for this course.You must know how to remotely login to that machine, how to use terminal and how to copy files back andforth between your computer and the Linux server.Page 1 of 119. For this PA, you have to write a report as well. Your answers must be typed. Only one PDF file is allowedfor submission.10. Each task explains what you have to do and [R-1] dictates what you have to report about the task.11. You must follow the submission instructions.How to obtain the skeleton/source code for this PA?Obtain student-pa5.tar.gz from Blackboard. Unpack the package to find the skeleton code required for this PA.The extracted directory is named student/.$ tar xzf student-pa5.tar.gzGradingPlease make sure you follow the instructions provided throughout this handout; otherwise your assignment will notbe graded properly. Not Following instructions will result in loss of points. You must not modify/edit/rename/move other existing files/directories in student/ or in its sub-directories. Your solutions must execute on the Linux machine (lcs-vc-cis486.syr.edu), otherwise your solutions willnot be considered for grading. You must not change the order of the tasks on your report. Your must follow the submission instructions, or it will not be graded properly.Submission InstructionsYou have to submit (a) code and (b) report using Blackboard. Please follow the instructions shown below to prepareyour submission.Source codeFollow these instructions when you are ready to submit.1. First clean up your nachos/code/ directory as follows:$ cd student/nachos/code/$ cd build.linux/$ make distclean# Now clean up test-pa directory$ cd ../test-pa/$ make distclean2. Go to (or change directory cd into) your student/ directory. Now student/ should be your currentworking directory. To verify your current directory, try this$ basename `pwd`studentPage 2 of 113. Use your netid to create a compressed archive (netid-pa5.tar.gz) of your solution directory. For instance,if your netid is johndoe, create a compressed archive as follows:$ cd .. # moving to the parent directory$ mv student/ johndoe-pa5/$ tar czf johndoe-pa5.tar.gz johndoe-pa5/4. To verify the content of your compressed archive, try tar tvzf netid-pa5.tar.gz.5. oFinally, download your compressed archive file (netid-pa5.tar.gz). You have to submit this package.Report You have to write a report. Your answer must be typed. You can use any typesetting program (e.g., Word, LATEX),but you have to convert the document to a PDF. Only one PDF file is allowed for submission. Mention your full name and SU NetID on the PDF. Use Blackboard to submit your PDF.Use the Blackboard submission link for submitting two files: (a) your code package (netid-pa5.tar.gz) and(b) report (in PDF).List of Tasks for this PA1 Implement System Calls for File I/O (40 pts) 42 Nachos Multi-programming (60 pts) 52.1 Non-preemptive and Preemptive Scheduling (10 pts) . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.2 Preemptive Multi-Programming (20 pts) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.3 System Calls for Multi-programming (30 pts) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9A Some Useful Links 11o All the tasks must be done on the lcs-vc-cis486.syr.edu, because nachos will not compileand run on a different machine.For example, if your implementation fails test cases, we will look into your implementation andgrade both implementation and testing holistically.Page 3 of 11Task 1 Implement System Calls for File I/O (40 pts)You may want to revisit PA-4 and your solution to refresh memory on how to implement a system call.For this task, you have to implement the following system calls that the Nachos user programs can utilize for file I/Ooperations. [Hint: It is recommended that you implement these syscalls in the same order presented below.]1. int Create(char *name): This system call creates a Unix file with the name given by the parameter name,and returns 1 if successful, and -1 if not, to the user program.Implement another function that extracts the file name from the user program memory so that you can re-usethe name extraction function for other Nachos system calls,call to implement this Nachos system call inexception.cc. (See the documentation of Unix/Linux open() syscall)[Hint: Implement this system call first. The Nachos Open() system call implementation is very similar. ]2. OpenFileId Open(char *name): This system call opens the Unix file name and returns an OpenFileId(aka, a file descriptor) if the file (name) exists. This OpenFileId can be used to read from and write to the file.If the file doesnt exist, Open() returns -1 to the user program to indicate an error.call to implement this Nachos system call inexception.cc. (See the documentation of Unix/Linux open() syscall)Hint: You dont have to maintain a separate mapping between Nachos OpenFileIds and Unix filedescriptors. Instead you can simple return the same file descriptor that the Unix open(…) syscallreturns.3. int Write(char *buffer, int size, OpenFileId id): Utilize your implementation of Write() fromPA-4 and extend it so that it can now write to an already open Unix file (specified by id) as well as print out tothe screen. This function returns the number of bytes actually written or -1 on failure.call to implement this Nachos system call inexception.cc. (See the documentation of Unix/Linux write() syscall)4. int Read(char *buffer, int size, OpenFileId id): This system call reads size bytes from the openfile (specified by id) and stores them into buffer. On success, it returns the number of bytes actually read fromthe file (note: it can be 0 too), and on failure, it returns -1.For this task, implement only the case for reading from a file; you dont have to implement the case for readingfrom ConsoleInput (i.e., keyboard input).call to implement this Nachos system call inexception.cc. (See the documentation of Unix/Linux read() syscall)5. int Close(OpenFileId id): Close the file specified by id. It returns 1 on success and -1 on failure.call to implement this Nachos system call inexception.cc. (See the documentation of Unix/Linux close() syscall)What to report for Task 1?Try the list of tasks below in the Order and provide answer/screenshot as stated.Do not forget to re-build nachos if required and run it from build.linux/[R-1] (4 5 = 20 pts) For each system call mentioned above, provide at least one screenshot of your implementation.Your screenshots should be legible. Therefore, if the implementation of a system call requires morethan one screenshots, please provide them and mark them appropriately. [Hint: There should be at least 5screenshots.]Page 4 of 11[R-2] ((1 + 1 + 1) 4 = 12 pts) Build the test programs in test-pa/ if needed. Build nachos in build.linux/.test-pa/ includes four test files for the file systems calls: file-test0.c, file-test1.c, file-test2.cand file-test3.c.Explain what should be the results of running each of these test programs, file-test0.c, file-test1.c,file-test2.c and file-test3.c. Explain what features of the file system calls are tested with eachtest program. Run these test programs with Nachos. Provide a screenshot of the output of running eachprogram, and provide your explanations. Some of these programs are expected to create some local files.Make sure to include a screenshot of each file along with the program output.[R-3] (8 pts) Write a new test program file-test4.c of your own to test the returned values of calling Read()and Close(). For each of these system calls, your program should demonstrate one success case andone failure case. Your program should print out the appropriate messages on the screen as shownbelow: For Read(): Print Read(): Successful: read x bytes, where x is the value returned by Read() onsuccess. Print Read(): Failed: returned y, where y is the value returned by Read() on failure. For Close(): Print Close(): Successful: returned x, where x is the value returned by Close() onsuccess. Print Close(): Failed: returned y, where y is the value returned by Close() on failure.Your file-test4.c should printout the returned values (i.e., integers) as shown above. Refer to file-test3.c.Your file-test4.c needs to have a similar function to convert an integer (i.e., positive, 0, negative) to astring.Do not forget to edit test-pa/Makefile for this new file-test4.c as you did in PA-4. Otherwiseyou will not be able to compile this New test program.Your report should include:1. (3 pts) Explain why we cant simply use printf() functions or other Unix I/O system calls to printout those outputs from Nachos user programs.2. (4 + 1 = 5 pts) Provide screenshots of your file-test4.c and the output of running this testprogramTask 2 Nachos Multi-programming (60 pts)Task 2.1 Non-preemptive and Preemptive Scheduling (10 pts)Preliminary Requirements for this task Replace your current version of threadtest.cc in your Nachos directory with the one located atstudent/extra/threadtest.cc Turn off the timer device by commenting out this line alarm = new Alarm(randomSlice); inside theKernel::Initialize() function located in kernel.cc of your Nachos directory. Simply use //. By turningthe timer device off, you will be able to see more predictable outputs. Compile nachos in build.linux/Page 5 of 11What to report for Task 2.1?Try the list of tasks below in the order and provide answer/screenshot as stated.Do not forget to re-build nachos If required and run it from build.linux/ [R-1] (1 pt) Run the command ./nachos -K. You should see the same output as below.prompt ./nachos -K*** thread 0 looped 0 times*** thread 0 looped 1 times*** thread 0 looped 2 times*** thread 0 looped 3 times*** thread 0 looped 4 times*** thread 1 looped 0 times*** thread 1 looped 1 times*** thread 1 looped 2 times*** thread 1 looped 3 times*** thread 1 looped 4 times*** thread 2 looped 0 times*** thread 2 looped 1 times*** thread 2 looped 2 times*** thread 2 looped 3 times*** thread 2 looped 4 times*** thread 3 looped 0 times*** thread 3 looped 1 times*** thread 3 looped 2 times*** thread 3 looped 3 times*** thread 3 looped 4 times^CCleaning up after signal 2promptProvide a screenshot of your output. [R-2] (1 + 3 = 4 pts) Notice that kernel-currentThread-Yield(); in void SimpleThread(int which)is commented out (see threadtest.cc). Uncomment (delete //) to activate the line and recompileNachos and run it with the same command ./nachos -K. Provide a screenshot of the output. Explainwhy this output is different from the previous one in [R-1] of Task 2.1. [R-3] (1 + 4 = 5 pts) Activate the Timer Device: Now turn on the timer device by activating line alarm =new Alarm(randomSlice); in Kernel::Initialize() of kernel.cc. Recompile Nachos and run itwith the same Command ./nachos -K. The output looks like: (The order of these lines in your output maydiffer)prompt ./nachos -K*** Thread 0 looped 0 times*** thread 1 looped 0 times*** thread 2 looped 0 times*** thread 3 looped 0 times*** thread 0 looped 1 times*** thread 1 looped 1 times*** thread 3 looped 1 timesPage 6 of 11*** thread 0 looped 2 times*** thread 1 looped 2 times*** thread 2 looped 1 times*** thread 3 looped 2 times*** thread 0 looped 3 times*** thread 1 looped 3 times*** thread 2 looped 2 times*** thread 3 looped 3 times*** thread 1 looped 4 times*** thread 2 looped 3 times*** thread 3 looped 4 times*** thread 0 looped 4 times*** thread 2 looped 4 times^CCleaning up after signal 2promptNotice that after activating the timer device, the output should look different from the previous one.Provide a screenshot of your output. Explain why this output is different from the one you observed whenthe timer was disabled (in [R-2] of Task 2.1).Task 2.2 Preemptive Multi-programming (20 pts)The current version of Nachos can run only one user program at a time. To enable Nachos load and run multipleprograms concurrently, we need to overcome the following three limitations. First Limitation: Nachos must be able to accept multiple user program names. Second Limitation: Nachos must be able to create an individual thread for each user program. Third Limitation: Nachos must be able to load multiple programs and create address space for each programin the main memory.For this task, you have to resolve the First and Second limitations. However, to resolve the third limitation, anew implementation addrspace.cc and addrspace.h have been provided in student/extra/. These newaddrspace.[cc|h] files implement a simple contiguous memory management scheme that can load multipleprograms in the memory without overwriting. Use the diff command to see the differences between the newaddrspace.[cc|h] and the original files. With these new addrspace.[cc|h] files, the memory management (asolution to the third limitation) is done for you. You have to replace the original addrspace.[cc|h] with the newversion located in student/extra/.Do not forget to replace the original addrspace.[cc|h] with the new version located in student/extra/before working on this taskTask 2.2.1 (7 pts) Overcoming the first limitationRecall that the Distribution version of Nachos runs a user-level program from the main thread. See RunUserProg ()in threads/main.cc. By default, Nachos can read only one user program name that is provided after the -x flag.No matter how many you specify with multiple -x flags, it can only remember and handle one user program.Page 7 of 11What to report for Task 2.2.1?Try the list of tasks below in the order and provide answer/screenshot as stated.Do not forget to re-build nachos if required and run it from build.linux/ [R-1] (4 pts) Modify threads/main.cc so Nachos can take many user programs via multiple -x flags (e.g.,./nachos -x ../test-pa/prog1 -x ../test-pa/prog2).[Hint: Do not hard code the user program names. Do not hard code or limit on how many -x flags can beprovided. The number of -x flags can vary. You can utilize the List class (lib/list.[cc|h]) provided innachos/code/, instead of implementing your own list type data structure or using a data structure from theC++ STL library]Provide a screenshot of your edits in main.cc. Explain what have you changed/inserted. [R-2] (2 pts) At this stage, if you test your implementation, you will not observe multiple user programs runningconcurrently, because you havent resolved the second limitation yet.Then how to test? You have to comment out the RunUserProg(userProgName); line of the main(…)in the threads/main.cc and add some extra printing code to see if nachos prints out the provided userprogram names correctly. Then test your implementation. An example output is shown below. Provide ascreenshot of your outputprompt./nachos -x ../test-pa/prog1 -x ../test-pa/prog2Program [0] = ../test-pa/prog1Program [1] = ../test-pa/prog2^CCleaning up after signal 2 [R-3] (1 pt) Now (a) turn off or remove those print out messages you added in the previous step [R-2]of Task 2.2.1 and (b) uncomment the RunUserProg(userProgName); line in main.cc. In other words,you are reverting the changes you made in [R-2] to test your implementation. Provide a screenshot ofyour code.Dont miss this step ( [R-3] of Task 2.2.1, otherwise the following task(s) will not work properly.Task 2.2.2 (13 pts) Overcoming the second limitationWhat to report for Task 2.2.2?Try the list of tasks below in the order and provide answer/screenshot as stated.Do not forget to re-build nachos if required and run it from build.linux/ [R-1] (1 pt) Revisit threads/main.cc. Which block of code in main.cc creates the address space to run a userprogram when you run ./nachos -x ../test-pa/prog1? [R-2] (1 + 1 = 2 pts) Carefully study the Fork() function defined in thread.[cc|h].1. What is the role of the first argument of the function Fork()?2. If we use a pointer to the function RunUserProg() as the first argument of Fork(), what will bethe effect?Page 8 of 11 [R-3] (6 pts) Carefully consider all the exercises above. With the new addrspace.[cc|h] files, Nachos canload multiple user programs in the main memory whenever you create an address space for each programto be executed (dictated by the multiple -x flags). The RunUserProg() function in main.cc shows anexample on how to create an address space for one user program. It loads the executable file in the codesegment and executes the user program. Notice that the AddressSpace::Execute() function neverreturns. Recall that the Current Nachos can only run one user program thread at a time.To load and run multiple user programs, your task is to create a new thread for each user program.Modify main.cc so it creates a user-level thread for each program specified by each -x flag. Provide oneor more screenshots of your implementation/edits in threads/main.cc.[Hint: Study carefully how threadtest.cc creates multiple threads. Pay aention to the first argument ofFork() and think about which function pointer should be the first argument to run a user program.] [R-4] (2 pts) Re-build nachos and test programs in test-pa/. Run nachos with command./nachos -x ../test-pa/prog1 -x ../test-pa/prog2. Your nachos must be able to run all userprograms provided in the command line. Provide a screenshot of the output. [R-5] (2 pts) Re-build nachos and test programs in test-pa/, if necessary. Run nachos with command./nachos -x ../test-pa/write -x ../test-pa/prog1 -x ../test-pa/prog2. Your nachosmust be able to run all user programs provided in the command line. Provide a screenshot of the output.Task 2.3 System Calls for Multi-programming (30 pts)At this stage, your nachos can execute multiple user programs provided in the command line arguments (usingmultiple -x flags). In this task, you are going to implement some additional system calls (Exec() and Join()) thatuser programs can use to spawn new processes. (In Nachos, Exec() is a combination of Unix fork() and exec()whereas Join() is similar to Unix waitpid()).[Hint: It is recommended that you implement these syscalls in the same order presented below.]1. SpaceId Exec(char* path_to_exec_name): this syscall takes one string argument that is the path to thefilename of a user program. The way Exec() creates threads is similar to what you did for the -x flag inTask 2.2.2. When a user program calls Exec(), Nachos kernel creates a new thread and a new address space forthe executable filename given as the argument. This new thread should execute the user program loaded intothe address space. (Recall RunUserProg()).On success, Exec() needs to return a SpaceId (defined in userprog/syscall.h) of this new thread, whichcan be used as the argument of Join(). SpaceId for each thread must be unique but not necessarily random.On failure, however, Exec() returns -1.Hint: For SpaceId, think about creating a pid for each Thread. You must make sure that each pid isunique but not necessarily random.2. int Join(SpaceId id): This system call helps the caller thread (say, Thread A) running the user programjoin another thread (say, Thread B, running a different program) specified by the argument id.On success, Thread A will stay blocked inside the Join() function while Thread B is alive and will returnfrom Join() once Thread B has finished its execution; in this case, Join() should return 0.On failure, Join() should return -1.HintsHints on how to implement Join(): READ CAREFULLYSpaceId is an integer; so given a SpaceId, you have to find out the corresponding thread object. YouPage 9 of 11have to implement the lookup mechanism by using a data structure, say, a thread table or a threadlist. (You can utilize the Nachos List class that you used in Task 2.2.1)Inside Join(), the caller thread (Thread A) needs to be associated with the to-be-joined thread (ThreadB) so that Thread B can wake up Thread A when Thread B is about to finish. After the successfulassociation, Thread A calls Thread::Sleep() to get itself blocked. However, if it fails, Thread Ashould not be blocked and Join() should return with -1.[Check the comment before Thread::Sleep() implementations. You should notice that it expects theinterrupts to be disabled when called. Check line 86 in threads/synch.cc for how to call Sleep() andlook at the surrounding code on how to disable and enable interrupts.]When Thread A wakes up, it assumes that Thread B has finished and therefore it can safely return 0from Join().When Thread B is finishing its execution, Thread B must wake up all the threads that have previouslyjoined it. In our example, Thread A has joined Thread B, and therefore Thread B must wake upThread A.By now, you may have realized that you need another mechanism, say a waiting-list, for each thread tokeep track of which threads are waiting for this thread to finish.When Thread A wants to associate itself with Thread B, Thread A simply adds itself to the ThreadBs waiting-list. The terminating thread (i.e., Thread B), in turn, can wake up each thread in its waitinglist by using Scheduler::ReadyToRun(). You have to implement this waking-up logic insideThread::Finish().What to report for Task 2.3?Try the list of tasks below in the order and provide answer/screenshot as stated.Do not forget to re-build nachos if required and run it from build.linux/ [R-1] (6 pts for Exec() + 10 pts for Join() = 16 pts) For each system call mentioned above, provide at least onescreenshot of your implementation. Your screenshots should be legible. Therefore, if the implementationof a system call requires more than on screenshots, please provide them and mark them appropriately.Points for the screenshot(s) of each system call are specified along with the description of thesystem call (see the syscalls in Task 2.3) [R-2] (14 pts) Build your nachos and the test programs in test-pa/, if necessary. There are three programs:prog3.c, prog3b.c and prog4.c. prog3 is for testing Exec(), prog3b is for testing both Exec() andJoin() together, and prog4 is called by the other two and is not intend to be executed directly.1. (3 pts) Explain what these user program, prog3.c, prog3b.c and prog4.c, do.2. (2 + 3 = 5 pts) Run the excutable of prog3 test program as ./nachos -x ../test-pa/prog3.Provide a screenshot of your output. Explain the output. A sample output is shown below:prompt ./nachos -x ../test-pa/prog3Hello from prog4Hello from prog3. Child process id: 2Exit system call made by ../test-pa/prog3Hello from prog4Page 10 of 11Hello from prog4Hello from prog4Hello from prog4Bye from prog4Exit system call made by ../test-pa/prog4^CCleaning up after signal 2promptNote that you may have Different id for the child process. But it is expected that prog3 should not waitfor prog4 to finish as shown above.3. (2 + 4 = 6 pts) Run the excutable of prog3b test program as ./nachos -x ../test-pa/prog3b.Provide a screenshot of your output. Explain the output. A sample output is shown below:prompt ./nachos -x ../test-pa/prog3bHello from prog4Hello from prog4Hello from prog4Hello from prog4Hello from prog4Bye from prog4Exit system call made by ../test-pa/prog4Hello from prog3b. Child process id: 2Exit system call made by ../test-pa/prog3b^CCleaning up after signal 2promptNote that you may have different id for the child process. But it is expected that prog3 must wait forprog4 to finish as shown above.Appendix A: Some Useful Links请加QQ:99515681 或邮箱:99515681@qq.com WX:codehelp
“
添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导。