Sublime Forum

No console output on printf() before sigwait()

#1

Hello,

For my prototype project I use basic Sublime Build setup. When I hit cmd+B I see no output in ST console for printf(“launched with pid %d\n”, getpid()); When I run it in the terminal everything works as intended.

Although I am new to C programming and maybe missing some aspects of shell/signals etc.

here is my build system:

{
	"folders": [ { "path": "." } ],
	"build_systems": [
		{
			"name": "***",
			"shell_cmd": "clang -g -O0 -v ${project_path}/src/what_ever -o /tmp/what_ever.out && printf \"\n\" && /tmp/what_ever.out",
		}
	]
}

here is the part of my code that outputs:

    ...
    ...
    sigset_t ss;
    sigemptyset(&ss);
    sigaddset(&ss, SIGINT);
    sigaddset(&ss, SIGTERM);
    if (pthread_sigmask(SIG_BLOCK, &ss, NULL) == 0) {
    	printf("launched with pid %d\n", getpid());
    	int s = 0;
        for (;;) {
	    	if (sigwait(&ss, &s) == 0) {
	    		switch (s) {
	    			case SIGINT:
	    			case SIGTERM:
	    				goto __exit;
	    			default:
	    				continue;
    ...
    ...

MacOS Catalina, version 10.15.2 (19C57)
Sublime Text, version: 3.2.2 (build 3211)


Best regards

0 Likes

#2

Your underlying issue here is that the C standard library uses different buffering strategies depending on the environment that the program is running in.

When you run the program in the terminal, it’s detected as an interactive terminal output and so the library chooses line buffering as the buffering mode, so that as lines of output are made available you see them (because in theory you’re interacting with the program).

When you execute the program in other environments, such as in Sublime or when redirecting the output to a file, it’s detected as a non-interactive output so the library chooses standard buffering instead; in this mode instead of the output being sent out on a line by line basis, it’s sent out when an internal buffer fills up (say 64kb or so).

In order to do what you want here, you need to alter your program. If you only intermittently generate output, then you can use fflush() to force the output buffer to flush so that Sublime can see the output.

Alternatively you can turn off output buffering for stdout (perhaps by modifying your program to do it when you use a specific command line parameter since buffering is a good idea generally, just not in this particular instance) by using setvbuf when your program starts up:

setvbuf(stdout, NULL, _IONBF, 0);

There’s more information on this generally in the following video, which shows what’s happening above in action and using fflush to make output available.

2 Likes

#3

@OdatNurd

Thank you, now it’s clear for me and thank you for that express course of how things work inside, for me it is important as for junior C developer :wink:

0 Likes