Skip to content

Instantly share code, notes, and snippets.

@peterbean410
peterbean410 / worker_thread.js
Last active November 1, 2024 16:15
worker_thread.js
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
if (isMainThread) {
// This code runs in the main thread
const worker = new Worker(__filename, {
workerData: { number: 5 } // Send data to the worker thread
});
worker.on('message', (result) => {
@peterbean410
peterbean410 / child_process.js
Created November 1, 2024 16:02
chid_process.js
const { spawn } = require('child_process');
// Function to calculate Fibonacci (same as before)
function fibonacci(n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
@peterbean410
peterbean410 / build_fork_execve_example.sh
Last active October 24, 2024 03:48
Fork and Execute
#!/usr/bin/env bash
gcc -o fork_execve_example fork_execve_example.c
chmod +x ./fork_execve_example
chmod +x ./test_script.sh
@peterbean410
peterbean410 / self_delete.c
Created October 17, 2024 04:54
self_delete.c
#include <stdio.h>
#include <unistd.h>
#include <dirent.h> // For directory handling
#include <stdlib.h> // For exit()
void ls() {
// Open the directory
DIR *dir = opendir(".");
if (dir == NULL) {
perror("Unable to open directory");
@peterbean410
peterbean410 / SensitiveData.java
Created June 5, 2022 06:20 — forked from RogerRiggs/SensitiveData.java
Example Using java.lang.ref.Cleaner as a Replacement for Finalize and Testing of the cleanup.
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
@peterbean410
peterbean410 / async.js
Last active October 4, 2022 10:41
How does async work in NodeJS
async function callme(message, level) {
console.log(message, "entering", level)
level--;
// let req = new XMLHttpRequest();
// req.open('GET', "https://thumbs.dreamstime.com/b/brown-dog-corso-corso-stands-field-green-grass-brown-dog-corso-cors-120197441.jpg");
// req.onload = function () {
// if (req.status == 200) {
// console.log(req.status, "Download ok");
// } else {