Lab9
Lab9
BS Software Engineering
Fall-2024
Page 1
Lab Policy and Rules:
1. 100% attendance is mandatory. In case of an emergency, you can avail yourself of up to 3 absences.
So, don't waste your absences , save them for emergencies. If you are debarred from the course due to
low attendance, do not come to me to correct it.
2. Disturbing the class environment during lab sessions. such as by talking, using mobile phones, eating,
etc. will result in penalties (e.g., deduction of marks).
3. Lab tasks will not be accepted if you are absent. If you miss a lab, you cannot submit that lab task.
4. Lab demos for each lab task will be conducted at the end of each session. If you miss the demo for
any reason, no retake will be allowed, and that lab will be marked as 0.
5. All quizzes will be unannounced. Be prepared for surprise quizzes. Quizzes will cover content from
previous labs as well as theory lectures from the corresponding theory course.
6. You can take help from the internet for lab tasks, but simply copying and pasting without
understanding will be marked as 0. You should be able to explain the syntax or material used in your
tasks.
7. Do not ask for personal favors. If you have concerns, such as short attendance, please speak with the
relevant authority (e.g., academics).
8. Students on warning: Now is the time to study and earn a good grade. Do not ask for extra marks at
the end if you are unable to clear your warning.
Page 2
Page 3
Lab 09: Dup System Calls
The dup and dup2 system calls in Linux are used to duplicate file descriptors.
The new file descriptor returned by dup is the lowest-numbered unused file descriptor.
Both file descriptors (the original and the new one) refer to the same open file description, so they share
the same file offset and file status flags.
Syntax:
int dup(int oldfd);
Example:
Page 4
It duplicates the file descriptor using dup, creating a new file descriptor (fd_copy).
Both fd and fd_copy refer to the same file, so writing through either affects the file.
Output:
Syntax:
int dup2(int oldfd, int newfd);
Example:
Page 5
It uses dup2 to duplicate fd onto stdout (file descriptor 1). 0 is for stdin (Standard input i.e keyboard), 1
for stdout (Standard output), 2 for stderr (Standard error).
Any write to stdout (like printf) will now go to test.txt instead of the terminal.
The dup2(fd, 1) command duplicates the file descriptor fd onto file descriptor 1, which is normally
stdout. This means any output that would normally go to the terminal (stdout) will be redirected to the
file represented by fd.
Output:
In dup2(fd, X), the second argument (X) can be any valid file descriptor. Here are the common values
you might use:
1 – Standard Output (stdout): Normally, this outputs to the terminal. Using dup2(fd, 1), you can redirect
the output to a file.
2 – Standard Error (stderr): Normally, error messages go to the terminal. You can redirect errors to a file
using dup2(fd, 2).
Page 6
Example using stdout and stderr:
Both the standard output and the error message are written to output.txt instead of the terminal.
Page 7
The program opens a file called input.txt in read-only mode.
fgets(buffer, sizeof(buffer), stdin) reads from the file instead of the terminal.
Contents of input.txt:
Page 8
Output:
When the program reads from stdin, it actually reads from input.txt because of the redirection with
dup2(fd, 0).
The file's content (Hello, world!) is read and then printed to stdout.
Page 9
Tasks:
1. Use dup() to duplicate a file descriptor for a file, store it in a variable fd_dup, then use
fd_dup in dup2() to copy it into stdout and redirect the output of printf to the file. It
means if we write printf(“Helllo world”); then it should not display on terminal but
instead it should be written into file.
2. Use dup2() to duplicate the file descriptor for stdin, so input can be taken from a file
instead of the terminal.
Use fgets(buffer, sizeof(buffer), stdin);
Usually when we use fgets(buffer, sizeof(buffer), stdin); then it takes input from stdin
which is set to keyboard so it takes input from keyboard and stores it in buffer variable
but when you duplicate the file descriptor of a file into stdin then it will take reads data
from a file and stores it in buffer.
4. Use unnamed pipe to send data from child to parent and parents then prints it. Don’t
use write() system call instead use printf so that if you write printf(“Messagge from
child”); then it shouldn’t be displayed on console but is should be written into write end
of unnamed pipe and parents then reads it using read end of pipe and displays it on
console.
Page 10