/var/lib/pcp/testsuite/src/chain.c is in pcp-testsuite 4.0.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | /*
* Chained pipe throughput -- based upon MUSBUS ....
*
* Context switching via synchronized unbuffered pipe i/o
*
* Header: context1.c,v 3.4 87/06/22 14:22:59 kjmcdonell Beta
*
* Yep! code from 1987.
*
* This code comes from the Musbus benchmark that was written by me
* and released into the public domain circa 1984.
* - Ken McDonell, Oct 2017
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
int
main(int argc, char **argv)
{
int i;
int iter = 0;
int numiter;
int links;
int first = 1;
int master = 1;
int result;
int pid;
int debug = 0;
int p1[2], p2[2];
char pbuf[512] = {0};
if (argc > 3 && strcmp(argv[1], "-d") == 0) {
debug=1;
argc--;
argv++;
}
if (argc != 3) {
fprintf(stderr, "Usage: chain links iter\n");
exit(1);
}
links = atoi(argv[1]);
if (links < 1) {
fprintf(stderr, "chain: bogus links (%s)\n", argv[1]);
exit(1);
}
numiter = atoi(argv[2]);
if (numiter < 1) {
fprintf(stderr, "chain: bogus iter (%s)\n", argv[2]);
exit(1);
}
if (pipe(p2)) {
perror("chain: p2 pipe failed");
exit(1);
}
close(0);
if (dup(p2[0]) < 0) {
perror("chain: p2 dup->0 failed");
exit(1);
}
close(p2[0]);
if (links == 1) {
close(1);
if (dup(p2[1]) < 0) {
perror("chain: p2 dup->1 failed");
exit(1);
}
}
else {
for (i=1; i<links; i++) {
if (pipe(p1)) {
perror("chain: p1 pipe failed");
exit(1);
}
master = fork();
if (master == -1) {
perror("chain: fork failed");
exit(1);
}
else if (master) {
close(1);
if (dup(p1[1]) < 0) {
perror("chain: p1 dup->1 failed");
exit(1);
}
}
else {
iter++;
close(0);
if (dup(p1[0]) < 0) {
perror("chain: p1 dup->0 failed");
exit(1);
}
if (first) {
close(1);
if (dup(p2[1]) < 0) {
perror("chain: p2 dup->1 failed");
exit(1);
}
close(p2[1]);
}
close(p1[0]);
close(p1[1]);
close(p2[1]);
}
close(p1[0]);
close(p1[1]);
first = 0;
if (!master)
break;
}
}
close(p2[1]);
if (master) {
if (debug)
fprintf(stderr, "initial write, master pid %d\n", getpid());
if (write(1, pbuf, sizeof(pbuf)) != sizeof(pbuf)) {
perror("master write failed");
exit(1);
}
}
while (1) {
result = read(0, pbuf, sizeof(pbuf));
/* do some computation to default Pyramid 9825 race condititon */
{ int x; for (x = 0; x < 5000; x++) ; }
if (result == 0)
break;
if (result != sizeof(pbuf)) {
perror("read failed");
exit(1);
}
if (debug)
fprintf(stderr, "read seq %d pid %d\n", iter, getpid());
if (iter >= numiter)
break;
iter += links;
if (debug)
fprintf(stderr, "write seq %d pid %d\n", iter, getpid());
if (write(1, pbuf, sizeof(pbuf)) != sizeof(pbuf)) {
perror("write failed");
exit(1);
}
}
close(1);
while (master && (pid = wait(&result)) != -1) {
if (debug)
fprintf(stderr, "wait -> %d and 0x%x\n", pid, result);
}
exit(0);
}
|