Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 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. pthread_mutex_t lock;
  26.  
  27. #define PRIMES_LIMIT 10000 // upper bound for primes search.
  28. // tune this to force each thread to use a significant fraction of its quantum.
  29.  
  30. #define ORDER_ERROR -1
  31. int Nthreads;
  32.  
  33. typedef struct threadArgsStruct {
  34. int nreps;
  35. FILE *fp;
  36. } ThreadArgsT;
  37.  
  38. /*
  39. PRE
  40. limit: largest number to inspect
  41. printmod: how often to print the most recent prime
  42. This is a computation waster. Feel free to provid a better algorithm!
  43. */
  44. void getprimes(long limit, long printmod) {
  45. long i,j;
  46. long recentprime = 1;
  47. long numprimes = 0;
  48. /* loop to limit */
  49. for (i = 2; i <= limit; i++) {
  50. /* try all numbers from 2 to limit-1 */
  51. for (j = 2; j < i; j++) {
  52. if (i%j == 0) { /* not prime */
  53. break;
  54. }
  55. }
  56. if (j == i) { /* made it to end of loop => number was prime */
  57. recentprime = i;
  58. numprimes++;
  59. /* print out every printmod number of primes */
  60. if ( printmod != 0 && (numprimes%printmod) == 0) {
  61. printf("%10ld\t%10ld\n",numprimes,recentprime);
  62. }
  63.  
  64. }
  65. }
  66. }
  67.  
  68.  
  69. /*
  70. Validate current file of integers. Returns negative value for error
  71. or the value of the largest integer read.
  72. PRE: file handle fp open for reading.
  73. PST: reads fp to EOF
  74. */
  75. int verifyOrder(FILE *fp) {
  76.  
  77. int value;
  78. int oldvalue;
  79.  
  80. rewind(fp); // rewind stream
  81.  
  82. // Get initial value, error if there isn't one.
  83. if (!feof(fp)) {
  84. fscanf(fp,"%d\n",&oldvalue);
  85. // printf("old %d new %d\n",oldvalue, value);
  86.  
  87. } else {
  88. return ORDER_ERROR;
  89. }
  90. if (oldvalue != 1) {
  91. return ORDER_ERROR;
  92. }
  93. // Now check all subsequent lines.
  94. while (!feof(fp)) {
  95. fscanf(fp,"%d\n",&value);
  96. // printf("old %d new %d\n",oldvalue, value);
  97. if (value != (oldvalue + 1) ) {
  98. return ORDER_ERROR;
  99. }
  100. oldvalue = value;
  101. }
  102.  
  103. return oldvalue; // no error so return last read value
  104. }
  105.  
  106. /*
  107. See to end of file and write num on a new line.
  108. PRE: fp open for writing.
  109. PST: num appended to fp.
  110. */
  111. void addLine(FILE *fp, int num) {
  112. fseek(fp,0L,SEEK_END); // seek to end of file to add next line
  113. fprintf(fp,"%d\n",num);
  114. fflush(fp);
  115. }
  116.  
  117. /**
  118. Function run to complete the task.
  119.  
  120. PRE: arg contains a ThreadArgsT that contains the file pointer for I/O
  121. and the termination value, nreps.
  122. */
  123. void *run_enum(void *arg) {
  124. int count;
  125. int status;
  126.  
  127. ThreadArgsT *args = (ThreadArgsT *) arg;
  128. int nreps = args->nreps;
  129. FILE *fp;
  130. fp = args->fp;
  131.  
  132. count = 2; // starts after 1 already written
  133. while (count < nreps) {
  134. getprimes(PRIMES_LIMIT,0); // waste some time
  135. pthread_mutex_lock(&lock);
  136. status = verifyOrder(fp);
  137.  
  138. if (status == ORDER_ERROR) {
  139. fprintf(stderr,"Error at number %d\n",count);
  140. break;
  141. }
  142. printf("nreps: %i, count: %i",nreps, count);
  143. if(count >nreps){
  144. pthread_mutex_unlock(&lock);
  145. pthread_exit(NULL);
  146. break;
  147. }
  148.  
  149. //printf("second check: %i\n",count);
  150. count = status + 1; // status has last number read, add 1
  151. //printf("third check: %i\n",count);
  152. //printf("-------------------------\n");
  153. addLine(fp,count); // output count and newline
  154. pthread_mutex_unlock(&lock);
  155. }
  156. pthread_exit(NULL);
  157. }
  158.  
  159.  
  160. int main(int argc, char *argv[]) {
  161. FILE *fp;
  162. int count = 1;
  163. int i;
  164. int threadCreateStatus;
  165.  
  166. /* Processing input arguments */
  167. // Process args (not very robust)
  168. if (argc != 3) {
  169. fprintf(stderr,"USAGE: enum NUM NTHREADS\n");
  170. fprintf(stderr,"Writes to file foo from 1 to NUM using NTHREADS threads.\n");
  171. exit(EXIT_FAILURE);
  172. }
  173.  
  174. /*convert given input from ascii to int*/
  175. int nreps = atoi(argv[1]); //number of lines to print
  176. int Nthreads = atoi(argv[2]); //number of threads to run
  177.  
  178.  
  179. /*check to see if number of lines is greater than number of threads*/
  180. if(Nthreads>nreps){//if there are more threads than lines
  181. fprintf(stderr,"number of threads must be less than number of lines");
  182. exit(EXIT_FAILURE);
  183. }
  184.  
  185. /* Initialize the threads */
  186. pthread_t *threadArray; //create an array that holds pthread pointers
  187. threadArray=(pthread_t *)malloc(sizeof(pthread_t)*(Nthreads));//dynamically allocate the thread array
  188. //FREE THIS
  189.  
  190. pthread_attr_t attrObject;// create a thread attribute object
  191. pthread_attr_init(&attrObject);//initialize the thread attribute object
  192. pthread_attr_setdetachstate(&attrObject,PTHREAD_CREATE_JOINABLE);//make it joinable
  193.  
  194. fp = fopen("foo","w+"); // open/truncate file writing AND reading
  195. fprintf(fp,"%d\n",count); // write 1 on first line
  196. fflush(fp); // ensure that data is ouput to file.
  197.  
  198. // Package args in structure for use when pthreads are used.
  199. ThreadArgsT args;
  200. args.nreps = nreps;
  201. args.fp = fp;
  202.  
  203. for(i=0;i<Nthreads;i++){
  204. printf("creating thread %i\n", i);
  205. pthread_create(&threadArray[i],&attrObject,run_enum,&args);
  206. }
  207. //run_enum(&args);
  208. /*
  209. if(threadCreateStatus!=0){
  210. printf("p_thread returned error code %d.\n",threadCreateStatus);
  211. exit(-1);
  212. }*/
  213.  
  214. for(i=0;i<Nthreads;i++){
  215. pthread_join(threadArray[i],NULL);
  216. }
  217.  
  218. fclose(fp); // close file
  219.  
  220. free(threadArray);
  221. return(0);
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement