test04.c 838 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* test readline() */
  2. #include "unpthread.h"
  3. static char *infile; /* from argv[1]; read-only by threads */
  4. void *
  5. myfunc(void *ptr)
  6. {
  7. int i, fdin;
  8. char buf[MAXLINE];
  9. FILE *fpout;
  10. snprintf(buf, sizeof(buf), "temp.%d", pthread_self());
  11. fpout = Fopen(buf, "w+");
  12. /* printf("created %s\n", buf); */
  13. for (i = 0; i < 5; i++) {
  14. fdin = Open(infile, O_RDONLY, 0);
  15. while (Readline(fdin, buf, sizeof(buf)) > 0) {
  16. fputs(buf, fpout);
  17. }
  18. Close(fdin);
  19. }
  20. Fclose(fpout);
  21. printf("thread %d done\n", pthread_self());
  22. return(NULL);
  23. }
  24. int
  25. main(int argc, char **argv)
  26. {
  27. int i, nthreads;
  28. pthread_t tid;
  29. if (argc != 3)
  30. err_quit("usage: test04 <input-file> <#threads>");
  31. infile = argv[1];
  32. nthreads = atoi(argv[2]);
  33. for (i = 0; i < nthreads; i++) {
  34. Pthread_create(&tid, NULL, myfunc, NULL);
  35. }
  36. pause();
  37. exit(0);
  38. }