πŸ’₯πŸ”₯ MIRROR MODE ENGAGED


🧠 04_fd_mirror_fanout

πŸ” FD Mirror Fanout

What happens when you dup() the same FD over and over?
Do they share the same offset?
Do they interfere?
Let’s mirror one FD into a fan of duplicates and test the behavior.


πŸ“„ 04_fd_mirror_fanout.c

(Perfect for single file β€” visual, observable, syscall-rich)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
 
#define MAX_MIRRORS 8
 
int	main(void)
{
	int		base_fd;
	int		mirrors[MAX_MIRRORS];
	char	buf[16];
	ssize_t	rd;
	int		i;
 
	base_fd = open("fanout.txt", O_CREAT | O_RDWR | O_TRUNC, 0644);
	if (base_fd == -1)
	{
		perror("open");
		return (1);
	}
	dprintf(base_fd, "HelloFromFanout!");
 
	printf("πŸ”— Base FD: %d\n", base_fd);
 
	// Create mirrors
	for (i = 0; i < MAX_MIRRORS; i++)
	{
		mirrors[i] = dup(base_fd);
		printf("πŸ“Ž Mirror %d β†’ FD %d\n", i, mirrors[i]);
	}
 
	printf("\nπŸ“– Reading from each mirror:\n");
	for (i = 0; i < MAX_MIRRORS; i++)
	{
		memset(buf, 0, sizeof(buf));
		rd = read(mirrors[i], buf, sizeof(buf) - 1);
		if (rd == -1)
			perror("read");
		else
			printf("  [%d] Read %ld bytes β†’ \"%s\"\n", mirrors[i], rd, buf);
	}
 
	printf("\nπŸ“– Reading from base FD:\n");
	memset(buf, 0, sizeof(buf));
	lseek(base_fd, 0, SEEK_SET); // Reset to beginning
	rd = read(base_fd, buf, sizeof(buf) - 1);
	printf("  [base] Read %ld bytes β†’ \"%s\"\n", rd, buf);
 
	// Close all
	close(base_fd);
	for (i = 0; i < MAX_MIRRORS; i++)
		close(mirrors[i]);
 
	return (0);
}

πŸ’‘ What You’ll See

Sample Output:

πŸ”— Base FD: 3
πŸ“Ž Mirror 0 β†’ FD 4
πŸ“Ž Mirror 1 β†’ FD 5
...
πŸ“– Reading from each mirror:
  [4] Read 15 bytes β†’ "HelloFromFanout!"
  [5] Read 0 bytes β†’ ""
  [6] Read 0 bytes β†’ ""
  ...
πŸ“– Reading from base FD:
  [base] Read 15 bytes β†’ "HelloFromFanout!"

πŸ” Observations

ActionResult
dup(fd)Shares same underlying open file description
All mirrorsShare file offset (i.e., position in file)
First readConsumes the file β€” next reads = empty
lseek() resets only one FDBut applies to all mirrors too (they’re not truly independent)

🧠 Key Learnings

  • dup() does not clone an FD object β€” it clones an FD number pointing to the same kernel struct

  • All dup’d FDs share:

    • πŸ” File offset

    • βœ‹ Lock state

    • πŸ” Access mode (O_RDONLY, etc.)

  • Duplicates are only useful for:

    • Redirecting stdin/out/err

    • Closing one end safely

    • Playing syscall shell games


πŸ“€ Pro Tip

Try this:

write(mirrors[0], "A", 1);
read(mirrors[1], buf, 1);

πŸ’₯ They’re reading/writing in sync.


βœ… Compile & Run:

gcc 04_fd_mirror_fanout.c -o fanout && ./fanout

Then:

cat fanout.txt

πŸš€ Up Next?

  • [[05_fd_exhaustion_test/]] β†’ how many FDs can we open before the system screams

  • [[06_malloc_after_fork_glitch/]] β†’ enter the forking memory corruption glitch vault

Say go.
You’re now studying the quantum entanglement of file descriptors.
πŸ§ πŸ”πŸ“Ž