首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
CS3103编程讲解、Operating Systems程序辅导、讲解C/C++语言程序 讲解留学生Processing|辅导Web开发
项目预算:
开发周期:
发布时间:
要求地区:
CS3103: Operating Systems
Spring 2021
Programming Assignment 1
1 Introduction
The purpose of this assignment is to help you better understand process management and system calls in Linux
environment. To achieve this goal, you will use C/C++ to implement a mini shell that supports running
programs not only in the foreground but also in the background: if a program is designated to run in the
foreground, then your shell should load and run the program, and wait for the program to terminate before
available for the next command line; in contrast, the shell does not wait for the program to terminate before
awaiting the next command line if the program runs in the background.
2 Requirements
2.1 Prompt for User Input
Your mini shell should display the prompt for user input. For example, if running your mini shell in the Linux
terminal,
$ . / s h e l l
it should prompt
sh >
indicating being ready for user input.
2.2 Foreground Execution of a Program
Your mini shell should allow a program to run in the foreground, which has the following form:
sh >fg [ path o f e x e c u t a bl e f i l e ] [ a l i s t o f arguments ]
For example, if you want to run the test program mentioned in Section 4 in the foreground, you can type:
sh >fg . / test h e l l o 2 5
where ./test is the path of the test program, and hello 2 5 are the arguments. When running a process in
the foreground, the shell is unable to accept the next command from the user until the foreground process
terminates. At any time, at most ONE process can run in the foreground.
2.3 Background Execution of Programs
The shell should also allow programs to run in the background:
sh >bg [ path o f e x e c u t a bl e f i l e ] [ a l i s t o f arguments ]
Unlike the fg command, the shell is able to accept the next command immediately after executing the bg
command. Below is an example:
sh >bg . / test h e l l o 2 5
sh >
Also note that the shell should allow ANY number of processes to exist in the background simultaneously.
1
2.4 Completion of Programs
When a foreground process or a background process completes and exits normally, your shell should display a
message. For example:
sh >fg . / test h e l l o 2 5
h e l l o
h e l l o
h e l l o
h e l l o
h e l l o
P ro c e s s 4096 completed
sh >
2.5 Ctrl-C and Ctrl-Z
When your mini shell program is running in the Linux terminal, it will receive a SIGINT (SIGTSTP) signal if
typing Ctrl-C (Ctrl-Z) on the keyboard. Your shell program is expected to handle the signal and terminate
(stop) the foreground process. If there is no foreground process running in the shell, then the signal should
have no effect.
For each process that is terminated (stopped) by Ctrl-C (Ctrl-Z), the shell should display the corresponding
message. For example, if a process with PID = 4096 is terminated by Ctrl-C, then the shell is expected to
display the following message:
sh >P ro c e s s 4096 t e rmina t e d
Similarly, if a process with PID = 4096 is stopped by Ctrl-Z, then the shell is expected to display the following
information:
sh >P ro c e s s 4096 s topped
2.6 List All Processes
The list command should list the information of all processes that are running in the background as well as all
the stopped processes. The information should include PID, state, path, and arguments. Note that terminated
processes are NOT supposed to appear in the list. Therefore, you need to reap the process if it completes and
exits normally, or it is terminated by Ctrl-C. Otherwise, the list command may behave unexpectedly. Here is
an example with correct behavior:
sh >bg . / test t e s t 1 2 5
sh >bg . / test t e s t 2 5 10
sh > l i s t
4096: running . / test t e s t 1 2 5
4097: running . / test t e s t 2 5 10
sh >fg . / test t e s t 3 2 5
P ro c e s s 4098 te rmi na t e d
sh >fg . / test t e s t 4 5 10
P ro c e s s 4099 s topped
sh > l i s t
4096: running . / test t e s t 1 2 5
4097: running . / test t e s t 2 5 10
4099: s topped . / test t e s t 4 5 10
sh >
2.7 Exit the Shell
The exit command should cause the shell program to exit. But before that, make sure that all processes have
been terminated and reaped. Here is an example where there are two processes in the background before
executing exit:
2
sh >ex it
P ro c e s s 4096 te rmi na t e d
P ro c e s s 4097 te rmi na t e d
$
2.8 Handling Unexpected Inputs
You can never make any assumption about what users will input in your shell. For example, a user may input
an empty line, an invalid command, or a wrong path of the executable file. You are encouraged to come up
with these unexpected inputs as many as possible and handle them properly.
3 Hints
• Study the man pages of the system calls used in your program. For example, the following command
displays the man pages of kill():
$ man 2 k i l l
• Use fork() and execvp() so that the parent process accepts user input and the child process executes
foreground/background processes.
• If a process is going to run in the foreground, use sigsuspend() or sleep() in a busy loop until the
foreground process terminates.
• Some signals are useful in this assignment. For example, typing Ctrl-C will trigger a SIGINT signal
automatically, and typing Ctrl-Z will trigger a SIGTSTP signal automatically. Besides, sending a
SIGTERM signal can terminate a process. Another signal that you might be interested in is the
SIGCHLD sigal, which will be sent to the parent process by the kernel whenever a child process stops or
terminates. Your program needs to handle these signals correctly. To handle signals, use signal() to
bind a signal to the corresponding handler that you implement. By the way, kill() is a useful system call
for sending a signal (don’t get confused by its name).
• When a process terminates for any reason, the process becomes a “zombie” until it is reaped by its
parent. To reap a terminated child process (if any), use waitpid(-1, &status, WNOHANG |
WUNTRACED). This system call will return immediately, with a return value 0 if none of the children
has stopped or terminated, or with a return value equal to the PID of one of the stopped or terminated
children.When the parent reaps the terminated child, the kernel passes the child’s exit status to the
parent and then discards the terminated process, at which point it ceases to exist. Also note that the exit
status may be useful when handling the SIGCHLD signal.
• When you run your shell from the standard Linux shell, your shell is running in the foreground process
group of the standard Linux shell. If your shell then creates a child process, by default that child will
also be a member of the foreground process group. Since typing Ctrl-C sends a SIGINT to every process
in the foreground group, typing Ctrl-C will send a SIGINT to your shell, as well as to every process
that your shell created. This is obviously not the desired behavior, since we hope only our shell can
receive this signal and handle the signal properly. Here is the workaround: After the fork(), but before
the execvp(), the child process should call setpgid(0, 0), which puts the child in a new process group
whose group ID is identical to the child’s PID. This ensures that there will be only one process, your
shell, in the foreground process group of the standard Linux shell. When you type Ctrl-C, your own shell
should catch the resulting SIGINT and handle it properly.
• When you use fork(), it is important that you do not create a fork bomb, which easily eats up all the
resources allocated to you. If this happens, you can try to use the command “kill” to terminate your
processes (http://cslab.cs.cityu.edu.hk/supports/unix-startup-guide). However, if you cannot
log into your account any more, you need to ask CSLab for help to kill your processes.
3
4 Helper Programs
4.1 args.cpp
This example program shows how to read a line from terminal, as well as parsing (cutting) the string using the
strtok() function. To compile the program, use the following command:
$ g++ a rg s . cpp −l r e a d l i n e −o a rg s
4.2 test.cpp
This program can be used to test your shell. It takes three arguments: the first argument is a single word to be
displayed repeatedly; the second argument is the number of seconds between two consecutive displays of the
word; the last argument is the number of times the word to be displayed. For example, the following command
displays the word “running” 5 times in 2-second interval:
$ . / test running 2 5
5 Marking
Important Note: You program will be tested on our CSLab Linux servers (cs3103-01, cs3103-02, cs3103-03).
You should describe clearly how to compile and run your program in the text file. If an executable file
cannot be generated and running successfully on our Linux servers, it will be considered as
unsuccessful.
• fg: 20%
• bg: 20%
• Ctrl-C: 10%
• Ctrl-Z: 10%
• list: 10%
• exit: 10%
• Reaping terminated processes correctly: 10%
• Handling unexpected inputs: 5%
• Programming style and in-program comments: 5%
6 Submission
• This assignment is to be done individually or by a group of two students. You are encouraged to discuss
the high-level design of your solution with your classmates but you must implement the program on
your own. Academic dishonesty such as copying another student’s work or allowing another student to
copy your work, is regarded as a serious academic offence.
• Each submission consists of two files: a source program file (.cpp file) and a text file containing user
guide, if necessary, and all possible outputs produced by your program (.txt file).
• Write down your name(s), eid(s) and student ID(s) in the first few lines of your program as comment.
• Use your student ID(s) to name your submitted files, such as 5xxxxxxx.cpp, 5xxxxxxx.txt for individual
submission, or 5xxxxxxx_5yyyyyyy.cpp, 5xxxxxxx_5yyyyyyyy.txt for group submission. Only ONE
submission is required for each group.
• Submit the files to Canvas.
• The deadline is 11:00am, 18-FEB-2021 (Thursday). No late submission will be accepted.
4
7 Questions?
• This is not a programming course. You are encouraged to debug the program on your own first.
• If you have any questions, please submit your questions to Mr ZHOU Zikang via the Discussion board
“Programming Assignment #1”.
• To avoid possible plagiarism, do not post your source code on the Discussion board.
• If necessary, you may also contact Mr ZHOU Zikang at zikanzhou2-c@my.cityu.edu.hk.
5
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
代写dts207tc、sql编程语言代做
2024-12-25
cs209a代做、java程序设计代写
2024-12-25
cs305程序代做、代写python程序...
2024-12-25
代写csc1001、代做python设计程...
2024-12-24
代写practice test preparatio...
2024-12-24
代写bre2031 – environmental...
2024-12-24
代写ece5550: applied kalman ...
2024-12-24
代做conmgnt 7049 – measurem...
2024-12-24
代写ece3700j introduction to...
2024-12-24
代做adad9311 designing the e...
2024-12-24
代做comp5618 - applied cyber...
2024-12-24
代做ece5550: applied kalman ...
2024-12-24
代做cp1402 assignment - netw...
2024-12-24
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!