0% found this document useful (0 votes)
2 views7 pages

Network Programming LAB 2

The document outlines a lab exercise for network programming involving the creation and use of FIFOs (named pipes) in C. It includes instructions for writing and executing two source code files, fifo_write.c and fifo_read.c, as well as explanations of the differences between FIFOs and pipes, and various file opening modes in C. Additionally, it provides solutions to questions regarding FIFO creation, program execution, and file handling modes.
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)
2 views7 pages

Network Programming LAB 2

The document outlines a lab exercise for network programming involving the creation and use of FIFOs (named pipes) in C. It includes instructions for writing and executing two source code files, fifo_write.c and fifo_read.c, as well as explanations of the differences between FIFOs and pipes, and various file opening modes in C. Additionally, it provides solutions to questions regarding FIFO creation, program execution, and file handling modes.
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/ 7

Network Programming LAB 2

Sl. No Name as appeared in Taxila BITS ID No.

1. Aniket Srivastava 2022wb86633

1)Username changed: -

Enter the below source code in text


editor and save with name fifo_write.c

Enter the below source code in text editor and save with name fifo_read.c
• In the Terminal-1 run below commands:
o ls -ltr à to view the files you have created
o gcc -o write fifo_write.c à To compile the program
o ./writeà To execute the program
• In the Terminal-2 run below commands:
o ls -ltr à to view the files you have created
o gcc -o read fifo_read.c à To compile the program
o ./read à To execute the program
1)Run the program as said and record the output of each run with screenshot covering username.

2) Which line of the code is responsible for creation of fifo and what is the name of FIFO created?
How are FIFO’s are different from pipes?

Sol: mkfifo(myfifo, 0666);

Difference between FIFOs and pipes:

Pipes:

Pipes are a form of IPC (Inter-Process Communication) that are used for communication between
related processes (i.e., processes that have a common ancestor, such as parent-child processes).

Pipes are anonymous, meaning they do not have a name and exist only for the duration of the
communication.

A pipe is created using pipe() and can only be used within a single process's context.

FIFOs (Named Pipes):

FIFOs are similar to pipes but are named, meaning they exist as a file in the filesystem, and can be
accessed by multiple processes, whether they are related or not.

They are useful for communication between unrelated processes since they are identified by a
name.

A FIFO is created using mkfifo(), and the FIFO file persists in the filesystem even after the process has
finished using it.

3) Which line of the code is responsible for creation of fifo and what is the name of FIFO created?
How are FIFO’s are different from pipes?

Solution:-
Yes, we can use FIFOs for communication between two unrelated processes. FIFOs are designed for
communication between processes that may not share a common ancestor (i.e., unrelated
processes). This is possible because FIFOs are named pipes, meaning they are represented as files in
the filesystem, and processes can open and communicate through them independently.

4) Which line of the code is responsible for creation of fifo and what is the name of FIFO created?
How are FIFO’s are different from pipes?

Solution:-

Yes, the program runs continuously until it is terminated by the user because of the infinite loop
(while(1)).

Explanation:

• The while(1) creates an infinite loop, meaning the code inside the loop will keep executing
indefinitely unless explicitly interrupted (e.g., by the user pressing Ctrl+C or closing the
program).

• Inside the loop, the program continuously reads data from the FIFO, processes it, and then
writes a response back to the FIFO. As long as this cycle keeps repeating, the program will
continue running.

5) What are the different modes on which files can be opened? Give syntax for each one of you
mentioned and explain.

Solution:-

a) O_RDONLY: Open file for reading only.

Syntax: int fd = open("filename", O_RDONLY);

Explanation: Opens the file in read-only mode. The file descriptor returned is used to read from the
file.

b) O_WRONLY: Open file for writing only.

Syntax: int fd = open("filename", O_WRONLY);

Explanation: Opens the file in write-only mode. The file descriptor returned is used to write to the
file.

c) O_RDWR: Open file for both reading and writing.

Syntax: int fd = open("filename", O_RDWR);

Explanation: Opens the file for both reading and writing. This mode allows both operations on the
file.
d) O_CREAT: Create the file if it does not exist.

Syntax: int fd = open("filename", O_CREAT, 0666);

Explanation: If the file does not already exist, this flag creates it. The third argument (0666) specifies
the file permissions for the created file.

e) O_EXCL: Ensures that the file is created only if it does not exist. If the file exists, open() will fail.

Syntax: int fd = open("filename", O_CREAT | O_EXCL, 0666);

Explanation: This flag is used together with O_CREAT to ensure that the file is created only if it
doesn't already exist. If the file exists, open() returns an error.

f) O_TRUNC: If the file exists and is opened for writing, it will be truncated to zero length.

Syntax: int fd = open("filename", O_WRONLY | O_TRUNC);

Explanation: If the file exists, it is truncated to zero length when opened in write mode.

g) O_APPEND: Open file for appending. Data is written to the end of the file.

Syntax: int fd = open("filename", O_WRONLY | O_APPEND);

Explanation: When writing, data will be appended to the end of the file rather than overwriting it.

h) O_NONBLOCK: Open file in non-blocking mode.

Syntax:int fd = open("filename", O_RDONLY | O_NONBLOCK);

Explanation: The file operations will not block the process. If the file cannot be read or written to
immediately, the system call will return with an error instead of blocking the process.

Lab Usage:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy