Skip to content

Instantly share code, notes, and snippets.

@gustavorv86
gustavorv86 / get_localtime.c
Last active October 3, 2019 06:14
Function that returns the current date in string format.
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
/**
* Function that returns the current date in string format.
*
* Output format example: "2018-1-4 10:17:12.508977"
@gustavorv86
gustavorv86 / c_pthread_shared_variable_example.c
Last active March 5, 2024 22:13
Shared a global variable into multiple threads.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
typedef struct {
pthread_t thread;
int id_thread;
long sec_usleep;
} thread_info_t;
@gustavorv86
gustavorv86 / c_tcp_client_server_threads.c
Last active January 13, 2022 12:15
TCP Sockets written in C
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
@gustavorv86
gustavorv86 / c_priority_queue_threads.c
Last active September 8, 2023 04:42
POSIX message priority queue example written in C/C++
/**
* Compile:
* gcc -std=gnu11 -Wall -Wextra c_priority_queue_threads.c -o priority_queue_threads -lpthread -lrt
*/
#include <errno.h>
#include <mqueue.h>
#include <fcntl.h> /* For O_* constants. */
#include <sys/stat.h> /* For mode constants. */
@gustavorv86
gustavorv86 / execute_process.py
Last active June 19, 2019 10:41
Execute command in Python and get stdout, stderr and return code.
#!/usr/bin/env python
import subprocess
def execute_process(cmd) :
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = p.communicate()
return {'stdout': out, 'stderr': err, 'retcode': p.returncode}
@gustavorv86
gustavorv86 / get_md5.py
Created March 27, 2019 14:19
Python get MD5 of file
#!/usr/bin/env python
import hashlib
import os
import sys
def md5(path_file):
checksum = hashlib.md5()
@gustavorv86
gustavorv86 / get_files.py
Created March 27, 2019 15:22
Python get all path files (not directories) of a directory recursively
#!/usr/bin/env python
import os
import sys
def _r_get_files(parent_dir, list_files):
for child_name in os.listdir(parent_dir):
## Ignore directories
# if child_name == ".git":
# continue
@gustavorv86
gustavorv86 / exec_function_timeout_process.py
Created March 28, 2019 11:49
Python: execute a function with timeout using multiprocess
#!/usr/bin/env python
import sys
import multiprocessing
import time
def fn_loop_count(message, start_count, end_count):
for i in range(start_count, end_count):
print(message + ":" + str(i))
start_count +=1
@gustavorv86
gustavorv86 / exec_function_timeout_signal.py
Created March 28, 2019 11:50
Python: execute a function with timeout using signals
#!/usr/bin/env python
import signal
import time
class SigalrmException(Exception):
pass
def sigalrm_handler(signum, frame):
raise SigalrmException()
@gustavorv86
gustavorv86 / check_ascii_file.py
Created May 16, 2019 12:45
Check if file contains ASCII printable characters.
#!/usr/bin/env python3
import os
import sys
PRINTABLE_ASCII = [
9, 10, 13, (32, 126)
]