.
Çatal tamamen yeni bir işlem oluşturur ve çağrı işlemine pid
alt sürecini ve yeni alt işlem için 0
değerini döndürür.
Çocuk işleminde, istediğiniz ikinci programı yürütmek için execl
gibi bir şey kullanabilirsiniz. ebeveyn süreçte tamamlamak çocuk için beklemek waitpid
kullanabilirsiniz.
#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
int main()
{
std::string cmd = "/bin/ls"; // secondary program you want to run
pid_t pid = fork(); // create child process
int status;
switch (pid)
{
case -1: // error
perror("fork");
exit(1);
case 0: // child process
execl(cmd.c_str(), 0, 0); // run the command
perror("execl"); // execl doesn't return unless there is a problem
exit(1);
default: // parent process, pid now contains the child pid
while (-1 == waitpid(pid, &status, 0)); // wait for child to complete
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
{
// handle error
std::cerr << "process " << cmd << " (pid=" << pid << ") failed" << std::endl;
}
break;
}
return 0;
}
i bir çatal() i) (çatal yaptığınızda, bana bir Fatal IO hatası 11 vermek yapmaya çalışıyor takip hata alıyorum (Kaynak: İşte
basit örnektir X, bazen (Başarı) veya (böyle bir dosya veya dizin yok) – user1265478http://advancedlinuxprogramming.com/~~dobj (http://advancedlinuxprogramming.com/) –