投影片 1

OS Assignment 3
Assignment3 Requirements
30% in the total grade
 Nachos:

◦ Four code writing jobs

Linux:
◦ Two code writing jobs

Choose one of them
NachOS assignment

Multiprogramming
◦ You have to write a scheduling program of RR
(Round Robin). Round-robin (RR) is one of
the scheduling algorithms for processes in an
operating system, which assigns time slices to
each process in equal portions and in circular
order, handling all processes without priority.
◦ You have to print the progress of several
processes, and show your time slice.
NachOS assignment

Thread
◦ You have to implement a Unix-like instruction
ps (process status). The ps utility displays a
header line, followed by lines containing
information about all of your processes that
have controlling terminals.
◦ You have to show USER, PID, TIME (excusing
time) and COMMAND at least.
NachOS assignment
•
Memory
– You have to implement a Unix-like instruction
top.
– The top provides an ongoing look at processor
activity in real time. It displays a listing of the
most CPU-intensive tasks on the system, and can
provide an interactive interface for manipulating
processes. The top utility display and update
information about the top cpu processes.
– You have to show USER, PID, SIZE (memory),
CPU time used and COMMAND at least. And it’s
ranking by CPU time used.
NachOS assignment

File system
◦ You have to implement a Unix-like instruction
ls –l (list directory contents).
◦ Please not only list directory contents, but
also show USER (who create), SIZE, TIME
(create time) and NAME (directory name or
file name) at least.
Linux assignment

Part I
◦ You must write a game about the racing of
turtles and rabbits. Your assignment should
include several processes, which represent the
turtles and the rabbits, and one process prints
the progress of the turtles and rabbits and
handles signal
◦ Turtle (⊕)
rabbit ( ¥ )
Game rules
Rabbit walks five paces per second
 Turtle walks two paces per second
 User press ctrl+z to generate a random
event and the even must elapse five
seconds
 Print the status of the racing on the
screen per second
 The winner is the first arriving sixty paces

Some event types

Increasing the speed of the rabbit
◦ 1/10 probability
◦ Rise from five to eight paces per second

Increasing the speed of the turtle
◦ 2/5 probability
◦ Rise from two to five paces per second

Stop the rabbit
◦ 1/5 probability

Wake the rabbit up
◦ 1/10 probability

Decreasing the speed of the turtle
◦ 1/5 probability
◦ Change from two to one paces per second
◦ Change from five to two paces per second
Result Example

EX:
-------------------------------------------¥ r1
¥r2
⊕t1
-------------------------------------------rabbit1 24 paces rabbit2 50 paces
turtle1 42 paces
Requirements
After compiling, the final program must be
named “hw3_race”
 You are responsible for writing a makefile
suitable for your whole program
 Usage example

◦ ./hw3_race 2 3
◦ Create two rabbits and three turtles
Linux assignment

Part II
◦ You must implement an odd-and-even sort
algorithm to sort 10 integers.
◦ Handle odd-and-even sort with some
semaphores to handle odd processes and
even processes
◦ You must print the status of each step of
sorting on the screens
◦ Please use semaphore to control your sort!
Odd-And-Even Sort
It is a comparison sort based on bubble sort with
which it shares many characteristics.
 It functions by comparing all (odd, even)-indexed pairs
of adjacent elements in the list and, if a pair is in the
wrong order (the first is larger than the second) the
elements are switched.
 The next step repeats this for (even, odd)-indexed
pairs (of adjacent elements). Then it alternates
between (odd, even) and (even, odd) steps until the
list is sorted.
 It can be thought of as using parallel processors, each
using bubble sort but starting at different points in
the list (all odd indices for the first step). -[Wikipedia]

hw3_sort 1 hw3_sort2 hw3_sort 3
hw3_sort 4
hw3_sort 5
3
2
1
6
5
4
10
9
7
8
2
3
1
6
4
5
9
10 7
8
2
1
3
4
6
5
9
7
10 8
1
2
3
4
5
7
9
8
6
… etc
10
Semaphore

Needed header files
◦
◦
◦
◦

