Signals, Pulses, and Messages in C

Further studies in pulses…messages… AND SIGNALS!

Sorry. Signals don’t rate capslock, tbh. But I shall attempt to teach you about them.

The usual includes. msg.h is my teachers’ concoction, and may be found here.

 

Gonna need some globals – the main and a couple of helpter functions both need to access the logfile, the channel id, and the timestruct.

Begin!

 

Initialize the timestruct, cos it is no longer 1970 and I am not Dennis Ritchie.

 

The void pointer – a promise of a method, soon to be revealed.

 

That logfile declaration from above? Time to use it! This is nothing more than a textfile where your program is going to write some stuff down.

 

A couple more variables; theMessage is a handle for data that is received from the client. rcvid will be used to tell the client which of it’s many messages you are currently replying to.

 

Upon startup, this message logger will report its process id, channel id, and node descriptor, and log that info in a pidfile. As I write this I can’t help noticing that chid isn’t initialized yet. I really hope I figure out an explanation for that later.

 

This bit is kinda weird. These two functions are both at the bottom of this program, so you’re sending pulses and signals to yourself. Why do that? Narcissism? Shpakism? Just do it.

This is also weird. You’re bascially announcing, “Hey, a signal might show up, and if it does, here’s what you do.” And that instruction is standing orders for the rest of the program, or til you change it.

K, with all that established, begin a for loop that will run until further notice, taking messages and reporting them in the logfile.

Now, this part confuses me. Cause I thought that a pulse was just a message, with a rcvid of zero. But the teacher has us, apparently, handling two cases that both match the definition of “pulse” in my mind.

So, the first “if” handles rcvid == 0.

The second “if” redirects to a helper method that has the same big switch statement from my previous post. It also handles a “pulse”, and like I said, I’m confused. However, I swear to god this code runs and drive as written.

Take note of the timeToQuit int there. It takes the return value of the handleMessage function – if the message was a … sigh … pulse, I guess … it returns something other than zero, which terminates the loop, and the program.

So where are these pulses and signals coming from? Upsettingly, they are coming from within this very program.

Here’s the setup for a pulse. It is straight out of Getting Started With QNX Neutrino, which can be found here, page 152. RTFM fools.

The signal setup works almost exactly the same, there’s like one line that’s different.

My message handler. It logs a message if a message turns up, and shuts the whole show down if a confusingly explained pulse shows up.

And finally, the signal handler. It’s pretty easy.

And that’s the whole mess. It runs on my machine; I take no responsibility if it causes your computer to have a stroke instead. Here’s the finished code.

Would you like to receive updates by email?

Published by

Shannon Graham

Shannon has been writing brief essays and occasional how-to articles at Rocketships, Unaffiliated (.ca) whenever inspiration strikes, since 2012.

She is interested in your opinion.

Leave a Reply

Your email address will not be published. Required fields are marked *