1

When I create a thread with Java or Python I can't find the pid among the operating system threads. In fact get_ident() in Python gives me a very large number that can't be the PID.

In fact I need to set the process affinity of all other threads to the first processor core then I want to dedicate the other cores to my specific threads run in a program to create a real time environment. The threads will be less than remaining processor cores.

As I have read in stackoverflow it is not possible in Java and there will be the esigence of native calls. Can it be done in Python or I must use C/C++?

The program will be installed in a Linux machine.

keyser
  • 18,829
  • 16
  • 59
  • 101
Soroush
  • 11
  • 3
  • Does the JVM even guarantee that a Java Thread will be executed on the same underlying OS thread from beginning to end? If it does, you could just call into C from that thread with JNI to set the affinity. – PeterT Dec 03 '14 at 14:03
  • @PeterT I've concluded that Java threads aren't visible in the operating system I can't find them neither in the process manager in Windows nor by running ps -aux in Linux. So I'm in doubt if it can be possible in python. Python looks to have the same problem but in Python I can manage the OS threads directly. I want to get sure if Python isn't the solution neither. To conclude I must add that Java threads aren't OS threads so JVM doesn't guarantee anything. I think that it runs on my threads on a unique Os thread. – Soroush Dec 03 '14 at 14:15
  • Anyway it's not completely impossible in Java : http://stackoverflow.com/questions/2238272/java-thread-affinity – Soroush Dec 03 '14 at 14:18
  • yeah, all of those solutions use JNI and assume that every JVM thread runs on one OS thread exclusively (which might be a safe assumption on most platforms). – PeterT Dec 03 '14 at 14:28

2 Answers2

0

For java threads, especially under Linux, there is https://github.com/OpenHFT/Java-Thread-Affinity

Ralf H
  • 1,392
  • 1
  • 9
  • 17
  • Intersting, it actually just loads `libc` `libpthread` or `kernel32` depending on platform and just calls into those directly . More or less at least, it shifts the JNI code into another library (JNA). – PeterT Dec 03 '14 at 14:48
  • Actually, I after rereading, I think this is not what OP wants, which was forcing all thread of all other processes on one core, then grab the rest for his own threads. – Ralf H Dec 03 '14 at 16:09
0

I'm not sure I understand what you want exactly, but in Java I remember that I could launch multiple JVM and run my java programs on different OS processes, using inter-processes communication (socket, pipe or whatever you want) to do multi-core processing and syncronization. Knowing that, it might be possible to then set a process (whole JVM) exclusively on a core. You can get the PID of the JVM.

Memophysic
  • 147
  • 1
  • 6
  • It look nice! So I don't dive into C. But will it be possible to disactivate other threads in JVM eg. Garbage collection so I will be sure that my programme will be run on the selected CPU core without going to sleep? The latency counts for me! for example if I need an absolute max 10microsecond latency execution of a small piece of code – Soroush Dec 03 '14 at 15:40
  • That'd be a different matter, it has to do with JVM tweaking, and I don't know if that's possible. Surely you can keep the thread alive and measure the latency, I haven't done it though and it will vary from machine to machine if you're looking for an absolute time. It's worth trying, but I'm simply not sure how to tweak the JVM. – Memophysic Dec 03 '14 at 17:11