When writing my server code I have this line:
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
When I run the program I get no errors, but the program just freezes, and I put a print statement at the first line of the main() (so it should run before anything runs)
but the print statement never gets executed.
This line of code is definitely the problem because once I comment it out, my print statements work.
What might create such bizarre behavior?
(I'm not allowed to post homework code, so unfortunately I can't post all of it)
Since this was apparently the answer, I'll write it here: If your printf format strings don't end with "\n", then they'll be buffered until either you do print a newline or your program exits. (I'm simplifying a bit.) Since your accept call stopped your program after that output was buffered, you couldn't see the output even though the printf calls were working fine.
The other option for you is to fflush() the stdout, which will force it to "print" anything buffered regardless of the '\n':
printf("print this now!");
fflush(stdout);
It's waiting for a connection. That's the purpose of the accept function.
Related
I am fairly new to using gdb debugger and so coming across the code being displayed when I ran gdb left me having no use for the debugger. I am unfamiliar with the code being displayed but a did a little research and I assume I accidentally opened up a "thread"? It's hard to explain something I do not understand but I will link a picture explaining what I am talking about. Basically I want to revert back to the "basic" display of my actual code and not this: displayed by the debugger
Your program called one of scanf family of functions, with a NULL stream.
Usually this happens when you don't check for errors. For example:
FILE *fp = fopen("/file/which/does/not/exist", "r");
char ch;
fscanf(fp, "%c", &ch); /* BUG: should check fp!=NULL first. */
You should always check return value from any function that may fail.
You can see which code called into the fscanf with GDB where command.
I write my output results which are emitted by Solver() function (a function in caffe third library), in a file with these command:
if(std::freopen("redir.txt", "w", stdout)) {
std::printf("stdout is redirected to a file\n"); // this is written to redir.txt
solver->Solve();
std::fclose(stdout);
}
but since the Solve() function emits outputs continuously, but the redir.txt will not be updated until the std::fclose(stdout); is executed. So I can't see the results real time.
How can I update my file in real time?
Use std::flush at regular intervals to flush the written (buffered) data to the file.
Don't flush too often though or performance will be impacted.
Commonly std::flush maybe works.
while I am trying to code in c for zombie process simulation ,I am getting alteration in output due to putting of \n in printf statements:
code1:
main()
{
int id;
id=fork();
if(id>0)
{
printf("Parent will sleep");//pf1//
sleep(5);
//system("ps -u");
}
if(id==0)
printf("I am child");//pf2//
}
output:
I am childParent will sleep
but while putting \n in pf1 or pf2 the order of execution of statements changes.what is the reason behind \n altering the order ?
stdout is line-buffered.
When you printf something to stdout, the library buffers this content up until a newline character is observed. At that point, the buffer is flushed, and will finally be displayed on your console. If you don't print a newline character, the data may stay in the buffer indefinitely, and you won't see it until the next time you print a newline character, or the process exits.
To ensure that your prints are always visible immediately, you can do one of these things:
fprintf(stderr, ...) instead. stderr is not buffered; data written to it will be visible immediately.
fflush(stdout) - Manually force the stream to be flushed immediately.
Update: Note that none of this actually matters, because of the inherent race condition involved. You have no control over whether the parent or child process executes first, and which one's data will be written to the console first. You could even possibly see the output interleaved as they both try to print at the same time. (Thanks to jschultz410 for pointing out my foolishness.)
I have a code that use popen to execute a script ,It works fine but randomly it's blocking and getting CPU to 100% ,after a little investigation I discover that it hangs on popen calls. I have put a printf after the popen showing the descriptor asigned and in this case when it blocks this printf never shows.
What can cause popen to block?
Edit: Code
FILE* pipe = popen(cpCommand, "r");
printf(....
if (pipe)
{
while (!feof(pipe))
{
if (DataReady(fileno(pipe),2500)>0)
{
if (fgets(output,sizeof(output),pipe) != NULL)
{
DataReady is just a select..
I have done a strace after it blocks and it seems to not doing anything
Not an answer ;-)
Try use strace to what it's doing and which syscall hangs.
Tterminal output is line-buffered, so make sure to flush output by using fflush() or using a newline (fflush(stdout); or printf("Debug text\n");) to ensure it doesn't really call the printf().
Run the script manually and ensure the script doesn't hang intermittently before suspecting popen().
Check the script called by popen() why it doesn't end.
As long a s the script does not end, popen() blocks, will say: does not return, as observed.
I strongly doubt this is a C issue.
I am working on a simple application written in C. I am working in a Unix environment.
My application is doing some simple I/O. I use printf to prompt the user for some input and then use scanf to get that input.
The problem is, I don't know how to tell my application that I am ready to proceed after entering in a value. Typing 'enter' provides a newline '\n' which makes sense. Control-d does allow scanf to capture my input but seems to ignore any subsequent scanf instructions.
Can someone help me out?
printf("Enter name\n");
scanf("%s",input);
printf("%s",input);
printf("enter more junk\n")
scanf("%s",morestuff); /* cntrl+d skips this*/
Check the return value from scanf(). Once it has gotten EOF (as a result of you typing control-D), it will fail each time until you clear the error.
Be cautious about using scanf(); I find it too hard to use in the real world because it does not give me the control over error handling that I think I need. I recommend using fgets() or an equivalent to read lines of data, and then use sscanf() - a much more civilized function - to parse the data.
See also a loosely related question: SO 3591642.
[EDIT: This answer is incorrect, as I stated below, I'm learning as well]
Have you tried CTRL-Z?
That sends EOF to scanf, which, according to its man page, should make scanf move to the next field. As you've entered only a string as the input format, that should terminate the scanf.
I can't test this right now, but you can give it a shot.
Man page is here:
http://www.slac.stanford.edu/comp/unix/package/rtems/doc/html/libc/libc.info.scanf.html