why it does not sleep?

J

JosephWu

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;
}

================================================================
 
J

John Harrison

JosephWu said:
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
 
M

Moonlit

Hi,

JosephWu said:
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;
}

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top