fork(2) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <sys/wait.h>
  8. #include <fcntl.h>
  9.  
  10.  
  11. int vf,pmax,SIZE,size,sudoku[25][25];
  12.  
  13. int isValid(int row,int col,int weight)
  14. {
  15. int i,j,start_row,start_col;
  16. for(i=0;i<SIZE;i++)
  17. if(sudoku[row][i]==weight)
  18. return 0;
  19.  
  20. for(i=0;i<SIZE;i++)
  21. if(sudoku[i][col]==weight)
  22. return 0;
  23.  
  24. start_row=(row/size)*size;
  25. start_col=(col/size)*size;
  26. for(i=start_row;i<start_row+size;i++)
  27. for(j=start_col;j<start_col+size;j++)
  28. if(sudoku[i][j]==weight)
  29. return 0;
  30. return 1;
  31. }
  32.  
  33. void printSolution()
  34. {
  35. int i,j;
  36. printf("\n");
  37. for(i=0;i<SIZE;i++)
  38. {
  39. for(j=0;j<SIZE;j++)
  40. printf("%d ",sudoku[i][j]);
  41. printf("\n");
  42. }
  43. printf("\n");
  44. }
  45.  
  46. void solveSudoku(int row,int col)
  47. {
  48. int fd2,fd,i,cn,tmp=0,rev,chn=0,anss=0;
  49. //printf("hi ");
  50. fd2=open("tmp2",O_RDWR|O_CREAT);
  51. flock(fd2,LOCK_EX);
  52. read(fd2,&anss,sizeof(anss));
  53. printf("anss= %d\n",anss);
  54. flock(fd2,LOCK_UN);
  55. close(fd2);
  56. if (anss==1) _exit(1);
  57. if(row==SIZE && col==0)
  58. {
  59. //printf("aaa\n");
  60. anss=1;
  61. fd2=open("tmp2",O_RDWR|O_CREAT);
  62. //flock(fd2,LOCK_EX);
  63. write(fd2,&anss,sizeof(anss));
  64. printSolution();
  65. flock(fd2,LOCK_UN);
  66. close(fd2);
  67. _exit(1);
  68. }
  69. else
  70. {
  71. if(sudoku[row][col]!=0) //encounters a pre-filled cell.
  72. {
  73. if (col==SIZE-1) solveSudoku(row+1,0);
  74. else solveSudoku(row,col+1);
  75. }
  76. else
  77. for(i=1;i<=SIZE;i++)
  78. if( isValid(row,col,i) && sudoku[row][col]==0)
  79. {
  80. fd=open("tmp",O_RDWR|O_CREAT);
  81. flock(fd,LOCK_EX);
  82. read(fd,&cn,sizeof(cn));
  83. printf("cn pre=%d\n",cn);
  84. if (cn=pmax)
  85. {
  86. sudoku[row][col]=i;
  87. printf("cn no fork=%d\n",cn);
  88. flock(fd,LOCK_UN);
  89. close(fd);
  90. if(col==SIZE-1) solveSudoku(row+1,0);
  91. else solveSudoku(row,col+1);
  92. sudoku[row][col]=0;
  93. //_exit(0);
  94. }
  95. else if (cn<pmax && cn>=0)
  96. {
  97. cn++;
  98. //flock(fd,LOCK_EX);
  99. write(fd,&cn,4);
  100. printf("cn after=%d\n",cn);
  101. flock(fd,LOCK_UN);
  102. close(fd);
  103. pid_t cpid;
  104. cpid=fork();
  105. if (cpid==-1)
  106. {
  107. sudoku[row][col]=i;
  108. if(col==SIZE-1) solveSudoku(row+1,0);
  109. else solveSudoku(row,col+1);
  110. sudoku[row][col]=0;
  111. }
  112. else
  113. if (cpid==0)
  114. {
  115. sudoku[row][col]=i;
  116. if(col==SIZE-1) solveSudoku(row+1,0);
  117. else solveSudoku(row,col+1);
  118. _exit(0);
  119. }
  120. else
  121. if (cpid>0) chn++;
  122. }//cn<pmax
  123. } //for all possible
  124. {
  125. //printf("chn=%d\n",chn);
  126. while(chn--){
  127. wait(&rev);
  128. //if (rev>0) printf("rev%d\n",rev);
  129. //if (tmp==1) _exit(1);
  130. if (WIFEXITED(rev))
  131. {
  132. tmp=WEXITSTATUS(rev);
  133. if (tmp==1)
  134. {
  135. anss=1;
  136. _exit(1);
  137. //break;
  138. }
  139. else
  140. if (tmp==0)
  141. {
  142. printf("*");
  143. fd=open("tmp",O_RDWR|O_CREAT);
  144. flock(fd,LOCK_EX);
  145. read(fd,&cn,sizeof(cn));
  146. printf("cnp=%d",cn);
  147. cn--;
  148. write(fd,&cn,sizeof(cn));
  149. printf("cnaf=%d",cn);
  150. flock(fd,LOCK_UN);
  151. close(fd);
  152. }
  153. }
  154. }//chn--
  155. }//parent process
  156. }
  157. }
  158.  
  159. int main(int argc,char* argv[])
  160. {
  161. int i,j,fd,fd2;
  162. for (i=0;i<=2;i++)
  163. {
  164. j=2*i+1;
  165. if (strcmp(argv[j],"-m")==0)
  166. {
  167. if (strcmp(argv[j+1],"vfork")==0) vf=1;
  168. else vf=0;
  169. }
  170. if (strcmp(argv[j],"-n")==0)
  171. {
  172. SIZE=atoi(argv[j+1]);
  173. }
  174. if (strcmp(argv[j],"-p")==0)
  175. {
  176. pmax=atoi(argv[j+1]);
  177. }
  178. }
  179. printf("vf=%d ",vf);
  180. printf("pmax=%d ",pmax);
  181. printf("SIZE=%d\n",SIZE);
  182. //scanf("%d",&SIZE);
  183. size=SIZE; SIZE*=SIZE;
  184. for (i=0;i<SIZE;i++)
  185. for (j=0;j<SIZE;j++)
  186. scanf("%d",&sudoku[i][j]);
  187. printSolution(); //Display inputted sudoku from the file
  188.  
  189.  
  190. i=0;
  191. fd2=open("tmp4",O_RDWR|O_CREAT);
  192. //flock(fd2,LOCK_EX);
  193. write(fd2,&i,4);
  194. printf("tmp4=%d\n",i);
  195. //flock(fd2,LOCK_UN);
  196. //close(fd2);
  197. //------------------
  198. /*
  199.   fd=open("tmp",O_RDWR|O_CREAT);
  200.   flock(fd,LOCK_EX);
  201.   write(fd,&i,sizeof(i));
  202.   read(fd,&i,4);
  203.   read(fd,&j,4);
  204.   printf("tmp=%d %d\n",i,j);
  205.   flock(fd,LOCK_UN);
  206.   close(fd);
  207.   char buf[100];
  208.   fd=open("tmp",O_RDWR|O_CREAT);
  209.   flock(fd,LOCK_EX);
  210.   read(fd,buf,4);
  211.   printf("tmp=%s\n",buf);
  212.   flock(fd,LOCK_UN);
  213.   close(fd);
  214.  
  215.   solveSudoku(0,0);*/
  216. return 0;
  217. }
  218.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘void solveSudoku(int, int)’:
prog.cpp:51: error: no matching function for call to ‘flock::flock(int&, int)’
/usr/include/bits/fcntl.h:145: note: candidates are: flock::flock()
/usr/include/bits/fcntl.h:145: note:                 flock::flock(const flock&)
prog.cpp:54: error: no matching function for call to ‘flock::flock(int&, int)’
/usr/include/bits/fcntl.h:145: note: candidates are: flock::flock()
/usr/include/bits/fcntl.h:145: note:                 flock::flock(const flock&)
prog.cpp:65: error: no matching function for call to ‘flock::flock(int&, int)’
/usr/include/bits/fcntl.h:145: note: candidates are: flock::flock()
/usr/include/bits/fcntl.h:145: note:                 flock::flock(const flock&)
prog.cpp:81: error: no matching function for call to ‘flock::flock(int&, int)’
/usr/include/bits/fcntl.h:145: note: candidates are: flock::flock()
/usr/include/bits/fcntl.h:145: note:                 flock::flock(const flock&)
prog.cpp:84: warning: suggest parentheses around assignment used as truth value
prog.cpp:88: error: no matching function for call to ‘flock::flock(int&, int)’
/usr/include/bits/fcntl.h:145: note: candidates are: flock::flock()
/usr/include/bits/fcntl.h:145: note:                 flock::flock(const flock&)
prog.cpp:101: error: no matching function for call to ‘flock::flock(int&, int)’
/usr/include/bits/fcntl.h:145: note: candidates are: flock::flock()
/usr/include/bits/fcntl.h:145: note:                 flock::flock(const flock&)
prog.cpp:144: error: no matching function for call to ‘flock::flock(int&, int)’
/usr/include/bits/fcntl.h:145: note: candidates are: flock::flock()
/usr/include/bits/fcntl.h:145: note:                 flock::flock(const flock&)
prog.cpp:150: error: no matching function for call to ‘flock::flock(int&, int)’
/usr/include/bits/fcntl.h:145: note: candidates are: flock::flock()
/usr/include/bits/fcntl.h:145: note:                 flock::flock(const flock&)
prog.cpp:52: warning: ignoring return value of ‘ssize_t read(int, void*, size_t)’, declared with attribute warn_unused_result
prog.cpp:63: warning: ignoring return value of ‘ssize_t write(int, const void*, size_t)’, declared with attribute warn_unused_result
prog.cpp:82: warning: ignoring return value of ‘ssize_t read(int, void*, size_t)’, declared with attribute warn_unused_result
prog.cpp:99: warning: ignoring return value of ‘ssize_t write(int, const void*, size_t)’, declared with attribute warn_unused_result
prog.cpp:145: warning: ignoring return value of ‘ssize_t read(int, void*, size_t)’, declared with attribute warn_unused_result
prog.cpp:148: warning: ignoring return value of ‘ssize_t write(int, const void*, size_t)’, declared with attribute warn_unused_result
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:161: warning: unused variable ‘fd’
prog.cpp:186: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
prog.cpp:193: warning: ignoring return value of ‘ssize_t write(int, const void*, size_t)’, declared with attribute warn_unused_result
stdout
Standard output is empty