Advertisement

02.19.2007 at 02:58PM PST, ID: 22399760
[x]
Attachment Details

Using POSIX semaphores

Asked by chsalvia in C Programming Language, Linux Programming, Unix Systems Programming

Tags: posix, semaphore

I just recently found out about POSIX semaphores.  POSIX semaphores are a way to synchronize access to shared resources between multiple processes.  Supposedly, they are superior to traditional UNIX sempahores because they are more lightweight.  However, there is frustratingly little online information about POSIX semaphores, so I'm not sure if I'm using them right.

I am wondering if any experts here have ever used POSIX semaphores before, or even heard of them?  I experimented with them a little, and I seem to have them working right, but I'm not sure.  Apparently, you need to place a semaphore in shared memory, and then it works kind of like a pthread_mutex in that you can lock it before accessing a critical section, and then unlock it when finished.

Here is the code:

int main() {
      /* create shared memory region */
      key_t segid = 50000;
      int shmid;
      shmid = shmget(segid, 1024, IPC_CREAT | 0660);
      void* sh_mem = shmat(shmid, (void*) 0, 0);

      /* create POSIX semaphore */
      sem_t* sem = (sem_t*) sh_mem;
      int sts = sem_init(sem, true, 1);

      /* spawn a child process */
      pid_t p = fork();
      if (p > 0) {
            sem_wait(sem);
            printf("Parent: Sleeping for 2 seconds...\n");
            sleep(2);
            printf("Parent: Done sleeping...\n");
            sem_post(sem);
      }
      else if (p == 0) {
            sem_wait(sem);
            printf("Child: Sleeping for 2 seconds...\n");
            sleep(2);
            printf("Child: Done sleeping...\n");
            sem_post(sem);
      }
}

I think I have it working correctly, because the output is correctly synchronized.  But I'm not sure, and would like an expert to review the above code to see if I'm doing this correctly.Start Free Trial
 
Loading Advertisement...
 
[+][-]02.19.2007 at 09:02PM PST, ID: 18568478

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.19.2007 at 11:35PM PST, ID: 18568883

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.20.2007 at 12:46AM PST, ID: 18569062

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C Programming Language, Linux Programming, Unix Systems Programming
Tags: posix, semaphore
Sign Up Now!
Solution Provided By: sunnycoder
Participating Experts: 1
Solution Grade: A
 
 
[+][-]02.23.2007 at 09:56AM PST, ID: 18597837

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.17.2007 at 11:04AM PDT, ID: 18740931

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]03.22.2007 at 06:29PM PDT, ID: 18776654

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32