WeixiangYan/CodeTransOcean huggingface dataset:
Task: Deal cards for FreeCell




Go: package main

import (
    "fmt"
    "math"
    "math/rand"
    "os"
    "strconv"
    "time"
)

const sSuits = "CDHS"
const sNums = "A23456789TJQK"
const rMax32 = math.MaxInt32

var seed = 1

func rnd() int {
    seed = (seed*214013 + 2531011) & rMax32
    return seed >> 16
}

func deal(s int) []int {
    seed = s
    t := make([]int, 52)
    for i := 0; i < 52; i++ {
        t[i] = 51 - i
    }
    for i := 0; i < 51; i++ {
        j := 51 - rnd()%(52-i)
        t[i], t[j] = t[j], t[i]
    }
    return t
}

func show(cs []int) {
    for i, c := range cs {
        fmt.Printf(" %c%c", sNums[c/4], sSuits[c%4])
        if (i+1)%8 == 0 || i+1 == len(cs) {
            fmt.Println()
        }
    }
}

func main() {
    var game int
    switch len(os.Args) {
    case 1:
        rand.Seed(time.Now().UnixNano())
        game = 1 + rand.Intn(32000)
    case 2:
        var err error
        game, err = strconv.Atoi(os.Args[1])
        if err == nil && game >= 1 && game <= 32000 {
            break
        }
        fallthrough
    default:
        fmt.Println("usage: deal [game]")
        fmt.Println("       where game is a number in the range 1 to 32000")
        return
    }
    fmt.Printf("\nGame #%d\n", game)
    show(deal(game))
}
Task: Deal cards for FreeCell


C++: #include <windows.h>
#include <iostream>


using namespace std;


class fc_dealer
{
public:
    void deal( int game )
    {
	_gn = game;
	fillDeck();
	shuffle();
	display();
    }

private:
    void fillDeck()
    {
	int p = 0;
	for( int c = 0; c < 13; c++ )
	    for( int s = 0; s < 4; s++ )
		_cards[p++] = c | s << 4;
    }

    void shuffle()
    {
	srand( _gn );
	int cc = 52, nc, lc;
	while( cc )
	{
	    nc = rand() % cc;
	    lc = _cards[--cc];
	    _cards[cc] = _cards[nc];
	    _cards[nc] = lc;
	}
    }

    void display()
    {
	char* suit = "CDHS";
	char* symb = "A23456789TJQK";
	int z = 0;
	cout << "GAME #" << _gn << endl << "=======================" << endl;
	for( int c = 51; c >= 0; c-- )
	{
	    cout << symb[_cards[c] & 15] << suit[_cards[c] >> 4] << " ";
	    if( ++z >= 8 )
	    {
		cout << endl;
		z = 0;
	    }
	}
    }

    int _cards[52], _gn;
};

int main( int argc, char* argv[] )
{
    fc_dealer dealer;
    int gn;
    while( true )
    {
	cout << endl << "Game number please ( 0 to QUIT ): "; cin >> gn;
	if( !gn ) break;

	system( "cls" );
	dealer.deal( gn );
	cout << endl << endl;
    }
    return 0;
}










Fortran: module Freecell
  use lcgs
  implicit none

  character(4)  :: suit = "CDHS"
  character(13) :: rank = "A23456789TJQK"
  character(2) :: deck(0:51)

contains

subroutine Createdeck()
  integer :: i, j, n

  n = 0
  do i = 1, 13
    do j = 1, 4
      deck(n) = rank(i:i) // suit(j:j)
      n = n + 1
    end do
  end do

end subroutine
 
subroutine Freecelldeal(game)
  integer, intent(in) :: game
  integer(i64) :: rnum
  integer :: i, n
  character(2) :: tmp

  call Createdeck()
  rnum = msrand(game)

  do i = 51, 1, -1
    n = mod(rnum, i+1)
    tmp = deck(n)
    deck(n) = deck(i)
    deck(i) = tmp
    rnum = msrand()
  end do

  write(*, "(a, i0)") "Game #", game
  write(*, "(8(a, tr1))") deck(51:0:-1)
  write(*,*)

