LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-15-2004, 09:19 AM   #1
socket9001
LQ Newbie
 
Registered: Dec 2003
Location: Israel
Distribution: Slackware
Posts: 21

Rep: Reputation: 15
shared memory and semaphores...


here's the deal...
i need two applications that i need to communicate through a shared memory segment...

lets say the user starts program A, i need it to create a shared memory segment and a semaphore,
when the user stats program B, i need it to attach itself to the segment and the semaphore.
i also need it to work the other way around, i cant tell which of the applications the users will start first.
i tried using a key for the semphore and the segment but i couldn't get it to work...

in both applications this is how i init the shared memory segment and the semaphore:

Code:
	shmid = shmget(skey, SEGMENT_SIZE, IPC_CREAT | 0666);
	buffer = (char *) shmat(shmid, NULL, 0);

	semid = semget(skey,1,IPC_CREAT | 0666);
	semctl(semid, 0, SETALL, 0);
who do i set the properties that will create a segment if there isnt an segment exist with the key, if the segment exists i want to get the segment id so i could attach to it, same goes for the semaphore...
i cant figure it out from the man pages.......

thanks kin advance for your help...
 
Old 02-15-2004, 03:19 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
See this thread:
http://www.linuxquestions.org/questi...120#post747120

This is about semaphores and message queue's, but you can read about how create / connect to IPC stuff. Should work for shared mem as well, at least partly.

To be able to tell if "the other" program is allready started you could create a (empty) file in /tmp and lock it. If one of your programs see the file is allready locked, you know another program is started, and you know that the IPC-stuff is already created and you should only connect. It maybe a good idea to also use the path of the tmp-lock file for generating the IPC key with ftok().
 
Old 02-16-2004, 09:56 AM   #3
socket9001
LQ Newbie
 
Registered: Dec 2003
Location: Israel
Distribution: Slackware
Posts: 21

Original Poster
Rep: Reputation: 15
thanks...

i have another problem with the semaphore...
how exactly do i use the semaphore to signal the processes?
i dont really get how am i suppose to use them, what should be the initial value of the semaphore..
how exactly do i implement a two communiaction ?
when can i find a source code example.....
 
Old 02-16-2004, 12:18 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Re: thanks...

Quote:
Originally posted by socket9001
i have another problem with the semaphore...
how exactly do i use the semaphore to signal the processes?
i dont really get how am i suppose to use them,
You can't "signal" a process through a semaphore, use signals for that. In short, a semaphore is used to prevent that a piece of code runs more than once at the same time.

How do you know you need them?
Quote:
what should be the initial value of the semaphore..
The initial value should be 1 in normal cases. In the code i posted in the other thread, it's done in this part of int create_semaphore(void):
Code:
     sem_union.val = 1;
     if (semctl(sem_id, 0, SETVAL, sem_union) == -1) {
          out_perror(errno, "create_semaphore()");
          return 0;
     }
Quote:
how exactly do i implement a two communiaction ?
when can i find a source code example.....
Download this tar.gz file with code listings from the book "Advanced Linux Programming" [new riders], and check out chapter-5. Note that you can download the entire book for free, in one PDF file per chapter from: http://www.advancedlinuxprogramming.com/alp-folder

And/or download the code listings from wrox' book "Beginning Linux Programming"
from: http://web.wrox.com/0764543733/2971.tar.gz (see chapter 12).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is shared memory expandable in memory size? Thinking Programming 4 08-16-2005 10:57 AM
Shared Memory/ Semaphores wildem Programming 3 06-20-2005 02:13 PM
Posix file based process shared semaphores dragonman Programming 9 05-25-2005 05:44 PM
About Shared Memory...? aegis_shiva Programming 1 08-25-2004 06:05 AM
shared memory socket9001 Programming 4 02-06-2004 03:08 PM


All times are GMT -5. The time now is 04:03 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration