int pid;
int fd;
int main()
{
int defout = dup(1);
fd=open("/prova/out.txt", O_RDWR|O_CREAT);
dup2(fd, 1); // redirect output to the file
pid = fork(); // create child
if (!pid) // if pid==0 then its a child process
{
close(fd);
close(defout);
printf("STAMPA1\n");
# <<< Missing an exit() here
}
# This part is executed twice, once by each process
dup2(defout, 1); // redirect output back to stdout
close(fd);
close(defout);
waitpid(pid, 0, 0); // wait until child finishes
printf("STAMPA2\n");
}