Skip to main content
Removed a point that was repeated verbatim.
Source Link
  • mutex serves no purpose. It is not shared between processes, and your program is not multithreaded.

    If you want an exclusion between unrelated instances of the program, you need to place it in a named shared memory segment (see example).

  • // Don't need any semaphores here - writing processes write to a temp file then rename so no issues with reading

    Not so. First of all, every process writes to the same temp file (tempIndex.bin).

    After you fix this, there is still a race condition. Process P1 writes to temp1. Meanwhile, process P2 writes to temp2. After renaming, one of the edits is lost.

  • The line

      index = fopen("index.bin", "r");
    

    at the end of addIndexLine has no effect. The caller still has the original file pointer. BTW, after the call to addIndexLin the pointed file is closed, and cannot be used by a caller anymore. Doesn't look promising.

  • remove("data.bin") is superfluous. Just rename works as well.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • const-qualification of arguments is sorely missing.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • compareString... What's wrong with strncmp?

PS: I didn't dig deep into the design of the DB files. To be honest, I was quickly lost.

  • mutex serves no purpose. It is not shared between processes, and your program is not multithreaded.

    If you want an exclusion between unrelated instances of the program, you need to place it in a named shared memory segment (see example).

  • // Don't need any semaphores here - writing processes write to a temp file then rename so no issues with reading

    Not so. First of all, every process writes to the same temp file (tempIndex.bin).

    After you fix this, there is still a race condition. Process P1 writes to temp1. Meanwhile, process P2 writes to temp2. After renaming, one of the edits is lost.

  • The line

      index = fopen("index.bin", "r");
    

    at the end of addIndexLine has no effect. The caller still has the original file pointer. BTW, after the call to addIndexLin the pointed file is closed, and cannot be used by a caller anymore. Doesn't look promising.

  • remove("data.bin") is superfluous. Just rename works as well.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • const-qualification of arguments is sorely missing.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • compareString... What's wrong with strncmp?

PS: I didn't dig deep into the design of the DB files. To be honest, I was quickly lost.

  • mutex serves no purpose. It is not shared between processes, and your program is not multithreaded.

    If you want an exclusion between unrelated instances of the program, you need to place it in a named shared memory segment (see example).

  • // Don't need any semaphores here - writing processes write to a temp file then rename so no issues with reading

    Not so. First of all, every process writes to the same temp file (tempIndex.bin).

    After you fix this, there is still a race condition. Process P1 writes to temp1. Meanwhile, process P2 writes to temp2. After renaming, one of the edits is lost.

  • The line

      index = fopen("index.bin", "r");
    

    at the end of addIndexLine has no effect. The caller still has the original file pointer. BTW, after the call to addIndexLin the pointed file is closed, and cannot be used by a caller anymore. Doesn't look promising.

  • remove("data.bin") is superfluous. Just rename works as well.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • const-qualification of arguments is sorely missing.

  • compareString... What's wrong with strncmp?

PS: I didn't dig deep into the design of the DB files. To be honest, I was quickly lost.

added 136 characters in body
Source Link
vnp
  • 57.3k
  • 4
  • 51
  • 140
  • mutex serves no purpose. It is not shared between processes, and your program is not multithreaded.

    If you want an exclusion between unrelated instances of the program, you need to place it in a named shared memory segment (see example).

  • // Don't need any semaphores here - writing processes write to a temp file then rename so no issues with reading

    Not so. First of all, every process writes to the same temp file (tempIndex.bin).

    After you fix this, there is still a race condition. Process P1 writes to temp1. Meanwhile, process P2 writes to temp2. After renaming, one of the edits is lost.

  • The line

      index = fopen("index.bin", "r");
    

    at the end of addIndexLine has no effect. The caller still has the original file pointer. BTW, after the call to addIndexLin the pointed file is closed, and cannot be used by a caller anymore. Doesn't look promising.

  • remove("data.bin") is superfluous. Just rename works as well.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • const-qualification of arguments is sorely missing.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • compareString... What's wrong with strncmp?

