Skip to content

Instantly share code, notes, and snippets.

View kunigami's full-sized avatar
:octocat:
Working from home

Guilherme Kunigami kunigami

:octocat:
Working from home
View GitHub Profile
if (!fork()) {
printf("I'm the child!\n");
} else {
printf("I'm the parent!\n");
}
getaddrinfo("www.example.com", "3490", &hints, &res);
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
connect(sockfd, res->ai_addr, res->ai_addrlen);
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_STREAM;
// Important! Fill in my IP for me
hints.ai_flags = AI_PASSIVE;
// Use the address from localhost
getaddrinfo(NULL, "3490", &hints, &res);
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
bind(sockfd, res->ai_addr, res->ai_addrlen);
getaddrinfo("www.example.com", "http", &hints, &res);
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
int status;
struct addrinfo hints;
struct addrinfo *servinfo; // will point to the results
memset(&hints, 0, sizeof hints); // make sure the struct is empty
hints.ai_family = AF_INET6; // IPv6
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
getaddrinfo("www.google.com", "https", &hints, &servinfo);
void sigint_handler(int sig) {
write(0, "Ahhh! SIGINT!\n", 14);
}
int main(void) {
void sigint_handler(int sig); /* prototype */
struct sigaction sa;
sa.sa_handler = sigint_handler;
sa.sa_flags = 0;
# Python 3.7+
import asyncio
async def sleep(id):
print("will sleep", id)
await asyncio.sleep(4)
print("slept", id)
async def main():
await sleep(1)
# Python 3.7+
import asyncio
async def sleep(id):
print("will sleep", id)
await asyncio.sleep(4)
print("slept", id)
async def main():
await asyncio.gather(sleep(1), sleep(2))
async def generator_async():
await generator2()
await generator3()
@kunigami
kunigami / async.py
Last active February 2, 2020 00:14
async def g_async():
return 10
# <class 'coroutine'>
print(type(g_async()))
def g_coro():
yield 10
# <class 'generator'>