<sys/ipc.h>
<sys/shm.h> (share memory relateded)
<sys/sem.h>
<sys/types.h>
Needed functions
◦ int semget(key_t key,int nsems,int semflg);
◦ int semctl(int semid,int semnum,int cmd,union
semun arg);
◦ int semop(int semid,struct sembuf *spos,int
nspos);
Semaphore Functions
• int semget(key_t key , int nsems , int semflg);
◦ semid = semget(SHMKEY, 1 , 0777|IPC_CREAT);

int semctl(int semid,int semnum,int
cmd,union semun arg);
◦ semctl(semid, 0, SETVAL, 1)

int semop(int semid,struct sembuf
*spos,int nspos);
◦ semop(semid, &buf, 1)
Part II Requirements



After compiling, the final program must be named “hw3_sort”
You are responsible for writing a makefile suitable for your whole
program
Use five screens to show your homeworks





Start screen: screen
Create screen: ctrl + a+ c
Switch to next screen: ctrl + a + n
Switch to previous screen: ctrl + a+ p
Please close your screens after finishing your jobs
◦ exit exit exit exit…

Usage examples
◦
◦
◦
◦
./hw3_sort (input 10 numbers)
./hw3_sort 1 (the first windows…etc)
./hw3_sort 2 (the second windows…etc)
…etc
linux ~/ $ ./hw3_sort
Please enter 10 intergers
num1:3
num2:2
num3:1
num4:6
num5:5
num6:4
num7:10
num8:9
num9:7
num10:8
enter 10 numbers finish. waiting for the child process...
Sorted
value0 = 1
value1 = 2
value2 = 3
value3 = 4
value4 = 5
value5 = 6
value6 = 7
value7 = 8
value8 = 9
value9 = 10
linux ~/ $ ./hw3_sort 1
not sorted: addr1[0] = 3 ,addr1[1] = 2
sorted : addr1[0] = 2 ,addr1[1] = 3
not sorted: addr1[1] = 3 ,addr1[2] = 1
sorted : addr1[1] = 1 ,addr1[2] = 3
not sorted: addr1[0] = 2 ,addr1[1] = 1
sorted : addr1[0] = 1 ,addr1[1] = 2
not sorted: addr1[1] = 2 ,addr1[2] = 3
sorted : addr1[1] = 2 ,addr1[2] = 3
not sorted: addr1[0] = 1 ,addr1[1] = 2
sorted : addr1[0] = 1 ,addr1[1] = 2
not sorted: addr1[1] = 2 ,addr1[2] = 3
sorted : addr1[1] = 2 ,addr1[2] = 3
not sorted: addr1[0] = 1 ,addr1[1] = 2
sorted : addr1[0] = 1 ,addr1[1] = 2
not sorted: addr1[1] = 2 ,addr1[2] = 3
sorted : addr1[1] = 2 ,addr1[2] = 3
not sorted: addr1[0] = 1 ,addr1[1] = 2
sorted : addr1[0] = 1 ,addr1[1] = 2
not sorted: addr1[1] = 2 ,addr1[2] = 3
sorted : addr1[1] = 2 ,addr1[2] = 3
linux ~/ $ ./hw3_sort 2
not sorted: addr1[2] = 1 ,addr1[3] = 6
sorted : addr1[2] = 1 ,addr1[3] = 6
not sorted: addr1[3] = 6 ,addr1[4] = 4
sorted : addr1[3] = 4 ,addr1[4] = 6
not sorted: addr1[2] = 3 ,addr1[3] = 4
sorted : addr1[2] = 3 ,addr1[3] = 4
not sorted: addr1[3] = 4 ,addr1[4] = 5
sorted : addr1[3] = 4 ,addr1[4] = 5
not sorted: addr1[2] = 3 ,addr1[3] = 4
sorted : addr1[2] = 3 ,addr1[3] = 4
not sorted: addr1[3] = 4 ,addr1[4] = 5
sorted : addr1[3] = 4 ,addr1[4] = 5
not sorted: addr1[2] = 3 ,addr1[3] = 4
sorted : addr1[2] = 3 ,addr1[3] = 4
not sorted: addr1[3] = 4 ,addr1[4] = 5
sorted : addr1[3] = 4 ,addr1[4] = 5
not sorted: addr1[2] = 3 ,addr1[3] = 4
sorted : addr1[2] = 3 ,addr1[3] = 4
not sorted: addr1[3] = 4 ,addr1[4] = 5
sorted : addr1[3] = 4 ,addr1[4] = 5
linux ~/ $ ./hw3_sort 3
not sorted: addr1[4] = 5 ,addr1[5] = 4
sorted : addr1[4] = 4 ,addr1[5] = 5
not sorted: addr1[5] = 5 ,addr1[6] = 9
sorted : addr1[5] = 5 ,addr1[6] = 9
not sorted: addr1[4] = 6 ,addr1[5] = 5
sorted : addr1[4] = 5 ,addr1[5] = 6
not sorted: addr1[5] = 6 ,addr1[6] = 7
sorted : addr1[5] = 6 ,addr1[6] = 7
not sorted: addr1[4] = 5 ,addr1[5] = 6
sorted : addr1[4] = 5 ,addr1[5] = 6
not sorted: addr1[5] = 6 ,addr1[6] = 7
sorted : addr1[5] = 6 ,addr1[6] = 7
not sorted: addr1[4] = 5 ,addr1[5] = 6
sorted : addr1[4] = 5 ,addr1[5] = 6
not sorted: addr1[5] = 6 ,addr1[6] = 7
sorted : addr1[5] = 6 ,addr1[6] = 7
not sorted: addr1[4] = 5 ,addr1[5] = 6
sorted : addr1[4] = 5 ,addr1[5] = 6
not sorted: addr1[5] = 6 ,addr1[6] = 7
sorted : addr1[5] = 6 ,addr1[6] = 7
linux ~/ $ ./hw3_sort 4
not sorted: addr1[6] = 10 ,addr1[7] = 9
sorted : addr1[6] = 9 ,addr1[7] = 10
not sorted: addr1[7] = 10 ,addr1[8] = 7
sorted : addr1[7] = 7 ,addr1[8] = 10
not sorted: addr1[6] = 9 ,addr1[7] = 7
sorted : addr1[6] = 7 ,addr1[7] = 9
not sorted: addr1[7] = 9 ,addr1[8] = 8
sorted : addr1[7] = 8 ,addr1[8] = 9
not sorted: addr1[6] = 7 ,addr1[7] = 8
sorted : addr1[6] = 7 ,addr1[7] = 8
not sorted: addr1[7] = 8 ,addr1[8] = 9
sorted : addr1[7] = 8 ,addr1[8] = 9
not sorted: addr1[6] = 7 ,addr1[7] = 8
sorted : addr1[6] = 7 ,addr1[7] = 8
not sorted: addr1[7] = 8 ,addr1[8] = 9
sorted : addr1[7] = 8 ,addr1[8] = 9
not sorted: addr1[6] = 7 ,addr1[7] = 8
sorted : addr1[6] = 7 ,addr1[7] = 8
not sorted: addr1[7] = 8 ,addr1[8] = 9
sorted : addr1[7] = 8 ,addr1[8] = 9
linux ~/ $ ./hw3_sort 5
not sorted: addr1[8] = 7 ,addr1[9] = 8
sorted : addr1[8] = 7 ,addr1[9] = 8
not sorted: addr1[8] = 10 ,addr1[9] = 8
sorted : addr1[8] = 8 ,addr1[9] = 10
not sorted: addr1[8] = 9 ,addr1[9] = 10
sorted : addr1[8] = 9 ,addr1[9] = 10
not sorted: addr1[8] = 9 ,addr1[9] = 10
sorted : addr1[8] = 9 ,addr1[9] = 10
not sorted: addr1[8] = 9 ,addr1[9] = 10
sorted : addr1[8] = 9 ,addr1[9] = 10
Assignment3 Requirements
Please do your homework on the work
station (140.118.155.25)
 If you insist doing your homework on your
own computer, please make sure your
homework can run at the workstation
 Upload your web pages(introduction) to FTP
(ftp://140.118.19.57/homework3)
 If you do your homework on your own
computer, please also upload your source
code to FTP too.
 Due Date : 2009/6/2 11:59 p.m.
