process shared semaphores

process shared semaphores

Post by Rajasekhar Inkul » Mon, 07 May 2001 00:36:50



Hello,

Following code is working on Alpha unix.  But is not working on SunOS
5.7.
It is returning proper semaphore id (which i'm making sure with 'ipcs
-s').
But semctl(semid, nsems, SETALL, initArray) is returning -1.
And outArray[0] from semctl(semid, nsems, GETALL, outArray) is having
some junk value.  Do i need to initialize anything else??

Thanks.

 #include <signal.h>
 #include <wait.h>
 #include <unistd.h>
 #include <time.h>
 #include <iostream.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/ipc.h>
 #include <sys/sem.h>

int semid;

void cleanup(int signalNum)
{
   semctl(semid, IPC_RMID, 0);
   return;

Quote:}

int main(void)
{
  for (int i=0; i<20; i++)
  {
     signal(i, cleanup);
  }

  key_t key = 1234;
  int semflg = IPC_CREAT | 0666;
  int nsems = 1;

  if ((semid = semget(key, nsems, semflg)) == -1)
  {
     cout << "error with semget" << endl;
     exit(1);
  }
  else
  {
     cout << "semget ret(semid): " << semid << endl;
  }

  int initArray[1]={87}, outArray[1];
  int ret = semctl(semid, nsems, SETALL, initArray);
  cout << "semctl SETALL ret: " << ret << endl;
  ret = semctl(semid, nsems, GETALL, outArray);
  cout << "semctl GETALL outArray: " << outArray[0] << endl;

/*
  struct sembuf psembuf, vsembuf;

  psembuf.sem_num = vsembuf.sem_num = 0;
  psembuf.sem_flg = vsembuf.sem_flg = SEM_UNDO;
  psembuf.sem_op = -1;
  vsembuf.sem_op = 1;

  ret = semop(semid, &psembuf, 1);
  cout << "lock ret: " << ret << endl;
  ret = semctl(semid, nsems, GETALL, outArray);
  cout << "semctl outArray: " << outArray[0] << endl;
  ret = semop(semid, &vsembuf, 1);
  cout << "unlock ret: " << ret << endl;
  ret = semctl(semid, nsems, GETALL, outArray);
  cout << "semctl outArray: " << outArray[0] << endl;
*/
  return 0;

Quote:}

 
 
 

process shared semaphores

Post by Andrew Giert » Mon, 07 May 2001 14:11:20


 Rajasekhar> Hello,
 Rajasekhar> Following code is working on Alpha unix.  But is not
 Rajasekhar> working on SunOS 5.7.  It is returning proper semaphore
 Rajasekhar> id (which i'm making sure with 'ipcs -s').  But
 Rajasekhar> semctl(semid, nsems, SETALL, initArray) is returning -1.

incorrect arguments to semctl.

you have to do this:

union semun {
    int     val;            /* value for SETVAL */
    struct  semid_ds *buf;  /* buffer for IPC_STAT & IPC_SET */
    unsigned short *array;  /* array for GETALL & SETALL */

Quote:};

    union semun arg;
    arg.array = initArray;
    if (semctl(semid, nsems, SETALL, arg) < 0) /* handle error */

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

1. Process shared semaphores (Pthreads)

Hi *,
   Anybody knows when on earth Linux's PThreads (LinuxThreads/glib)
is going to support process-shared semaphores and p-s
condition variables? I am looking for 'drop-in' replacement for
the pthread_mutexattr_setpshared() and pthread_condattr_setpshared()
functions.

Also, these functions are Optional in the POSIX standard, but
they are required in the Unix98 standard, since kernel 2.2 is
supposed to have some (most? all?) Unix98 conformance, I wonder
if 2.2 does support these two problem-makers?

                TIA,
                                Emilio
--
*** The opinions expressed hereby are mine and not my employer's ***

2. Perl question .....

3. a process notice other processes by semaphore

4. XF86Setup Error?

5. Can a process signal a semaphore that is waited by a diff. process?

6. MPPC or LZS Stac Compression

7. Solaris 10: Increasing the process data space; shared memory segments & intimate shared memory problems

8. Read/Unread bit in popper?

9. help sharing shared memory between non sibling processes

10. Producers/Consumers and shared memory/semaphores

11. Problem with semaphores/shared memory

12. Wrong version of semaphore functions called from shared library.

13. timings in semaphores and shared memory.