From owner-freebsd-questions@FreeBSD.ORG Tue Mar 30 00:55:53 2010 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id A5B4D106566C for ; Tue, 30 Mar 2010 00:55:53 +0000 (UTC) (envelope-from zepedaherbey@gmail.com) Received: from ey-out-2122.google.com (ey-out-2122.google.com [74.125.78.25]) by mx1.freebsd.org (Postfix) with ESMTP id 2D7C58FC19 for ; Tue, 30 Mar 2010 00:55:52 +0000 (UTC) Received: by ey-out-2122.google.com with SMTP id d26so848641eyd.9 for ; Mon, 29 Mar 2010 17:55:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:received:message-id :subject:from:to:content-type; bh=PH9Pr7q9Ci3vHfIayzphN/s+M43tqCgyMcCtI/A8RsM=; b=q2b0PwW46L+oFDP8WDcCZPIZC78XJwk0R2JX6VNkhUW/osGAqn790D016GOqnFrER1 wpBW05ub+HVaVxC9JK+0c8vTg0n/hoXgptfC/dQ+JSHoxeJEFX25CN3o09O+73lb72Bu 2p69GPa3CU/jY/nztuHZQ3AwB/biXwAjvrUpw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=iuYGkOlcB0jOYbMkMTDWhPa3z+I8MQh1y6b7jpb+B9Q+W2Uzy2M8uQr6xE4jbSHJ6Q 7Z/QuF/acqDxpsxIt3xnO+AudaA/CFCNyrqps12zYhtf5NjYVHN3ByA9bNM+NdxOUSH0 dqdOpQEI0Lztww3MSzz0X08Chr+6DelA/O1FM= MIME-Version: 1.0 Received: by 10.216.29.145 with HTTP; Mon, 29 Mar 2010 17:26:34 -0700 (PDT) Date: Mon, 29 Mar 2010 20:26:34 -0400 Received: by 10.216.172.76 with SMTP id s54mr1506250wel.100.1269908794765; Mon, 29 Mar 2010 17:26:34 -0700 (PDT) Message-ID: <49856ef1003291726x7ac06d29x96ef03f1d68a6fe1@mail.gmail.com> From: herbey zepeda To: freebsd-questions@freebsd.org Content-Type: text/plain; charset=ISO-8859-1 X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Subject: Problem with shmget? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 30 Mar 2010 00:55:53 -0000 Hi, I'm trying to set up shared memory using a structure. Everything seems to work fine when the memory creator creates it and the writer attaches to the created memory segment. The problem is that when the data writer tries to write or read in the shared memory segment, there is a segmentation fault (core dumped) message and data is not read or written. Following are the 3 files I'm using, the section that fails is indicated almost at the end of the code. any ideas? thanks you ------------------------- hzSharedMemoryMessageUnit.h --------------------------- #include #define HZ_SHMEMFORDATAGRABBER 159 //start memory segments at 101 #define TEXT_SZ 256 struct shared_use_st { int memWrittenAndReadyToBeRead; char some_text[TEXT_SZ]; };//hzSt; ----------------- hzSharedMemorySetter.cpp ------------------------ #include #include #include #include #include #include #include #include #include "hzSharedMemoryMessageUnit.h" using namespace std; class hzNotifications { public: void hzNotifyWhenError(const char* errorMsg,int exitCodeStrg); }; void hzNotifications::hzNotifyWhenError(const char* errorMsg,int exitCodeStrg) { cerr << errorMsg << endl; exit(exitCodeStrg); } int main() { void *shared_memory=(void *)0; struct shared_use_st *shared_stuff; int shmid; hzNotifications hzNotif; shmid=shmget((key_t)HZ_SHMEMFORDATAGRABBER,sizeof(struct shared_use_st), 0666 | IPC_CREAT ); if(shmid==-1) { cerr <<"hz the error of shmget is: " << errno <memWrittenAndReadyToBeRead=2; sleep(20); cout << "hz you wrote " << shared_stuff->memWrittenAndReadyToBeRead << " from another process " << endl; if(shmdt(shared_memory)==-1) {hzNotif.hzNotifyWhenError("hz shmdt failed",EXIT_FAILURE);} if(shmctl(shmid,IPC_RMID,0)==-1) {hzNotif.hzNotifyWhenError("hz shmctl(IPC_RMID) failed",EXIT_FAILURE);} return 0; } ---------------------------- hzSharedMemoryGetter.cpp ------------------------------ #include #include #include #include #include #include #include #include #include #include "hzSharedMemoryMessageUnit.h" using namespace std; class hzNotifications { public: void hzNotifyWhenError(const char* errorMsg,int exitCodeStrg); }; void hzNotifications::hzNotifyWhenError(const char* errorMsg,int exitCodeStrg) { cerr << errorMsg << endl; exit(exitCodeStrg); } int main() { /* hzSetSharedMemory hzSm; struct shared_use_st *shared_stuff; shared_stuff=(struct shared_use_st *)hzSm.hzAttachToExistingSharedMemoryChunk(HZ_SHMEMFORDATAGRABBER); */ //int running=1; void *shared_memory=(void *)0; struct shared_use_st *shared_stuff; int shmid; hzNotifications hzNotif; shmid=shmget((key_t)HZ_SHMEMFORDATAGRABBER,sizeof(struct shared_use_st), 0666 | IPC_CREAT ); //shmid=shmget((key_t)HZ_SHMEMFORDATAGRABBER,0,0666 ); if(shmid==-1) { cerr <<"hz the error of shmget is: " << errno <memWrittenAndReadyToBeRead); -------IT FAILS HERE shared_stuff->memWrittenAndReadyToBeRead=10; //------------ IT FAILS HERE!!!!!!!! -------IT FAILS HERE cout << "program completed" <