Skip to content

Latest commit

 

History

History
54 lines (28 loc) · 3.09 KB

shm_ip.md

File metadata and controls

54 lines (28 loc) · 3.09 KB

Basic Concept:

The code revolves around two main functions - store_IP and get_IP.

  1. store_IP is used to store an IP address in a shared memory region.
  2. get_IP fetches an IP address from this shared memory.

Shared memory is a technique that allows multiple processes on the same computer to share a single piece of memory. It's like having a shared whiteboard where one process writes something and the other processes can read from it.


Header Files:

  • These are like reference books in a library. Before you start writing a research paper (your program), you note down which books (header files) you'll be referencing. They give you tools and functionality that you can use in your code.

Function: store_IP:

Goal: This function's main task is to take a MAC address and its corresponding IP address and save the IP address in a special shared memory, using the MAC address as a unique key or identifier.

Here's a step-by-step breakdown:

  1. shm_open: Think of shared memory like a storage locker. Before you store anything, you need to create or access this locker. shm_open does just that. It tries to open a storage locker using the MAC address as its unique name.

  2. ftruncate: Once you have a locker, you need to decide how big it needs to be. This step determines the size of the locker based on the length of the IP address.

  3. mmap: Now that we have a storage locker of the right size, we need a way to access it. This function maps that shared memory region into our program's own memory space, giving us direct access to it.

  4. memset and memcpy: First, we make sure the storage space is clean (memset). Then, we copy the IP address into this shared memory (memcpy).

  5. munmap: After copying the IP address, we unmap (or disconnect) the shared memory region from our program's memory. It's like closing the door to the storage locker after you've put your stuff inside.

  6. close: Finally, we close our connection to the shared memory locker.

Function: get_IP:

Goal: Fetch the IP address that corresponds to a given MAC address from the shared memory.

  1. shm_open: This time, we're trying to access an already existing storage locker named after the MAC address.

  2. mmap: We map the storage locker to our program's memory, so we can read the IP address from it.

  3. memcpy: We copy the IP address from the shared memory to our local variable ip.

  4. munmap and close: Just like before, after accessing the locker's contents, we disconnect from it and close our connection.


In Simple Terms:

Imagine you have a massive parking lot (shared memory). Each car (IP address) has a unique license plate (MAC address).

The store_IP function's job is to park a car in the lot in a spot corresponding to its license plate. If someone asks where a car is, you can quickly point them to the correct spot using the license plate.

On the other hand, the get_IP function's role is to find the car in the parking lot based on its license plate and tell you the details of the car (like its color, make, or model, which in this analogy, is the IP address).