Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Get Thread PID

843829Dec 6 2007 — edited Dec 9 2007
Hello,

I've to write small library using JNI. Its purpose is to provide access to CPU time of specified java threads. Class should have two public methods: one registering current Thread to be included in stats and second one to return stats as Map<Thread,long> (where long is CPU time). Whole thing should work on Solaris.

My idea to write it looks like this:
- registering method gets current thread PID (JNI) and saves Thread<->PID information somewhere
- return-stats method takes all PIDs collected by register method and for each of them retrieve CPU time (JNI), then puts Thread<->Time to Map and job is done

Question: using C getpid() function will I get current thread PID? or maybe just "java" PID ?

I know how to write most of code (both c and java), but I experienced some linkage problems while compiling c code.

Problem: while compiling this code:
#include "j_unistd.h"
#include <unistd.h>
#include <string.h>
JNIEXPORT jlong JNICALL Java_unistd_getpid
  (JNIEnv *envp, jclass clazz)
  {
        return getpid();
  }
using
gcc -shared -I. -I"$JHOME"/include -I"$JHOME"/include/solaris j_unistd.c -o libj_unistd.so
I get
Text relocation remains                         referenced
    against symbol                  offset      in file
getpid                              0x7         /var/tmp//cchvXk7h.o
ld: fatal: relocations remain against allocatable but non-writable sections
collect2: ld returned 1 exit status
I googled for this error but I found nothing interesting. So the question is: can you help me? :)

Thank you.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 6 2008
Added on Dec 6 2007
3 comments
330 views