WeixiangYan/CodeTransOcean huggingface dataset:
Task: Handle a signal








VB: Module Module1
    Dim startTime As Date

    Sub Main()
        startTime = Date.Now
        
        AddHandler Console.CancelKeyPress, AddressOf Console_CancelKeyPress

        Dim counter = 0
        While True
            counter += 1
            Console.WriteLine(counter)
            Threading.Thread.Sleep(500)
        End While
    End Sub

    Sub Console_CancelKeyPress(sender As Object, e As ConsoleCancelEventArgs)
        Dim stopTime = Date.Now
        Console.WriteLine("This program ran for {0:000.000} seconds", (stopTime - startTime).TotalMilliseconds / 1000)
        Environment.Exit(0)
    End Sub

End Module




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;
}
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);
    }
}
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;
}
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);
        }
    }
}
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()
Task: Handle a signal








VB: Module Module1
    Dim startTime As Date

    Sub Main()
        startTime = Date.Now
        
        AddHandler Console.CancelKeyPress, AddressOf Console_CancelKeyPress

        Dim counter = 0
        While True
            counter += 1
            Console.WriteLine(counter)
            Threading.Thread.Sleep(500)
        End While
    End Sub

    Sub Console_CancelKeyPress(sender As Object, e As ConsoleCancelEventArgs)
        Dim stopTime = Date.Now
        Console.WriteLine("This program ran for {0:000.000} seconds", (stopTime - startTime).TotalMilliseconds / 1000)
        Environment.Exit(0)
    End Sub

End Module
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
        }
    }
}
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);
    }
}








Fortran: program signal_handling
  use, intrinsic :: iso_fortran_env, only: atomic_logical_kind
  implicit none

  interface
    integer(C_INT) function usleep(microseconds) bind(c)
      use, intrinsic :: iso_c_binding, only: C_INT, C_INT32_T
      integer(C_INT32_T), value :: microseconds
    end function usleep
  end interface

  integer, parameter :: half_second = 500000
  integer, parameter :: sigint = 2
  integer, parameter :: sigquit = 3

  logical(atomic_logical_kind) :: interrupt_received[*]
  integer :: half_seconds
  logical :: interrupt_received_ref

  interrupt_received = .false.
  half_seconds = 0

  
  call signal(sigint, signal_handler)
  call signal(sigquit, signal_handler)

  
  do
    if (usleep(half_second) == -1) &
      print *, "Call to usleep interrupted."

    call atomic_ref(interrupt_received_ref, interrupt_received)
    if (interrupt_received_ref) then
      print "(A,I0,A)", "Program ran for ", half_seconds / 2, " second(s)."
      stop
    end if

    half_seconds = half_seconds + 1
    print "(I0)", half_seconds
  end do

contains

  subroutine signal_handler(sig_num)
    use, intrinsic :: iso_c_binding, only: C_INT
    integer(C_INT), value, intent(in) :: sig_num
    
    

    select case (sig_num)
      case (sigint)
        print *, "Received SIGINT."
      case (sigquit)
        print *, "Received SIGQUIT."
    end select

    call atomic_define(interrupt_received, .true.)
  end subroutine signal_handler

end program signal_handling
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;
}









Fortran: program signal_handling
  use, intrinsic :: iso_fortran_env, only: atomic_logical_kind
  implicit none

  interface
    integer(C_INT) function usleep(microseconds) bind(c)
      use, intrinsic :: iso_c_binding, only: C_INT, C_INT32_T
      integer(C_INT32_T), value :: microseconds
    end function usleep
  end interface

  integer, parameter :: half_second = 500000
  integer, parameter :: sigint = 2
  integer, parameter :: sigquit = 3

  logical(atomic_logical_kind) :: interrupt_received[*]
  integer :: half_seconds
  logical :: interrupt_received_ref

  interrupt_received = .false.
  half_seconds = 0

  
  call signal(sigint, signal_handler)
  call signal(sigquit, signal_handler)

  
  do
    if (usleep(half_second) == -1) &
      print *, "Call to usleep interrupted."

    call atomic_ref(interrupt_received_ref, interrupt_received)
    if (interrupt_received_ref) then
      print "(A,I0,A)", "Program ran for ", half_seconds / 2, " second(s)."
      stop
    end if

    half_seconds = half_seconds + 1
    print "(I0)", half_seconds
  end do

