LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 06-19-2009, 10:58 AM   #1
GeorgeOfTheBush
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Rep: Reputation: 0
Question >> Problem with SEMAPHORE - does not WAIT <<


I am trying out the following piece of code to implement a simple semaphore.

Steps

1. Create Semaphore
2. Initialize it to 1
3. Enter Critical section - SEMAPHORE P Operation
4. >>>> print some numbers >>>>
5. Leave Critical section - SEMAPHORE V Operation
6. Delete Semaphore
Running the program

Execute the program in one terminal.
In another terminal, execute the same program while it is running in terminal 1.

I expect the program in terminal 2 to WAIT for the program in terminal 1 to leave its critical section before it starts running. But this does not happen. Both the programs run simultaneously.

Maybe, my understanding of semaphores is wrong. Could you please point the error in my program?

Thanks.


Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

#define KEY 12

union semun 
{
    int val;
    struct semid_ds *buf;
    unsigned short *array;
};

int main()
{
  int sem_id;
  int i;
 
  printf("PID = **%d**\n",getpid());

  // CREATE SEMAPHORE
  sem_id = semget((key_t)KEY, 1, IPC_CREAT); 

  printf("Semaphore id = %d\n",sem_id);

  // INITIALIZE SEMAPHORE to 1
  union semun cmd1;
  cmd1.val = 1; //any value.
  if(semctl(sem_id, 0, SETVAL, cmd1) == 0) 
    printf("Semaphore SETVAL to %d SUCCESSFUL.\n",cmd1.val);
  else
    {
      printf("Semaphore SETVAL FAILED\n");
      exit(EXIT_FAILURE);
    }


  struct sembuf sem_p = {0,-1,SEM_UNDO};

  // SEMAPHORE P Operation
  if(semop(sem_id, &sem_p, 1) == -1)
  {
    printf("Semaphore P operation FAILED.\n");
    exit(EXIT_FAILURE);
  }
  else
  {
    printf("Process %d >>>>entering critical section>>>>\n",getpid());
  }

  for(i=0;i<10;i++)
    {
      printf("%d\n",i);
      sleep(1);
    }

  struct sembuf sem_v = {0,1, SEM_UNDO};

  // SEMAPHORE V Operation
  if(semop(sem_id, &sem_v, 1) == -1)
  {
    printf("Semaphore V operation FAILED.\n");  
    exit(EXIT_FAILURE);
  }
  else
  {
    printf("Process %d <<<<leaving critical section<<<<\n",getpid());
  }

  // DELETE SEMAPHORE
  union semun cmd2;
  if(semctl(sem_id, 0, IPC_RMID,cmd2) == 0)
    printf("Process %d deleted semaphore %d.\n",getpid(),sem_id);
  else
    printf("Process %d failed to delete semaphore %d.\n",getpid(),sem_id);
 
  exit(EXIT_SUCCESS);
}
 
Old 06-19-2009, 11:35 AM   #2
noctilucent
Member
 
Registered: Jun 2009
Distribution: slackware
Posts: 123

Rep: Reputation: 34
A quick look over your code tells me the following:

Process 1: creates semaphore, sets semaphore to "1", P(), CS(), V().
Process 2: sets semaphore to "1", P(), CS(), V().

Both processes execute this particular code piece:

Code:
  // INITIALIZE SEMAPHORE to 1
<...>
P1 sets semaphore to 1, when it calls P() the semaphore should become 0. But then P2 runs and sets semaphore to 1 [in the "initialize semaphore to 1" phase]. I hope I'm being clear. Perhaps you should write some sort of "init" program, to set up the semaphore. Also, it would help you (I think) to make P() and V() actual separate functions.
 
  


Reply



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
problem in semaphore and how Asynchronous threads eramax Programming 5 07-16-2008 10:52 AM
problem with semaphore handling ksrinivas Linux - Kernel 0 07-28-2007 11:36 PM
Kernel Compile problem (FC 3, 2.6.11.6 -> 2.6.13.3, semaphore.c) U4ea Linux - Software 1 10-07-2005 03:29 AM
Driver Development - Semaphore problem! Help please! 7.e.Q Programming 1 11-10-2004 10:57 PM
semaphore init problem in linux marry_jeo Programming 0 02-25-2003 11:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:22 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration