0% found this document useful (0 votes)
52 views15 pages

Os Assignment4A 21103027

The document contains an assignment submission for an Operating Systems course, with 5 questions answered regarding Linux processes and Bash shell commands. It explores process trees, built-in vs external Bash commands, I/O redirection using pipes and files, and implements a Collatz sequence generator using fork(). The answers provide details on how various Linux and Bash features are implemented under the hood.

Uploaded by

Nalin Gupta
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)
52 views15 pages

Os Assignment4A 21103027

The document contains an assignment submission for an Operating Systems course, with 5 questions answered regarding Linux processes and Bash shell commands. It explores process trees, built-in vs external Bash commands, I/O redirection using pipes and files, and implements a Collatz sequence generator using fork(). The answers provide details on how various Linux and Bash features are implemented under the hood.

Uploaded by

Nalin Gupta
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/ 15

Assignment 4A

Operating System
Course ID-CS1402
4th Semester 2nd Year

Submitted By-Kamalpreet Singh


Nalin kumar Bachhal
gupta
SID-21103029
21103027
Assignment No-4A
Submitted to-Dr. Sanjeev Sofat
1)Open a bash shell. Find its PID. Write down the process
tree starting from the first init-process (PID = 1) to your bash
shell, and describe how you obtained it. You may want to use
the pstree command.
Ans- Process ID for Bash-

The Path from Root process to bash- pstree -s -p <ProcessID>


Process ID is obtained by running the ps command.
For getting the complete Process Tree, we run the pstree
command.
The Complete Tree –
2) Consider the following commands you can type in the bash
shell- cd, ls, history, ps. Which of these are system programs
executed by the bash shell, and which are implemented by
the bash code itself?
Ans- When we execute a command in the Bash shell, the
shell will look for an executable program in the system's
PATH directories that matches the command name. If the
executable program is found, the shell will execute it and
provide any necessary arguments.

On the other hand, when a command is implemented by the


Bash code itself, it means that the command is built-in to the
shell and is implemented directly by the shell's code. This
allows the shell to execute the command more quickly and
with greater control over its behaviour.
"cd" -> Bash code "ls" -> Bash shell
"history" -> Bash code "ps" -> Bash shell
Searching for Directories for these commands-
3) Run the following command in bash
$./cpu-print > /tmp/tmp.txt &
Find out the PID of the new process spawned to run this
command. Go to the proc folder of this process, and describe
where its I/O file descriptors 0, 1, 2 are pointing to. Can you
describe how I/O redirection is being implemented by bash?
Ans-
In Bash, we can change where a command's input and output
goes using symbols like the greater than sign (>) to send
output to a file, the less than sign (<) to read input from a file,
or the double greater than sign (>>) to add output to a file
that already exists.
When Bash sees one of these symbols, it sets up a special
way to handle the input or output.
In terms of I/O redirection , when Bash encounters an I/O
redirection operator in a command, it sets up the
appropriate file descriptors to handle the redirection. For
example, if you use > /tmp/tmp.txt to redirect the output of
a command to a file, Bash will open the file /tmp/tmp.txt and
associate it with file descriptor 1 (which is the standard
output descriptor) or in simple words , if you use the greater
than sign (>) to send output to a file like /tmp/tmp.txt, Bash
opens the file and sets up a way to write output to it.
4) Run the following command with cpu-print $./cpu-print
| grep hello
Once again, identify which processes are spawned by bash,
look at the file descriptor information in their proc folders,
and use it to explain how pipes work in bash.
Ans- The pipe (|) symbol in the command tells Bash to
connect the standard output of cpu-print to the standard
input of grep hello, effectively creating a unidirectional
communication channel, or pipe, between the two processes.
(A pipe is a mechanism that allows two processes to
communicate with each other by connecting the standard
output of one process to the standard input of the other
process.)

Bash creates a pipe by calling the pipe() system call, which


creates two file descriptors: one for the read end of the pipe
and one for the write end. It then sets up the file descriptors
for the two child processes to enable the communication
channel.

In the case of the command $./cpu-print | grep hello, Bash


sets up the pipe so that the write end is associated with the
standard output file descriptor of cpu-print and the read end
is associated with the standard input file descriptor of grep
hello. When cpu-print writes output to its standard output,
the data is written to the write end of the pipe. This data can
then be read by grep hello from its standard input, which is
connected to the read end of the pipe.

When the two child processes are running, they read and
write data to their respective file descriptors, and the pipe
allows them to communicate and share data with each other.
5) The Collatz conjecture concerns what happens when we
take any positive integer n and apply the following algorithm-
n = n/2 if n is even
=3×n + 1, if n is odd
The conjecture states that when this algorithm is continually
applied, all positive integers will eventually reach 1. For
example, if n = 35, the sequence is
35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1
Implement using any language, use the fork() system call that
generates this sequence in the child process. The starting
number will be provided from the command line. For
example, if 8 is passed as a parameter on the command line,
the child process will output 8, 4, 2, 1.
Because the parent and child processes have their own
copies of the data, it will be necessary for the child to output
the sequence. Have the parent invoke the wait() call to wait
for the child process to complete before exiting the program.
Perform necessary error checking to ensure that a positive
integer is passed on the command line.
Ans-

You might also like