Jump to content

Funçao fork() em c Sistemas Operativos (Linux)


raider

Recommended Posts

Será que alguém me conseguirá ajudar a responder à seguinte questão:

Quantas vezes Imprime a mensagem SISTEMAS OPERATIVOS, no seguinte programa e porquê?

Código:

for(i=0;i<3;i++)
{
    pid = fork();

    if(pid ==0)
         pid = fork();

    if(pid > 0)
         printf("SISTEMAS OPERATIVOS");
}
Edited by pwseo
tags code, syntax highlight.
Link to comment
Share on other sites

isto é uma questão muito específica, tens como confirmar que não estás a fazer um exame ?

dica :

- o thread principal corre o ciclo 3 vezes

> escreve a mensagem 3 vezes

> cria 3 thread

- cada thread filho directo do thread principal executa o ciclo 2 vezes (i = 1 até 3)

> escreve a mensagem 2 vezes

> cria 2 threads

... etc, etc ...

Edited by HappyHippyHippo
IRC : sim, é algo que ainda existe >> #p@p
Link to comment
Share on other sites

Eu compilei o programa e ele imprime 26 vezes a mensagem "SISTEMAS OPERATIVOS"

E se executar o mesmo programa mas com um execlp().

Apenas irá imprimir uma vez certo, pois o comando execlp(), apagará tudo o que está antes de ser chamado???

for(i=0;i<3;i++)
{
   pid = fork();

   if(pid ==0)
       pid = fork();

   if(pid > 0)
       execlp("SO", "SO",0);
}
Edited by pwseo
tags code, syntax highlight.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.