Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Open Source

Checkpointing, CHPOX, and Linux


Testing Your CHPOX Installation

Once you have finished, you can test your first CHPOX installation using Listing One (chpoxtest.c).

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <errno.h>

static volatile sig_atomic_t print_count = 0;

void sighandler(int signo)
{
     
     printf("Executing signal handler\n");
     sleep(10);
     print_count = 1;
}
void reader_process(int fd, const char *fname, int delay)
{
     FILE *input, *pfile;
     char buf[256];
     
     printf("reader enter\n");
     pfile = fdopen(fd, "w");
     input = fopen(fname, "r");
     if (!input) {
          perror(fname);
          exit(EXIT_FAILURE);
     }
     while (fgets(buf, sizeof(buf), input) != NULL) {
          printf("reader\n");
          sleep(delay);
          fputs(buf, pfile);
          fflush(pfile);
     }
     printf("reader exit\n");
}
void writer_process(int fd, const char *fname)
{
     FILE *output, *sfile;
     char buf[256];

     printf("writer enter\n");
     sfile = fdopen(fd, "r");
     output = fopen(fname, "w");
     if (!output) {
          perror(fname);
          exit(EXIT_FAILURE);
     }
     while (fgets(buf, sizeof(buf), sfile) != NULL) {
          printf("writer\n");
          fputs(buf, output);
          fflush(output);
     }
     printf("writer exit\n");
}
int main(int argc, char **argv)
{
     /* Pipe discriptors */
     int pfd[2];
     /* Socketpair descriptors */
     int sfd[2];
     int delay;
     pid_t pid;
     FILE *input, *output;
     char buf[256];
     int val;
     struct sigaction sa;
     int count = 0;
     sigset_t mask;
     
     if (argc != 4) {
          fprintf(stderr, "Usage: %s <delay> <input> <output>\n", argv[0]);
          exit(EXIT_FAILURE);
     }
     delay = atoi(argv[1]);

     /* Let's create communication channels ...*/
     if (pipe(pfd) == -1) {
          perror("pipe");
          exit(EXIT_FAILURE);
     }
     if (socketpair(PF_UNIX, SOCK_STREAM, 0, sfd) == -1) {
          perror("socketpair");
          exit(EXIT_FAILURE);
     }
     /* ... and fork children */
     pid = fork();
     switch (pid) {
          case -1:
               perror("fork 1");
               exit(EXIT_FAILURE);
          case 0:
               close(pfd[0]);
               close(sfd[0]);
               close(sfd[1]);
               reader_process(pfd[1], argv[2], delay);
               exit(EXIT_SUCCESS);
     }
     close(pfd[1]);
     pid = fork();
     switch (pid) {
          case -1:
               perror("fork 2");
               exit(EXIT_FAILURE);
          case 0:
               close(pfd[1]);
               close(sfd[1]);
               writer_process(sfd[0], argv[3]);
               exit(EXIT_SUCCESS);
     }
     close(sfd[0]);
     
     /* And let's do our hard work */
     input = fdopen(pfd[0], "r");
     output = fdopen(sfd[1], "w");
     
     memset(&sa, 0, sizeof(sa));
     sa.sa_handler = sighandler;
     sigaction(SIGUSR1, &sa, NULL);
     sigemptyset(&mask);
     sigaddset(&mask, SIGUSR1);
     
     for (;;) {
          if (print_count) {
               printf("numbers processed: %d\n", count);
               print_count = 0;
          }
          sigprocmask(SIG_BLOCK, &mask, NULL);
          if (fgets(buf, sizeof(buf), input) == NULL)
               break;
          printf("main\n");
          val = atoi(buf);
          fprintf(output, "%d\n", val * val);
          fflush(output);
          sigprocmask(SIG_UNBLOCK, &mask, NULL);
          count++;
     }
     fclose(input);
     fclose(output);
     wait(NULL); wait(NULL);
     exit(EXIT_SUCCESS);
}
Listing One: chpoxtest.c

Listing Two is the Makefile for compiling chpoxtest.c.

The program forks two new children, creates a pipe and a UNIX socket for IPC between forked children, and sets a signal handler for the parent. This enables the parent to print the current progress to standard output every time a SIGUSR1 signal is received.

 CFLAGS = -Wall -O2
 CC=gcc

 chpoxtest: chpoxtest.o

 clean:
     rm -f -- chpoxtest *.o

 .PHONY: all clean

Listing Two: Makefile.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.