contains

  subroutine signal_handler(sig_num)
    use, intrinsic :: iso_c_binding, only: C_INT
    integer(C_INT), value, intent(in) :: sig_num
    
    

    select case (sig_num)
      case (sigint)
        print *, "Received SIGINT."
      case (sigquit)
        print *, "Received SIGQUIT."
    end select

    call atomic_define(interrupt_received, .true.)
  end subroutine signal_handler

end program signal_handling
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;
}










Fortran: program signal_handling
  use, intrinsic :: iso_fortran_env, only: atomic_logical_kind
  implicit none

  interface
    integer(C_INT) function usleep(microseconds) bind(c)
      use, intrinsic :: iso_c_binding, only: C_INT, C_INT32_T
      integer(C_INT32_T), value :: microseconds
    end function usleep
  end interface

  integer, parameter :: half_second = 500000
  integer, parameter :: sigint = 2
  integer, parameter :: sigquit = 3

  logical(atomic_logical_kind) :: interrupt_received[*]
  integer :: half_seconds
  logical :: interrupt_received_ref

  interrupt_received = .false.
  half_seconds = 0

  
  call signal(sigint, signal_handler)
  call signal(sigquit, signal_handler)

  
  do
    if (usleep(half_second) == -1) &
      print *, "Call to usleep interrupted."

    call atomic_ref(interrupt_received_ref, interrupt_received)
    if (interrupt_received_ref) then
      print "(A,I0,A)", "Program ran for ", half_seconds / 2, " second(s)."
      stop
    end if

    half_seconds = half_seconds + 1
    print "(I0)", half_seconds
  end do

contains

  subroutine signal_handler(sig_num)
    use, intrinsic :: iso_c_binding, only: C_INT
    integer(C_INT), value, intent(in) :: sig_num
    
    

    select case (sig_num)
      case (sigint)
        print *, "Received SIGINT."
      case (sigquit)
        print *, "Received SIGQUIT."
    end select

    call atomic_define(interrupt_received, .true.)
  end subroutine signal_handler

end program signal_handling
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);
        }
    }
}






Fortran: program signal_handling
  use, intrinsic :: iso_fortran_env, only: atomic_logical_kind
  implicit none

  interface
    integer(C_INT) function usleep(microseconds) bind(c)
      use, intrinsic :: iso_c_binding, only: C_INT, C_INT32_T
      integer(C_INT32_T), value :: microseconds
    end function usleep
  end interface

  integer, parameter :: half_second = 500000
  integer, parameter :: sigint = 2
  integer, parameter :: sigquit = 3

  logical(atomic_logical_kind) :: interrupt_received[*]
  integer :: half_seconds
  logical :: interrupt_received_ref

  interrupt_received = .false.
  half_seconds = 0

  
  call signal(sigint, signal_handler)
  call signal(sigquit, signal_handler)

  
  do
    if (usleep(half_second) == -1) &
      print *, "Call to usleep interrupted."

    call atomic_ref(interrupt_received_ref, interrupt_received)
    if (interrupt_received_ref) then
      print "(A,I0,A)", "Program ran for ", half_seconds / 2, " second(s)."
      stop
    end if

    half_seconds = half_seconds + 1
    print "(I0)", half_seconds
  end do

contains

  subroutine signal_handler(sig_num)
    use, intrinsic :: iso_c_binding, only: C_INT
    integer(C_INT), value, intent(in) :: sig_num
    
    

    select case (sig_num)
      case (sigint)
        print *, "Received SIGINT."
      case (sigquit)
        print *, "Received SIGQUIT."
    end select

    call atomic_define(interrupt_received, .true.)
  end subroutine signal_handler

