Named pipe
(たとえば、同じ共有メモリを使用している 2 つの独立した無関係なプロセス)を実装しようとしている間、私は使用pthread_atfork
したい と を読み続けますatexit
。
私はミューテックスとセマフォの使用に完全に同意します。これらを使用して、いつ読み取り/書き込みを行うか、process A
いつ読み取り/書き込みを行うかを決定できprocess B
ます。
pthread_atfork
しかし、どのような理由でスレッドを使用したいのでしょうか?
編集:
セマフォを使用しないと非常にコストがかかる例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/times.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <assert.h>
// Simple busy-wait loop to throw off our timing.
void busywait(void)
{
clock_t t1 = times(NULL);
while (times(NULL) - t1 < 2);
}
int main(int argc, char *argv[])
{
const char *message = "Hello World\n";
int n = strlen(message) / 2;
pid_t pid = fork();
int i0 = (pid == 0) ? 0 : n;
int i;
for (i = 0; i < n; i++) {
write(1, message + i0 + i, 1);
busywait();
}
}