I'm a little bit confused how does the function pause() works in the sense of calling order. For example:
int main(){
printf("Start\n");
pause();
printf("Finish\n");
}
I was expecting to get output "Start" before pause, but program just immediately pauses instead. Please, explain why. Thanks in advance.
Your terminal emulator doesn't apparently flush standard output on every newline. You can manually flush it with fflush(stdout) if you want to see the output immediately:
int main(){
printf("Start\n");
fflush(stdout);
pause();
printf("Finish\n");
}
Related
I am trying to change the default behaviour of the terminal when I press ctrl+S (freeze) and ctrl+Q (unfreeze). My code is:
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
void sig_handler(int signum){
printf("\nInside handler function\n");
}
int main(){
signal(SIGSTOP, sig_handler);
for(int i=1;;i++){
printf("%d : Inside main function\n",i);
sleep(1);
}
return 0;
}
Before I run the code I disable the default behaviour by giving this command: stty -ixon -ixoff.
The problem is that when I run the code and press ctrl+S nothing happens, the characters ^S just get printed on the screen. Can I have some guidance oh how to do this or what to look for more information? Thanks.
1 The signals SIGKILL and SIGSTOP cannot be caught or ignored
2 maybe using tcsetattr is a way for resolving your problem. modify characters for VSTART/VSTOP, then process ctrl+S by your program
I used the command "clear" in C language at infinite loop just for test and the kill signal does not stop it.
Why the program does not stop when i press Ctrl + C ?
#include <stdlib.h>
int main () {
while (1) {
system("clear");
}
return 0;
}
Killing clear will not help, because the infinite loop is in your own program. Kill that.
ControlC is only useful if you can get the system to pay attention. It sounds as if your terminal is too busy writing to the screen to do this.
From another terminal, run top and kill your program (which likely is near the top of the screen).
You could include a signal handler to 'catch' your CRTL-C input (which is SIGINT below) and then exit. Hopefully, this stub code will give you the idea. If not, I can provide a complete working example but would like to see you try first. :-)
#include <signal.h>
...
void stop (int signal) {
exit;
}
int main() {
signal(SIGINT, stop);
...
}
HTH
I m doing some C program using fork()
But I'm getting unexpected output.
#include <stdio.h>
main(){
printf("\nStart of Program");
fork();
printf("\nEnd of Program");
}
Output:
Start of Program
End of ProgramStart of Program
End of Program
Why do I get this output ?
Output should be this:
Start of Program
End of Program
End of Program
This issue is happening because of buffering.
Quoting this post.
When the output of your program is going to a terminal (screen), it is line buffered. When the output of your program goes to a pipe, it is fully buffered.
The posts also contains line by line explanation.
This is because of printf(), printf is buffering the output and only print when he reaches a '\n', so when reaching printf("\nEnd of Program"); printf() prints the content of the buffer which is "Start of Program".
Because fork() duplicates your stack, printf() buffer is duplicated too.
Try:
#include<stdio.h>
main(){
printf("Start of Program\n");
fork();
printf("End of Program\n");
}
Few things here need to check.
From where it is printing "student". I do not see any where you are printing student in your code.
The ideal output is
Start of Program
End of Program
End of Program
If you are not getting above output I will suspect, when fork executed and child got created probably your standard output print was not complete. Flush or \n as last char in print is a must to complete the print job,
To verify this what you can do is, before fork just use fflush.
Also in printf use \n as last char.
#include<stdio.h>
main()
{
printf("Start of Program\n");
fflush(NULL);
fork();
printf("End of Program\n");
}
Try this and let us know.
when fork is come then new process is created.
so end of program before fork call process and after fork call is printed. so that's logic.
I compiled that code myself, and I get following output:
Start of Program
End of Program
Start of Program
End of Program
Which is correct since fork() spawns exactly the same child process its forked from.
Edit: Thanks to the comments, I found out whats really going on. Fork behaves as expected, but the input buffer is not getting flushed before the child process is executed. I've modified the original code to this:
#include <stdio.h>
main(){
printf("\nStart of Program\n");
fflush(stdout);
fork();
printf("\nEnd of Program\n");
}
And the output now reads:
Start of Program
End of Program
End of Program
Which indicates that indeed, fork() only spawns the child process executing downwards from after the fork() call.
I have write a program by C and run it in Ubuntu,the main code is follow:
int main(){
pid_t pid=fork();
if(pid==0){
printf("d");
exit(0);
}
else{
printf("a");
sleep(4);
}
}
The question is: why the code sleep(4); run before printf("a");
hope someone can give me a answer,thanks!
It is not. Most probably, printf() buffered its output until a chance to output the buffer (in your case, when the process terminated).
Q: why the code sleep(4) run before printf("a")?
A: printf("a") actually runs BEFORE "sleep(4)", just like you see in the code.
However, it doesn't DISPLAY immediately.
The issue is "buffering".
Look here for more details:
buffering behaviour of stdout in c
http://www.pixelbeat.org/programming/stdio_buffering/
SUGGESTED ALTERNATIVE:
pid_t pid=fork();
if(pid==0){
fprintf(stderr, "d");
exit(0);
}
else{
fprintf(stderr, "a");
sleep(4);
}
The reason is that "stderr" is unbuffered: you'll see the output immediately.
Alternatives to using stderr include calling fflush(stdout) or modifying the stream.
It's not, but it may appear that way. printf puts its output into a buffer that probably only gets flushed after the sleep runs. Try putting a call to fflush(stdout) after the printf, but before the call to sleep.
I thought I was doing something simple here, but C decided to go asynchronous on me. I'm not sure what's going on. Here's my code:
#include <stdio.h>
int main() {
printf("start");
sleep(5);
printf("stop");
}
When I compile and run, I notice that sleep(5) works like a charm. But the compiler decided it was a good idea to skip the first printf() and go out of order, so when running, the program waits for 5 seconds and then prints startstop.
What's the deal? My theory is that the program initiates the print operation with the shell, then continues with the program, leaving Bash to wait until the program is no longer busy to actually render the strings. But I really don't know.
Thanks
printf uses buffered output. This means that data first accumulates in a memory buffer before it is flushed to the output source, which in this case is stdout (which generally defaults to console output). Use fflush after your first printf statement to force it to flush the buffered data to the output source.
#include <stdio.h>
int main() {
printf("start");
fflush(stdout);
sleep(5);
printf("stop");
}
Also see Why does printf not flush after the call unless a newline is in the format string?
Try adding '\n' to your printf statements, like so:
#include <stdio.h>
int main() {
printf("start\n");
sleep(5);
printf("stop\n");
}
The compiler is not executing this out of order. Just the output is getting accumulated, and then displayed when the program exits. The '\n' will invoke the line discipline in the tty drivers to flush the output.
Read this Q&A, it explains it.