tag:blogger.com,1999:blog-25615223596962417492024-03-13T03:07:45.556-07:00Program for shared memory in c++This blog describes how to use the shared memory.bashahttp://www.blogger.com/profile/07798620232473967205noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-2561522359696241749.post-4644437708920012732008-02-22T18:57:00.000-08:002008-05-20T02:22:03.771-07:00Program for shared memory in c++See to understand the program first u need to have an idea of shared memory.<br /><br />Shared memory comes under Inter-Process Communication.<br /><br />Shared memory is the fastest of all IPC.<br /><br />Shared memory comes under System V IPC.<br /><br />SHARED MEMORY:<br /><br />Shared memory is one of the three System V IPC.<br /><br />It allows two unrelated processes to access the same logical memory.<br /><br />Shared memory is a very efficient way of transferring data between two running processes.<br /><br /><br />In the program i have used these functions.<br /><br />shmget and shmat for shared memory.<br /><br />shmget is used to create shared memory.<br /><br />shmat is used to attach the shared memory to the address space of the process.<br /><br />When we come to the program,i have created two process i.e process-1 and process-2.<br /><br />process-1 and process-2 uses same memory i.e shared memory.<br /><br />I have created a single bit shared memory to control the two process not to access the shared memory at a time.<br /><br />When process-1 uses the shared memory then process-2 waits until process-1 completes its work and wise versa .<br /><br />Run the two process at a time (i.e in two terminals ) and u will observe that When process-1 uses the shared memory then process-2 waits until process-1 completes its work and wise versa .<br /><br /><span style="font-weight:bold;">The programs are as follows:</span><br /><br /><span style="font-weight:bold;">process1.cpp</span><br /><br />#include<iostream.h><br />#include <sys/types.h><br />#include <sys/ipc.h><br />#include <sys/shm.h><br />#include<unistd.h><br /><br />#define SHMSZ 1024<br /><br />int write(int *b)<br />{<br /> char c;<br /> int shmid;<br /> key_t key;<br /> int *shm;<br /> char *s;<br /><br /> /*<br /> * We'll name our shared memory segment<br /> * "5678".<br /> */<br /> key = 5678;<br /><br /> /*<br /> * Create the segment.<br /> */<br /> if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {<br /> cerr<<"shmget";<br /> exit(1);<br /> }<br /><br /> /*<br /> * Now we attach the segment to our data space.<br /> */<br /> if ((shm = (int *)/*(char *)*/shmat(shmid, 0, 0)) == /*(char *)*/(int *) -1) {<br /> cerr<<"shmat";<br /> exit(1);<br /> }<br /><br /> /*<br /> * Now put some things into the memory for the<br /> * other process to read.<br /> */<br /> // s = shm;<br /> int i;<br /> for (i=0; i<=1000; i++,shm++)<br /> *shm = i;<br /><br /> *b=0;<br /> cout<<"write is complited";<br /> cout<<*b;<br /> return(*b);<br />}<br />main()<br />{<br /><br /> char c;<br /> int shmid;<br /> key_t key;<br /> static int *b;<br /><br /> key = 5679;<br /> /*<br /> * Create the one bit segment.<br /> */<br /> if ((shmid = shmget(key, 1, IPC_CREAT | 0666)) < 0) {<br /> cerr<<"shmget";<br /> exit(1);<br /> }<br /><br /> /*<br /> * Now we attach the segment to our data space.<br /> */<br /> if ((b = ( int *)shmat(shmid, 0, 0)) == (int *) -1) <br /> {<br /> cerr<<"shmat";<br /> exit(1);<br /><br /> }<br /> cout<<*b;<br /> while(1)<br /> {<br /> if(*b==1)<br /> {<br /> write(b);<br /> }<br /> else<br /> wait();<br /> }<br />}<br /><br /><span style="font-weight:bold;">Process2.cpp</span><br /><br />#include<iostream.h><br />#include <sys/types.h><br />#include <sys/ipc.h><br />#include <sys/shm.h><br />#include<unistd.h><br /><br />#define SHMSZ 1024<br /><br />int read(int *b)<br />{<br /><br /> char c;<br /> int shmid;<br /> key_t key;<br /> int *shm;<br /> char *s;<br /><br /> /*<br /> * We'll name our shared memory segment<br /> * "5678".<br /> */<br /> key = 5678;<br /><br /> /*<br /> * Create the segment.<br /> */<br /> if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {<br /> cerr<<"shmget";<br /> exit(1);<br /> }<br /><br /> /*<br /> * Now we attach the segment to our data space.<br /> */<br /> if ((shm = (int *)shmat(shmid, 0, 0)) == (int *) -1) {<br /> cerr<<"shmat";<br /> exit(1);<br /> }<br /><br /><br /> /*now read what the server put in the memory.<br /> */<br /> int i;<br /> for (i=0; i<=1000; i++,shm++)<br /> cout<<*shm;<br /> cout<<"read is completed";<br /> *b=1;<br /> cout<<"*b is "<<*b;<br /> return(*b);<br />}<br />main()<br />{<br /><br /> char c;<br /> int shmid;<br /> key_t key;<br /> static int *b;<br /><br /> key = 5679;<br /><br /> /*<br /> * Create the one bit segment.<br /> */<br /> if ((shmid = shmget(key, 1, IPC_CREAT | 0666)) < 0) {<br /> cerr<<"shmget";<br /> exit(1);<br /> }<br /><br /> /*<br /> * Now we attach the segment to our data space.<br /> */<br /> if ((b = (int *)shmat(shmid, 0, 0)) == (int *) -1) {<br /> cerr<<"shmat";<br /> exit(1);<br /><br /> }<br /><br /> cout<<" *b is "<<*b;<br /><br /> while(1)<br /> {<br /> if(*b==0)<br /> {<br /> read(b);<br /> }<br /> else<br /> wait();<br /> }<br />}<a href="http://writingdevicedrivers.blogspot.com"></a>bashahttp://www.blogger.com/profile/07798620232473967205noreply@blogger.com1