Skip to content

Instantly share code, notes, and snippets.

@binh12A3
Last active September 3, 2023 14:44
Show Gist options
  • Save binh12A3/1d3892da4a2a40c4664ffc0a4cf1cc15 to your computer and use it in GitHub Desktop.
Save binh12A3/1d3892da4a2a40c4664ffc0a4cf1cc15 to your computer and use it in GitHub Desktop.
#include <iostream> // for using std, printf(),...
#include <unistd.h> // for using fork()
int main(int argc, char** argv)
{
printf("[PID:%d] Hello world\n", getpid());
int a = 0;
int pid = fork();
printf("[PID:%d] Fork pid : %d\n", getpid(), pid);
if(pid != 0)
{
// Parent process
for(int i = 0; i < 3; i++)
{
a++;
printf("[PID:%d] This is Parent process, forked pid = %d, a = %d\n", getpid(), pid, a);
}
}
else
{
// Child process
for(int i = 0; i < 5; i++)
{
a--;
printf("[PID:%d] This is Child process, forked pid = %d, a = %d\n", getpid(), pid, a);
}
}
printf("[PID:%d] Goodbye, a = %d\n", getpid(), a);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment