IPC-Semaphore

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello out there,
Im working on a solution to delete a semaphore, which is used by other 
processes.
The semaphore was created by a server-process. It write the semaphore-key into 
a file ( where the clients can read it, and get access to the semaphore )
After that the server proceeds its work. 
When the server finished its work, it destroys the semaphore. And here we have 
quandary. In this very moment the clients still need the semaphore.

In the semctl-manual we have a several arguments, amongst others.
GETZCNT     The  system  call returns the value of semzcnt for the sem-
                   num-th semaphore of the set (i.e. the number  of  processes
                   waiting for semval of the semnum-th semaphore of the set to
                   become 0).  The calling process must have read access priv-
                   ileges on the semaphore set.
or
GETNCNT     The  system  call returns the value of semncnt for the sem-
                   num-th semaphore of the set (i.e. the number  of  processes
                   waiting  for  an  increase  of  semval  for  the  semnum-th
                   semaphore of the set).  The calling process must have  read
                   access privileges on the semaphore set.

based on this I made this function:
/* Loeschen einer Semaphore                                               */
int loesche_semaphore ( int kennung )
{
  union semun mysem;
  int semnum      = 0,
      rueckgabe_1 = 0,
      rueckgabe_2 = 0;

  while ( 1 )
  {
     if ( ( rueckgabe_1 = semctl ( kennung, semnum, GETZCNT, mysem ) ) == -1 )
     {
         perror ( "Fehler beim bestimmen der Semaphor-Wartelist\n" );
         exit ( errno );
     }
     else if ( ( rueckgabe_2 = semctl ( kennung, semnum, GETNCNT, mysem ) ) == 
-1 )
     {
         perror ( "Fehler beim bestimmen der Semaphor-Wartelist\n" );
         exit ( errno );
     }
     else
     {
         printf ( "Die Menge der Prozesse ist gleich: %d und %d \n", 
rueckgabe_1, rueckgabe_2 );
         break;
     }
  }

  if ( ( semctl ( kennung, 0, IPC_RMID, mysem ) ) == -1 )
  {
      perror ( "Fehler beim loeschen der Semaphore\n" );
      exit ( errno );
  }

 return ( 1 );
}

I know that so far this function doesn't work. It should only show me if some 
process try to access the semaphore. 
But neither GETZCNT nor GETNCNT give me info about processes which try to 
access the semaphore. Both gave me a '0'

What is the problem, what I have missed?

Thank you all...

Gruss Christian

-- 
Christian Stalp
Institut für Medizinische Biometrie, Epidemiologie und Informatik
Johannes-Gutenberg-Universität Mainz
Tel.: 06131 / 17-3107
E-Mail: stalp@xxxxxxxxxxxxxxxxxx
-
: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux