I'm storing a series of pid
s (i.e. Linux process ids) in an array of long
s. I realize that a pid
is not a long
, but I have no choice in terms of using a different variable type.
The issue I'm having occurs when I try and print the pid
using printf
. If I print the long
that stores the pid
using %ld
, I get the wrong pids:
8435315771308 process_ancesto
8358006359962 bash
8353711392665 login
4294967297 init
0 swapper/0
However, if I print using %d
(which generates a compiler warning), I get the correct result (i.e. the result returned by typing ps
into the terminal):
1969 process_ancesto
1946 bash
1945 login
1 init
0 swapper/0
What is causing this behaviour? A pid -> long cast is a widening conversion and shouldn't cause any problems.
Here is the program I use to make the system call that returns the pids:
int main(int argc, char *argv[])
{
struct process_info arr[10];
long n=0;
int result = syscall(_CS300_PROCESS_ANCESTORS_, arr,10,&n);
assert(result==0);
for(int i=0;i<n;i++) {
printf("%d %s\n",arr[i].pid,arr[i].name);
}
return 0;
}
If I replace the %d
with %ld
it prints incorrect info.
Here is the line from my system call where I record the pid
:
if(copy_long_to_user(&info_array[num_filled_kernel].pid, &curr_task->pid)) {
return -EFAULT;
}
info_array[num_filled_kernel].pid
is a long.