Network Programming LAB 2
Network Programming LAB 2
1)Username changed: -
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?
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 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:-
Explanation: Opens the file in read-only mode. The file descriptor returned is used to read from the
file.
Explanation: Opens the file in write-only mode. The file descriptor returned is used to write to the
file.
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.
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.
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.
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.
Explanation: When writing, data will be appended to the end of the file rather than overwriting it.
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:-