Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. /**
  2.  
  3. This program overwrites the file "foo", writing the integer 1 on
  4. the first line.
  5.  
  6. Then the program iterates N times, specified by a command line argument.
  7. On each iteration:
  8.  
  9. 1. The program positions the read marker at the beginning of the file.
  10.  
  11. 2. It then reads all integers from a file, each on a line, verifying that
  12. the integers are in ascending order with a difference of one
  13. between successive lines. If an error is found, the program stops looping
  14. and reports where the error occurred.
  15.  
  16. 3. If no error is found, the program adds one more integer to the
  17. integers in the file.
  18.  
  19. */
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <pthread.h>
  24.  
  25. #define PRIMES_LIMIT 10000 // upper bound for primes search.
  26. // tune this to force each thread to use a significant fraction of its quantum.
  27.  
  28. #define ORDER_ERROR -1
  29.  
  30. typedef struct threadArgsStruct {
  31. int nreps;
  32. FILE *fp;
  33. } ThreadArgsT;
  34.  
  35. pthread_mutex_t lock;
  36. int Nthreads;
  37. int count = 1;
  38.  
  39. /*
  40. PRE
  41. limit: largest number to inspect
  42. printmod: how often to print the most recent prime
  43. This is a computation waster. Feel free to provid a better algorithm!
  44. */
  45. void getprimes(long limit, long printmod) {
  46. long i,j;
  47. long recentprime = 1;
  48. long numprimes = 0;
  49. /* loop to limit */
  50. for (i = 2; i <= limit; i++) {
  51. /* try all numbers from 2 to limit-1 */
  52. for (j = 2; j < i; j++) {
  53. if (i%j == 0) { /* not prime */
  54. break;
  55. }
  56. }
  57. if (j == i) { /* made it to end of loop => number was prime */
  58. recentprime = i;
  59. numprimes++;
  60. /* print out every printmod number of primes */
  61. if ( printmod != 0 && (numprimes%printmod) == 0) {
  62. printf("%10ld\t%10ld\n",numprimes,recentprime);
  63. }
  64.  
  65. }
  66. }
  67. }
  68.  
  69.  
  70. /*
  71. Validate current file of integers. Returns negative value for error
  72. or the value of the largest integer read.
  73. PRE: file handle fp open for reading.
  74. PST: reads fp to EOF
  75. */
  76. int verifyOrder(FILE *fp) {
  77.  
  78. int value;
  79. int oldvalue;
  80.  
  81. rewind(fp); // rewind stream
  82.  
  83. // Get initial value, error if there isn't one.
  84. if (!feof(fp)) {
  85. fscanf(fp,"%d\n",&oldvalue);
  86. // printf("old %d new %d\n",oldvalue, value);
  87.  
  88. } else {
  89. return ORDER_ERROR;
  90. }
  91. if (oldvalue != 1) {
  92. return ORDER_ERROR;
  93. }
  94. // Now check all subsequent lines.
  95. while (!feof(fp)) {
  96. fscanf(fp,"%d\n",&value);
  97. // printf("old %d new %d\n",oldvalue, value);
  98. if (value != (oldvalue + 1) ) {
  99. return ORDER_ERROR;
  100. }
  101. oldvalue = value;
  102. }
  103.  
  104. return oldvalue; // no error so return last read value
  105. }
  106.  
  107. /*
  108. See to end of file and write num on a new line.
  109. PRE: fp open for writing.
  110. PST: num appended to fp.
  111. */
  112. void addLine(FILE *fp, int num) {
  113. fseek(fp,0L,SEEK_END); // seek to end of file to add next line
  114. fprintf(fp,"%d\n",num);
  115. fflush(fp);
  116. }
  117.  
  118. /**
  119. Function run to complete the task.
  120.  
  121. PRE: arg contains a ThreadArgsT that contains the file pointer for I/O
  122. and the termination value, nreps.
  123. */
  124. void *run_enum(void *arg) {
  125. int status;
  126.  
  127. ThreadArgsT *args = (ThreadArgsT *) arg;
  128. int nreps = args->nreps;
  129. FILE *fp;
  130. fp = args->fp;
  131. count = 2; // starts after 1 already written
  132. //printf("%d\n",nreps);
  133.  
  134. while (count < nreps) {
  135. getprimes(PRIMES_LIMIT,0); // waste some time
  136.  
  137. pthread_mutex_lock(&lock);
  138.  
  139. status = verifyOrder(fp);
  140. if (status == ORDER_ERROR) {
  141. fprintf(stderr,"Error at number %d\n",count);
  142. break;
  143. }
  144. if(count>=nreps){
  145. pthread_mutex_unlock(&lock);
  146. pthread_exit(NULL);
  147. }
  148. count = status + 1; // status has last number read, add 1
  149. printf("%d\n",count);
  150. addLine(fp,count); // output count and newline
  151. pthread_mutex_unlock(&lock);
  152. }
  153. pthread_exit(NULL);
  154. }
  155.  
  156.  
  157. int main(int argc, char *argv[]) {
  158. FILE *fp;
  159. int i;
  160.  
  161. // Process args (not very robust)
  162. if (argc != 3) {
  163. fprintf(stderr,"USAGE: enum NUM NTHREADS\n");
  164. fprintf(stderr,"Writes to file foo from 1 to NUM using NTHREADS threads.\n");
  165. exit(EXIT_FAILURE);
  166. }
  167. int nreps = atoi(argv[1]); // convert ascii to int
  168. int Nthreads = atoi(argv[2]); // ignored at present
  169.  
  170. fp = fopen("foo","w+"); // open/truncate file writing AND reading
  171. fprintf(fp,"%d\n",count); // write 1 on first line
  172. fflush(fp); // ensure that data is ouput to file.
  173.  
  174. // Package args in structure for use when pthreads are used.
  175. ThreadArgsT args;
  176. args.nreps = nreps;
  177. args.fp = fp;
  178.  
  179. pthread_attr_t attr;
  180. pthread_t *pid_array;
  181.  
  182. pid_array = (pthread_t*)malloc(sizeof(pthread_t)*Nthreads);
  183.  
  184. pthread_attr_init(&attr);
  185. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  186.  
  187. // Mutex lock
  188. pthread_mutex_init(&lock, NULL);
  189.  
  190. // Create thread
  191. for(int i=0; i<Nthreads; i++){
  192. //pthread_create(&thread_ID,NULL,run_enum,&args);
  193. pthread_create(&pid_array[i], NULL, run_enum, &args);
  194. }
  195.  
  196. for(i = 0; i < Nthreads; i++) pthread_join(pid_array[i], NULL);
  197.  
  198. //run_enum(&args);
  199.  
  200. fclose(fp); // close file
  201. pthread_mutex_destroy(&lock);
  202. free(pid_array);
  203. return(0);
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement