Try sys_ioperm (101?) first. You'll need root permissions to do this. To give your program root permissions, without the "user" having to be root... As root:
chown root:root myprog
chmod +s myprog
I'm not sure what your proposed code is going to do for you, but I think you'll be able to do it without segfault if you do sys_ioperm first...
Best,
Frank