LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shared memory and semaphores... (https://www.linuxquestions.org/questions/programming-9/shared-memory-and-semaphores-146320/)

socket9001 02-15-2004 08:19 AM

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...

Hko 02-15-2004 02:19 PM

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().

socket9001 02-16-2004 08:56 AM

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.....

Hko 02-16-2004 11:18 AM

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).


All times are GMT -5. The time now is 12:58 PM.