# Multiple clients calling a server IPC

**URL:** https://forum.legato.io/t/multiple-clients-calling-a-server-ipc/4532
**Category:** Uncategorized
**Created:** [July 5, 2019, 2:43am UTC](https://forum.legato.io/t/multiple-clients-calling-a-server-ipc/4532 "2019-07-05T02:43:00Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![sast5](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sast5](https://forum.legato.io/u/sast5)
#### Post date: [July 5, 2019, 2:43am UTC](https://forum.legato.io/t/multiple-clients-calling-a-server-ipc/4532/1 "2019-07-05T02:43:00Z")

</div>

Is this safe? I have a server that writes to a file and I want multiple apps to be able to call the server to write new files. If they call while the last function is still executing while this be a problem or is the state saved?

for example  
void FileManager\_StoreFile(const char\* writeData)  
{  
FILE\* fp = fopen(filename,“w+”);  
//\<— Another client calls api  
//write to file etc  
}

will write Data be lost if another client calls the function before it is written? if so how can I fix this?

---

<div class="post-metadata">

### Author: ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)
#### Post date: [July 5, 2019, 3:13am UTC](https://forum.legato.io/t/multiple-clients-calling-a-server-ipc/4532/2 "2019-07-05T03:13:07Z")

</div>

did you try to use semaphore API to lock this critical section?

> **[Semaphore API - Legato Docs](https://docs.legato.io/latest/c_semaphore.html)**
>
> legato™ is an open source Linux-based embedded platform designed to simplify connected IoT application development

---

<div class="post-metadata">

### Author: ![sast5](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sast5](https://forum.legato.io/u/sast5)
#### Post date: [July 5, 2019, 3:15am UTC](https://forum.legato.io/t/multiple-clients-calling-a-server-ipc/4532/3 "2019-07-05T03:15:46Z")

</div>

No Sorry I’m not super familiar with thread concepts but that’s what I was looking for I think thank you

---

<div class="post-metadata">

### Author: ![sast5](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sast5](https://forum.legato.io/u/sast5)
#### Post date: [July 7, 2019, 9:11pm UTC](https://forum.legato.io/t/multiple-clients-calling-a-server-ipc/4532/4 "2019-07-07T21:11:57Z")

</div>

Actually I don’t think that will work it says for threads in the same process theres not really a way for me to get a reference to the semaphore from outside of the executable?

---

<div class="post-metadata">

### Author: ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)
#### Post date: [July 8, 2019, 1:19am UTC](https://forum.legato.io/t/multiple-clients-calling-a-server-ipc/4532/5 "2019-07-08T01:19:36Z")

</div>

did you write a sample code to try that?

---

<div class="post-metadata">

### Author: ![raf](https://avatars.discourse-cdn.com/v4/letter/r/848f3c/32.png) [@raf](https://forum.legato.io/u/raf)
#### Post date: [July 8, 2019, 1:21am UTC](https://forum.legato.io/t/multiple-clients-calling-a-server-ipc/4532/6 "2019-07-08T01:21:20Z")

</div>

Hi @sast5.

You really only need the reference to the semaphore _within_ the executable providing the API.

Basically, you could create a semaphore in the `COMPONENT_INIT` section with an initial count of `1` then use that reference in your API function(s).

For example:

```auto
COMPONENT_INIT
{
  /*
   * Create a semaphore with an initial count of 1 
   * This signals that 1 resource (a single file) is available
   */
}

```

```auto
void FileManager_StoreFile(const char* writeData)
{
  /*
   * Pend (wait) on the semaphore using either:
   * le_sem_WaitWithTimeOut()
   * le_sem_Wait()
   * le_sem_TryWait()
   * Note: the first client calling the API will `consume` the semaphore
   */
   
   // If le_sem above returned LE_OK, it is safe to `use` the resource.

   FILE* fp = fopen(filename,“w+”);

   // ... do stuff with the file, etc.

   /* 
    * ***<— Another client calls api***
    * Other clients won't be able to acquire the resource until 
    * the `consuming` client releases (posts) it.
    * The behavior will depend on which `waiting` method you chose to implement.
    */

   // Finally, Post the semaphore so other clients may `use` it.
   le_sem_Post(MySemRef);
}

```

Raf

---

<div class="post-metadata">

### Author: ![sast5](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sast5](https://forum.legato.io/u/sast5)
#### Post date: [July 8, 2019, 2:48am UTC](https://forum.legato.io/t/multiple-clients-calling-a-server-ipc/4532/7 "2019-07-08T02:48:50Z")

</div>

Oh ok thanks makes more sense with an example (Couldn’t see any other posts about semaphores on the forums/Doc samples)