end program signal_handling
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()




Fortran: program signal_handling
  use, intrinsic :: iso_fortran_env, only: atomic_logical_kind
  implicit none

  interface
    integer(C_INT) function usleep(microseconds) bind(c)
      use, intrinsic :: iso_c_binding, only: C_INT, C_INT32_T
      integer(C_INT32_T), value :: microseconds
    end function usleep
  end interface

  integer, parameter :: half_second = 500000
  integer, parameter :: sigint = 2
  integer, parameter :: sigquit = 3

  logical(atomic_logical_kind) :: interrupt_received[*]
  integer :: half_seconds
  logical :: interrupt_received_ref

  interrupt_received = .false.
  half_seconds = 0

  
  call signal(sigint, signal_handler)
  call signal(sigquit, signal_handler)

  
  do
    if (usleep(half_second) == -1) &
      print *, "Call to usleep interrupted."

    call atomic_ref(interrupt_received_ref, interrupt_received)
    if (interrupt_received_ref) then
      print "(A,I0,A)", "Program ran for ", half_seconds / 2, " second(s)."
      stop
    end if

    half_seconds = half_seconds + 1
    print "(I0)", half_seconds
  end do

contains

  subroutine signal_handler(sig_num)
    use, intrinsic :: iso_c_binding, only: C_INT
    integer(C_INT), value, intent(in) :: sig_num
    
    

    select case (sig_num)
      case (sigint)
        print *, "Received SIGINT."
      case (sigquit)
        print *, "Received SIGQUIT."
    end select

    call atomic_define(interrupt_received, .true.)
  end subroutine signal_handler

end program signal_handling
Task: Handle a signal








VB: Module Module1
    Dim startTime As Date

    Sub Main()
        startTime = Date.Now
        
        AddHandler Console.CancelKeyPress, AddressOf Console_CancelKeyPress

        Dim counter = 0
        While True
            counter += 1
            Console.WriteLine(counter)
            Threading.Thread.Sleep(500)
        End While
    End Sub

    Sub Console_CancelKeyPress(sender As Object, e As ConsoleCancelEventArgs)
        Dim stopTime = Date.Now
        Console.WriteLine("This program ran for {0:000.000} seconds", (stopTime - startTime).TotalMilliseconds / 1000)
        Environment.Exit(0)
    End Sub

End Module



Fortran: program signal_handling
  use, intrinsic :: iso_fortran_env, only: atomic_logical_kind
  implicit none

  interface
    integer(C_INT) function usleep(microseconds) bind(c)
      use, intrinsic :: iso_c_binding, only: C_INT, C_INT32_T
      integer(C_INT32_T), value :: microseconds
    end function usleep
  end interface

  integer, parameter :: half_second = 500000
  integer, parameter :: sigint = 2
  integer, parameter :: sigquit = 3

  logical(atomic_logical_kind) :: interrupt_received[*]
  integer :: half_seconds
  logical :: interrupt_received_ref

  interrupt_received = .false.
  half_seconds = 0

  
  call signal(sigint, signal_handler)
  call signal(sigquit, signal_handler)

  
  do
    if (usleep(half_second) == -1) &
      print *, "Call to usleep interrupted."

    call atomic_ref(interrupt_received_ref, interrupt_received)
    if (interrupt_received_ref) then
      print "(A,I0,A)", "Program ran for ", half_seconds / 2, " second(s)."
      stop
    end if

    half_seconds = half_seconds + 1
    print "(I0)", half_seconds
  end do

contains

  subroutine signal_handler(sig_num)
    use, intrinsic :: iso_c_binding, only: C_INT
    integer(C_INT), value, intent(in) :: sig_num
    
    

    select case (sig_num)
      case (sigint)
        print *, "Received SIGINT."
      case (sigquit)
        print *, "Received SIGQUIT."
    end select

    call atomic_define(interrupt_received, .true.)
  end subroutine signal_handler

end program signal_handling
Task: Handle a signal






PHP: <?php
declare(ticks = 1);

$start = microtime(true);

