In Minitalk, the client doesn’t simply fire-off text messages over to the server; remember we are dealing at the low-level. The client crafts a symphony of character-by-character, 8 bit at a time with ‘1’s and ‘0’s, uses the SIGUSR1 and SIGUSR2 signals switching to compose the melody.
The server is on the other end, putting together these ‘1’s and ‘0’s and deciphering the message. So, when the server receives the signals using signal()
, it simply has to put back together these 1’s and 0’s into place (remember, you wrote your ft_btoa_bitshift.c
in the previous chapter) and to write()
them in the terminal.
Thus, Minitalk isn’t typical exchange of messages but a symphony of signals.
NOTE: In these articles, I aim to guide readers so they can master the process independently. I avoid giving away the entire code because I believe in your ability to write it yourself.
Following my instructions, you now have the key pieces neatly arranged in just two files: client.c
and server.c
.
Well. While the client and server are performing the concert, remember the Minitalk subject’s warning about the delay?
TODO: image of delay
Sometimes, these signals may not make it through till the server, similar to a note getting lost in the music. Out of curiosity, I measured how long it takes to send each bit using this little clock()
⏰
void atob_bitshift(int pid, char c)
{
int i;
int current_bit;
clock_t start, end; //#include <time.h>
double cpu_time_used; //#include <time.h>
i = 0;
while (i < 8)
{
start = clock();
// your logic here to convert character to binary one bit at a time
// if that bit is a 1, send a SIGUSR1 - kill(pid, SIGUSR1);
// if that bit is a 0, send a SIGUSR2 - kill(pid, SIGUSR2);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
// Print or log the time taken
printf("Time taken to transfer bit %d: %f seconds\n", i, cpu_time_used);
usleep(200);//Read further to know why there is this deliberate pause
i++;
}
}
Here, did you notice that one of the bits took 122 microseconds to reach the server? There, you need to add a touch of a deliberate pause, a delay to the Minitalk tune. Adjusting this delay is similar to fine-tuning the pace of our musical conversation. There you adjust the usleep()
. For example, usleep(200)
was enough on my 42 workstation, but while I used it on my laptop, I had to adjust it to usleep(1000)
.
With this, the Minitalk symphony (mandatory) is now set.
TODO: These are notes to myself. I will fit these into the article as and when I find answers. You can help me if you have answers to these.
1. how to handle problems with kill function…exit()?
2. should I naturally exit the server? what happens, if I abruptly close the server after I tested? what happens to the process?