Skip to content

Instantly share code, notes, and snippets.

@kostikbel
Created March 7, 2020 21:11
Show Gist options
  • Save kostikbel/b2844258b7fba6e8ce3ccd8ef9422e5a to your computer and use it in GitHub Desktop.
Save kostikbel/b2844258b7fba6e8ce3ccd8ef9422e5a to your computer and use it in GitHub Desktop.
/* $Id: pipe_enomem.c,v 1.3 2020/03/07 21:02:04 kostik Exp kostik $ */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct pipepair {
int pp[2];
};
static struct pipepair *p;
int
main(void)
{
int error, pp[2];
size_t i, k, nsz, sz;
char x;
sz = 1024;
p = calloc(sz, sizeof(struct pipepair));
if (p == NULL) {
fprintf(stderr, "calloc: %s\n", strerror(errno));
exit(1);
}
for (i = 0;; i++) {
if (pipe(pp) == -1) {
printf("created %zd pipes. syscall error %s\n",
i, strerror(errno));
break;
}
if (i >= sz) {
nsz = sz * 2;
p = reallocf(p, nsz * sizeof(struct pipepair));
if (p == NULL) {
fprintf(stderr, "reallocf: %s\n",
strerror(errno));
exit(1);
}
memset(p + sz, 0, (nsz - sz) * sizeof(struct pipepair));
sz = nsz;
}
p[i].pp[0]= pp[0];
p[i].pp[1]= pp[1];
}
x = 'a';
for (k = 0; k < i; k++) {
error = write(p[k].pp[1], &x, 1);
if (error == -1)
printf("pipe %zd fds %d %d error %s\n",
k, p[k].pp[0], p[k].pp[1], strerror(errno));
else if (error == 0)
printf("pipe %zd fds %d %d EOF\n",
k, p[k].pp[0], p[k].pp[1]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment