Skip to content

Instantly share code, notes, and snippets.

@azat
Last active August 29, 2015 14:01
Show Gist options
  • Save azat/a8dac74aff100ef1948c to your computer and use it in GitHub Desktop.
Save azat/a8dac74aff100ef1948c to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
pipe=test.pipe
lines=2
mount -o size=100M -t tmpfs tmpfs /tmp
cd /tmp
gcc -xc - <<-IFH
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int num = atoi(argv[1]);
int i;
for (i = 0; i < num; ++i) {
if (write(STDOUT_FILENO, "\n", 1) != 1) {
fprintf(stderr, "Write failed\n");
return 1;
}
}
fprintf(stderr, "Write success (%i)\n", num);
return 0;
}
IFH
function writeToPipe()
{
echo "Writing to pipe $1 ($lines lines)"
./a.out $lines
}
function readFromPipe()
{
echo "Reading from pipe $1 ($lines lines)"
local l=0
while [[ ! $l -eq $lines ]]; do
readed=$(wc -l "$1" | awk '{print $1}')
let l+=$readed
echo "Readed $readed lines"
done
echo "Finished reading"
}
export -f readFromPipe writeToPipe
export lines
# XXX: group to allow fast-copying
rm -f $pipe && mkfifo $pipe && timeout -s TERM 5s xargs -P2 -I{} bash -c '{}' <<-IFH
readFromPipe $pipe
sleep 1 && writeToPipe $pipe
IFH
#
# After this seq of commands it can loose one record,
# and will be killed by timeout
# And it can reproduced only using shell-buildin, and this is pretty obvious,
# because it must be _faster_ then read, while with execve(2) it won't be.
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment