0% found this document useful (0 votes)
12 views3 pages

Fifo Explanation

Uploaded by

karunakar
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)
12 views3 pages

Fifo Explanation

Uploaded by

karunakar
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/ 3

Step-by-Step Execution

1. Create a Named FIFO

c
CopyEdit
#define FIFO_NAME "/tmp/my_fifo"
if (mkfifo(FIFO_NAME, 0666) == -1) {
perror("mkfifo");
exit(EXIT_FAILURE);
}

 mkfifo() creates a named pipe (FIFO file) at /tmp/my_fifo.


 The permissions (0666) allow read and write access for all users.
 If the FIFO already exists, it returns an error.

2. Get User Input

c
CopyEdit
printf("Enter a message to send to the child process: ");
if (fgets(buffer, MAX_MESSAGE_SIZE, stdin) == NULL) {
perror("fgets");
exit(EXIT_FAILURE);
}
buffer[strcspn(buffer, "\n")] = 0; // Remove trailing newline

 User enters a message, which is stored in buffer.


 strcspn(buffer, "\n") removes the newline character added by fgets.

3. Create a Child Process

c
CopyEdit
pid = fork();

if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}

 fork() creates a child process.


 If fork() fails, an error is displayed.
4. Communication Between Parent & Child

Child Process (Reader)

c
CopyEdit
if (pid == 0) {
fifo_fd = open(FIFO_NAME, O_RDONLY); // Open FIFO for reading
if (fifo_fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}

read(fifo_fd, buffer, MAX_MESSAGE_SIZE); // Read from FIFO


printf("Child (Reader) received: %s\n", buffer);
close(fifo_fd); // Close FIFO after reading
}

 Opens FIFO file in read-only mode (O_RDONLY).


 Reads the message from the FIFO file.
 Prints the received message.
 Closes the FIFO file descriptor.

Parent Process (Writer)

c
CopyEdit
else {
fifo_fd = open(FIFO_NAME, O_WRONLY); // Open FIFO for writing
if (fifo_fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}

write(fifo_fd, buffer, strlen(buffer) + 1); // Write to FIFO


printf("Parent (Writer) sent: %s\n", buffer);
close(fifo_fd); // Close FIFO after writing
}

 Opens FIFO file in write-only mode (O_WRONLY).


 Writes the user input message to FIFO.
 Closes the FIFO file descriptor after writing.

5. Remove FIFO File

c
CopyEdit
unlink(FIFO_NAME);
 Removes the FIFO file from the filesystem after use.

Summary of Execution

1. Parent Process
o Creates a named FIFO file (/tmp/my_fifo).
o Takes input from the user.
o Creates a child process.
o Writes the message into FIFO.
o Closes the FIFO file descriptor.
2. Child Process
o Opens FIFO for reading.
o Reads the message from FIFO.
o Prints the received message.
o Closes the FIFO file descriptor.
3. After execution, the FIFO file is deleted.

Example Output
css
CopyEdit
Enter a message to send to the child process: Hello, Child!
Parent (Writer) sent: Hello, Child!
Child (Reader) received: Hello, Child!

You might also like