Scanf не работает в отчете - PullRequest
0 голосов
/ 09 августа 2010
#include <stdio.h>
#include <unistd.h>
//#include <iostream.h>
#include <sys/types.h>
#include <sys/wait.h>   
#include <sys/types.h>

int t_array[100];
int h_array[100];
int race_length=0;

void cursor(int y)
{
    int i;
    printf("%c[%d;%df",0x1B,y,0);
}

void turtle_fun()
{   
    int Ti=0;

    while(Ti<=race_length-1)
    {
    //cursor(10);       
    //  printf("t      ");
    //  fflush(stdout);     
        Ti++;
        sleep(3);
    }

}

void hare_fun(int rh[])
{
    int k;
    int i=0;
    char pos_h;

    while(i<=race_length-1)
    {
    //  cursor(5);
//printf("h ");
//  fflush(stdout);
        read(rh[0],&pos_h,1);
        if(pos_h==1) write(rh[1],&i,1);
        i++;
        sleep(1);
    }

}

void god_fun(pid_t id)
{
}

void report_fun(int rh[],int rg[],int rt[])
{   
    int k,m,pos;
    int pos_h,pos_t;

    close(rh[1]);

    if(k=fork()==0) hare_fun(rh);
    else
    {
        if(fork()==0) turtle_fun();
        else
        {       
            printf("press 1 to know current position \n");
            fflush(stdout);
            scanf("%d\n",&pos);

            if(pos==1) write(rh[1],&pos,1);

             read(rh[0],&pos_h,1);
             printf("H%d|T\n",pos_h);
             printf("j");

             //while(h_comp!=1||c!=1);*/
         }

    }

}

void main()
{
    int rg[2],rh[2],rt[2],gh[2],gt[2],ht[2];
    int child_id;
    pid_t cpid;

    printf("what is the length of the race");
    scanf("%d",&race_length);
    cpid=fork();

    if(cpid==0)
    {
        pipe(rg);
        pipe(rh);
        pipe(rt);
        report_fun(rh,rg,rt);
    }
    else
    {       
        pipe(gh);
        pipe(gt);
        god_fun(cpid);
    }

}   

1 Ответ

0 голосов
/ 09 августа 2010

Не включайте \n в строку формата scanf(), это может привести к тому, что ваша программа будет ожидать два ввода.

Так что в report_fun() изменить

scanf("%d\n",&pos);

до

scanf("%d",&pos);

Обновление

В main() вы вызываете report_fun () в дочернем процессе вместо родительского. Возможно, вы захотите отменить тест if (cpuid == 0).

И, пожалуйста, используйте int main(void) вместо void main() и завершите основную функцию с помощью return 0;.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...