0% found this document useful (0 votes)
8 views10 pages

Lab9

The document outlines the lab policies and rules for the CL 2006 Operating System course, emphasizing mandatory attendance, penalties for disruptions, and the importance of understanding lab tasks. It also details the dup and dup2 system calls in Linux for duplicating file descriptors, providing syntax, examples, and tasks for students to complete. Submission guidelines for lab tasks are specified, requiring a word document with code and screenshots in PDF or ODT format.

Uploaded by

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

Lab9

The document outlines the lab policies and rules for the CL 2006 Operating System course, emphasizing mandatory attendance, penalties for disruptions, and the importance of understanding lab tasks. It also details the dup and dup2 system calls in Linux for duplicating file descriptors, providing syntax, examples, and tasks for students to complete. Submission guidelines for lab tasks are specified, requiring a word document with code and screenshots in PDF or ODT format.

Uploaded by

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

CL 2006 OPERATING SYSTEM

BS Software Engineering
Fall-2024

Course Instructors:Syed Daniyal Hussain Shah, Hifza Umar


Course Coordinator: Syed Daniyal Hussain Shah

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.

dup System Call:


The dup system call duplicates an existing file descriptor.

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:

The program opens a file test.txt with write access.

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.

The file is written using both descriptors.

Both descriptors are closed.

Output:

dup2 System Call:


dup2 allows you to specify the new file descriptor. It duplicates the first file descriptor into the second
file descriptor.

Syntax:
int dup2(int oldfd, int newfd);

Example:

The program opens a file test.txt for writing.

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 output is redirected to the file.

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:

Common File Descriptors:


0 – Standard Input (stdin): Normally, this reads input from the terminal. If you redirect this to a file
using dup2(fd, 0), the file will become the input source.

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:

dup2(fd, 1): Redirects standard output to the file output.txt.

dup2(fd, 2): Redirects standard error to the same file.

Output in output.txt file:

Both the standard output and the error message are written to output.txt instead of the terminal.

Example: Redirecting stdin from a File:


This example opens a file for reading and then uses dup2 to redirect stdin so that when you use
functions like scanf or fgets, the input comes from the file instead of the keyboard.

Page 7
The program opens a file called input.txt in read-only mode.

dup2(fd, 0) redirects the standard input (file descriptor 0) to the file.

fgets(buffer, sizeof(buffer), stdin) reads from the file instead of the terminal.

The program prints the content it read from the file.

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.

3. Redirect stderr to a file using dup2() and simulate an error message.

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.

Task Submission Guidelines:


1. Make a word document and paste the code with output’s screenshot there and save it
as pdf or odt.
2. Include your name and roll no. at the front page.
3. Files other than pdf or odt will not be accepted and will be marked as 0.

Page 10

You might also like