Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

please check sleep code.

+5
Christopher
DineshThangaraju
ARVINTH B.J
anand
Navitha
9 posters

Go down

please check sleep code. Empty please check sleep code.

Post by Navitha Fri Mar 12, 2010 1:10 pm

#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.

Navitha

Posts : 14
Points : 24
Join date : 2010-02-26

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by anand Fri Mar 12, 2010 1:11 pm

... its infinite loop ... its sleepin infinitely......


Last edited by anand on Fri Mar 12, 2010 1:24 pm; edited 1 time in total
anand
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by anand Fri Mar 12, 2010 1:17 pm

#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.......
anand
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by ARVINTH B.J Fri Mar 12, 2010 1:20 pm

@anand.

for( ; ; ) means only the loop will run infinitely and not sleep.

ARVINTH B.J

Posts : 20
Points : 31
Join date : 2010-02-26

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by anand Fri Mar 12, 2010 1:25 pm

we have sleep command in infinite loop ... i mean it .....
anand
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by DineshThangaraju Fri Mar 12, 2010 1:26 pm

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

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by anand Fri Mar 12, 2010 1:32 pm

yeah it works fine..
anand
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by Navitha Fri Mar 12, 2010 1:39 pm

ya the last code is working ..

Navitha

Posts : 14
Points : 24
Join date : 2010-02-26

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by Christopher Fri Mar 12, 2010 1:39 pm

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.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by itssrinath Fri Mar 12, 2010 1:53 pm

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

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by Christopher Fri Mar 12, 2010 1:55 pm

its k.. but how??? why?? confused confused
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by ARVINTH B.J Fri Mar 12, 2010 1:59 pm

Dint know \n has this much of significance :-P

ARVINTH B.J

Posts : 20
Points : 31
Join date : 2010-02-26

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by mightyganesh Fri Mar 12, 2010 2:03 pm

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
mightyganesh
mightyganesh

Posts : 53
Points : 92
Join date : 2010-02-26
Age : 35

http://ganeshguna.blogspot.com

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by urvershi Fri Mar 12, 2010 2:03 pm

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

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by Christopher Fri Mar 12, 2010 2:05 pm

if u don want to print a new line, add "fflush(stdout)" after printf stmt.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by mightyganesh Fri Mar 12, 2010 2:08 pm

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
mightyganesh
mightyganesh

Posts : 53
Points : 92
Join date : 2010-02-26
Age : 35

http://ganeshguna.blogspot.com

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by ARVINTH B.J Fri Mar 12, 2010 2:18 pm

@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.

ARVINTH B.J

Posts : 20
Points : 31
Join date : 2010-02-26

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by Navitha Fri Mar 12, 2010 2:24 pm

The "fflush(stdout)" command is not working.
Again no display.

Navitha

Posts : 14
Points : 24
Join date : 2010-02-26

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by ARVINTH B.J Fri Mar 12, 2010 2:29 pm

Sorry,I was wrong.

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

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by Christopher Fri Mar 12, 2010 2:35 pm

The "fflush(stdout)" command is not working.
Again no display.

include appropriate header files.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by akalya Fri Mar 12, 2010 3:57 pm

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

Back to top Go down

please check sleep code. Empty Re: please check sleep code.

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum