0% found this document useful (0 votes)
12 views4 pages

OSlab Asgn2 Shell Design 2023

Uploaded by

xahage9739
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

OSlab Asgn2 Shell Design 2023

Uploaded by

xahage9739
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Operating Systems Laboratory (CS39002)

Spring Semester 2022-2023

Assignment 2: Use of syscall

The assignment will be given in two parts. This is Part 1.

Assignment given on: January 25, 2022


Assignment deadline: February 07, 2022, 11:59 PM [for both parts]
____________________________________________________________________________________________________
● You learned about syscalls in your lectures. Syscalls are function calls that can be
used by your program (i.e., user space program) to let OS perform privileged
tasks for you (e.g., reading directly from the keyboard or sending data to your
printer). Specifically, the following syscalls in Linux might be useful for you in
this assignment and in general.
○ dup
○ excelp
○ fork
○ signal
○ pipe
○ select
○ poll
○ open, read, write
○ chmod
● In this assignment, we will build a Linux-type shell with some basic and some
advanced functionalities.
____________________________________________________________________________________________________
Implement a shell that will run as an application program on top of the Linux kernel. The
shell will accept user commands (one line at a time), and execute the same. The
following features must be implemented:

a) Run an external command


The external commands refer to executables that are stored as files. They have to
be executed by spawning a child process and invoking execlp() or some similar
system calls. Example user commands:
./a.out myprog.c
cc –o myprog myprog.c
ls -l
b) Run an external command by redirecting standard input from a file
The symbol “<” is used for input redirection, where the input will be read from
the specified file and not from the keyboard. You need to use a system call like
dup() or dup2() to carry out the redirection. Example user command:
./a.out < infile.txt
sort < somefile.txt
c) Run an external command by redirecting standard output to a file
The symbol “>” is used for output redirection, where the output will be written
to the specified file and not to the screen. You need to use a system call like dup()
or dup2() to carry out the redirection. Example user commands:
./a.out > outfile.txt
ls > abc

d) Combination of input and output redirection


Here we use both “<” and “>” to specify both types of redirection. Example user
command:
./a.out < infile.txt > outfile.txt

e) Run an external command in the background with possible input and


output redirections
We use the symbol “&” to specify running a command in the background. The
shell prompt will appear and the next command can be typed while the said
command is being executed in the background. Example user commands:
./a.out &
./myprog < in.txt > out.txt &

f) Run several external commands in the pipe mode


The symbol “|” is used to indicate pipe mode of execution. Here, the standard
output of one command will be redirected to the standard input of the next
command, in sequence. You need to use the pipe() system call to implement this
feature. Example user commands:
ls | more
cat abc.c | sort | more

g) Interrupting commands running in your shell (using signal call)


● Implement a feature to halt a command running in your shell during
runtime. For instance, if the user presses "Ctrl - c" while a program is
executing, the program should stop executing, and the shell prompt
should reappear. Note that the shell should not stop if the user presses
"Ctrl - c".

● Implement a feature to move a command in execution to the background.


If the user presses "Ctrl - z" while a program is executing, the program
execution should move to the background and the shell prompt should
reappear.
Implementation Help:
For redirecting the standard input or output, you can refer to the book: “Design of the
Unix Operating System” by Maurice Bach. Actually, the kernel maintains a file descriptor
table or FDT (one per process), where the first three entries (index 0, 1 and 2)
correspond to standard input (stdin), standard output (stdout), and standard error
(stderr). When files are opened, new entries are created in the table. When a file is
closed, the corresponding entry is logically deleted. There is a system call dup(xyz),
which takes a file descriptor xyz as its input and copies it to the first empty location in
FDT. So if we write the two statements: close(stdin); dup(xyz); the file descriptor xyz
will be copied into the FDT entry corresponding to stdin. If the program wants to read
something from the keyboard, it will actually get read from the file corresponding to xyz.
Similarly, to redirect the standard output, we have to use the two statements:
close(stdout); dup(xyz);
Normally, when the parent forks a child that executes a command using execlp() or
execvp(), the parent calls the function wait(), thereby waiting for the child to terminate.
Only after that, it will ask the user for the next command. However, if we want to run a
program in the background, we do not give the wait(), and so the parent asks for the
next command even while the child is in execution.
A pipe between two processes can be created using the pipe() system call, followed by
input and output redirection. Consider the command: ls | more. The parent process
finds out there is a pipe between two programs, creates a pipe, and forks two child
processes (say, X and Y). X will redirect its standard output to the output end of the pipe
(using dup()), and then call execlp() or execvp() to execute ls. Similarly, Y will redirect
its standard input to the input end of the pipe (again using dup()), and then call
execlp() or execvp() to execute more. If there is a pipe command involving N
commands in general, then you need to create N-1 pipes, create N child processes, and
connect each pair of consecutive child processes by a pipe.

Submission Guideline:
● Create a single program for the assignment, and name it
Assignment2_<groupno>_<roll no. 1>_< roll no. 2>_< roll no. 3>_< roll no.
4>.c or .cpp (replace <groupno> and <roll no.> by your group number
and roll numbers ), and upload it.
● You must show the running version of the program(s) to your assigned
TA during the lab hours.
Evaluation Guidelines:
Total marks for this assignment is 100. Part 1 and Part 2 will be of 50 marks each.
Items Marks
Part 1
Process creation and running an external command 8
Redirection of stdin 8
Redirection of stdout 3
Redirection of stdin and stdout together 3
Running an external command in background with I/O 8
redirection
Running several external commands in pipe mode 10
Interrupting commands running in your shell 10
Total 50
Part 2 (to be declared)

You might also like