28 Nisan 2020 Salı

fork metodu ve thread - Yeni Yaratılan Uygulamada Tek Thread Olur

Giriş
fork() çağrısından sonra yeni yaratılan uygulamada sadece tek bir thread vardır. Bu thread de fork() çağrısını yapan ilk uygulamadaki thread'dir. Bu konuyu daha detaylı açıklayan bir yazıyı burada bulabilirsiniz. Bir başka açıklama ise burada var.

Zaten man fork sayfasında da şu cümle geçiyor.
The child process is created with a single thread — the one that called fork().
POSIX açıklaması ise şöyle
A process shall be created with a single thread. If a multi-threaded process calls fork(), the new process shall contain a replica of the calling thread and its entire address space, possibly including the states of mutexes and other resources. Consequently, to avoid errors, the child process may only execute async-signal-safe operations until such time as one of the exec functions is called.
Örnek - double fork
Şöyle yaparız. Daemon haline getirmenin yollarından birisi. Açıklaması burada
void spawn(const Arg arg) {
  if(fork() == 0) {
    if(fork() == 0) {
      if(dis)
        close(ConnectionNumber(dis));

      setsid();
      execvp((char*)arg.com[0],(char**)arg.com);
    }
    exit(0);
  }
}


Hiç yorum yok:

Yorum Gönder