end subroutine  
end module Freecell

program Freecell_test
  use Freecell
  implicit none

  call Freecelldeal(1)
  call Freecelldeal(617)
   
end program
Task: Deal cards for FreeCell

C: #include <stdio.h>
#include <stdlib.h>
#include <locale.h>

wchar_t s_suits[] = L"♣♦♥♠", s_nums[] = L"A23456789TJQK";

#define RMAX32 ((1U << 31) - 1)
static int seed = 1;
int  rnd(void) { return (seed = (seed * 214013 + 2531011) & RMAX32) >> 16; }
void srnd(int x) { seed = x; }

void show(const int *c)
{
	int i;
	for (i = 0; i < 52; c++) {
		printf("  \033[%dm%lc\033[m%lc", 32 - (1 + *c) % 4 / 2,
			s_suits[*c % 4], s_nums[*c / 4]);
		if (!(++i % 8) || i == 52) putchar('\n');
	}
}

void deal(int s, int *t)
{
	int i, j;
	srnd(s);

	for (i = 0; i < 52; i++) t[i] = 51 - i;
	for (i = 0; i < 51; i++) {
		j = 51 - rnd() % (52 - i);
		s = t[i], t[i] = t[j], t[j] = s;
	}
}

int main(int c, char **v)
{
	int s, card[52];
	if (c < 2 || (s = atoi(v[1])) <= 0) s = 11982;

	setlocale(LC_ALL, "");

	deal(s, card);
	printf("Hand %d\n", s);
	show(card);

	return 0;
}










Fortran: module Freecell
  use lcgs
  implicit none

  character(4)  :: suit = "CDHS"
  character(13) :: rank = "A23456789TJQK"
  character(2) :: deck(0:51)

contains

subroutine Createdeck()
  integer :: i, j, n

  n = 0
  do i = 1, 13
    do j = 1, 4
      deck(n) = rank(i:i) // suit(j:j)
      n = n + 1
    end do
  end do

end subroutine
 
subroutine Freecelldeal(game)
  integer, intent(in) :: game
  integer(i64) :: rnum
  integer :: i, n
  character(2) :: tmp

  call Createdeck()
  rnum = msrand(game)

  do i = 51, 1, -1
    n = mod(rnum, i+1)
    tmp = deck(n)
    deck(n) = deck(i)
    deck(i) = tmp
    rnum = msrand()
  end do

  write(*, "(a, i0)") "Game #", game
  write(*, "(8(a, tr1))") deck(51:0:-1)
  write(*,*)

end subroutine  
end module Freecell

program Freecell_test
  use Freecell
  implicit none

  call Freecelldeal(1)
  call Freecelldeal(617)
   
end program
Task: Deal cards for FreeCell




Go: package main

import (
    "fmt"
    "math"
    "math/rand"
    "os"
    "strconv"
    "time"
)

const sSuits = "CDHS"
const sNums = "A23456789TJQK"
const rMax32 = math.MaxInt32

var seed = 1

func rnd() int {
    seed = (seed*214013 + 2531011) & rMax32
    return seed >> 16
}

func deal(s int) []int {
    seed = s
    t := make([]int, 52)
    for i := 0; i < 52; i++ {
        t[i] = 51 - i
    }
    for i := 0; i < 51; i++ {
        j := 51 - rnd()%(52-i)
        t[i], t[j] = t[j], t[i]
    }
    return t
}

func show(cs []int) {
    for i, c := range cs {
        fmt.Printf(" %c%c", sNums[c/4], sSuits[c%4])
        if (i+1)%8 == 0 || i+1 == len(cs) {
            fmt.Println()
        }
    }
}

func main() {
    var game int
    switch len(os.Args) {
    case 1:
        rand.Seed(time.Now().UnixNano())
        game = 1 + rand.Intn(32000)
    case 2:
        var err error
        game, err = strconv.Atoi(os.Args[1])
        if err == nil && game >= 1 && game <= 32000 {
            break
        }
        fallthrough
    default:
        fmt.Println("usage: deal [game]")
        fmt.Println("       where game is a number in the range 1 to 32000")
        return
    }
    fmt.Printf("\nGame #%d\n", game)
    show(deal(game))
}







