why it does not sleep?

Discussion in 'C++' started by JosephWu, Sep 9, 2004.

  1. JosephWu

    JosephWu Guest

    Hi all.

    I have the following code and the 1000 numbers has stored before calling
    fifo(),in fifo,why it does not sleep for 1 sec ?it just stop there,but
    if i delete sleep(1) ,the number will successfully printed on screen.

    Thanks a lot guys!

    ==========================================================

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    int n[1000];//copy of 1000 numbers
    //============================================

    //fcfs,fifo:first in first out
    void fifo(void){
    int reqinter[1000];
    for (int i=0;i<1000;i++){
    printf("%d ",n);
    sleep(1);
    }
    }

    //sst:shortest seek time
    void sst(void){

    }

    //elev:elevator
    void elev(void){

    }

    //cscan:circular scan
    void cscan(void){

    }

    //============================================

    //============================================
    int main (int argc,char* argv[]) {
    //=========get input=================
    for(int i=0;i<1000;i++){
    scanf("%d",&n);

    }

    //==========begin to request==========

    fifo();
    sst();
    elev();
    cscan();

    //====================================
    return 0;
    }

    ================================================================
     
    JosephWu, Sep 9, 2004
    #1
    1. Advertising

  2. "JosephWu" <> wrote in message
    news:Xns955FF1E6D8C73wutongjoe@202.180.64.19...
    > Hi all.
    >
    > I have the following code and the 1000 numbers has stored before calling
    > fifo(),in fifo,why it does not sleep for 1 sec ?it just stop there,but
    > if i delete sleep(1) ,the number will successfully printed on screen.
    >


    You probably need to flush the standard output to make sure that the output
    occurs immediately. See below.

    > Thanks a lot guys!
    >
    > ==========================================================
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <unistd.h>
    >
    > int n[1000];//copy of 1000 numbers
    > //============================================
    >
    > //fcfs,fifo:first in first out
    > void fifo(void){
    > int reqinter[1000];
    > for (int i=0;i<1000;i++){
    > printf("%d ",n);


    Add this

    fflush(stdout);

    > sleep(1);
    > }
    > }
    >


    john
     
    John Harrison, Sep 9, 2004
    #2
    1. Advertising

  3. JosephWu

    Moonlit Guest

    Hi,

    "JosephWu" <> wrote in message
    news:Xns955FF1E6D8C73wutongjoe@202.180.64.19...
    > Hi all.
    >
    > I have the following code and the 1000 numbers has stored before calling
    > fifo(),in fifo,why it does not sleep for 1 sec ?it just stop there,but
    > if i delete sleep(1) ,the number will successfully printed on screen.
    >
    > Thanks a lot guys!
    >
    > ==========================================================
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <unistd.h>
    >
    > int n[1000];//copy of 1000 numbers
    > //============================================
    >
    > //fcfs,fifo:first in first out
    > void fifo(void){
    > int reqinter[1000];
    > for (int i=0;i<1000;i++){
    > printf("%d ",n);
    > sleep(1);
    > }
    > }


    Check the return value of sleep

    while( sleep(1) );
    Though I am not sure what happens with a one second sleep.

    sleep can be interrupted on most unix systems by an interrupt and will then
    return even if the time hasn't expired!
    It will return the seconds that is has to go

    Regards, Ron AF Greve.

    >
    > //sst:shortest seek time
    > void sst(void){
    >
    > }
    >
    > //elev:elevator
    > void elev(void){
    >
    > }
    >
    > //cscan:circular scan
    > void cscan(void){
    >
    > }
    >
    > //============================================
    >
    > //============================================
    > int main (int argc,char* argv[]) {
    > //=========get input=================
    > for(int i=0;i<1000;i++){
    > scanf("%d",&n);
    >
    > }
    >
    > //==========begin to request==========
    >
    > fifo();
    > sst();
    > elev();
    > cscan();
    >
    > //====================================
    > return 0;
    > }
    >
    > ================================================================
     
    Moonlit, Sep 9, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gonzalo Moreno
    Replies:
    2
    Views:
    12,437
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    909
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,996
    Smokey Grindel
    Dec 2, 2006
  4. Sam Roberts
    Replies:
    16
    Views:
    381
    Daniel Berger
    Feb 18, 2005
  5. Richard
    Replies:
    7
    Views:
    234
    Richard
    May 22, 2007
Loading...

Share This Page