Hello! I have recently began to program in UNIX environments and I am currently reading about signals.So i tried a snippet that seems it doesn't do what i intended to.So i want to print the counter i from a for loop until a value is reached, than stop the process.Instead i get the process killed without any output.

1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <unistd.h>
  4 #include <signal.h>
  5
  6 int main()
  7 {
  8         pid_t my_pid;
  9
 10         my_pid=getpid();
 11
 12         int i;
 13         for (i=0;i<30;i++)
 14         {
 15
 16                 fprintf(stdout,"%d ",i);
 17                 if (i==20)
 18                         kill(my_pid,SIGSTOP);
 19         }
 20
 21         return 0;
 22 }

Any help is more then welcome.Thank you.

Try adding fflush(stdout); immediately after your fprintf() .

Linux is probably buffering the output, so the display waits until the output buffer is full before displaying the data. But it never gets filled because of the kill() . fflush(stdout) will force the buffer to be displayed and cleared.

Try adding fflush(stdout); immediately after your fprintf() .

Linux is probably buffering the output, so the display waits until the output buffer is full before displaying the data. But it never gets filled because of the kill() . fflush(stdout) will force the buffer to be displayed and cleared.

That worked well.
Thanks a lot WaltP.

Anytime. Oh, and by the way, when you post code, don't post with line numbers. If we want to test the code it's difficult.

And good code formatting, too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.