Skip to content

Instantly share code, notes, and snippets.

@binh12A3
Last active September 3, 2023 16:09
Show Gist options
  • Save binh12A3/095caa9d444e01a633ab16651195d92b to your computer and use it in GitHub Desktop.
Save binh12A3/095caa9d444e01a633ab16651195d92b to your computer and use it in GitHub Desktop.
#include "helper.h"
string my_pad_right(string const& str, int length, char c)
{
if ( str.size() < length )
return str + string(length - str.size(), c);
else
return str;
}
string my_pad_left(string const& str, int length, char c)
{
if ( str.size() < length )
return std::string(length - str.size(), c) + str;
else
return str;
}
// Cross-platform (Windows, Unix) sleep function
void my_sleep(int milliseconds)
{
#ifdef _WIN32
Sleep(milliseconds);
#else
usleep(milliseconds * 1000);
#endif
}
void my_log(string str, string log_level)
{
time_t tmNow;
tmNow = time(NULL);
struct tm t = *localtime(&tmNow);
string date_time = my_pad_left(to_string(t.tm_mday ), 2, '0') + "/"
+ my_pad_left(to_string(t.tm_mon + 1), 2, '0') + "/"
+ my_pad_left(to_string(t.tm_year + 1900), 4, '0') + " "
+ my_pad_left(to_string(t.tm_hour ), 2, '0') + ":"
+ my_pad_left(to_string(t.tm_min ), 2, '0') + ":"
+ my_pad_left(to_string(t.tm_sec ), 2, '0');
//printf("%s", date_time.c_str()); //For printf you need a c-style string
int pid, ppid;
#ifdef _WIN32
pid = ::GetCurrentProcessId();
#else
pid = ::getpid();
ppid = ::getppid();
#endif
cout << "[" << date_time << "][PID:" << pid << "][PPID:" << ppid << "]" << log_level << " " << str << endl;
}
// This is a protection to avoid cross duplidated when many sources include this .h file
#ifndef _HELPER_
#define _HELPER_
#include <iostream> // for using std
#include <string> // for using std::string
#include <ctime> // for using time
#ifdef _WIN32
#include <windows.h> // for using Sleep() on Windows
#else
#include <unistd.h> // for using usleep() on Unix
#endif // _WIN32
using std::cout;
using std::endl;
using std::string;
using std::to_string;
#define LL_FATAL "[FATAL]"
#define LL_ERROR "[ERROR]"
#define LL_WARNING "[WARN ]"
#define LL_INFO "[INFO ]"
#define LL_DEBUG "[DEBUG]"
#define LL_TRACE "[TRACE]"
void my_log(const string str, string log_level = LL_INFO);
void my_sleep(const int milliseconds);
string my_pad_right(string const& str, const int length, const char c);
string my_pad_left(string const& str, const int length, const char c);
#endif // _HELPER_
/*
COMPILE : gcc test.cpp mylib/helper.cpp -lstdc++ -o test
*/
#include <iostream> // for using std, printf(),...
#include <string> // for using std::string
#include <cstring> // for using strcpy()
#include <unistd.h> // for using fork()
#include "mylib/helper.h"
using std::cout;
using std::endl;
using std::string;
using std::to_string;
int main(int argc, char** argv) {
my_log("Hello world");
int pid;
pid = fork();
if(pid != 0)
{
while(1)
{
my_log("Parent process");
my_sleep(1000);
}
}
else
{
while(1)
{
my_log("Child process", LL_DEBUG);
my_sleep(1000);
// copy a into NULL pointer to simulate a coredump in order to produce a Zombie process
strcpy("a", NULL);
}
}
return 0;
}
@binh12A3
Copy link
Author

binh12A3 commented Sep 3, 2023

btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux/prog$ ls -ltr
total 32
drwxrwxrwx 1 btnguyen btnguyen  4096 Aug 26 23:56 mylib
-rwxrwxrwx 1 btnguyen btnguyen 26768 Sep  3 22:02 test
-rwxrwxrwx 1 btnguyen btnguyen   710 Sep  3 22:06 test.cpp
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux/prog$ cd mylib
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux/prog/mylib$ ls -ltr
total 8
-rwxrwxrwx 1 btnguyen btnguyen  971 Aug 28 00:16 helper.h
-rwxrwxrwx 1 btnguyen btnguyen 1428 Sep  3 22:09 helper.cpp
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux/prog/mylib$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment