comp.lang.ada
 help / color / mirror / Atom feed
* Handling signals in ADA
@ 2001-03-13 11:09 Tomas Hlavaty
  2001-03-15  5:21 ` DuckE
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Tomas Hlavaty @ 2001-03-13 11:09 UTC (permalink / raw)


Hi, could you help me with translating following C code to ADA?

-- My C program is:

#include <signal.h>

int stop = 0;

void handler(int signo)
{
        stop = 1;
        printf("Signal %i received\n", signo);
}

int main()
{
        signal(SIGUSR1, handler);
        while (stop == 0);
        return 0;
}

-- How to translate it to ADA?

Thanks

Tomas



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Handling signals in ADA
  2001-03-13 11:09 Handling signals in ADA Tomas Hlavaty
@ 2001-03-15  5:21 ` DuckE
  2001-03-15 18:48 ` David Starner
  2001-03-15 22:05 ` Florian Weimer
  2 siblings, 0 replies; 12+ messages in thread
From: DuckE @ 2001-03-15  5:21 UTC (permalink / raw)


Yes, here it is, but that's probably not what you really want to do.

WITH Signal_Handler_Package;

PROCEDURE SignalTest IS

BEGIN
  Signal_Handler_Package.Test_Signal;
END SignalTest;

PACKAGE Signal_Handler_Package IS

  PROCEDURE Test_Signal;

END Signal_Handler_Package;

with Ada.Text_Io;
with Ada.Integer_Text_Io;

