Fork

pid_t pid = fork();
 
if (pid<0) {
  // failed
}
 
if (pid == 0) {
  // child
} else {
  // parent
} 

likvidace zombie procesů

struct sigaction sa;
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT;
sigaction(SIGCHLD, &sa, NULL);
pid_t pid1, pid2;
pid1 = fork();
 
if (pid1 == -1) {
	// fork failed
	exit(1);
}
 
if (pid1 == 0) {
	// child
	pid2 = fork(); //second fork
	if (pid2 == -1) {
		// fork failed
		exit(1);
	} 
 
	if (pid2 == 0) {
		// child
		ret = system(cmd);
		exit(ret);
	} 
 
	// parent
	exit(0);
} 
 
//parent
waitpid(pid1, &ret, 0);