Jump to content
Fórum Script Brasil
  • 0

Estou em duvidas neste codigo [ FORK ]


CyberLACS

Question

Amigos,boa noite estou em duvidas referente ao codigo abaixo

a parte que estou em duvida é referente ao fork o que ele faz ao ser executado

Observação estou a um tempo tentando descobrir e somente agora resolvi postar

fico no aguardo obrigado

#include <stdio.h> 
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#inc :huh: lude <stdlib.h>
#define MAXPROCESS 10
int main(void) {
  int pids[MAXPROCESS], pid, qtde, live, i;
  char state[MAXPROCESS], action;
  do {
    printf("Qtos processos deseja criar [0 para sair]: ");
    scanf("%d",&qtde);
    action=getchar();
    if (qtde==0)
      exit(1);
  } while(qtde>MAXPROCESS);
  for(i=0;i<qtde;i++) {
    pids[i]=fork();
    if(pids[i]==0)
      while(1);
    state[i]='R';
  }

  live=qtde;
  while(live>0)
  {
    for(i=0;i<qtde;i++)
        {
            if(state[i]!='K')
                printf("%d - %c\n",pids[i],state[i]);
        }
        printf("Selecione o processo (0 para sair): ");
 scanf("%d",&pid);
        action=getchar();
        if(pid==0)
                exit(1);
        i=0;
        while((pids[i]!=pid) && (i<qtde))
                i++;
        if (pids[i]!=pid)
                printf("Processo não encontrado\n");
J            else
            {
                if (state[i]=='S')
                        printf("Selecione a acao [K]ill [R]un [C]ancel: ");
                        else
                           printf("Selecione a acao [K]ill [S]top [C]ancel: ");
                           action=getchar();
                           if (action>96)
                                action-=32;
                                switch(action)
                                {
                                        case 'K':
                                                kill(pid,SIGKILL);
                                                live--;
                                                state[i]='K';
                                                break;
                                        case 'R':
                                                kill(pid,SIGCONT);
                                                state[i]='R';
                                                break;
                                        case 'S':
                                                  kill(pid,SIGSTOP);
                                                  state[i]='S';
                                                  break;

Edited by Durub
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...