forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test04.c
50 lines (38 loc) · 838 Bytes
/
test04.c
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
/* test readline() */
#include "unpthread.h"
static char *infile; /* from argv[1]; read-only by threads */
void *
myfunc(void *ptr)
{
int i, fdin;
char buf[MAXLINE];
FILE *fpout;
snprintf(buf, sizeof(buf), "temp.%d", pthread_self());
fpout = Fopen(buf, "w+");
/* printf("created %s\n", buf); */
for (i = 0; i < 5; i++) {
fdin = Open(infile, O_RDONLY, 0);
while (Readline(fdin, buf, sizeof(buf)) > 0) {
fputs(buf, fpout);
}
Close(fdin);
}
Fclose(fpout);
printf("thread %d done\n", pthread_self());
return(NULL);
}
int
main(int argc, char **argv)
{
int i, nthreads;
pthread_t tid;
if (argc != 3)
err_quit("usage: test04 <input-file> <#threads>");
infile = argv[1];
nthreads = atoi(argv[2]);
for (i = 0; i < nthreads; i++) {
Pthread_create(&tid, NULL, myfunc, NULL);
}
pause();
exit(0);
}