Memory sharing is a common way of communication between processes, enabling two completely independent process communication.
When accessing shared memory, there is also a need for the semaphore for access control.
Use the IPCS-M command to view system shared memory and IPCE-M + key to delete the specified shared memory.
For shared memory operations, shared memory is protected with semaphores, similar to mutex locks in threads. Can be seen as a critical resource protection through PV operations.
P: The semaphore marker bit-1, gets the semaphore, if the mark bit is 0, indicates that there are other processes occupying resources that cannot be obtained.
V: The semaphore is marked bit +1, the semaphore is released, and the resource is freed for use by other processes.
Semaphores and memory sharing need to use a similar function, the following is a specific code implementation, function parameters are not specifically described.
1 //Shmwrite.cpp2#include <sys/types.h>3#include <sys/sem.h>4#include <sys/shm.h>5#include <string.h>6#include <iostream>7 8 using namespacestd;9 Ten Const intSemtag =0x2456; One Const intShmtag =0x3443; A Const intMaxbufflen =1024x768; - - classSemOp the { - Public: -SemOp (): M_isemid (0){}; - voidInitsem (); + voidGetsem (); - voidReleasesem (); + voidDelsem (); A Private: at intM_isemid; - }; - - classShmop - { - Public: inShmop (): M_ishmid (0){}; - voidInitshm (); to voidWriteshm (); + voidReadshm (); - voidDelshm (); the Private: * intM_ishmid; $ void*m_memaddr;Panax Notoginseng }; - the voidSemop::initsem () + { A if(M_isemid = Semget (Semtag,1,0600| ipc_creat)) <=0) thecout<<"Get Semid failure!"<<Endl; +Semctl (M_isemid,0, Setval,1); - } $ voidSemop::getsem () $ { - structsembuf Sem_get; -Sem_get.sem_num =0; theSem_get.sem_op =-1; -SEM_GET.SEM_FLG =Sem_undo;WuyiSemop (M_isemid,&sem_get,1); the } - voidSemop::releasesem () Wu { - structsembuf sem_release; AboutSem_release.sem_num =0; $Sem_release.sem_op =1; -SEM_RELEASE.SEM_FLG =Sem_undo; -Semop (M_isemid,&sem_release,1); - } A voidSemOp::D elsem () + { theSemctl (M_isemid,0, ipc_rmid); - } $ the voidShmop::initshm () the { the if(M_ishmid = Shmget (Shmtag, Maxbufflen,0600| ipc_creat)) <=0) thecout<<"Get M_ishmid failure!"<<m_iShmID<<Endl; - } in voidShmop::writeshm () the { the CharBuff[maxbufflen]; AboutCin>>Buff; theM_MEMADDR = reinterpret_cast<void*> (Shmat (M_ishmid, NULL,0 )); thememcpy (M_memaddr,buff,sizeof(Buff)); the + } - voidShmop::readshm () the {Bayi CharBuff[maxbufflen]; theM_MEMADDR = reinterpret_cast<void*> (Shmat (M_ishmid, NULL,0 )); thememcpy (BUFF,M_MEMADDR,sizeof(Buff)); -cout<<Buff; - } the voidshmop::D elshm () the { the SHMDT (m_memaddr); the } - the intMain () the { the SemOp sem;94 shmop SHM; the the SEM. Initsem (); the SHM. Initshm ();98 About SEM. Getsem (); - SHM. Writeshm ();101 SEM. Releasesem ();102 103 SHM. Delshm ();104 SEM. Delsem (); the return 0;106}
Similar to reading shared memory code, you only need to modify the main function to perform read operations on shared memory.
1 intMain ()2 {3 SemOp sem;4 shmop SHM;5 6 SEM. Initsem ();7 SHM. Initshm ();8 9 SEM. Getsem ();Ten SHM. Readshm (); One SEM. Releasesem (); A - SHM. Delshm (); - SEM. Delsem (); the return 0; -}
After you perform a write operation, you can use the IPCS command to view the system shared memory condition.
Execute code Result:
Process Communication (ii)--Semaphore & memory Sharing