Monday, 15 August 2011

c - Playing around with signals -


I am currently working on a project to learn about signals, basically I have clients and servers , The server Pid on which I should have the input as a parameter with the string with the client, after the launch, the customer sends this string to the server and the server displays it.

What I have done so far is the string in the binary and if the integer is a 0 then I otherwise send SIGUSR1 (if it is 1) I send the SIGUSR2, in this way I want to convert binary to string I am thinking but how can I do this?

Here the main one is for the server, as you can see that it shows its PID (so we can launch the customer after this) and are waiting for the signal. Once it gets hold of one, it calls my_sig (which you will find later in this thread).

  int main () {int pid; Pid = getpid (); Printf ("Server is Paid:% d \ n", pid); If (sign (SIGUSR1, my_sig) == SIG_ERR) {printf ("Error. \ N"); Exit (1); } If (sign (SIGUSR2, my_sig) == SIG_ERR) {printf ("Error. \ N"); Exit (1); } While (1); }  

And here's my simple simplest my_sig, as you can see that it receives the SigrousR 1 as the signal, it displays a 0, otherwise 1.

  zero my_sig (int param) {char * src; Four * destination; If (param == SIGUSR1) {my_printf ("0"); } If (param == SIGUSR2) {my_printf ("1"); }}  

What it does at the end of the day, the word given in the client is displayed in binary four characters.

How do I use it? Edit: As much as I would like to know about other signals, I would love to know about other signals, unfortunately, I I am limited to the projects.

Using both signals to transmit binary data will be slow and disabled if you still do so If you insist, you will have to pack bits in byte rather than print "1" or "0"

Here's a way you can do that.

  # MAX_BYTES64 constant id current_bit = 0, current_byte = 0; Fixed four bytes = 0, output_byte [MAX_BYTES]; Zero my_sig {char * src; Four * destination; If (param == SIGUSR1) {bytes and = ~ (128>> current_beat); } If (param == SIGUSR2) {byte | = (128>> current_beat); } Current_bit ++; If (current_bit> 7) {current_bit = 0; Output_byte [current_byte] = byte; Byte = 0; If (current_byte & lt; (MAX_BYTES-1)) {current_byte ++; }}}  

This is bad code but the idea of ​​sending signals data is not good anyway.

Output will be in the output_byte array.


No comments:

Post a Comment