function mySigHandler() {
  global $start;
  $elapsed = microtime(true) - $start;
  echo "Ran for $elapsed seconds.\n";
  exit();
}

pcntl_signal(SIGINT, 'mySigHandler');

for ($n = 0; ; usleep(500000)) // 0.5 seconds
   echo ++$n, "\n";
?>





Fortran: program signal_handling
  use, intrinsic :: iso_fortran_env, only: atomic_logical_kind
  implicit none

  interface
    integer(C_INT) function usleep(microseconds) bind(c)
      use, intrinsic :: iso_c_binding, only: C_INT, C_INT32_T
      integer(C_INT32_T), value :: microseconds
    end function usleep
  end interface

  integer, parameter :: half_second = 500000
  integer, parameter :: sigint = 2
  integer, parameter :: sigquit = 3

  logical(atomic_logical_kind) :: interrupt_received[*]
  integer :: half_seconds
  logical :: interrupt_received_ref

  interrupt_received = .false.
  half_seconds = 0

  
  call signal(sigint, signal_handler)
  call signal(sigquit, signal_handler)

  
  do
    if (usleep(half_second) == -1) &
      print *, "Call to usleep interrupted."

    call atomic_ref(interrupt_received_ref, interrupt_received)
    if (interrupt_received_ref) then
      print "(A,I0,A)", "Program ran for ", half_seconds / 2, " second(s)."
      stop
    end if

    half_seconds = half_seconds + 1
    print "(I0)", half_seconds
  end do

contains

  subroutine signal_handler(sig_num)
    use, intrinsic :: iso_c_binding, only: C_INT
    integer(C_INT), value, intent(in) :: sig_num
    
    

    select case (sig_num)
      case (sigint)
        print *, "Received SIGINT."
      case (sigquit)
        print *, "Received SIGQUIT."
    end select

    call atomic_define(interrupt_received, .true.)
  end subroutine signal_handler

end program signal_handling
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;
}









COBOL:        identification division.
       program-id. signals.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.
          88 signalled value 1.
       01 half-seconds usage binary-long.
       01 start-time   usage binary-c-long.
       01 end-time     usage binary-c-long.
       01 handler      usage program-pointer.
       01 SIGINT       constant as 2.

       procedure division.
       call "gettimeofday" using start-time null
       set handler to entry "handle-sigint"
       call "signal" using by value SIGINT by value handler

       perform until exit
           if signalled then exit perform end-if
           call "CBL_OC_NANOSLEEP" using 500000000
           if signalled then exit perform end-if
           add 1 to half-seconds
           display half-seconds
       end-perform

       call "gettimeofday" using end-time null
       subtract start-time from end-time
       display "Program ran for " end-time " seconds"
       goback.
       end program signals.

       identification division.
       program-id. handle-sigint.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.

       linkage section.
       01 the-signal   usage binary-long.

       procedure division using by value the-signal returning omitted.
       move 1 to signal-flag
       goback.
       end program handle-sigint.
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);
    }
}







COBOL:        identification division.
       program-id. signals.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.
          88 signalled value 1.
       01 half-seconds usage binary-long.
       01 start-time   usage binary-c-long.
       01 end-time     usage binary-c-long.
       01 handler      usage program-pointer.
       01 SIGINT       constant as 2.

       procedure division.
       call "gettimeofday" using start-time null
       set handler to entry "handle-sigint"
       call "signal" using by value SIGINT by value handler

       perform until exit
           if signalled then exit perform end-if
           call "CBL_OC_NANOSLEEP" using 500000000
           if signalled then exit perform end-if
           add 1 to half-seconds
           display half-seconds
       end-perform

       call "gettimeofday" using end-time null
       subtract start-time from end-time
       display "Program ran for " end-time " seconds"
       goback.
       end program signals.

       identification division.
       program-id. handle-sigint.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.

       linkage section.
       01 the-signal   usage binary-long.

       procedure division using by value the-signal returning omitted.
       move 1 to signal-flag
       goback.
       end program handle-sigint.