Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/fork-test.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / fork-test.c` 310 lines ยท 9832 bytes Raw Blame History 1 /* 2 ** The program demonstrates how a child process created using fork() 3 ** can continue to use SQLite for a database that the parent had opened and 4 ** and was writing into when the fork() occurred. 5 ** 6 ** This program executes the following steps: 7 ** 8 ** 1. Create a new database file. Open it, and populate it. 9 ** 2. Start a transaction and make changes. 10 ** ^-- close the transaction prior to fork() if --commit-before-fork 11 ** 3. Fork() 12 ** 4. In the child, close the database connection. Special procedures 13 ** are needed to close the database connection in the child. See the 14 ** implementation below. 15 ** 5. Commit the transaction in the parent. 16 ** 6. Verify that the transaction committed in the parent. 17 ** 7. In the child, after a delay to allow time for (5) and (6), 18 ** open a new database connection and verify that the transaction 19 ** committed by (5) is seen. 20 ** 8. Add make further changes and commit them in the child, using the 21 ** new database connection. 22 ** 9. In the parent, after a delay to account for (8), verify that 23 ** the new transaction added by (8) can be seen. 24 ** 25 ** Usage: 26 ** 27 ** fork-test FILENAME [options] 28 ** 29 ** Options: 30 ** 31 ** --wal Run the database in WAL mode 32 ** --vfstrace Enable VFS tracing for debugging 33 ** --commit-before-fork COMMIT prior to the fork() in step 3 34 ** --delay-after-4 N Pause for N seconds after step 4 35 ** 36 ** How To Compile: 37 ** 38 ** gcc -O0 -g -Wall -I$(SQLITESRC) \ 39 ** f1.c $(SQLITESRC)/ext/misc/vfstrace.c $(SQLITESRC/sqlite3.c \ 40 ** -ldl -lpthread -lm 41 ** 42 ** Test procedure: 43 ** 44 ** (1) Run "fork-test x1.db". Verify no I/O errors occur and that 45 ** both parent and child see all three rows in the t1 table. 46 ** 47 ** (2) Repeat (1) adding the --wal option. 48 ** 49 ** (3) Repeat (1) and (2) adding the --commit-before-fork option. 50 ** 51 ** (4) Repeat all prior steps adding the --delay-after-4 option with 52 ** a timeout of 15 seconds or so. Then, while both parent and child 53 ** are paused, run the CLI against the x1.db database from a separate 54 ** window and verify that all the correct file locks are still working 55 ** correctly. 56 ** 57 ** Take-Aways: 58 ** 59 ** * If a process has open SQLite database connections when it fork()s, 60 ** the child can call exec() and all it well. Nothing special needs 61 ** to happen. 62 ** 63 ** * If a process has open SQLite database connections when it fork()s, 64 ** the child can do anything that does not involve using SQLite without 65 ** causing problems in the parent. No special actions are needed in 66 ** the child. 67 ** 68 ** * If a process has open SQLite database connections when it fork()s, 69 ** the child can call sqlite3_close() on those database connections 70 ** as long as there were no pending write transactions when the fork() 71 ** occurred. 72 ** 73 ** * If a process has open SQLite database connections that are in the 74 ** middle of a write transaction and then the processes fork()s, the 75 ** child process should close the database connections using the 76 ** procedures demonstrated in Step 4 below before trying to do anything 77 ** else with SQLite. 78 ** 79 ** * If a child process can safely close SQLite database connections that 80 ** it inherited via fork() using the procedures shown in Step 4 below 81 ** even if the database connections were not involved in a write 82 ** transaction at the time of the fork(). The special procedures are 83 ** required if a write transaction was active. They are optional 84 ** otherwise. No harm results from using the special procedures when 85 ** they are not necessary. 86 ** 87 ** * Child processes that use SQLite should open their own database 88 ** connections. They should not attempt to use a database connection 89 ** that is inherited from the parent. 90 */ 91 #include 92 #include 93 #include 94 #include 95 #include 96 #include 97 #include 98 #include "sqlite3.h" 99 100 /* 101 ** Process ID of the parent 102 */ 103 static pid_t parentPid = 0; 104 105 /* 106 ** Return either "parent" or "child", as appropriate. 107 */ 108 static const char *whoAmI(void){ 109 return getpid()==parentPid ? "parent" : "child"; 110 } 111 112 /* 113 ** This is an sqlite3_exec() callback routine that prints all results. 114 */ 115 static int execCallback(void *pNotUsed, int nCol, char **aVal, char **aCol){ 116 int i; 117 const char *zWho = whoAmI(); 118 for(i=0; ipMethods && pJrnl->pMethods->xFileControl ){ 251 pJrnl->pMethods->xFileControl(pJrnl, SQLITE_FCNTL_NULL_IO, 0); 252 } 253 } 254 sqlite3_close(db); 255 /* 256 ** End of special close procedures for SQLite database connections 257 ** inherited via fork(). 258 ***********************************************************************/ 259 260 printf("%s: database connection closed\n", whoAmI()); fflush(stdout); 261 }else{ 262 /* Pause the parent briefly to give the child a chance to close its 263 ** database connection */ 264 sleep(1); 265 } 266 267 if( nDelayAfter4>0 ){ 268 printf("%s: Delay for %d seconds\n", whoAmI(), nDelayAfter4); 269 fflush(stdout); 270 sleep(nDelayAfter4); 271 printf("%s: Continue after %d delay\n", whoAmI(), nDelayAfter4); 272 fflush(stdout); 273 } 274 275 /** Step 5 **/ 276 if( child!=0 ){ 277 printf("Step 5:\n"); 278 if( !bCommitBeforeFork ) sqlExec(db, "COMMIT", 0); 279 sqlExec(db, "SELECT x FROM t1;", 1); 280 } 281 282 283 /** Step 7 **/ 284 if( child==0 ){ 285 sleep(2); 286 printf("Steps 7 and 8:\n"); 287 rc = sqlite3_open(zFilename, &db); 288 if( rc ){ 289 printf("Child unable to reopen the database. rc = %d\n", rc); 290 exit(1); 291 } 292 sqlExec(db, "SELECT * FROM t1;", 1); 293 294 /** Step 8 **/ 295 sqlExec(db, "INSERT INTO t1 VALUES('Third row');", 0); 296 sqlExec(db, "SELECT * FROM t1;", 1); 297 sleep(1); 298 return 0; 299 } 300 c2 = wait(&status); 301 printf("Process %d finished with status %d\n", c2, status); 302 303 /** Step 9 */ 304 if( child!=0 ){ 305 printf("Step 9:\n"); 306 sqlExec(db, "SELECT * FROM t1;", 1); 307 } 308 309 return 0; 310 }