please check sleep code.
+5
Christopher
DineshThangaraju
ARVINTH B.J
anand
Navitha
9 posters
please check sleep code.
#include<sys/stat.h>
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
int main(int agrc, char* argv[])
{
printf("HELLO");
for( ; ; )
{
printf("Thank you");
sleep(20);
}
return 0;
}
[335937@oracleclient ~]$ gcc slep.c
[335937@oracleclient ~]$ ./a.out
No error but the output is not displayed.
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
int main(int agrc, char* argv[])
{
printf("HELLO");
for( ; ; )
{
printf("Thank you");
sleep(20);
}
return 0;
}
[335937@oracleclient ~]$ gcc slep.c
[335937@oracleclient ~]$ ./a.out
No error but the output is not displayed.
Navitha- Posts : 14
Points : 24
Join date : 2010-02-26
Re: please check sleep code.
... its infinite loop ... its sleepin infinitely......
Last edited by anand on Fri Mar 12, 2010 1:24 pm; edited 1 time in total
anand- Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36
Re: please check sleep code.
#include<sys/stat.h>
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
int main(int agrc, char* argv[])
{
int i;
printf("HELLO");
for(i=0 ;i<5 ;i++ )
{
printf("Thank you");
sleep(1);
}
return 0;
}
this ll work.......
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
int main(int agrc, char* argv[])
{
int i;
printf("HELLO");
for(i=0 ;i<5 ;i++ )
{
printf("Thank you");
sleep(1);
}
return 0;
}
this ll work.......
anand- Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36
Re: please check sleep code.
@anand.
for( ; ; ) means only the loop will run infinitely and not sleep.
for( ; ; ) means only the loop will run infinitely and not sleep.
ARVINTH B.J- Posts : 20
Points : 31
Join date : 2010-02-26
Re: please check sleep code.
we have sleep command in infinite loop ... i mean it .....
anand- Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36
Re: please check sleep code.
- Code:
#include<fcntl.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/stat.h>
int main(int argc, char* argv[])
{
char buff[512];
int n;
n = read(0, buff, sizeof buff);
for(;;)
{
write(1,buff,n);
sleep(10);
}
return 0;
}
This works fine with infinite loop..
DineshThangaraju- Posts : 18
Points : 19
Join date : 2010-02-26
Age : 35
Location : India
Re: please check sleep code.
yeah it works fine..
anand- Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36
Re: please check sleep code.
ya the last code is working ..
Navitha- Posts : 14
Points : 24
Join date : 2010-02-26
Re: please check sleep code.
it gives more priority to signals.
@ dinesh
in ur example, read,write,sleep are interrupts. so it gives equal priority to all the calls. but printf is not a interrupt. tatsy sleep is executed first.
@ dinesh
in ur example, read,write,sleep are interrupts. so it gives equal priority to all the calls. but printf is not a interrupt. tatsy sleep is executed first.
Re: please check sleep code.
Christopher wrote:it gives more priority to signals.
@ dinesh
in ur example, read,write,sleep are interrupts. so it gives equal priority to all the calls. but printf is not a interrupt. tatsy sleep is executed first.
No, thats not correct.
Navitha's program works fine if u just include "\n" at the end of the printf statements.
Equal priority to printf and sleep.
- Code:
#include<sys/stat.h>
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
int main(int agrc, char* argv[])
{
printf("HELLO\n");
for( ; ; )
{
printf("Thank you\n");
sleep(20);
}
return 0;
}
itssrinath- Posts : 4
Points : 5
Join date : 2010-02-26
Re: please check sleep code.
Dint know \n has this much of significance :-P
ARVINTH B.J- Posts : 20
Points : 31
Join date : 2010-02-26
Re: please check sleep code.
as far as i hope!
there are 2 modes!
character by character updation and line by line updation!
first one does not use buffer and later uses buffer
buffer is displayed once it encounters a newline character or at the end of program .
so in the code of navitha the output produced will be HELLOThankyouHELLOThankyou..... and so on!
so the output is not displayed as the program does not end!
but displays HELLO & ThankYou if u add \n at the end as it encounters newline char
there are 2 modes!
character by character updation and line by line updation!
first one does not use buffer and later uses buffer
buffer is displayed once it encounters a newline character or at the end of program .
so in the code of navitha the output produced will be HELLOThankyouHELLOThankyou..... and so on!
so the output is not displayed as the program does not end!
but displays HELLO & ThankYou if u add \n at the end as it encounters newline char
Re: please check sleep code.
stdio is buffered dats why its not displaying nything. if it encounters ny escape character it display all d buffered content.
urvershi- Posts : 21
Points : 70
Join date : 2010-02-26
Re: please check sleep code.
if u don want to print a new line, add "fflush(stdout)" after printf stmt.
Re: please check sleep code.
- Code:
#include<sys/stat.h>
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
int main(int agrc, char* argv[])
{
printf("HELLO");
for( ; ; )
{
sleep(6);
printf("Thank you\n");
sleep(20);
printf("second line");
}
return 0;
}
hey ppl
use this code!
u will see that it will not print HELLO immediately but will wait for 6 sec then instantly will display HELLOThankYou
bcoz newline char is present in printf("Thankyou\n"); command
similerly the second lineThankyou in loop
Re: please check sleep code.
@urveshi
But is it the case for a code without sleep()?
Code with sleep :
int main()
{
printf("hello");
printf("jksad \n");
printf("hsagdh");
sleep(20);
}
The output for the code above would be
hellojksad ---->as it encountered a \n character.
And it didn't buffer out the contents of third printf().
Consider this code now without sleep and /n :
int main()
{
printf("dsf");
printf("sad");
}
If stdio doesn't buffer out until it recognizes an escape sequence.,this program shouldn't display anything but we know wat it would.
So there is something else with sleep() and \n we need to crack it out.
But is it the case for a code without sleep()?
Code with sleep :
int main()
{
printf("hello");
printf("jksad \n");
printf("hsagdh");
sleep(20);
}
The output for the code above would be
hellojksad ---->as it encountered a \n character.
And it didn't buffer out the contents of third printf().
Consider this code now without sleep and /n :
int main()
{
printf("dsf");
printf("sad");
}
If stdio doesn't buffer out until it recognizes an escape sequence.,this program shouldn't display anything but we know wat it would.
So there is something else with sleep() and \n we need to crack it out.
ARVINTH B.J- Posts : 20
Points : 31
Join date : 2010-02-26
Re: please check sleep code.
The "fflush(stdout)" command is not working.
Again no display.
Again no display.
Navitha- Posts : 14
Points : 24
Join date : 2010-02-26
Re: please check sleep code.
Sorry,I was wrong.
Printf buffers out wen it encounters a \n character. If not,it buffers out when the program terminates.
Printf buffers out wen it encounters a \n character. If not,it buffers out when the program terminates.
ARVINTH B.J- Posts : 20
Points : 31
Join date : 2010-02-26
Re: please check sleep code.
The "fflush(stdout)" command is not working.
Again no display.
include appropriate header files.
Re: please check sleep code.
ARVINTH B.J wrote:@urveshi
But is it the case for a code without sleep()?
Code with sleep :
int main()
{
printf("hello");
printf("jksad \n");
printf("hsagdh");
sleep(20);
}
The output for the code above would be
hellojksad ---->as it encountered a \n character.
And it didn't buffer out the contents of third printf().
look at this:
int main()
{
printf("hello");
printf("jksad \n");
printf("hsagdh");
sleep(5);
printf("hi back again");
}
[335802@oracleclient ~]$ ./a.out
hellojksad .....(after delay of 5sec it prints the second line)
hsagdhhi back again
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Permissions in this forum:
You cannot reply to topics in this forum
|
|