You are on page 1of 3

Lab 4 Report

Roll Number: 130050014, 130050031


-------------------------------------------------------------------------------------------------------------------------------

Ex 1:
Command: ps -aux|grep bash
Output: rawalkh+ 5965 0.0 0.0 16496 5908 pts/0

Ss 13:29 0:00 bash

Pid = 5965

Command: pstree -h
(-h flag highlights the path from root to current process).
Follow the highlighted processes from the "init" process to "bash" in the output.
The process tree is as follows:
init -> lightdm -> lightdm -> init -> xterm_bash
-------------------------------------------------------------------------------------------------------------------------------

Ex 2:
Executed by shell: ls, ps
Implemented by shell: cd, history
Go to the "/bin" directory, this directory stores the executables for the processes executed
on the system.
Here we can find binary files for "ls" and "ps".
-------------------------------------------------------------------------------------------------------------------------------

Ex 3:

Get the process id using "ps -aux|grep cpu1print.c"

Go to directory of file descriptors for the process (cpu1print), cd /proc/<pid>/fd

Command: ls -lt
Output:
lrwx------ 1 rawalkhirodkar ug13 64 Feb 1 14:12 0 -> /dev/pts/0
l-wx------ 1 rawalkhirodkar ug13 64 Feb 1 14:12 1 -> /tmp/tmp.txt
lrwx------ 1 rawalkhirodkar ug13 64 Feb 1 14:12 2 -> /dev/pts/0

Description:
The parent shell forks a child shell process which executes cpu1print.c.
According to the general description of the fork( ) function, the memory image of parent and child shell
processes are same after executing the fork( ).
After the fork, in the code describing the child process in shell's code ( "if(pid ==0)" section ), child takes as
argument the filename (from argv), this is the file ( "/tmp/tmp.txt" in this case) where the output is
supposed to be redirected. Initially child closes its file descriptor "1" (used for stdout).
Now child opens the file and the first file descriptor which is vacant is pointed to this file ("1" stdout in this
case).
Further child process invokes exec( ) with appropriate arguments to overwrite the child process's memory
image and loads the binary for the cpu1print.c
(Note: exec system call does not make any changes to the file descriptor array)
This creates the illusion that the child is writing to its stdout but instead it is writing to the intended
file (/tmp/tmp.txt)
-------------------------------------------------------------------------------------------------------------------------------

Ex 4:
Get pid of processes "cpu1print.c" and "grep" (same steps from Ex 3).
Pid of cpu1print.c is 9477:
Pid of grep is 9478:
(Note the consecutive allotment of process ids)

Process cpuprint1.c's file descriptor directory:


Command: ls -lt /proc/9477/fd/
Output:
lrwx------ 1 palakjain ug13 64 Feb 1 14:31 0 -> /dev/pts/2
l-wx------ 1 palakjain ug13 64 Feb 1 14:31 1 -> pipe:[715422]
lrwx------ 1 palakjain ug13 64 Feb 1 14:30 2 -> /dev/pts/2

Process grep's file descriptor directory:


Command: ls -lt /proc/9478/fd/
Output:
lr-x------ 1 palakjain ug13 64 Feb 1 14:30 0 -> pipe:[715422]
lrwx------ 1 palakjain ug13 64 Feb 1 14:30 1 -> /dev/pts/2
lrwx------ 1 palakjain ug13 64 Feb 1 14:30 2 -> /dev/pts/2

Description:
The parent bash spawns the children cpu1print.c and grep. Pipe has a read end and write end.
Before loading the binary memory image of the children, the parent (binary before exec call) points the
output file descriptor (1) for cpu1print.c to pipe and the input fd(0) for grep process to the other end of the
pipe.
Each child remains under the illusion that it is reading/writing to/from stdin/stdout. However, the 2
processes are actually communicating with each other using pipe [715422].

-------------------------------------------------------------------------------------------------------------------------------

Ex 5:
Add sleep (1) to server file.

-------------------------------------------------------------------------------------------------------------------------------

Ex 6:
Write standalone client get-one-file.c

-------------------------------------------------------------------------------------------------------------------------------

Ex 7:
Modify client to handle signals, file get-one-file-sig.c

You might also like