With a client-server architecture the underlying database contains all the information that is necessary to describe what to put on a new page. The crucial question is when to update a page. When you use server-pull you have to anticipate after how many seconds you want a new page. Hence, this time interval should not be dependent on actions of other participants. However, with server-push actions of other participants may trigger an update of a page.
For instance, only after all users have entered a stage of an experiment, we want to update their screen and give them some information. Or, only after a player's opponent has made a choice, we can inform the player about the opponent's action.
An inefficient way to do this would be to have all cgi-scripts (we have one for each player) constantly checking, whether a certain condition that requires action (a server-push) has occured. Actively checking, however, requires resources.
A more efficient way to do this is to send all cgi-scripts to sleep and wake them up when the condition that requires action occurs.
In order to wake up we use signals. There are several signals2.1, most of them are used by the system, but the signal USR1 can be used by us.
The following C-fragment should clarify the procedure:
getpid();gives us our pid. We have to store it either in a file or in a database. Having done that we have to specify the function to be called when the signal arrives. This function will then typically push a new page.
signal (SIGUSR1, <function that describes what to do>)Notice that signal() does not call this function immediately. Instead it tells the system to call the function when the signal arrives. We typically activate signals only when we do not have anything better to do - thus, the best thing is to sleep for a long while. It is a good idea to wake up and terminate if no signal arrives for a long while. In this example we assume that after nothing happens for 3600 seconds, i.e. one hour, we can safely assume that the experiment broke down and terminate.
sleep (3600); exit (0);If a signal arrives in the meantime, the cgi-script will jump to the function specified above. This function may then first disable the signal with
signal (SIGUSR1,SIG_IGN)and then push the page. Having done that, the function will possibly activate another signal and go to sleep again.
kill (pid,SIGUSR1);The above send the signal USR1 to the process pid. If we want to send signals to several other cgi-scripts, e.g., the last player that reaches a certain stage notifies the others that he or she has made it, we have to send signals sequentially to several pids. Notice that in most cases we do not want to send the signal to ourselves.