Fortran: module Freecell
  use lcgs
  implicit none

  character(4)  :: suit = "CDHS"
  character(13) :: rank = "A23456789TJQK"
  character(2) :: deck(0:51)

contains

subroutine Createdeck()
  integer :: i, j, n

  n = 0
  do i = 1, 13
    do j = 1, 4
      deck(n) = rank(i:i) // suit(j:j)
      n = n + 1
    end do
  end do

end subroutine
 
subroutine Freecelldeal(game)
  integer, intent(in) :: game
  integer(i64) :: rnum
  integer :: i, n
  character(2) :: tmp

  call Createdeck()
  rnum = msrand(game)

  do i = 51, 1, -1
    n = mod(rnum, i+1)
    tmp = deck(n)
    deck(n) = deck(i)
    deck(i) = tmp
    rnum = msrand()
  end do

  write(*, "(a, i0)") "Game #", game
  write(*, "(8(a, tr1))") deck(51:0:-1)
  write(*,*)

end subroutine  
end module Freecell

program Freecell_test
  use Freecell
  implicit none

  call Freecelldeal(1)
  call Freecelldeal(617)
   
end program
Task: Deal cards for FreeCell





Java: import java.util.Arrays;

public class Shuffler {
	
	private int seed;
	
	private String[] deck = {
			"AC", "AD", "AH", "AS",
			"2C", "2D", "2H", "2S",
			"3C", "3D", "3H", "3S",
			"4C", "4D", "4H", "4S",
			"5C", "5D", "5H", "5S",
			"6C", "6D", "6H", "6S",
			"7C", "7D", "7H", "7S",
			"8C", "8D", "8H", "8S",
			"9C", "9D", "9H", "9S",
			"TC", "TD", "TH", "TS",
			"JC", "JD", "JH", "JS",
			"QC", "QD", "QH", "QS",
			"KC", "KD", "KH", "KS",
	};
	
	private int random() {
		seed = (214013 * seed + 2531011) & Integer.MAX_VALUE;
		return seed >> 16;
	}
	
	
	private String[] getShuffledDeck() {
		String[] deck = Arrays.copyOf(this.deck, this.deck.length);
		for(int i = deck.length - 1; i > 0; i--) {
			int r = random() % (i + 1);
			String card = deck[r];
			deck[r] = deck[i];
			deck[i] = card;
		}
		return deck;
	}
	
	
	public void dealGame(int seed) {
		this.seed = seed;
		String[] shuffledDeck = getShuffledDeck();
		for(int count = 1, i = shuffledDeck.length - 1; i >= 0; count++, i--) {
			System.out.print(shuffledDeck[i]);
			if(count % 8 == 0) {
				System.out.println();
			} else {
				System.out.print(" ");
			}
		}
		System.out.println();
	}
	
	public static void main(String[] args) {
		Shuffler s = new Shuffler();
		s.dealGame(1);
		System.out.println();
		s.dealGame(617);
	}
	
}






Fortran: module Freecell
  use lcgs
  implicit none

  character(4)  :: suit = "CDHS"
  character(13) :: rank = "A23456789TJQK"
  character(2) :: deck(0:51)

contains

subroutine Createdeck()
  integer :: i, j, n

  n = 0
  do i = 1, 13
    do j = 1, 4
      deck(n) = rank(i:i) // suit(j:j)
      n = n + 1
    end do
  end do

end subroutine
 
subroutine Freecelldeal(game)
  integer, intent(in) :: game
  integer(i64) :: rnum
  integer :: i, n
  character(2) :: tmp

  call Createdeck()
  rnum = msrand(game)

  do i = 51, 1, -1
    n = mod(rnum, i+1)
    tmp = deck(n)
    deck(n) = deck(i)
    deck(i) = tmp
    rnum = msrand()
  end do

  write(*, "(a, i0)") "Game #", game
  write(*, "(8(a, tr1))") deck(51:0:-1)
  write(*,*)

end subroutine  
end module Freecell

