Java JVM crash with semaphores and JNI

F

Fuma

Hello,
I have run into a strange problem. While atempting to decrement a
semaphore in JNI code, the JVM will crash with the following error
(this is a very simple ipc program):

JVMDG215: Dump Handler has Processed Exception Signal 11.
Segmentation fault

The code that creates the semaphore and shared memory is below. First
"getSharedMemory()" is called, which creates a semaphore and shared
memory. This appears to be working fine (Confirmed by checking ipcs).
After "getSharedMemory()" returns, "insertMessage()" is called.
"insertMessage()"almost immediately calls "sem_wait()" which is the
function that is crashing when it calls "sem_op". The purpose of
sem_wait() is to provide a wrapper to the sem_op decrement call. Very
simple. I have tested my design in a pure C environment and it works
correctly. When I attempt to run my program via JNI, the program
crashes when it calls sem_op.

The java core dump is provided below.

Any insight is greatly appreciated!

Thanks,
Chris


JNIEXPORT jint JNICALL Java_AClassWithNativeMethods_getSharedMemory
( JNIEnv *env, jobject thisObj )
{
// Create, or access semaphore
if( ( semid = sem_create( SEM_KEY ) ) < 0 )
{
perror( "Error: Semaphore could not accessed.\n" );
exit( 1 );
}

// Create, or at least get handle to shared memory
if( ( shmid = shmget( SHM_KEY, SHM_SIZE,
SHM_MODE|IPC_CREAT|IPC_EXCL ) ) < 0 )
{
perror( "Error: Could not get shared memory\n" );
exit( 1 );
}

// Attach the shared memory to our process
if( ( messageDataList = (MSGDATA_LIST *) shmat( shmid, 0, 0 ) )
== (void *
) -1 )
{
perror( "Error: attaching shared memory\n" );
exit( 1 );
}

// if shared memory has not been accessed, initialize list ptr
if( messageDataList->flag != 140 )
{
messageDataList->last = 0;
}

// Shared memory has been initialized
messageDataList->flag = 140;

// Debug
printf( "messageDataList->last = %d\n", messageDataList->last );

return 1;
}


