473,804 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ICP shared memory

1 New Member
hi people i have been teaching myself C for the last few months and have recently moved onto ICP shared memory. I have with aid developed some simple code that prints out some chars i then wanted to do the same with int but cannont seem to get it to work.


producer


#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

#define SHMSZ 27

int main()
{
int c;
int shmid;
key_t key;
int *shm, *s;

key = 5678;


if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
perror("shmget" );
return(1);
}


if ((shm = shmat(shmid, NULL, 0)) == (int *) -1) {
perror("shmat") ;
return(1);
}

s = shm;

for (c = 10; c <= 20; c++)
*s++ = c;
*s = -1;


while (*shm != '*')
sleep(1);

return(0);
}


consumer


#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

#define SHMSZ 27

int main()
{
int shmid;
key_t key;
char *shm, *s;


key = 5678;


if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
perror("shmget" );
return(1);
}


if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat") ;
return(1);
}


for (s = shm; *s != 0; s++)
putchar(*s);
putchar('\n');


*shm = '*';

return(0);
}
Nov 20 '07 #1
1 3342
rajarora
33 New Member
hi people i have been teaching myself C for the last few months and have recently moved onto ICP shared memory. I have with aid developed some simple code that prints out some chars i then wanted to do the same with int but cannont seem to get it to work.


producer


#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

#define SHMSZ 27

int main()
{
int c;
int shmid;
key_t key;
int *shm, *s;

key = 5678;


if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
perror("shmget" );
return(1);
}


if ((shm = shmat(shmid, NULL, 0)) == (int *) -1) {
perror("shmat") ;
return(1);
}

s = shm;

for (c = 10; c <= 20; c++)
*s++ = c;
*s = -1;


while (*shm != '*')
sleep(1);

return(0);
}


consumer


#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

#define SHMSZ 27

int main()
{
int shmid;
key_t key;
char *shm, *s;


key = 5678;


if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
perror("shmget" );
return(1);
}


if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat") ;
return(1);
}


for (s = shm; *s != 0; s++)
putchar(*s);
putchar('\n');


*shm = '*';

return(0);
}
-------------------------------------------
Hi cavemandave,

I have also worked on Shared Memory. I diagonised the following problems in your code :-
1. shmat() returns a pointer to void. So variable "shm" must be of type void pointer. But you declared it as char*.
2. When a new segment is created, the contents of the segment are initialized with zeros. (relate this point to point 3).
3. The shared memory IPC Mechanism does not have any automatic means of synchronisation for processes which are communicating. The programmer needs to implement the synchronisation at its own. And that is why when before your producer program makes the data of shared memory to some non zero value, the consumer process might be reading it before that.

If you need, then I can post the running version of your program. Feel free to mail me if any more help is still needed.

Regards,
Raj Kumar Arora
Dec 21 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
4412
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with SharedMemAccess_Mutex_ctypes.py or SharedMemAccess_Mutex_win32all.py
3
2151
by: alanrn | last post by:
I would like to start a dialog on how to implement the equivalent functionality of UNIX shared memory in .NET. I work with a factory automation system. The bulk of the system is written in C/C++. It was ported from UNIX to run under Windows using .NET. If for no other reason than as an educational exercise, I have been wondering what it would take to rewrite the system under C#. The system currently has about a 100K “shared...
11
4908
by: Michael Schuler | last post by:
The use of STL in shared memory poses a real problem since (non-smart) pointers are not allowed there. Is there any solution for containers in shared memory using smart pointers? Where can I find templates?
1
2120
by: myren, lord | last post by:
When I first discovered shared memory (between multiple processes) I immediately started thinking of how to build my own VM subsystem + locking mechanisms for a large single block of memory. This seems like one option, the other appears to be just having each "object" you want to share be a shared mem space to itself: allocate objects into a defined shared mem space. But here you have many many objects being shared. Having a VM...
14
8230
by: phil_gg04 | last post by:
Dear C++ Experts, Over the last couple of months I have been writing my first program using shared memory. It has been something of an "in-at-the-deep-end" experience, to say the least. At present the shared memory contains a few fixed-size structs, but I really need to be able to store more complex variable-sized data in there. So the next task is to work out how to store C++ objects, and if possible STL containers, in this shared...
12
5549
by: Jeremy | last post by:
Hi all, I'm getting very confused about how DB2 uses shared memory and I wonder if someone could clarify matters for me, please ? We are running 32bit DB2 V7.2 FP9 under AIX 4.3.3 on a machine with 64 Gb of memory with a single non-partitioned database using extended storage and with intra-parallelism enabled. I've been experimenting with changing various parameters in an attempt
5
7087
by: Jim | last post by:
Hello, I have a broken server that we are going to be moving off to a new server with a new version of DB2 but here is what I have right now: RedHat 7.0 (2.2.24smp) DB2 v6.1.0.40 I am getting this error when I try to run the command 'db2 "backup database dbname online to /opt/BACKUP"' on my 3 databases: SQL1042C An unexpected system error occurred. SQLSTATE=58004
4
3118
by: herbert | last post by:
I am coding a dozen "background" realtime apps for factory automation in .NET 2.0. The apps need to share a common memory as there are lots of variables to be shared (and synchronized of course). I need distinct apps for reasons like user interaction/access rights, error compartments, and because the size of the code, access to various ports, ... In the end the apps should run as Windows Service(s).
21
8393
by: llothar | last post by:
Hello, i need to manage a heap in shared memory. Does anybody know about a portable (win32+mac+posix) c implementation for this.
5
587
by: Sune | last post by:
Hi all, I want to make data stored in-memory (not disk) available to several processes. My concern is that poorly written C applications with dangling pointers may(will) damage the data in this memory segment if it is open to all, i.e. shared memory mapped into all processes memory area. I don't want to use TCP/IP client/server between the apps and a data store process due to the overhead.
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10101
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.