CPSC 457: Principles of Operating Systems Assignment 1 due May

CPSC 457: Assignment 1
CPSC 457: Principles of Operating Systems
Assignment 1
due May 28, 2015
Assignment Objectives
This an individual assignment. You must submit your own work. This assignment has two
objectives: (1) review important concepts covered in lectures so far, and (2) practice on C
programming in a Linux environment. For the programming question, please structure and
document your code properly. Start the assignment early and avoid procrastination.
Deadline
• Written part: 9:00am on May 28, 2015 (delivered in class).
• Programming part: 10:00pm on May 28, 2015 (uploaded on D2L).
Helps Available
• The second tutorial session in the week of May 20 will be dedicated to providing helps
on this assignment.
• Forum for Assignment 1 on Piazza provides a platform for online discussion for this
assignment. Cyriac James (TA) will be in charge of this forum.
• TA office hours: Cyriac James (TA) will offer two office hours, 2pm - 3pm on May 25
and May 27. You may find him at CT desk in MS computer lab.
• Cyriac James (TA) will be available at CT desk in MS computer lab from 3pm to 5pm
on May 28.
• The instructor is also available during office hours (TR, 12pm - 1pm) or by appointment.
PART 1: Written Questions (25 marks)
1. (5 marks) What is the difference between timesharing and multiprogramming systems?
2. (5 marks) Instructions related to accessing I/O devices typically can be executed in kernel mode but not in user mode. Give a reason why it is better to run these instructions
in kernel mode.
1
CPSC 457: Assignment 1
3. (5 marks) To a programmer, a system call looks like any other call to a library procedure. Is it important that a programmer know which library procedures result in
system calls? If so, why?
4. (5 marks) A computer system has enough room to hold five programs in its main
memory. These programs are idle waiting for I/O 40% of the time. What fraction of
the CPU time can be utilized if multiprogramming is used?
5. (5 marks) Is it possible for a process to go directly from ready state to blocking state?
What about going directly from blocking state to running state? Explain your answer.
PART 2: Programming Question (40 marks)
In this part of assignment, you must write a program in C or C++ language and
upload the source file (ps457.c or ps457.cpp) to D2L Dropbox.
ps is a Unix / Linux command that reports a snapshot of the current running programs,
called processes. For example, if you login to your RAC virtual machine and run ps, the
output will be similar to this:
[fedora@cpsc457 ~]$ ps
PID TTY
TIME CMD
17846 pts/0
00:00:00 bash
18113 pts/0
00:00:00 ps
where the PID column shows the process identifier. If you need more information about the
running programs including those run by the operating system, you can use ps -All and
the output will be like this:
[fedora@cpsc457 ~]$ ps
F S
UID
PID PPID
4 S
0
1
0
1 S
0
2
0
1 S
0
3
2
1 S
0
5
2
1 S
0
6
2
1 S
0
7
2
1 S
0
8
2
1 S
0
9
2
...
-All
C PRI NI ADDR SZ
0 80
0 - 12437
0 80
0 0
0 80
0 0
0 60 -20 0
0 80
0 0
0 -40
- 0
0 80
0 0
0 80
0 0
WCHAN
ep_pol
kthrea
smpboo
worker
worker
smpboo
rcu_gp
rcu_gp
TTY
?
?
?
?
?
?
?
?
TIME
00:00:04
00:00:00
00:00:00
00:00:00
00:00:06
00:00:00
00:00:00
00:00:04
CMD
systemd
kthreadd
ksoftirqd/0
kworker/0:0H
kworker/u8:0
migration/0
rcu_bh
rcu_sched
The goal of this programming question is to develop a customized version of command
ps, called ps457. To do that, you need to extract the information that the operating system
keeps about the processes. This information can be extracted by looking at a virtual directory
located at /proc. Along with some other information, there is a directory for each process
inside /proc, where the name of the directory is the id of the process.
2
CPSC 457: Assignment 1
[fedora@cpsc457 ~]$ ls /proc
1
185
280 36
78
10
186
284 37
8
1063
2
287 38
80
11
20
29
46
805
12
21
3
499 808
13
219
30
5
81
15
22
31
6
89
15064 220
310 63
9
16
22154 312 64
acpi
17
23
313 65
asound
17842 231
318 66
buddyinfo
17845 241
32
660 bus
17846 246
320 67
cgroups
179
25
322 7
cmdline
18
26
323 722 consoles
182
27
33
727 cpuinfo
18260 278
34
76
crypto
184
28
35
77
devices
diskstats
dma
dri
driver
execdomains
fb
filesystems
fs
interrupts
iomem
ioports
irq
kallsyms
kcore
keys
key-users
kmsg
kpagecount
kpageflags
loadavg
locks
mdstat
meminfo
misc
modules
mounts
mtrr
net
pagetypeinfo
partitions
sched_debug
scsi
self
slabinfo
softirqs
stat
swaps
sys
sysrq-trigger
sysvipc
timer_list
timer_stats
tty
uptime
version
vmallocinfo
vmstat
zoneinfo
In this list, we are only interested in directories where the name is numerical, i.e., a
process identifier. Inside each process information folder, we are interested in three files:
stat, statm and cmdline.
[fedora@cpsc457 /]$ cd /proc/17846
[fedora@cpsc457 17846]$ ls
attr
cpuset
loginuid
autogroup
cwd
map_files
auxv
environ maps
cgroup
exe
mem
clear_refs
fd
mountinfo
cmdline
fdinfo
mounts
comm
io
mountstats
coredump_filter limits
net
ns
numa_maps
oom_adj
oom_score
oom_score_adj
pagemap
personality
root
sched
sessionid
smaps
stack
stat
statm
status
syscall
task
timers
wchan
[fedora@cpsc457 17846]$ cat stat
17846 (bash) S 17845 17846 17846 34816 18673 4218880 2196 21462 0 19 5 6 32 26
20 0 1 0 27587284 118120448 516 18446744073709551615 4194304 5096916
140736478801008 140736478799688 139957905249852 0 65536 3686404 1266761467
18446744071579274898 0 0 17 0 0 0 0 0 0 7196144 7232144 29679616 140736478805624
140736478805630 140736478805630 140736478805998 0
3
CPSC 457: Assignment 1
[fedora@cpsc457 17846]$ cat statm
28838 516 419 221 0 120 0
[fedora@cpsc457 17846]$ cat cmdline
bash
In this assignment you must write a program that can browse the /proc directory and
extract a small set of information from aforementioned process report files.
Program Features
Your program supports features implemented by the following options:
• -p <pid> Display process information only for the process whose number is pid, hereafter called target process. If this option is not present then display the requested
information for all processes of the current user.
• -s Display the single-character state information about the target process(es). This
information is found in the stat file in process’s directory, looking at the state field,
which is the third space-separated field. If this option is not present, do not display
this information. For process 17846, the state is S:
17846 (bash) S 17845 17846 17846 ...
• -U Display the amount of user time consumed so far by the target process(es). This
information is found in the stat file in process’s directory, looking at the utime field,
which is the 14th field. This option defaults to be true, so if it is not present, then this
information is displayed. For process 17846, the value of utime is 5:
17846 (bash) S 17845 17846 17846 34816 18673 4218880 2196 21462 0 19 5 6
32 26 ...
• -S Display the amount of system time consumed so far by the target process(es). This
information is found in the stat file in process’s directory, looking at the stime field,
which is the 15th field. This option defaults to be true, so if it is not present, then this
information is displayed. For process 17846, the value of stime is 6:
17846 (bash) S 17845 17846 17846 34816 18673 4218880 2196 21462 0 19 5 6
32 26 ...
• -v Display the amount of virtual memory currently being used (in pages, we will
talk about memory pages when we cover Chapter 3) by the target process(es). This
information is found in the statm file in process’s directory, looking at the size field,
which is the first field. For process 17846, the value of size is 28838:
28838 516 419 221 0 120 0
• -c Display the command-line that started the target process(es). This information is
found in the cmdline file in process’s directory. Be careful on this one, because this
4
CPSC 457: Assignment 1
file contains a list of null (zero byte) terminated strings. This option defaults to be
true, so if it is not present, then this information is displayed. For process 17846, the
command line that started the process is bash.
Tip: You may also see this webpage for more information on process information files in
/proc: http://www.mjmwired.net/kernel/Documentation/filesystems/proc.txt
• For statm file format, see Table 1-3 (line 254)
• For stat file format, see Table 1-4 (line 269)
Program Output
If all the information is requested for a process, as follows, then the output should provide
each process in one line and each piece of information in one column, with the order shown
in this example:
[fedora@cpsc457 ~]$./ps457 -p 17846 -s -U -S -v -c
PID
state
utime
stime
vmem
cmdline
17846
S
5
6
28838
bash
[fedora@cpsc457 ~]$./ps457 -p 17846 -S
PID
stime
17846
6
As a reminder, if the process identifier is not given, your program must list all the
processes that have a representative numerical folder in /proc and print the information
according to the given input parameters. Please note that your program should accept the
input parameters in any given order. The order of columns in the output must not change.
Please make sure that the name of your source code file is “ps457.c” or
“ps457.cpp”.
Listing Files and Directories
To get the name of files and directories that are located in /proc, you are allowed to use
system function from standard C library (you will need to include stdlib.h), pass a shell
command such as ls to this function, and parse the return string. Trivially, you can also
use other standard C functions to browse the directory structure of Linux, such as readdir
function from dirent.h library.
Testing Your Program
First test the separate parts of your program, such as processing command line options,
listing process information directories in /proc, and reading and parsing the individual files
in a process’s /proc directory. Next, start assembling these pieces into a whole program,
testing the options one at a time.
5
CPSC 457: Assignment 1
Deliverables
Please upload your program (ps457.c or ps457.cpp) to D2L Dropbox before 10pm
on assignment due date (May 28). Note that you must test your programs on your
Fedora virtual machine on Rapid Access Cloud. The standard Fedora virtual machine on
RAC will be used to compile and test your programs by your TAs.
6