Conditional move experiments
example2.c source
Project updated · 12 May 2010
/*
* example3.c
* - Example program for the "Emulating the CMOV instruction" article.
* - In this example the parent clears SIGTERM generated by the child.
*
* Michal Ludvig <michal@logix.cz> (c) 2003
* Homepage: /devel/cmov-article
*
* This code is public domain. Use it as you want to,
* but don't blame me if something doesn't work as you
* expect.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ptrace.h>
void child ()
{
ptrace (PTRACE_TRACEME, 0, NULL, NULL);
kill (getpid (), SIGTERM);
exit (5);
}
void parent ()
{
int status;
pid_t pid;
while ((pid = wait4 (-1, &status, WUNTRACED, NULL)) >= 0)
{
printf ("PID[%lu]: ", pid);
if (WIFEXITED (status))
printf ("WEXITSTATUS: %d\n", WEXITSTATUS (status));
else if (WIFSIGNALED (status))
printf ("WTERMSIG: %d (%s)\n", WTERMSIG (status),
strsignal (WTERMSIG (status)));
else if (WIFSTOPPED (status))
{
int signal = WSTOPSIG (status);
printf ("WSTOPSIG: %d (%s)\n", signal, strsignal (signal));
if (signal == SIGTERM)
signal = 0;
ptrace (PTRACE_CONT, pid, NULL, signal);
}
}
}
int
main ()
{
if (fork ())
parent ();
else
child ();
exit (0);
}