System FAQ - goofycoder/knowdb GitHub Wiki

/*
Dekkar's algorithm

Free from
    - deadlock
    - starvation
*/
volatile bool flag[2] = {false, false};
volatile int turn = 0;  // or 1, the initial value does not matter

void proc_0()
{
    flag[0] = true;

    while (flag[1] == true) {
    if (turn != 0) {
        flag[0] = false;

        while(turn!=0) {
            // busy wait
        }

        flag[0] = true;
    }
}

// critical section starts
....

turn = 1;
flag[0] = false;
// critical section ends

...  // proc_0 proceeds

}

proc_1() { flag[1] = true;

if(turn == 0) {
    flag[1] = false;

    while (turn != 1) {
        // busy wait
    }

    flag[1] = true;
}

// critical section starts
...

turn = 0;
flag[1] = fale;
// critical section ends

... // proc_1 proceeds

}

================================================= VIM: fix the ident:

gg=G

Q: How to write to syslog and check the syslog?

A: The syslog is at /var/log/syslog Write to syslog:

  #include <syslog.h>
  syslog(LOG_INFO, "hello");

Q: How to send signal to a process?

A: Using 'kill' command.

$kill -l           // list all the signals
$kill -TERM 7777   // send SIGTERM to process 7777 
-9: SIGKILL   // sent to a process to cause it to terminate immediately (kill). 
              // In contrast to SIGTERM and SIGINT, this signal cannot be caught or ignored
              // the receiving process cannot perform any clean-up upon receiving this signal.
SIGPIPE       // sent to a process when it attempts to write to a pipe without a process 
              // connected to the other end.
SIGSEGV       // sent to a process when it makes an invalid virtual memory reference, 
              // or segmentation fault, i.e. when it performs a segmentation violation.
SIGTERM       // Unlike the SIGKILL signal, it can be caught and interpreted or ignored by the process.
SIGINT        // sent to a process by its controlling terminal when a user wishes to interrupt the process. 
              // This is typically initiated by pressing Ctrl-C

Q: Is memcpy() thread-safe?

A: No, memcpy is typically made for speed. If you need thread-safe memcpy(), put it in the critical section guarded by mutex.

lock(&mutex);
memcpy(dst, src, count);
unlock(&mutex);`
⚠️ **GitHub.com Fallback** ⚠️