it throws Illegal seek error.
I doubt that. Perl's msgget() never 'throws', it just returns undef to indication failure. If you are printing an error message that include $! that then say "Illegal seek", that likely has little to do with your call to msgget(). The underlying system's msgget(2) doesn't appear to be able to fail for that reason.
Most likely, you are reporting $! even though msgget() didn't actually fail or you are doing too much before $! is output such that the value of $! gets overwritten.