program Freecell_test
  use Freecell
  implicit none

  call Freecelldeal(1)
  call Freecelldeal(617)
   
end program
Task: Deal cards for FreeCell







Python: def randomGenerator(seed=1):
    max_int32 = (1 << 31) - 1
    seed = seed & max_int32

    while True:
        seed = (seed * 214013 + 2531011) & max_int32
        yield seed >> 16

def deal(seed):
    nc = 52
    cards = list(range(nc - 1, -1, -1))
    rnd = randomGenerator(seed)
    for i, r in zip(range(nc), rnd):
        j = (nc - 1) - r % (nc - i)
        cards[i], cards[j] = cards[j], cards[i]
    return cards

def show(cards):
    l = ["A23456789TJQK"[int(c/4)] + "CDHS"[c%4] for c in cards]
    for i in range(0, len(cards), 8):
        print(" ".join(l[i : i+8]))

if __name__ == '__main__':
    from sys import argv
    seed = int(argv[1]) if len(argv) == 2 else 11982
    print("Hand {}".format(seed))
    deck = deal(seed)
    show(deck)




Fortran: module Freecell
  use lcgs
  implicit none

  character(4)  :: suit = "CDHS"
  character(13) :: rank = "A23456789TJQK"
  character(2) :: deck(0:51)

contains

subroutine Createdeck()
  integer :: i, j, n

  n = 0
  do i = 1, 13
    do j = 1, 4
      deck(n) = rank(i:i) // suit(j:j)
      n = n + 1
    end do
  end do

end subroutine
 
subroutine Freecelldeal(game)
  integer, intent(in) :: game
  integer(i64) :: rnum
  integer :: i, n
  character(2) :: tmp

  call Createdeck()
  rnum = msrand(game)

  do i = 51, 1, -1
    n = mod(rnum, i+1)
    tmp = deck(n)
    deck(n) = deck(i)
    deck(i) = tmp
    rnum = msrand()
  end do

  write(*, "(a, i0)") "Game #", game
  write(*, "(8(a, tr1))") deck(51:0:-1)
  write(*,*)

end subroutine  
end module Freecell

program Freecell_test
  use Freecell
  implicit none

  call Freecelldeal(1)
  call Freecelldeal(617)
   
end program
Task: Deal cards for FreeCell






PHP: class FreeCell_Deal {

    protected $deck = array( 
        'AC', 'AD', 'AH', 'AS', '2C', '2D', '2H', '2S', '3C', '3D', '3H', '3S',
        '4C', '4D', '4H', '4S', '5C', '5D', '5H', '5S', '6C', '6D', '6H', '6S', 
        '7C', '7D', '7H', '7S', '8C', '8D', '8H', '8S', '9C', '9D', '9H', '9S',
        'TC', 'TD', 'TH', 'TS', 'JC', 'JD', 'JH', 'JS', 'QC', 'QD', 'QH', 'QS', 
        'KC', 'KD', 'KH', 'KS'
    );
    
    protected $game;        // Freecell Game Number
    protected $state;       // Current state of the LCG

    public $deal = array(); // Generated card sequence to deal

    function __construct( $game ) {

        $this->game = max( min( $game, 32000 ), 1 );

        $this->state = $this->game;

        while ( ! empty( $this->deck ) ) {

            $i = $this->lcg_rnd() % count( $this->deck );

            $this->deal[] = $this->deck[ $i ];

            $this->deck[ $i ] = end( $this->deck );

            array_pop( $this->deck );

        }

    }

    protected function lcg_rnd() {
        return ( $this->state = ( $this->state * 214013 + 2531011 ) % 2147483648 ) >> 16;
    }

    function print( $cols = 8 ) {
        echo str_pad( " Game " . $this->game . " ", $cols * 3 - 1, '=', STR_PAD_BOTH ), PHP_EOL;
        foreach ( array_chunk( $this->deal, $cols ) as $row ) {
            echo implode( " ", $row ), PHP_EOL;
        }
        echo PHP_EOL;
    }

}

$tests = array( 1, 617, 11982 );

