i => j1 = 2*i 0 => 0 1 => 2 2 => 4
j2 = 2*i+1 0 => 1 1 => 3 2 => 5
i = j//2 0 => 0 2 => 1 4 => 2
1 => 0 3 => 1 5 => 2
i => j1 = 2*i + 1 0 => 1 1 => 3 2 => 5
j2 = 2*i + 2 0 => 2 1 => 4 2 => 6
#!/usr/bin/env bash | |
set -euo pipefail | |
create_bridge() { | |
local nsname="$1" | |
local ifname="$2" | |
echo "Creating bridge ${nsname}/${ifname}" |
#!/usr/bin/env bash | |
set -euo pipefail | |
create_bridge() { | |
local nsname="$1" | |
local ifname="$2" | |
echo "Creating bridge ${nsname}/${ifname}" |
#!/usr/bin/env python3 | |
# Usage: ethsend.py eth0 ff:ff:ff:ff:ff:ff 'Hello everybody!' | |
# ethsend.py eth0 06:e5:f0:20:af:7a 'Hello 06:e5:f0:20:af:7a!' | |
# | |
# Note: CAP_NET_RAW capability is required to use SOCK_RAW | |
import fcntl | |
import socket | |
import struct |
#!/usr/bin/env bash | |
set -xeuo pipefail | |
create_bridge() { | |
local nsname="$1" | |
local ifname="$2" | |
echo "Creating bridge ${nsname}/${ifname}" |
#!/usr/bin/env python3 | |
import json | |
import sys | |
for line in sys.stdin: | |
ds = eval(line) | |
print(json.dumps(ds)) |
# python3 | |
import asyncio | |
import sys | |
counter = 0 | |
async def run_server(host, port): | |
server = await asyncio.start_server(serve_client, host, port) | |
await server.serve_forever() |
const fs = require('fs'); | |
const path = require('path'); | |
async function *walkdir(dir) { | |
const stack = [dir]; | |
while (stack.length) { | |
const filename = stack.pop(); | |
const stat = await fs.promises.stat(filename); | |
if (stat.isDirectory()) { | |
const files = (await fs.promises.readdir(filename)) |
const http = require('http'); | |
const log = console.log; | |
console.log = (...args) => { | |
log.apply(console, [new Date().toISOString()].concat(args)); | |
}; | |
const port = process.argv[2]; | |
const server = http.createServer((req, res) => { | |
console.log('Incoming request'); |
Description
As everybody knows, execve()
system call replaces a calling process image with a new process image. At the same time the file descriptors table remains the same.
The idea of this example is to show how launched via execve()
processes can access file descriptors from their parent.
The parent process creates 4 pipes (unidirectional data flows) for stdin/stdout streams of its two children. Then the parent forks itself twice to spawn its children.
Each child process binds corresponding pipes to its stdin & stdout and then runs child
executable via execl()
. To simplify the example, child
processes also explicitly shares their stdin file descriptors between each other via command line argument. In the end parent is able to read from
children stdouts and write to their stdins, while children are able to write to sibling's stdins (see the schema below).