<commit_before>#define _POSIX_SOURCE

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

using std::cout;
using std::endl;

int main() {
  
  cout.setf(std::ios_base::unitbuf); // turn off buffering for cout
  pid_t pid, wpid;                   // various PIDs
  int pstatus;                       // process pstatus

  cout << "before fork" << endl;

  if ((pid = fork()) < 0) {          // error 
    perror("FORK ERROR");
  } else if (pid == 0) {             // in child
    for (int i = 0; i < 100; ++i) {
      cout << "i = "        << i         << ", "
	   << "my pid = "   << getpid()  << ", "
	   << "fork = "     << pid       << ", "
	   << "ppid = "     << getppid() << endl;
    } // for
    exit(42);
  } else {                           // in parent
    if ((wpid = waitpid(pid, &pstatus, WNOHANG)) == -1) {
      perror("waitpid");
    } else if (wpid == 0) {
      cout << "no pstatus changes detected" << endl;
    } else if (WIFEXITED(pstatus)) {
      cout << "child with pid = "                << wpid                 << " "
	   << "exited normally with pstatus = "  << WEXITSTATUS(pstatus) << endl;
    } else if (WIFSIGNALED(pstatus)) {
      cout << "child with pid = "                << wpid                 << " "
	   << "exited abnormally from signal = " << WTERMSIG(pstatus)    << endl;
    } // if
  } // if
  return EXIT_SUCCESS;
} // main

<commit_msg>updated the waitpid WNOHANG example<commit_after>#define _POSIX_SOURCE

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

using std::cout;
using std::endl;

int main() {
  
  cout.setf(std::ios_base::unitbuf); // turn off buffering for cout
  pid_t pid, wpid;                   // various PIDs
  int pstatus;                       // process pstatus

  cout << "before fork" << endl;

  if ((pid = fork()) < 0) {          // error 
    perror("FORK ERROR");
  } else if (pid == 0) {             // in child
    for (int i = 0; i < 100; ++i) {
      cout << "i = "        << i         << ", "
	   << "my pid = "   << getpid()  << ", "
	   << "fork = "     << pid       << ", "
	   << "ppid = "     << getppid() << endl;
    } // for
    exit(42);
  } else {                           // in parent
    /* waitpid(): on success, returns the process ID of the child whose state
     * has changed; if WNOHANG was specified and one or more child(ren)
     * specified by pid exist, but have not yet changed state, then 0 is
     * returned. On error, -1 is returned.
     */
    if ((wpid = waitpid(pid, &pstatus, WNOHANG)) == -1) {
      perror("waitpid");
    } else if (wpid == 0) {
      cout << "no pstatus changes detected" << endl;
    } else if (WIFEXITED(pstatus)) {
      cout << "child with pid = "                << wpid                 << " "
	   << "exited normally with pstatus = "  << WEXITSTATUS(pstatus) << endl;
    } else if (WIFSIGNALED(pstatus)) {
      cout << "child with pid = "                << wpid                 << " "
	   << "exited abnormally from signal = " << WTERMSIG(pstatus)    << endl;
    } // if
  } // if
  return EXIT_SUCCESS;
} // main


{'fraction_non_alphanumeric': 0.16427232979387882, 'fraction_numerical': 0.006870705808869456, 'mean_word_length': 1.7540842648323303, 'pattern_counts': {'":': 0, '<': 131, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5843767', 'n_tokens_mistral': 1109, 'n_tokens_neox': 1058, 'n_words': 296}