int sem_create( key_t key )
{
int id;
union semun semctl_arg;

// Create, or access already created semaphore
if( ( id = semget( key, 1, 0666 | IPC_CREAT ) ) < 0 )
{
fprintf( stderr, "Error: sem_create, invalid semid
%d\n", id );
return -1;
}

// Make a binary semaphore ( 1 | 0 )
semctl_arg.val = 1;

// Now set the value of the semaphore
if( semctl( id, 0, SETVAL, semctl_arg ) < 0 )
{
perror( "Error: sem_create, semctl SETVAL\n" );
return -1;
}
printf("sem_create gets a sem id of %d\n", id);
return id;
}

JNIEXPORT jint JNICALL Java_AClassWithNativeMethods_insertMessage
( JNIEnv *env, jobject thisObj, jint messageType )
{
int i, retval;
int ip = inet_addr( "130.49.221.177" );

printf("step1 : semid %d\n", semid);

// Acquire exclusive access to the shared memory
sem_wait( semid );

printf("step2\n");


// Check for duplicate messages in the message list
for (i = 0; i < messageDataList->last; i++)
{
// Duplicate found
if( ( messageDataList->msg.type == messageType) &&
( messageDataList->msg.ip == ip ) )
{
//sem_signal (semid);
return 2;
}
}

printf("step3\n");


// Add message to list
messageDataList->msg[messageDataList->last].type = messageType;
messageDataList->msg[messageDataList->last].ip = ip;
messageDataList->last++;

printf("step4\n");

// Should do some circular type of list here ???

// Release exclusive access to the shared memory
//sem_signal (semid);

printf("Step5\n");

return 1;
}


void sem_wait( int id )
{
// Prepare to decrement the semaphore to gain exclusive access
// struct sembuf op[] = { { 0, -1, SEM_UNDO } };

//struct sembuf sem_b;
struct sembuf *sem_b;
sem_b = (struct sembuf *)malloc( sizeof( struct sembuf ) );


sem_b->sem_num = 0;
sem_b->sem_op = -1; /* P() */
sem_b->sem_flg = SEM_UNDO;

printf( "sem_wait1 with id %d\n", id );

///// ------------ CODE CRASHES HERE --------------------------------
// Perform the operation
if( semop( id, sem_b, 1 ) == -1 ) {
fprintf( stderr, "sem_wait failed\n" );
}

printf( "sem_wait2\n" );
}


An unexpected exception has been detected in native code outside the
VM.
Unexpected Signal : 11 occurred at PC=0x40035FB0
Function=sem_wait+0x50
Library=/lib/i686/libpthread.so.0

Current Java thread:
at AClassWithNativeMethods.insertMessage(Native Method)
at Main.main(Main.java:16)

Dynamic libraries:
08048000-0804e000 r-xp 00000000 00:09 498952922
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/bin/java
0804e000-0804f000 rw-p 00005000 00:09 498952922
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/bin/java
40000000-40013000 r-xp 00000000 08:01 12003 /lib/ld-2.3.2.so
40013000-40014000 rw-p 00013000 08:01 12003 /lib/ld-2.3.2.so
40014000-4001d000 r-xp 00000000 00:09 498951866
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/i386/native_threads/libhpi.so
4001d000-4001e000 rw-p 00008000 00:09 498951866
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/i386/native_threads/libhpi.so
4001f000-4002a000 r-xp 00000000 08:01 12036
/lib/libnss_files-2.3.2.so
4002a000-4002b000 rw-p 0000a000 08:01 12036
/lib/libnss_files-2.3.2.so
4002b000-40038000 r-xp 00000000 08:01 26007
/lib/i686/libpthread-0.10.so
40038000-4003b000 rw-p 0000d000 08:01 26007
/lib/i686/libpthread-0.10.so
4007c000-4007e000 r-xp 00000000 08:01 12016 /lib/libdl-2.3.2.so
4007e000-4007f000 rw-p 00002000 08:01 12016 /lib/libdl-2.3.2.so
4007f000-40397000 r-xp 00000000 00:09 498951874
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/i386/client/libjvm.so
40397000-4057b000 rw-p 00317000 00:09 498951874
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/i386/client/libjvm.so
4058c000-4059f000 r-xp 00000000 08:01 12020 /lib/libnsl-2.3.2.so
4059f000-405a0000 rw-p 00012000 08:01 12020 /lib/libnsl-2.3.2.so
405a2000-405c3000 r-xp 00000000 08:01 26005
/lib/i686/libm-2.3.2.so
405c3000-405c4000 rw-p 00021000 08:01 26005
/lib/i686/libm-2.3.2.so
405c4000-405d4000 r-xp 00000000 00:09 498951882
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/i386/libverify.so
405d4000-405d6000 rw-p 0000f000 00:09 498951882
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/i386/libverify.so
405d6000-405f7000 r-xp 00000000 00:09 498951884
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/i386/libjava.so
405f7000-405f9000 rw-p 00020000 00:09 498951884
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/i386/libjava.so
405f9000-4060e000 r-xp 00000000 00:09 498951888
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/i386/libzip.so
4060e000-40610000 rw-p 00014000 00:09 498951888
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/i386/libzip.so
40610000-41cdf000 r--s 00000000 00:09 498952870
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/rt.jar
41d22000-41d39000 r--s 00000000 00:09 498951938
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/sunrsasign.jar
41d39000-41daa000 r--s 00000000 00:09 498951952
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/jsse.jar
41daa000-41dbd000 r--s 00000000 00:09 498951940
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/jce.jar
41fe7000-41fea000 r--s 00000000 00:09 498951932
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/ext/dnsns.jar
41fea000-41ff8000 r--s 00000000 00:09 498951936
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/ext/ldapsec.jar
41ff8000-41ffa000 r-xp 00000000 00:09 740098058
/afs/cs.pitt.edu/usr0/cjsst55/jniExample/libNative.so
41ffa000-41ffb000 rw-p 00001000 00:09 740098058
/afs/cs.pitt.edu/usr0/cjsst55/jniExample/libNative.so
41ffb000-41ffe000 rw-s 00000000 00:04 1769474 /SYSV00000050
(deleted)
42000000-42134000 r-xp 00000000 08:01 26003
/lib/i686/libc-2.3.2.so
42134000-42139000 rw-p 00134000 08:01 26003
/lib/i686/libc-2.3.2.so
4213b000-423f7000 r--s 00000000 00:09 498952840
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/charsets.jar
443f7000-443ff000 r-xp 00000000 08:01 12041
/lib/libnss_nis-2.3.2.so
443ff000-44400000 rw-p 00008000 08:01 12041
/lib/libnss_nis-2.3.2.so
4c481000-4c640000 r--p 00000000 08:07 192875
/usr/lib/locale/locale-archive
4c844000-4c861000 r--s 00000000 00:09 498951930
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/ext/sunjce_provider.jar
4c861000-4c900000 r--s 00000000 00:09 498951934
/afs/cs.pitt.edu/system/@Sys/usr/local/java/jdk1.4.1_02/jre/lib/ext/localedata.jar
4c900000-4c906000 r--s 00000000 08:07 128783
/usr/lib/gconv/gconv-modules.cache

Local Time = Fri Jun 25 15:46:40 2004
Elapsed Time = 0
#
# The exception above was detected in native code outside the VM
#
# Java VM: Java HotSpot(TM) Client VM (1.4.1_02-b06 mixed mode)
#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top