In Linux, can you use C shared memory to store pointers?

Updated on technology 2024-03-04
9 answers
  1. Anonymous users2024-02-06

    man shmget as well as shmat(2), shmctl(2), shmdt(2).

    If the operation you can refer to.

    Beginning Linux Programming, 4th example of shared memory.

  2. Anonymous users2024-02-05

    The error comparison is not obvious, the most important error is int shmid=shmget(key,4,ipc creat|ipc_excl|0660);The flag bit ipc excl in this line ensures that a new unique shared memory is created, and if it already exists (i.e. it has been used once before), it will return an error, so this flag bit is removed, and the rest is not very serious, ignoring that the shared memory is not deleted at the end, but only detached the shared memory. Modify ** as follows:

    #include

    #include

    #include

    #include

    #include

    int main()

    key_t key=ftok(".",100);

    if(key==-1){

    perror("ftok");

    return -1;

    int shmid=shmget(key,0,0);

    if(shmid==-1)

    perror("shmget");

    exit(-1);

    void*p=shmat(shmid,0,0);

    if(p==(void*)-1)

    perror("shmat");

    exit(-1);

    int *pi=p;

    printf("*pi=%d",*pi);

    shmdt(p);

    #include

    #include

    #include

    #include

    #include

    int main()

    key_t key=ftok(".",100);

    if(key==-1){

    perror("ftok");

    return -1;

    int shmid=shmget(key,4,ipc_creat|0660);

    if(shmid==-1)

    perror("shmget");

    exit(-1);

    void*p=shmat(shmid,0,0);

    if(p==(void*)-1)

    perror("shmat");

    exit(-1);

    int *pi=p;

    pi=100;

    shmdt(p);

  3. Anonymous users2024-02-04

    I can read 100 with your **, and besides, what is (void*)-1 trying to express? null?

  4. Anonymous users2024-02-03

    When Program 2 finishes running, it exits.

    Refer to someone else's analysis.

  5. Anonymous users2024-02-02

    Shared memory is arguably the most useful form of inter-process communication and the fastest form of IPC. Two different processes A and B share memory, meaning that the same piece of physical memory is mapped to the respective process address spaces of processes A and B.

    Process A can instantly see process B's updates to the data in shared memory and vice versa. Since multiple processes share the same area of memory, some kind of synchronization mechanism is required, both mutexes and semaphores.

    One of the obvious benefits of using shared memory communication is efficiency, as the process can read and write directly to memory without requiring any copies of the data.

    For communication methods such as pipelines and message queues, four copies of the data are required in the kernel and user space

  6. Anonymous users2024-02-01

    Yes. And yes"Yes"Locking. Locks can be added using semaphores.

  7. Anonymous users2024-01-31

    Linux shared memory can be unlocked, but a mechanism is needed to mark the read and write status of shared memory;

    That is, to let both processes know:

    1) The process responsible for writing must know whether the current shared memory can be written, and whether the content of the last write has been read by the process responsible for reading;

    2) The process responsible for reading must know whether the current shared memory needs to be read to prevent repeated reads.

    This general tagging mechanism is simply implemented by:

    1) Control by reading and writing locks;

    2) Set a place on the shared memory to store the read and write status of the current shared memory;

  8. Anonymous users2024-01-30

    Suppose the server's daemon is d, which maintains a broadcast linked list. Suppose the linked list element is struct bentry;

    Every time a client connects to the fork, the ipc means are set up when the fork is done, regardless of the pipe, or message, or shared memory, and the server subprocess generated by the client connection to the fork is subn n n as the subscript.

    Then if you want to broadcast the message, let the subn notify d (you can use the ipc method in the broadcast linked list, or you can create another one), and then d broadcasts the message to each subm, subx, and these sub processes are then sent to the client, so that it can be broadcast at the same time, which is more efficient.

  9. Anonymous users2024-01-29

    Shared memory allows two or more processes to share a given storage area and is the fastest inter-process communication mechanism because data does not need to be replicated back and forth. Shared memory can map normal files via mmap().

    In special cases, it can also be implemented by the anonymous mapping) mechanism, or by the system V shared memory mechanism. The application interface and principle are simple, but the internal mechanism is complex. In order to achieve more secure communication, it is often used in conjunction with synchronization mechanisms such as traffic lights.

    The mechanism of mmap is to create a file on the disk, and each process memory creates a separate space for mapping. If there are multiple processes, then the actual physical memory (main memory) will not be consumed too much.

    The mechanism of SHM: The shared memory of each process is mapped directly to the actual physical memory.

    1. The mmap is saved to the actual hard disk, but the actual storage is not reflected on the main memory. Advantages: the storage capacity can be large (more than the main memory); Disadvantages: Inter-process read and write speeds are slower than those of main memory.

    2. SHM is saved to the physical memory (main memory), and the actual storage amount is directly reflected to the main memory. Advantages, inter-process access speed (read and write) is faster than disk; The disadvantage is that the storage capacity cannot be very large (more than the main memory).

    In terms of use: if the allocated storage is not large, then use SHM; If the storage volume is large, then use mmap.

Related questions
6 answers2024-03-04

After installing the app, open it, at this time, the system will ask you to enter your personal mobile phone number to get a verification code, and you can log in to the app after successful verification. Some apps don't require you to verify and register with your phone at the beginning, but when you want to use the features through the app, you will be asked to enter your phone number for verification code. >>>More

4 answers2024-03-04

Let's start with the relatively complex server implementation of PHP. If you have PHP installed on your server, you can execute PHP files, regardless of whether you have installed server environment software such as Nginx or Apache. In Linux, using the command line and crontab to schedule tasks is an excellent choice, and it is also the most efficient choice. >>>More

14 answers2024-03-04

First of all, make sure that you are in the same network segment, that is, if your IP is, then his IP is. >>>More

4 answers2024-03-04

Explanation of the tax situation.

Municipal Local Taxation Bureau: >>>More

6 answers2024-03-04

When using a shared car, you should pay attention to the **software and use, you should choose the official APP, which is generally available on the body of the shared car, and if you want to use the shared car, you can scan it directly**, and you can register directly by entering the mobile phone number. >>>More