fork download
  1. #include <pthread.h>
  2.  
  3. #include <stdio.h>
  4.  
  5. #include <stdlib.h>
  6.  
  7. #include <string.h>
  8.  
  9. #include <unistd.h>
  10.  
  11. #include <time.h>
  12.  
  13.  
  14.  
  15. #define MAX_SIZE 2000000
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. struct Data{
  26.  
  27. char name[20];
  28.  
  29. int value;
  30.  
  31. };
  32.  
  33.  
  34.  
  35. Data *sort (Data *data , int numOfData) ; // insert sort using struct-datatype , ranked by the value)
  36.  
  37.  
  38.  
  39. pthread_t tid1,tid2;
  40.  
  41. pthread_mutex_t lock;
  42.  
  43.  
  44.  
  45. int numOfData =0;
  46.  
  47. FILE *rank;
  48.  
  49. Data *data = (Data*)malloc(sizeof(Data)*MAX_SIZE); // use struct datastructure ( char name[20] , int value)
  50.  
  51. char nameData[20]; // two temp data for scanning from the file
  52.  
  53. int valueData;
  54.  
  55. int count = 0;
  56.  
  57.  
  58.  
  59.  
  60.  
  61. void *rankP (void *param){ // <------ pthread
  62.  
  63.  
  64.  
  65. while(!feof(rank)){
  66.  
  67. pthread_mutex_lock(&lock);
  68.  
  69.  
  70.  
  71. if(fscanf(rank,"%s",nameData)== EOF)
  72.  
  73. break;
  74.  
  75. else
  76.  
  77. strcpy(data[numOfData].name,nameData);
  78.  
  79. fscanf(rank,"%d",&valueData); // get userID(%s) and userScore(%d)
  80.  
  81. data[numOfData].value = valueData;
  82.  
  83. numOfData++;
  84.  
  85. printf(" working ........ count = %d \n",count++);
  86.  
  87.  
  88.  
  89. pthread_mutex_unlock(&lock);
  90.  
  91.  
  92.  
  93. }
  94.  
  95.  
  96.  
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103. int main (int argc , char* argv[]){
  104.  
  105.  
  106.  
  107.  
  108.  
  109. rank = fopen(argv[1],"r");
  110.  
  111. time_t t1,t2;
  112.  
  113. t1 = time(NULL);
  114.  
  115. if(pthread_mutex_init(&lock, NULL) != 0)
  116.  
  117. {
  118.  
  119. printf("\n mutex init failed\n");
  120.  
  121. return 1;
  122.  
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129. pthread_attr_t attr1 , attr2;
  130.  
  131. pthread_attr_init(&attr1);
  132.  
  133. pthread_attr_init(&attr2);
  134.  
  135. pthread_create(&tid1,&attr1,rankP,NULL);
  136.  
  137. pthread_create(&tid2,&attr2,rankP,NULL);
  138.  
  139. pthread_join(tid1,NULL);
  140.  
  141. pthread_join(tid2,NULL);
  142.  
  143.  
  144.  
  145. t2 = time(NULL);
  146.  
  147. printf(" sorted after %d seconds \n", t2-t1);
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155. // <---------------- parent process
  156.  
  157. printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  158.  
  159.  
  160.  
  161. char ch[5];
  162.  
  163. Data *t = sort(data,numOfData);
  164.  
  165.  
  166.  
  167. FILE *result = fopen(argv[2],"w"); // write in the data to file
  168.  
  169. for(int i=0 ; i<=numOfData-1 ; i++){
  170.  
  171. sprintf(ch,"%d",t[i].value);
  172.  
  173. fprintf(result,t[i].name);
  174.  
  175. fprintf(result,"\t");
  176.  
  177. fprintf(result,ch);
  178.  
  179. fprintf(result,"\n");
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:35: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
prog.c:49: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
prog.c: In function ‘rankP’:
prog.c:77: error: ‘data’ undeclared (first use in this function)
prog.c:77: error: (Each undeclared identifier is reported only once
prog.c:77: error: for each function it appears in.)
prog.c:79: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result
prog.c: In function ‘main’:
prog.c:147: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘time_t’
prog.c:163: error: ‘Data’ undeclared (first use in this function)
prog.c:163: error: ‘t’ undeclared (first use in this function)
prog.c:163: warning: implicit declaration of function ‘sort’
prog.c:163: error: ‘data’ undeclared (first use in this function)
prog.c:169: error: ‘for’ loop initial declaration used outside C99 mode
prog.c:173: warning: format not a string literal and no format arguments
prog.c:177: warning: format not a string literal and no format arguments
prog.c:179: error: expected declaration or statement at end of input
prog.c:179: error: expected declaration or statement at end of input
stdout
Standard output is empty