foreach ( $tests as $game_num ) {
    $deal = new FreeCell_Deal( $game_num ); 
    $deal->print();
}





Fortran: module Freecell
  use lcgs
  implicit none

  character(4)  :: suit = "CDHS"
  character(13) :: rank = "A23456789TJQK"
  character(2) :: deck(0:51)

contains

subroutine Createdeck()
  integer :: i, j, n

  n = 0
  do i = 1, 13
    do j = 1, 4
      deck(n) = rank(i:i) // suit(j:j)
      n = n + 1
    end do
  end do

end subroutine
 
subroutine Freecelldeal(game)
  integer, intent(in) :: game
  integer(i64) :: rnum
  integer :: i, n
  character(2) :: tmp

  call Createdeck()
  rnum = msrand(game)

  do i = 51, 1, -1
    n = mod(rnum, i+1)
    tmp = deck(n)
    deck(n) = deck(i)
    deck(i) = tmp
    rnum = msrand()
  end do

  write(*, "(a, i0)") "Game #", game
  write(*, "(8(a, tr1))") deck(51:0:-1)
  write(*,*)

end subroutine  
end module Freecell

program Freecell_test
  use Freecell
  implicit none

  call Freecelldeal(1)
  call Freecelldeal(617)
   
end program
Task: Deal cards for FreeCell






PHP: class FreeCell_Deal {

    protected $deck = array( 
        'AC', 'AD', 'AH', 'AS', '2C', '2D', '2H', '2S', '3C', '3D', '3H', '3S',
        '4C', '4D', '4H', '4S', '5C', '5D', '5H', '5S', '6C', '6D', '6H', '6S', 
        '7C', '7D', '7H', '7S', '8C', '8D', '8H', '8S', '9C', '9D', '9H', '9S',
        'TC', 'TD', 'TH', 'TS', 'JC', 'JD', 'JH', 'JS', 'QC', 'QD', 'QH', 'QS', 
        'KC', 'KD', 'KH', 'KS'
    );
    
    protected $game;        // Freecell Game Number
    protected $state;       // Current state of the LCG

    public $deal = array(); // Generated card sequence to deal

    function __construct( $game ) {

        $this->game = max( min( $game, 32000 ), 1 );

        $this->state = $this->game;

        while ( ! empty( $this->deck ) ) {

            $i = $this->lcg_rnd() % count( $this->deck );

            $this->deal[] = $this->deck[ $i ];

            $this->deck[ $i ] = end( $this->deck );

            array_pop( $this->deck );

        }

    }

    protected function lcg_rnd() {
        return ( $this->state = ( $this->state * 214013 + 2531011 ) % 2147483648 ) >> 16;
    }

    function print( $cols = 8 ) {
        echo str_pad( " Game " . $this->game . " ", $cols * 3 - 1, '=', STR_PAD_BOTH ), PHP_EOL;
        foreach ( array_chunk( $this->deal, $cols ) as $row ) {
            echo implode( " ", $row ), PHP_EOL;
        }
        echo PHP_EOL;
    }

}

$tests = array( 1, 617, 11982 );

foreach ( $tests as $game_num ) {
    $deal = new FreeCell_Deal( $game_num ); 
    $deal->print();
}
Task: Deal cards for FreeCell






PHP: class FreeCell_Deal {

    protected $deck = array( 
        'AC', 'AD', 'AH', 'AS', '2C', '2D', '2H', '2S', '3C', '3D', '3H', '3S',
        '4C', '4D', '4H', '4S', '5C', '5D', '5H', '5S', '6C', '6D', '6H', '6S', 
        '7C', '7D', '7H', '7S', '8C', '8D', '8H', '8S', '9C', '9D', '9H', '9S',
        'TC', 'TD', 'TH', 'TS', 'JC', 'JD', 'JH', 'JS', 'QC', 'QD', 'QH', 'QS', 
        'KC', 'KD', 'KH', 'KS'
    );
    
    protected $game;        // Freecell Game Number
    protected $state;       // Current state of the LCG

    public $deal = array(); // Generated card sequence to deal

    function __construct( $game ) {

        $this->game = max( min( $game, 32000 ), 1 );

        $this->state = $this->game;

        while ( ! empty( $this->deck ) ) {

            $i = $this->lcg_rnd() % count( $this->deck );

            $this->deal[] = $this->deck[ $i ];

            $this->deck[ $i ] = end( $this->deck );

            array_pop( $this->deck );

        }

    }

    protected function lcg_rnd() {
        return ( $this->state = ( $this->state * 214013 + 2531011 ) % 2147483648 ) >> 16;
    }

    function print( $cols = 8 ) {
        echo str_pad( " Game " . $this->game . " ", $cols * 3 - 1, '=', STR_PAD_BOTH ), PHP_EOL;
        foreach ( array_chunk( $this->deal, $cols ) as $row ) {
            echo implode( " ", $row ), PHP_EOL;
        }
        echo PHP_EOL;
    }

}

$tests = array( 1, 617, 11982 );

foreach ( $tests as $game_num ) {
    $deal = new FreeCell_Deal( $game_num ); 
    $deal->print();
}






Ada: with Ada.Text_IO; use Ada.Text_IO;
procedure FreeCell is
   type State is mod 2**31;
   type Deck is array (0..51) of String(1..2);

   package Random is
      procedure Init(Seed: State);
      function Rand return State;
   end Random;
   package body Random is
      S : State := State'First;
      procedure Init(Seed: State) is begin S := Seed; end Init;
      function Rand return State is begin
         S := S * 214013 + 2531011;  return S / 2**16;
      end Rand;
   end Random;

   procedure Deal (num : State) is
      thedeck : Deck;  pick : State;
      Chars : constant String := "A23456789TJQKCDHS";
   begin
      for i in thedeck'Range loop
         thedeck(i):= Chars(i/4+1) & Chars(i mod 4 + 14);
      end loop;
      Random.Init(num);
      for i in 0..51 loop
         pick := Random.Rand mod State(52-i);
         Put(thedeck(Natural(pick))&' ');
         if (i+1) mod 8 = 0 then New_Line; end if;
         thedeck(Natural(pick)) := thedeck(51-i);
      end loop; New_Line;
   end Deal;

begin
   Deal(1);
   New_Line;
   Deal(617);
end FreeCell;
Task: Handle a signal



C#: using System; 
class Program
{
    static DateTime start;
    static void Main(string[] args)
    {
        start = DateTime.Now;
        
        Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
        int counter = 0;
        while (true)
        {
            Console.WriteLine(++counter);
            System.Threading.Thread.Sleep(500);
        }
    }
    static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
    {
        var end = DateTime.Now;
        Console.WriteLine("This program ran for {0:000.000} seconds.", (end - start).TotalMilliseconds / 1000);
        Environment.Exit(0);
    }
}









Ada: with Ada.Interrupts; use Ada.Interrupts;
with Ada.Interrupts.Names; use Ada.Interrupts.Names;

package Sigint_Handler is
      protected Handler is
      entry Wait;
      procedure Handle;
      pragma Interrupt_Handler(Handle);
      pragma Attach_Handler(Handle, Sigint);
      private
      Call_Count : Natural := 0;
   end Handler;

end Sigint_Handler;
Task: Handle a signal

C: #include <stdio.h>
#include <stdlib.h>	
#include <signal.h>
#include <time.h>	
#include <unistd.h>	

volatile sig_atomic_t gotint = 0;

void handleSigint() {
    
    gotint = 1;
}
 
int main() {
    clock_t startTime = clock();
    signal(SIGINT, handleSigint);
    int i=0;
    for (;;) {
        if (gotint)
            break;
        usleep(500000);
        if (gotint)
            break;
	printf("%d\n", ++i);
    }
    clock_t endTime = clock();
    double td = (endTime - startTime) / (double)CLOCKS_PER_SEC;
    printf("Program has run for %5.3f seconds\n", td);
    return 0;
}











Ada: with Ada.Interrupts; use Ada.Interrupts;
with Ada.Interrupts.Names; use Ada.Interrupts.Names;

package Sigint_Handler is
      protected Handler is
      entry Wait;
      procedure Handle;
      pragma Interrupt_Handler(Handle);
      pragma Attach_Handler(Handle, Sigint);
      private
      Call_Count : Natural := 0;
   end Handler;

end Sigint_Handler;
Task: Handle a signal


C++: #include <chrono>
#include <csignal>
#include <ctime>
#include <iostream>
#include <thread>

volatile sig_atomic_t gotint = 0;

void handler(int signum) {
	
	gotint = 1;
}

int main() {
	using namespace std;

	signal(SIGINT, handler);

	int i = 0;
	clock_t startTime = clock();
	while (true) {
		if (gotint) break;
		std::this_thread::sleep_for(std::chrono::milliseconds(500));
		if (gotint) break;
		cout << ++i << endl;
	}
	clock_t endTime = clock();

	double dt = (endTime - startTime) / (double)CLOCKS_PER_SEC;
	cout << "Program has run for " << dt << " seconds" << endl;

	return 0;
}










Ada: with Ada.Interrupts; use Ada.Interrupts;
with Ada.Interrupts.Names; use Ada.Interrupts.Names;

package Sigint_Handler is
      protected Handler is
      entry Wait;
      procedure Handle;
      pragma Interrupt_Handler(Handle);
      pragma Attach_Handler(Handle, Sigint);
      private
      Call_Count : Natural := 0;
   end Handler;

end Sigint_Handler;
Task: Handle a signal




Go: package main

import (
    "fmt"
    "os"
    "os/signal"
    "time"
)

func main() {
    start := time.Now()
    k := time.Tick(time.Second / 2)
    sc := make(chan os.Signal, 1)
    signal.Notify(sc, os.Interrupt)
    for n := 1; ; {
        
        
        select {
        case <-k:
            fmt.Println(n)
            n++
        case <-sc:
            fmt.Printf("Ran for %f seconds.\n",
                time.Now().Sub(start).Seconds())
            return
        }
    }
}








Ada: with Ada.Interrupts; use Ada.Interrupts;
with Ada.Interrupts.Names; use Ada.Interrupts.Names;

package Sigint_Handler is
      protected Handler is
      entry Wait;
      procedure Handle;
      pragma Interrupt_Handler(Handle);
      pragma Attach_Handler(Handle, Sigint);
      private
      Call_Count : Natural := 0;
   end Handler;

end Sigint_Handler;
Task: Handle a signal





Java: import sun.misc.Signal;
import sun.misc.SignalHandler;

public class ExampleSignalHandler {
    public static void main(String... args) throws InterruptedException {
        final long start = System.nanoTime();
        Signal.handle(new Signal("INT"), new SignalHandler() {
            public void handle(Signal sig) {
                System.out.format("\nProgram execution took %f seconds\n", (System.nanoTime() - start) / 1e9f);
                System.exit(0);
            }
        });
        int counter = 0;
        while(true) {
            System.out.println(counter++);
            Thread.sleep(500);
        }
    }
}







Ada: with Ada.Interrupts; use Ada.Interrupts;
with Ada.Interrupts.Names; use Ada.Interrupts.Names;

package Sigint_Handler is
      protected Handler is
      entry Wait;
      procedure Handle;
      pragma Interrupt_Handler(Handle);
      pragma Attach_Handler(Handle, Sigint);
      private
      Call_Count : Natural := 0;
   end Handler;

end Sigint_Handler;
Task: Handle a signal







Python: import time

def counter():
    n = 0
    t1 = time.time()
    while True:
        try:
            time.sleep(0.5)
            n += 1
            print n
        except KeyboardInterrupt, e:
            print 'Program has run for %5.3f seconds.' % (time.time() - t1)
            break

counter()





Ada: with Ada.Interrupts; use Ada.Interrupts;
with Ada.Interrupts.Names; use Ada.Interrupts.Names;

package Sigint_Handler is
      protected Handler is
      entry Wait;
      procedure Handle;
      pragma Interrupt_Handler(Handle);
      pragma Attach_Handler(Handle, Sigint);
      private
      Call_Count : Natural := 0;
   end Handler;

end Sigint_Handler;