1

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.

Community
  • 1
  • 1
bholanath
  • 1,699
  • 1
  • 22
  • 40
  • If you're in control of how the process gets executed you can set a timer in the new process after it's been forked and before the program gets exec'ed. Standard alarm(2) takes seconds. `setitimer(2)` can take microseconds so that looks like what you might be looking for. – Petr Skocik Apr 29 '16 at 20:02
  • 1
    Possible duplicate of [Blocking and resuming execution of an independent process in linux](http://stackoverflow.com/questions/36901621/blocking-and-resuming-execution-of-an-independent-process-in-linux) – Martin James Apr 29 '16 at 20:03
  • @PSkocik, I think alarm is used for signal sending and there must be some signal handler to handle that signal. I can't add anything inside Program-2. Second how settimer can be used inside program-1 or bash script to control Program-2. I want program-2 to run for few microsec. – bholanath Apr 29 '16 at 20:07
  • I (mis?) understood you wanted to kill the program for good after your time limit expires. That's what the SIGALRM does by default (=when there's no handler). – Petr Skocik Apr 30 '16 at 04:54

0 Answers0