Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tmprocess.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1999-2012 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "sftest.h"
21
22
/* Test to see if multiple writers to the same file create
23
** a consistent set of records.
24
*/
25
26
#ifndef N_PROC
27
#define N_PROC 3
28
#endif
29
30
#define N_REC 1000
31
#define B_SIZE 256
32
33
#if __STD_C
34
static ssize_t inspect(Sfio_t* f, const Void_t* buf, size_t n, Sfdisc_t* disc)
35
#else
36
static ssize_t inspect(f, buf, n, disc)
37
Sfio_t* f;
38
Void_t* buf;
39
size_t n;
40
Sfdisc_t* disc;
41
#endif
42
{
43
int w, k;
44
char* s;
45
Sfio_t* sf;
46
47
if(*((char*)buf + n-1) != '\n')
48
terror("Not writing a whole record");
49
50
sf = sfnew(NIL(Sfio_t*),(Void_t*)buf,n,-1,SF_STRING|SF_READ);
51
while((s = sfgetr(sf,'\n',0)) )
52
{ w = sfvalue(sf)-1;
53
if(s[0] != s[w-1]-1)
54
terror("Bad record");
55
k = (s[2]-'0')*10 + (s[3]-'0');
56
if(k != w)
57
terror("Expect %d, get %d",k,w);
58
}
59
sfclose(sf);
60
61
if((w = write(sffileno(f),buf,n)) != (int)n)
62
terror("Write %d returns %d",n,w);
63
64
return w;
65
}
66
67
static Sfdisc_t Disc[N_PROC];
68
69
tmain()
70
{
71
ssize_t size[N_PROC][N_REC];
72
int count[N_PROC];
73
char record[N_PROC][128], *s, *file;
74
int i, r, n, pid;
75
Sfio_t* f;
76
Sfio_t* fa[N_PROC];
77
char buf[N_PROC][B_SIZE], b[N_PROC][128];
78
unsigned long u;
79
80
/* create pseudo-random record sizes */
81
u = 1;
82
for(i = 0; i < N_PROC; ++i)
83
for(r = 0; r < N_REC; ++r)
84
{ u = u * 0x63c63cd9L + 0x9c39c33dL;
85
size[i][r] = (ssize_t)(u%63) + 16;
86
}
87
88
/* records for different processes */
89
for(i = 0; i < N_PROC; ++i)
90
for(r = 0; r < 128; ++r)
91
record[i][r] = '0' + 2*i;
92
93
/* create file */
94
file = tstfile("sf", 0);
95
if(!(f = sfopen(NIL(Sfio_t*),file,"w+")) )
96
terror("Opening temporary file %s", file);
97
98
/* open file for appending */
99
for(i = 0; i < N_PROC; ++i)
100
if(!(fa[i] = sfopen(NIL(Sfio_t*), file, "a")) )
101
terror("Open %s to append", file);
102
103
/* fork processes */
104
for(i = 0; i < N_PROC; ++i)
105
{
106
#ifdef DEBUG
107
#define FORK() 0
108
#define RETURN(v)
109
#else
110
#define FORK() fork()
111
#define RETURN(v) texit(v)
112
#endif
113
if((pid = (int)FORK()) < 0 )
114
terror("Creating process %d", i);
115
else if(pid == 0)
116
{ /* write to file */
117
sfsetbuf(fa[i], (Void_t*)buf[i], sizeof(buf[i]));
118
sfset(fa[i], SF_WHOLE, 1);
119
Disc[i].writef = inspect;
120
sfdisc(fa[i], &Disc[i]);
121
122
for(r = 0; r < N_REC; ++r)
123
{ n = size[i][r]; s = b[i];
124
memcpy(s,record[i],n-1);
125
s[1] = '(';
126
s[2] = ((n-1)/10) + '0';
127
s[3] = ((n-1)%10) + '0';
128
s[4] = ')';
129
s[n-2] = s[0] + 1;
130
s[n-1] = '\n';
131
if(sfwrite(fa[i],s,n) != n)
132
terror("sfwrite failed");
133
}
134
sfsync(fa[i]);
135
136
RETURN(0);
137
}
138
}
139
140
for(i = 0; i < N_PROC; ++i)
141
{ wait(&r);
142
count[i] = 0;
143
}
144
145
n = 0;
146
while((s = sfgetr(f,'\n',0)) )
147
{ n += 1;
148
if((i = s[0] - '0') < 0 || (i%2) != 0 || (i /= 2) >= N_PROC )
149
terror("%d: Wrong record type", n);
150
151
r = sfvalue(f);
152
if(s[r-2] != s[0]+1)
153
terror("%d: process%d, number=%d", n, i, count[i]);
154
for(r -= 3; r > 4; --r)
155
if(s[r] != s[0])
156
terror("%d: process%d, count=%d", n, i, count[i]);
157
158
if(sfvalue(f) != size[i][count[i]])
159
terror("%d: process%d number=%d expected size=%d read size=%d",
160
n, i, count[i], size[i][count[i]], sfvalue(f));
161
162
count[i] += 1;
163
}
164
165
for(i = 0; i < N_PROC; ++i)
166
if(count[i] != N_REC)
167
terror("Bad count%d %d", i, count[i]);
168
169
texit(0);
170
}
171
172