PS: I didn't dig deep into the design of the DB files. To be honest, I was quickly lost.

  • mutex serves no purpose. It is not shared between processes, and your program is not multithreaded.

    If you want an exclusion between unrelated instances of the program, you need to place it in a named shared memory segment (see example).

  • // Don't need any semaphores here - writing processes write to a temp file then rename so no issues with reading

    Not so. Process P1 writes to temp1. Meanwhile, process P2 writes to temp2. After renaming, one of the edits is lost.

  • The line

      index = fopen("index.bin", "r");
    

    at the end of addIndexLine has no effect. The caller still has the original file pointer. BTW, after the call to addIndexLin the pointed file is closed, and cannot be used by a caller anymore. Doesn't look promising.

  • remove("data.bin") is superfluous. Just rename works as well.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • const-qualification of arguments is sorely missing.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • compareString... What's wrong with strncmp?

PS: I didn't dig deep into the design of the DB files. To be honest, I was quickly lost.

  • mutex serves no purpose. It is not shared between processes, and your program is not multithreaded.

    If you want an exclusion between unrelated instances of the program, you need to place it in a named shared memory segment (see example).

  • // Don't need any semaphores here - writing processes write to a temp file then rename so no issues with reading

    Not so. First of all, every process writes to the same temp file (tempIndex.bin).

    After you fix this, there is still a race condition. Process P1 writes to temp1. Meanwhile, process P2 writes to temp2. After renaming, one of the edits is lost.

  • The line

      index = fopen("index.bin", "r");
    

    at the end of addIndexLine has no effect. The caller still has the original file pointer. BTW, after the call to addIndexLin the pointed file is closed, and cannot be used by a caller anymore. Doesn't look promising.

  • remove("data.bin") is superfluous. Just rename works as well.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • const-qualification of arguments is sorely missing.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • compareString... What's wrong with strncmp?

PS: I didn't dig deep into the design of the DB files. To be honest, I was quickly lost.

added 92 characters in body
Source Link
vnp
  • 57.3k
  • 4
  • 51
  • 140
  • mutex serves no purpose. It is not shared between processes, and your program is not multithreaded.

    If you want an exclusion between unrelated instances of the program, you need to place it in a named shared memory segment (see example).

  • // Don't need any semaphores here - writing processes write to a temp file then rename so no issues with reading

    Not so. Process P1 writes to temp1. Meanwhile, process P2 writes to temp2. After renaming, one of the edits is lost.

  • The line

      index = fopen("index.bin", "r");
    

    at the end of addIndexLine has no effect. The caller still has the original file pointer. BTW, after the call to addIndexLin the pointed file is closed, and cannot be used by a caller anymore. Doesn't look promising.

  • remove("data.bin") is superfluous. Just rename works as well.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • const-qualification of arguments is sorely missing.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • compareString... What's wrong with strncmp?

PS: I didn't dig deep into the design of the DB files. To be honest, I was quickly lost.

  • mutex serves no purpose. It is not shared between processes, and your program is not multithreaded.

    If you want an exclusion between unrelated instances of the program, you need to place it in a named shared memory segment (see example).

  • // Don't need any semaphores here - writing processes write to a temp file then rename so no issues with reading

    Not so. Process P1 writes to temp1. Meanwhile, process P2 writes to temp2. After renaming, one of the edits is lost.

  • The line

      index = fopen("index.bin", "r");
    

    at the end of addIndexLine has no effect. The caller still has the original file pointer. BTW, after the call to addIndexLin the pointed file is closed, and cannot be used by a caller anymore. Doesn't look promising.

  • remove("data.bin") is superfluous. Just rename works as well.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • const-qualification of arguments is sorely missing.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • compareString... What's wrong with strncmp?

  • mutex serves no purpose. It is not shared between processes, and your program is not multithreaded.

    If you want an exclusion between unrelated instances of the program, you need to place it in a named shared memory segment (see example).

  • // Don't need any semaphores here - writing processes write to a temp file then rename so no issues with reading

    Not so. Process P1 writes to temp1. Meanwhile, process P2 writes to temp2. After renaming, one of the edits is lost.

  • The line

      index = fopen("index.bin", "r");
    

    at the end of addIndexLine has no effect. The caller still has the original file pointer. BTW, after the call to addIndexLin the pointed file is closed, and cannot be used by a caller anymore. Doesn't look promising.

  • remove("data.bin") is superfluous. Just rename works as well.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • const-qualification of arguments is sorely missing.

  • Don't spell out the program name in the help/error messages. Use argv[0].

  • compareString... What's wrong with strncmp?

PS: I didn't dig deep into the design of the DB files. To be honest, I was quickly lost.

Source Link
vnp
  • 57.3k
  • 4
  • 51
  • 140
Loading