CS Operating Systems HOMEWORK
Spring 2017, COMP4100 H/W-3 (2/9) Due date (2/16) Part I:
1. The following is the C# sample code that shows two threads use mutex to communicate. Note mutex is a simplified semaphore. Refer to this piece of sample code, write a program that conduct the followings: (1) It contains two thread, t1 and t2; (2) t1 and t2 share a variable, myNum; (3) When myNum is empty (use 0 to show empty), t1 will generate a random
number between 1 and 99, and save to myNum; (4) When myNum is not empty, t2 will get this number and save it to an
array, myNumArray, in ascending order. (5) After t2 insert a new number in myNumArray, it also shows the result
on screen. // mutex sample code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; class Test { // Create a new Mutex. The creating thread owns the // Mutex. private static Mutex mut = new Mutex(true); private const int numIterations = 1; private const int numThreads = 2; static void Main() { // Create the threads that will use the protected resource. for (int i = 0; i < numThreads; i++) { Thread myThread = new Thread(new ThreadStart(MyThreadProc)); myThread.Name = String.Format("Thread{0}", i + 1); myThread.Start(); } // Wait one second before allowing other threads to // acquire the Mutex. Console.WriteLine("Creating thread owns the Mutex."); Thread.Sleep(1000); Console.WriteLine("Creating thread releases the Mutex.\r\n"); mut.ReleaseMutex(); //Console.ReadKey(); }
private static void MyThreadProc() { for (int i = 0; i < numIterations; i++) { UseResource(); } } // This method represents a resource that must be synchronized // so that only one thread at a time can enter. private static void UseResource() { // Wait until it is safe to enter. mut.WaitOne(); Console.WriteLine("{0} has entered the protected area", Thread.CurrentThread.Name); // Place code to access non-reentrant resources here. // do work here!! Thread.Sleep(500); Console.WriteLine("{0} is leaving the protected area\r\n", Thread.CurrentThread.Name); // Release the Mutex. mut.ReleaseMutex(); } } Part II: Login to Linux (Hyper-v Virtural machine), study Linux and following the following questions.
1.Which of these is a UNIX command which will list the contents of a directory?
A. pwd B. ls C. list D. files E. directory
2. To make a new directory, you would use which command?
A. directory B. create C. make D. mkdir E. dirmk
3. To remove a directory, you would use which command?
A. remove B. erase C. delete D. deldir E. rmdir
4. The command which terminate a process is called:
A. copy B. file C. filecopy D. cp E. kill
5. Partial output of ls –F command is:
Work1*
Work2/
Which of the following is correct?
A. Work2 is executable, Work1 is a directory B. Both are executable C. Work1* and Work2/ are two files D. Work1 is executable, Work2 is a directory E. None of above
6. To check the process status, which command you would use:
A. ps B. left C. cat D. quotas E. disk
7. The command, makedir, will create a sub-directory. True or false?
A. True B. False
8. _____________ is the command that changes the file permission.
A. pr B. top C. chmod D. head
10. The ________ command will display the absolute pathname for the directory that you are wokring in.
A. dir B. whereami C. pwd D. ls
11. In Fedora 20, a regular user, john, wants to become root user and do some administration tasks. Which of the following should he use?
A root
B super
C admin
D su root
12. In Fedora 20, which of the following display process state dynamically? A ps
B top
C more
D cat
13. In Fedora 20, a regular user, john, wants to check whether a program, DemoApp, is running or not. Which of the following should he use?
A find DemoApp
B whereis DemoApp
C man DemoApp
D ps –ax
14. In Fedora 20, a regular user, john, wants to check the options for ps command. He type man ps
Which of the following will terminate man command?
A c
B q
C x
D z