Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7439697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:44:35+00:00 2026-05-29T10:44:35+00:00

Can we force quit a C program’s execution after a fraction of seconds (range

  • 0

Can we force quit a C program’s execution after a fraction of seconds (range of milliseconds) or by the size of memory it uses? This way, I want to restrict printing content in an infinite loop and restrict buffer overruns.

I am using cygwin GCC4.3 compiler and want to implement it as a tool in PHP, which takes in the C source as input and displays the corresponding output.

PS –
1. I say milliseconds because my C problems will only involve very simple arithmetic/computations.
2. To restrict the execution time, set_time_limit()in php would restrict the entire PHP script’s execution time and not the time allotted to exec( )

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:44:36+00:00Added an answer on May 29, 2026 at 10:44 am

    You should be able to use the alarm() function. While it is found in unistd.h it is a POSIX.1-2001 function and should be available under cygwin. If SIGALRM is not handled it kills the process.

    Try it:

    #include <stdio.h>
    #include <unistd.h>
    
    int main() {
    
         alarm(5); /* raise alarm after 5 seconds */
         
         while(1) {
                 printf("Running forever\n");
         }
         return 0; /* never reached */
    }
    

    update

    As jonathan points out alarm(2) only works in seconds so you can use setitimer instead (also POSIX compliant)

    #include <stdio.h>
    #include <sys/time.h>
    
    int main() {
    
         /* --CUT HERE-- */
         struct itimerval timer;
         timer.it_value.tv_sec = 0;
         timer.it_value.tv_usec = 5000; /* 5 milliseconds */
         timer.it_interval.tv_sec = 0;
         timer.it_interval.tv_usec = 0; 
    
         setitimer(ITIMER_REAL, &timer, NULL);
         /* --END CUT-- */
         while(1) {
                 printf("Running forever\n");
         }
         return 0; /* never reached */
    }
    

    if the above works on your system,
    copy code from –CUT HERE– to –END CUT– and paste it into your main;

    update 2

    to limit memory try using setrlimit:

    see Set stack size with setrlimit() and provoke a stack overflow/segfault

    for an example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I force validation when user clicks button? One would think this should
How can I disable force quit from within my java application? Either that, or
On Visual studio I can force use of 32-bit time_t by declaring _USE_32BIT_TIME_T .
I was wondering how i can force a user who has requested a page
Does anyone know how I can force CanExecute to get called on a custom
Using Castle ActiveRecord / NHibernate: Is there a way you can force an ICriterion
Okay, I've got my normal app which is in portrait mode. I can force
can I force the DBML designer to use my custom types instead of the
Can I force the gcc to use a null canary or terminator canary, when
How can I force the input's onchange script to run before the RangeValidator's script?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.