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 6138111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:48:35+00:00 2026-05-23T17:48:35+00:00

I’m trying to implement a checkpoint function to checkpoint a process. I do it

  • 0

I’m trying to implement a checkpoint function to checkpoint a process. I do it by forking a child process. However I need to pause the child process at start. Later on, we can restart from a checkpoint by unpausing the child process and making the parent kill itself.

Here is the code I’ve written for checkpoint and restart_from_checkpoint along with an example of how to call them.

#include <stdio.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <sys/wait.h>

pid_t checkpoint();
void restart_from_checkpoint( pid_t pid );

int main( int argc, char *argv[] )
{
  int i;
  pid_t child_pid;
  pid_t parent_pid = getpid();

  for( i = 0; i < 10; i++ )
  {
    if ( i == 4 )
    {
      printf( "%6s: Checkpointing!\n", (getpid() == parent_pid)? "parent":"child" );
      child_pid = checkpoint();
    }

    if ( i == 7 )
    {
      printf( "%6s: Restarting!\n", (getpid() == parent_pid)? "parent":"child" );
      restart_from_checkpoint( child_pid );
    }

    printf( "%6s: i = %d\n", (getpid() == parent_pid)? "parent":"child", i );
  }

  return 0;
}

pid_t checkpoint()
{
    pid_t pid;
    int wait_val;

    switch (pid=fork()) 
    {
    case -1: 
        perror("fork"); 
        break;
    case 0:         // child process starts
        ptrace(PTRACE_TRACEME,0,0,0);
        raise( SIGTRAP ); // Note that this is the solution to first part
                              // of the question, which I added after
                              // asking this question.
        break;  // child process ends
    default:        // parent process starts
        wait(&wait_val);
        return pid;
    }
}

void restart_from_checkpoint( pid_t pid )
{
    ptrace(PTRACE_CONT, pid, NULL, NULL);
    wait(NULL); // I'm just waiting here, but actually 
                //  I need to kill the calling process.
}

I don’t know how to stop the child process after calling ptrace(PTRACE_TRACEME,0,0,0). Secondly, I don’t know how to kill the parent process while letting the child continue at restart_from_checkpoint.

The best thing would be the possibility of using a parameter with ptrace that stops the forked process at the beginning and later started with PTRACE_CONT. Unfortunately, PTRACE_TRACEME only stops at an exec function call.

  • 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-23T17:48:35+00:00Added an answer on May 23, 2026 at 5:48 pm

    OK, I’ve found the solution. Its working perfectly now. Here is the code for those of you who are interested.

    #include <stdio.h>
    #include <unistd.h>
    #include <sys/ptrace.h>
    #include <sys/wait.h>
    #include <stdlib.h>
    
    pid_t checkpoint();
    void restart_from_checkpoint( pid_t pid );
    
    int main( int argc, char *argv[] )
    {
      int i;
      pid_t child_pid;
      pid_t parent_pid = getpid();
    
      for( i = 0; i < 10; i++ )
      {
        if ( i == 4 )
        {
          printf( "%6s: Checkpointing!\n", (getpid() == parent_pid)? "parent":"child" );
          child_pid = checkpoint();
        }
    
        if ( i == 7 && ( getpid() == parent_pid ) )
        {
          printf( "%6s: Restarting!\n", (getpid() == parent_pid)? "parent":"child" );
          restart_from_checkpoint( child_pid );
        }
    
        printf( "%6s: i = %d\n", (getpid() == parent_pid)? "parent":"child", i );
      }
    
      return 0;
    }
    
    pid_t checkpoint()
    {
        pid_t pid;
        int wait_val;
    
        switch (pid=fork()) 
        {
        case -1: 
            perror("fork"); 
            break;
        case 0:         // child process starts
            ptrace(PTRACE_TRACEME,0,0,0);
            raise(SIGTRAP);
            break;  // child process ends
        default:        // parent process starts
            wait(&wait_val);
            return pid;
        }
    }
    
    void restart_from_checkpoint( pid_t pid )
    {
        ptrace(PTRACE_CONT, pid, NULL, NULL);
        ptrace(PTRACE_DETACH, pid, NULL, NULL);
        exit( 1 );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.