The solution in the above link blocking-and-resuming-execution-of-an-independent-process-in-linux says, I can use ptrace to achieve my goal.
I have tried to run run the code from-
how ptrace work between 2 processes
, but not getting any output.
This is specifically asked to know how to use ptrace to make a program execute only few instructions like in debugger case.
How to use ptrace in following situation to restrict other process to access few instructions?
I have two independent C programs under linux. Program1 is running in CPU core-1 and Program-2 is running in CPU core-2.
Program-2 is executing shared library function, func-2, consists of 200 lines of instruction to perform operation (add+shift) on data .
shared library function
-------
func-2()
{
// code to perform operation (add+shift) on data
}
--------
Program2.
main()
{
while(1)
func-2();
}
program1
main()
{
while()
{
// ptrace
// OR
//kill STOP <pid of program2>
// kill CONT <pid of program2>
}}
I want to restrict program-2 from inside Program-1 or bash , so that Program-2 can execute only few instructions or restrict to run for 1-2 microsec, I can't add any code inside Program2. Program-1 knows PID of program2 and base address of func-2.
I heard about ptrace which can be used to control other process and using ptrace it is possible to restrict process to execute only 1 instruction. For me even restrict process for 1-2 microsec ( 5-10 instructions) will be sufficient.
How can I control Program-2 which is running other CPU core? Any link to relevant documents is highly appreciated. Thanks in advance. I am using gcc under linux.