0
$\begingroup$

I am taking an Operating Systems course and as a project, we are printing the content of the PCB of a process from task_struct of that process and its mm_struct & vm_area_struct pointers. During my stack growth experiments, I used a recursive summation method with parameter n, which returns the sum of first n integers. However I realized that in some cases, same parameters do not result in same growths. What might be the reason for this?

Here's how we find the stack boundaries, in case it is necessary:

  • Using for_each_process, we find the task_struct of the process with pid = x
  • From task struct, we get the process' mm_struct & vm_area_struct pointers
  • Using a linear search, we detect the stack as follows: vma->vm_start < mm->start_stack && vma->vm_end >= mm->start_stack; where vma is a virtual_area_struct

Note that this procedure is done with a kernel module.

$\endgroup$
  • $\begingroup$ Can you explain your experiment in more detail? How does the number of recursive calls relate to the parameter n? Might the size of the stack frame of the recursive function vary? Might the process be doing anything else beyond running your recursive function? Is it dynamically linked? (If so it is running the dynamic linker.) Are you using malloc, dynamic memory allocation, etc.? Are you calling any C standard library functions? All of those might be doing nontrivial things that aren't immediately apparent to you. $\endgroup$ – D.W. May 1 '18 at 0:35
  • $\begingroup$ Recursive function calculates the sum of first n integers, each step is 1 + sum(n-1) where n is the parameter & base case is n = 0, at which I wait for user input and a chance to load and remove the kernel module to see the PCB content. Process, until I give the input, waits at input call getchar(). For dynamic allocation, I use malloc(), but not during the stack experiments. In source code, I first comment dynamic allocation lines to make the output more readable to me. Feel free to ask any more details if these are not enough. $\endgroup$ – benazus May 1 '18 at 8:03
  • $\begingroup$ Waiting for user input might cause nondeterminism. I suggest measuring the size of the stack before and after user input to see if that is having an effect. $\endgroup$ – D.W. May 1 '18 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.