| |

VerySource

 Forgot password?
 Register
Search
View: 28|Reply: 4

2 processes, how to effectively share variables?

[Copy link]

1

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-1-28 14:00:02
| Show all posts |Read mode
2 processes,

A global variable in one process that needs to be accessible by the other process

I use the general method, and always get the initial value of the variable,

How should I solve that?
Reply

Use magic Report

0

Threads

12

Posts

9

Credits

Newbie

Rank: 1

Credits
9

 Invalid IP Address

Post time: 2020-3-2 06:30:01
| Show all posts
Inter-process communication ...
Shared memory
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-5-13 02:30:02
| Show all posts
shared mem
Reply

Use magic Report

0

Threads

4

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

Post time: 2020-5-14 13:00:01
| Show all posts
Use shared memory

Shared storage allows two or more processes to share a given storage area and is the fastest kind of IPC. The kernel sets up a shmid_ds structure for each shared memory segment.

The function shmget obtains a shared storage identifier, whose function prototype is as follows:
#include <sys / types.h>
#include <sys / ipc.h>
#include <sys / shm.h>
int shmget (key_t key, int size, int flag);
Description:
The parameter key is a shared storage field.
size is the minimum value of the shared memory segment. If you are creating one
For new segments, you must specify their size. If you are accessing an existing segment,
Then specify size as 0.
Reply

Use magic Report

0

Threads

4

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

Post time: 2020-5-14 17:45:01
| Show all posts
flag can set the access permission of the shared storage segment, can also be set
Two flags: IPC_CREAT and IPC_EXCL.
The successful call of shmget returns a non-negative integer, which is connected to the key
Shared storage segment id. If it is a new shared storage segment, shmget
Set all units in the initial shared storage segment to 0, and create and parameter key at the same time
The connected data structure shmid_ds.
Failure to call shmget will return -1 and set errno to indicate the cause of the error.
The shmctl function performs various operations on the shared storage segment, and its function prototype is as follows
under:
#include <sys / types.h>
#include <sys / ipc.h>
#include <sys / shm.h>
int shmctl (int shmid, int cmd, struct
shmid_ds * buf);
Explanation:
The cmd parameter specifies one of the following five commands to make it refer to the shmid
Execute on the specified segment:
IPC_STAT takes the shmid_ds structure for this segment and stores it in the structure pointed to by buf.
IPC_SET sets the following three fields in the structure related to this segment by the value in the structure pointed to by buf: shm_perm.uid, shm_perm.gid, and shm_perm.mode
IPC_RMID deletes the shared storage segment from the system. Because each shared storage segment has a connection count (shm_nattch in the shmid_ds structure), the storage segment will not actually be deleted unless the last process using the segment terminates or is disconnected from the segment. Regardless of whether the segment is still in use, the segment identifier is deleted immediately, so you can no longer use shmat to connect to the segment.
SHM_LOCK locks the shared memory segment. This command can only be executed by the super user.
SHM_UNLOCK unlocks the shared storage segment. This command can only be executed by the super user.
Once a shared storage segment is created, the process can call shmat to
Connected to its address space, its function prototype is as follows:
#include <sys / types.h>
#include <sys / ipc.h>
#include <sys / shm.h>
void * shmat (int shmid, void * addr, int flag);
Description:
The address of the calling process where the shared memory segment is connected to addr
The parameters are related to whether the SHM_RND bit is specified in the flag.
(1) If addr is 0, this segment is connected to the first available address selected by the kernel.
(2) If addr is not 0, and SHM_RND is not specified, this segment is connected to the address specified by addr.
(3) If addr is not 0, and SHM_RND is specified, this segment is connected to the address indicated by (addr- (addr mod SHMLBA)).
If the SHM_RDONLY bit is specified in the flag, read-only
Connect this segment. Otherwise, connect this segment by reading and writing. shmat return value
Is the actual address connected to the segment, or -1 if an error occurs.
When the process no longer needs a shared storage segment, you can call the shmdt function
Separate it from the address space of the process. The function prototype is as follows:
#include <sys / types.h>
#include <sys / ipc.h>
#include <sys / shm.h>
int shmdt (void * addr);
Description:
The addr parameter is the return value from the previous call to shmat. Note that this
This function is just a shared memory segment that is no longer empty with the address of the calling process
Connected, it does not actually delete the shared storage segment itself, delete the shared storage
The storage section is the function of the shmctl function IPC_RMID command.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list