Hi all!
This problem had me foxed for a while. Basically I am forking a process. One process increments a number and writes it to shared memory every 60 seconds. The child, every 10 seconds, increments another number, reads the shared memory and subtracts the number it finds there. It then prints the result.
If you run this badly written code you will see that 3-1 really does equal 2 in this wonderful perl world. However, 7-2 equals 6!! 13-3 equals 12!!
What seems to be happening is that when the script reads shared memory into the variable $message, it will display it correctly when the contents of shared memory change BUT when you do arithmetic on $message it uses the first value of $message it saw, even after it changes. The solution is to "undef" $message before you read shared memory but it seems a bit odd to me.
<Am i missing some clever thing I really ought to know about (apart from wasting 99 bytes of RAM). I´ve tried this with version 5.8.4 on Solaris and 5.10.0 on OSXUpdate: In case anybody is interested, here is the bug report and some responses. There is even a patch :)
#!/usr/bin/perl use IPC::SysV qw(IPC_STAT IPC_PRIVATE IPC_CREAT IPC_EXCL S_IRUSR S_IWU +SR IPC_RMID); $key=shmget(IPC_PRIVATE,100,IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); my $ret=fork(); if($ret>0){ my $int=0; while(1){ ++$int; shmwrite($key,$int,0,99); sleep 60; } } else { my $t=0; while(1){ shmread($key,$message,0,99); ++$t; $result=$t-$message; print "$t - $message is $result\n"; sleep 10; } }
In reply to Is this a bug in perl or a bug in me? by Wheely
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |