This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!fork()) { | |
printf("I'm the child!\n"); | |
} else { | |
printf("I'm the parent!\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getaddrinfo("www.example.com", "http", &hints, &res); | |
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def generator_async(): | |
await generator2() | |
await generator3() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def g_async(): | |
return 10 | |
# <class 'coroutine'> | |
print(type(g_async())) | |
def g_coro(): | |
yield 10 | |
# <class 'generator'> |