A resposta curta é: fork
está no Unix porque era fácil se encaixar no sistema existente na época e porque sistema predecessor em Berkeley usou o conceito de garfos.
De A Evolução do Sistema de Compartilhamento de Tempo Unix (texto relevante foi realçado ):
Process control in its modern form was designed and implemented within a couple of days. It is astonishing how easily it fitted into the existing system; at the same time it is easy to see how some of the slightly unusual features of the design are present precisely because they represented small, easily-coded changes to what existed. A good example is the separation of the fork and exec functions. The most common model for the creation of new processes involves specifying a program for the process to execute; in Unix, a forked process continues to run the same program as its parent until it performs an explicit exec. The separation of the functions is certainly not unique to Unix, and in fact it was present in the Berkeley time-sharing system, which was well-known to Thompson. Still, it seems reasonable to suppose that it exists in Unix mainly because of the ease with which fork could be implemented without changing much else. The system already handled multiple (i.e. two) processes; there was a process table, and the processes were swapped between main memory and the disk. The initial implementation of fork required only
1) Expansion of the process table
2) Addition of a fork call that copied the current process to the disk swap area, using the already existing swap IO primitives, and made some adjustments to the process table.
In fact, the PDP-7's fork call required precisely 27 lines of assembly code. Of course, other changes in the operating system and user programs were required, and some of them were rather interesting and unexpected. But a combined fork-exec would have been considerably more complicated, if only because exec as such did not exist; its function was already performed, using explicit IO, by the shell.
Desde aquele papel, o Unix evoluiu. fork
seguido por exec
não é mais a única maneira de executar um programa.
-
O
-
vfork foi criado para ser uma bifurcação mais eficiente para o caso em que o O novo processo pretende fazer um exec logo após o fork. Depois de executar um vfork, os processos pai e filho compartilham o mesmo espaço de dados e o processo pai é suspenso até que o processo filho execute um programa ou saia.
-
posix_spawn cria um novo processo e executa um arquivo em uma única chamada de sistema . São necessários vários parâmetros que permitem compartilhar seletivamente os arquivos abertos do chamador e copiar sua disposição de sinal e outros atributos para o novo processo.