package body Signal_Handler_Package is

   Stop : Integer := 0;
   pragma Atomic( Stop );

   sigusr1 : constant := 1;

   type Handler_Procedure is access procedure( Signo : Integer );
   pragma Convention( C, Handler_Procedure );

   procedure Signal (
         Sig_Type : Integer;
         handler_access : Handler_Procedure );
   pragma Import( C, Signal );

   procedure Handler (
         Signo : Integer );
   pragma Convention( C, Handler);

   procedure Handler (
         Signo : Integer ) is
   begin
      Stop := 1;
      Ada.Text_Io.Put( "Signal " );
      Ada.Integer_Text_Io.Put( Signo );
      Ada.Text_Io.Put_Line( " received" );
   end Handler;


   procedure Test_Signal is
   begin
      Signal( Sigusr1, Handler'access );
      while Stop = 0 loop
         null;
      end loop;
   end Test_Signal;

end Signal_Handler_Package;


I don't know the correct definition for sigusr1, so I made one up just to
get the code to build.

But as I mentioned, this most likely isn't what you want to do since Ada has
protected types and tasking built into the language for performing functions
of this nature.  The above example does however illustrate that if you want
to do it the 'C' way in Ada, you can.

SteveD


"Tomas Hlavaty" <hlavaty@labe.felk.cvut.cz> wrote in message
news:3AADFFCF.691F1F8@labe.felk.cvut.cz...
> Hi, could you help me with translating following C code to ADA?
>
> -- My C program is:
>
> #include <signal.h>
>
> int stop = 0;
>
> void handler(int signo)
> {
>         stop = 1;
>         printf("Signal %i received\n", signo);
> }
>
> int main()
> {
>         signal(SIGUSR1, handler);
>         while (stop == 0);
>         return 0;
> }
>
> -- How to translate it to ADA?
>
> Thanks
>
> Tomas





^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Handling signals in ADA
  2001-03-13 11:09 Handling signals in ADA Tomas Hlavaty
  2001-03-15  5:21 ` DuckE
@ 2001-03-15 18:48 ` David Starner
  2001-03-16 17:17   ` Robert A Duff
  2001-03-15 22:05 ` Florian Weimer
  2 siblings, 1 reply; 12+ messages in thread
From: David Starner @ 2001-03-15 18:48 UTC (permalink / raw)


On Tue, 13 Mar 2001 12:09:03 +0100, Tomas Hlavaty
<hlavaty@labe.felk.cvut.cz> wrote:
>Hi, could you help me with translating following C code to ADA?

Someone else posted a literal translation*. The question is, what do you
want to do? For most Ada programming, signals aren't the right tool for
the job. Tasks and exceptions can handle many signal uses; others may
take a little more thought to rewrite, but it's usually better to
program in the language you're programming in, than force a design for C
into Ada. (As a side note, SIGUSR1 is not always safe to use in an Ada
program - GNAT in the standard (ACT) configuration uses it for
threading.) 

However, sometimes the external enviroment hands a signal you have to
handle. In which case, my solution would be to write a little C
encapsulating all the signal details and call it. You could also check
out the Ada packages listed below, or look up Florist, a free Ada Posix
binding.

* To properly use SIGUSR1, you could write a one line C function 
returning SIGUSR1 and import it, or write a compile time program that
finds the value of SIGUSR1 and includes it. If you're using GNAT, you
should check out Ada.Interrupts.Names and System.OS_Interface, which
also tell you all the reserved signals.

-- 
David Starner - dstarner98@aasaa.ofe.org
Pointless website: http://dvdeug.dhis.org
"I don't care if Bill personally has my name and reads my email and 
laughs at me. In fact, I'd be rather honored." - Joseph_Greg



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Handling signals in ADA
  2001-03-13 11:09 Handling signals in ADA Tomas Hlavaty
  2001-03-15  5:21 ` DuckE
  2001-03-15 18:48 ` David Starner
@ 2001-03-15 22:05 ` Florian Weimer
  2001-03-15 23:42   ` Ed Falis
                     ` (2 more replies)
  2 siblings, 3 replies; 12+ messages in thread
From: Florian Weimer @ 2001-03-15 22:05 UTC (permalink / raw)


Tomas Hlavaty <hlavaty@labe.felk.cvut.cz> writes:

> Hi, could you help me with translating following C code to ADA?

The programming language is called 'Ada'.

> -- How to translate it to ADA?

The standard Ada library does not support signals.  Typical Ada
programs use other communication mechanisms, and a verbatim
translation is not possible.  If you need signal handling because it's
required by your environment, have a look at POSIX.5.



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Handling signals in ADA
  2001-03-15 22:05 ` Florian Weimer
@ 2001-03-15 23:42   ` Ed Falis
  2001-03-16  1:27   ` David C. Hoos, Sr.
  2001-03-16  3:06   ` (null)
  2 siblings, 0 replies; 12+ messages in thread
From: Ed Falis @ 2001-03-15 23:42 UTC (permalink / raw)


Florian Weimer wrote:
> Tomas Hlavaty <hlavaty@labe.felk.cvut.cz> writes:
>
>> Hi, could you help me with translating following C code to ADA?
>
> The programming language is called 'Ada'.
>
>> -- How to translate it to ADA?
>
> The standard Ada library does not support signals.  Typical Ada
> programs use other communication mechanisms, and a verbatim
> translation is not possible.  If you need signal handling because it's
> required by your environment, have a look at POSIX.5.

Actually, it depends on the implementation.  If you're using GNAT, you
can handle signals using the Ada interrupt handling facilities defined
in Annex C, or Ada 83-style interrupt entries, or if you prefer,
directly with POSIX facilities.

- Ed Falis
ACT



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Handling signals in ADA
  2001-03-15 22:05 ` Florian Weimer
  2001-03-15 23:42   ` Ed Falis
@ 2001-03-16  1:27   ` David C. Hoos, Sr.
  2001-03-16  1:48     ` Jeffrey Carter
  2001-03-16 21:06     ` Florian Weimer
  2001-03-16  3:06   ` (null)
  2 siblings, 2 replies; 12+ messages in thread
From: David C. Hoos, Sr. @ 2001-03-16  1:27 UTC (permalink / raw)



"Florian Weimer" <fw@deneb.enyo.de> wrote in message
news:87y9u6k7s6.fsf@deneb.enyo.de...
> Tomas Hlavaty <hlavaty@labe.felk.cvut.cz> writes:
>
> > Hi, could you help me with translating following C code to ADA?
>
> The programming language is called 'Ada'.
>
> > -- How to translate it to ADA?
>
> The standard Ada library does not support signals.  Typical Ada
> programs use other communication mechanisms, and a verbatim
> translation is not possible.  If you need signal handling because it's
> required by your environment, have a look at POSIX.5.

This answer is just plain wrong. The following program demonstrates
the use of signals in Ada, using only language-defined units.

Now. to be sure, because Ada restricts interrupt handlers to being
parameterless protected procedures, it is not possible to
pass parameter like what can be done under UNIX with the
standard C runtime, for example.

But the "standard Ada library" _DOES_ support signals.

-- begin source code

package body Signal_Handlers
is

   SIG_INT_Received : Boolean := False;

     function SIGINT_RECEIVED return BOOLEAN
     is
     begin
        return SIG_INT_Received;
     end SIGINT_Received;

   protected body Signals
   is
      procedure Handle_SIGINT
      is
      begin
         Ada.Text_IO.Put_Line ("SIGINT received..; Terminating.");
         SIG_INT_Received := True;
      end Handle_SIGINT;
      procedure Handle_SIGUSR1
      is
      begin
         Ada.Text_IO.Put_Line ("SIGUSR1 received and handled.");
      end Handle_SIGUSR1;
      procedure Handle_SIGUSR2
      is
      begin
         Ada.Text_IO.Put_Line ("SIGUSR2 received and handled.");
      end Handle_SIGUSR2;
   end Signals;

end Signal_Handlers;
with Ada.Interrupts.Names;
with Ada.Text_IO;
package Signal_Handlers
is
   pragma Unreserve_All_Interrupts;

   function SIGINT_Received return Boolean;
   protected Signals is
      procedure Handle_SIGINT;
      procedure Handle_SIGUSR1;
      procedure Handle_SIGUSR2;
   private
      pragma Attach_Handler
        (Handle_SIGINT, Ada.Interrupts.Names.SIGINT);
      pragma Attach_Handler
        (Handle_SIGUSR1, Ada.Interrupts.Names.SIGUSR1);
      pragma Attach_Handler
        (Handle_SIGUSR2, Ada.Interrupts.Names.SIGUSR2);
   end Signals;
end Signal_Handlers;
with Ada.Text_IO;
with Signal_Handlers;
procedure Test_Signal_Handler
is
begin
   loop
      exit when Signal_Handlers.SIGINT_Received;
      Ada.Text_IO.Put_Line ("I'm alive...");
      delay 1.0;
   end loop;
end Test_Signal_Handler;
.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Handling signals in ADA
  2001-03-16  1:27   ` David C. Hoos, Sr.
@ 2001-03-16  1:48     ` Jeffrey Carter
  2001-03-16 21:06     ` Florian Weimer
  1 sibling, 0 replies; 12+ messages in thread
From: Jeffrey Carter @ 2001-03-16  1:48 UTC (permalink / raw)


"David C. Hoos, Sr." wrote:
> 
>       pragma Attach_Handler
>         (Handle_SIGINT, Ada.Interrupts.Names.SIGINT);
>       pragma Attach_Handler
>         (Handle_SIGUSR1, Ada.Interrupts.Names.SIGUSR1);
>       pragma Attach_Handler
>         (Handle_SIGUSR2, Ada.Interrupts.Names.SIGUSR2);

Since the constants declared in Ada.Interrupts.Names are
implementation-defined, the most you can say is that this works on
certain compilers & OS's, which you should list.

-- 
Jeff Carter
"Go and boil your bottoms."
Monty Python & the Holy Grail



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Handling signals in ADA
  2001-03-15 22:05 ` Florian Weimer
  2001-03-15 23:42   ` Ed Falis
  2001-03-16  1:27   ` David C. Hoos, Sr.
@ 2001-03-16  3:06   ` (null)
  2001-03-16 13:10     ` Tomas Hlavaty
  2 siblings, 1 reply; 12+ messages in thread
From: (null) @ 2001-03-16  3:06 UTC (permalink / raw)


In article <87y9u6k7s6.fsf@deneb.enyo.de>,
Florian Weimer  <fw@deneb.enyo.de> wrote:
>Tomas Hlavaty <hlavaty@labe.felk.cvut.cz> writes:
>
>> Hi, could you help me with translating following C code to ADA?
>
>
>The standard Ada library does not support signals.  Typical Ada
>programs use other communication mechanisms, 

Yes, but typical Unix programs use signals.

Florist will have what you want.  You can find those at 

 ftp://ftp.cs.fsu.edu/pub/PART 

Alternatively if you're using the gnat compiler you can the facilities
in Ada.Interrupts.  Here's an example-


-- cut here --
with Ada.Text_IO; use Ada.Text_IO;

with Ada.Interrupts.names;

with Interrupt_Handler; use Interrupt_Handler;

pragma Unreserve_All_Interrupts;

procedure When_Is_It_Over is
   Fat_Lady:  Singer(Ada.Interrupts.Names.SIGINT);
begin
   loop
      New_Line;
      delay 1.0;
      Put("It ain't over...");
      exit when Fat_Lady.Sings;
   end loop;
   Put_Line(" until the fat lady sings.");
end When_Is_It_over;



-- cut here -- 
with Ada.Interrupts;

package Interrupt_Handler is
   protected type Singer (Id: Ada.Interrupts.Interrupt_Id) is
      function Sings return Boolean;
      procedure Handler;
      pragma Attach_Handler(Handler, Id);
   private
      Done: Boolean := False;
   end Singer;
end;

-- cut here --
package body Interrupt_Handler is
   protected body Singer is
      function Sings return Boolean is
      begin
         return Done;
      end Sings;

      procedure handler is
      begin
         done := True;
      end handler;
   end Singer;
end;
-- 
=======================================================================
 Life is short.                  | Craig Spannring 
      Bike hard, ski fast.       | cts@internetcds.com
 --------------------------------+------------------------------------



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Handling signals in ADA
  2001-03-16  3:06   ` (null)
@ 2001-03-16 13:10     ` Tomas Hlavaty
  0 siblings, 0 replies; 12+ messages in thread
From: Tomas Hlavaty @ 2001-03-16 13:10 UTC (permalink / raw)


Thank you for your help. I've got by the problem using protected type
and pragma Attach_Handler. Its very easy.

Tomas



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Handling signals in ADA
  2001-03-15 18:48 ` David Starner
@ 2001-03-16 17:17   ` Robert A Duff
  0 siblings, 0 replies; 12+ messages in thread
From: Robert A Duff @ 2001-03-16 17:17 UTC (permalink / raw)


dvdeug@x8b4e53cd.dhcp.okstate.edu (David Starner) writes:

> Someone else posted a literal translation*. The question is, what do you
> want to do? For most Ada programming, signals aren't the right tool for
> the job. Tasks and exceptions can handle many signal uses; others may
> take a little more thought to rewrite, but it's usually better to
> program in the language you're programming in, than force a design for C
> into Ada. (As a side note, SIGUSR1 is not always safe to use in an Ada
> program - GNAT in the standard (ACT) configuration uses it for
> threading.) 

I don't think using signals constitutes "a design for C".  Signals are
an operating system feature.  Many operating systems have similar
features ("per-process interrupts" or "asynchronous system traps" or
whatever).  That includes operating systems that are not so (evilly)
C-centric as Unix.

Anyway, if you want interface to signals, use a protected object with a
pragma Attach_Handler, and the various related features.  I have done
this, for example, to catch control-C interrupts -- portably to both
Unix and Windows.

> * To properly use SIGUSR1, you could write a one line C function 
> returning SIGUSR1 and import it, or write a compile time program that
> finds the value of SIGUSR1 and includes it. If you're using GNAT, you
> should check out Ada.Interrupts.Names and System.OS_Interface, which
> also tell you all the reserved signals.

You should check out Ada.Interrupts.Names even if you're *not* using
GNAT -- this package is standard (although its contents are obviously
system dependent).

- Bob



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Handling signals in ADA
  2001-03-16  1:27   ` David C. Hoos, Sr.
  2001-03-16  1:48     ` Jeffrey Carter
@ 2001-03-16 21:06     ` Florian Weimer
  2001-03-17  1:00       ` David C. Hoos, Sr.
  1 sibling, 1 reply; 12+ messages in thread
From: Florian Weimer @ 2001-03-16 21:06 UTC (permalink / raw)


"David C. Hoos, Sr." <david.c.hoos.sr@ada95.com> writes:

> > The standard Ada library does not support signals.  Typical Ada
> > programs use other communication mechanisms, and a verbatim
> > translation is not possible.  If you need signal handling because it's
> > required by your environment, have a look at POSIX.5.
> 
> This answer is just plain wrong. The following program demonstrates
> the use of signals in Ada, using only language-defined units.

There is no requirement in the standard that Ada interrupts are
mapped to C signals.  I believe that there are quite a number of
implementations where no such correspondence exists (it would make
sense for a DOS implementation, for example).



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Handling signals in ADA
  2001-03-16 21:06     ` Florian Weimer
@ 2001-03-17  1:00       ` David C. Hoos, Sr.
  0 siblings, 0 replies; 12+ messages in thread
From: David C. Hoos, Sr. @ 2001-03-17  1:00 UTC (permalink / raw)


Remember that in the context of the original inquirer's question,
he was using an OS with Unix-style signals, so he couldn't have been
talking about a DOS implementation where Unix-style signals
do not exist.

I have yet to see an Ada95 compiler targeted to a platform with
Unix-style signals that does not support the correspondence between
signals and interrupts.  Perhaps there is one or more, but I've not
seen them.  Do you know of any?

"Florian Weimer" <fw@deneb.enyo.de> wrote in message
news:87u24t4e5y.fsf@deneb.enyo.de...
> "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com> writes:
>
> > > The standard Ada library does not support signals.  Typical Ada
> > > programs use other communication mechanisms, and a verbatim
> > > translation is not possible.  If you need signal handling because it's
> > > required by your environment, have a look at POSIX.5.
> >
> > This answer is just plain wrong. The following program demonstrates
> > the use of signals in Ada, using only language-defined units.
>
> There is no requirement in the standard that Ada interrupts are
> mapped to C signals.  I believe that there are quite a number of
> implementations where no such correspondence exists (it would make
> sense for a DOS implementation, for example).




^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2001-03-17  1:00 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-13 11:09 Handling signals in ADA Tomas Hlavaty
2001-03-15  5:21 ` DuckE
2001-03-15 18:48 ` David Starner
2001-03-16 17:17   ` Robert A Duff
2001-03-15 22:05 ` Florian Weimer
2001-03-15 23:42   ` Ed Falis
2001-03-16  1:27   ` David C. Hoos, Sr.
2001-03-16  1:48     ` Jeffrey Carter
2001-03-16 21:06     ` Florian Weimer
2001-03-17  1:00       ` David C. Hoos, Sr.
2001-03-16  3:06   ` (null)
2001-03-16 13:10     ` Tomas Hlavaty

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox