Sleep program execution in C for a few seconds [duplicate] - c

This question already has answers here:
What is the proper #include for the function 'sleep()'?
(6 answers)
Closed 7 years ago.
How can I sleep or pause the program excecution by a few seconds in C? I'm looking for something like this that is used in Java:
Thread.sleep(interval);
Is possible do this using C ?
Thanks in advance.

#include <stdio.h>
#include <unistd.h>
int main (void){
sleep(3);
return 0;
}
Read more here.

There is an answer here: What is the proper #include for the function 'sleep' in C?
Be careful that it works differently in Unix and Windows, whereas in one is measured in millisecs and in the other in secs.

Related

How do I get Linux system's up-time? [duplicate]

This question already has answers here:
What API do I call to get the system uptime?
(5 answers)
Closed 5 years ago.
I want to find a way to find the linux system's uptime.
I do not want to read the uptime file in /proc/.
Any other way?
#include <stdio.h>
#include <sys/sysinfo.h>
int main(void)
{
struct sysinfo sys_info;
sysinfo(&sys_info);
printf("Seconds since uptime: %lu\n", sys_info.uptime);
return 0;
}
This should get you started. If you need more help, type man sysinfo into the terminal.

Printf() not executing before the "while(1)" loop in C language in Linux [duplicate]

This question already has answers here:
Why does printf not flush after the call unless a newline is in the format string?
(10 answers)
Closed 5 years ago.
I noticed something strange with the following code.
int main()
{
printf("Test"); // Section 1 do something here....
while(1)
{
;
}
}
Section 1 should be executed first, then the program should get stuck in while loop.
But the result was that "Test" didn't get printed, but it got stuck in the while loop. I wonder why the code in Section 1 does not get executed?
I ran the code on Ubuntu 14.04 LTS(compiled with the default gcc compiler)
The stdout stream is buffered, therefore it will only display what's in the buffer after it reaches a newline. Add :
fflush(stdout);
after line :
printf("Test");
See also this link for other alternatives.
This must work:
#include <stdio.h>
int main() {
printf("Test");
while(1){}
}
To compile:
gcc file.c
To execute:
./a.out

Printf before fork() is being printed twice [duplicate]

This question already has answers here:
printf anomaly after "fork()"
(3 answers)
Closed 7 years ago.
I was writing a multi-process program using fork() and i bumped into a problem.
Below is a sample code reproducing the problem (without any sort of error checking):
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
printf("hello world");
fork();
}
This code prints 2 "hello world" statements (one from parent and the other from child). However this should not be the case since the printffunction call is prior to the fork() system call.
After testing, the problem appears to be solved by the following:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
printf("hello world\n"); \\ addition of the new line character
// or by using fflush(stdout);
fork();
}
My guessing is that the printf buffer is being copied while its not flushed, so the child process is emptying its copy of this buffer before exiting. Hence the other printf shows.
Can anyone provide a better explanation of this issue? Or even better, correct me if i am wrong or missing something.
The file handle stdout (which is used by printf) is by default line buffered, which means output using printf will be flushed (and shown in the console) either when there's a newline or when the buffer is full.
As fork creates an exact duplicate of the parent process, both processes have the same contents in the (un-flushed) output buffer, and both will be flushed when the two processes exits.
So yes you're correct in your guessing.

Why am I unable to lock semaphore in C code? [duplicate]

This question already has answers here:
Program using Semaphores runs fine on Linux...unexpected results on Mac osX
(2 answers)
sem_wait not working in basic code
(1 answer)
Closed 8 years ago.
As far as I know the below code should result in a deadlock and NOT print out "hello world". However, when I compile on my computer (Macbook Air late 2013, 10.9.2) with gcc, the code unexpectedly prints "hello world" and finishes execution.
Why does the below code not result in deadlock?
#include <stdio.h>
#include <semaphore.h>
int main() {
sem_t prod_slots;
sem_init(&prod_slots, 0, 0);
sem_wait(&prod_slots);
sem_wait(&prod_slots);
sem_wait(&prod_slots);
printf("%s\n", "hello world");
return 1;
}

C program sqrt not working [duplicate]

This question already has answers here:
Why do you have to link the math library in C?
(14 answers)
Closed 9 years ago.
I also entered include<math.h> but it still doesnt work. People are saying to enter -Im but im new to this where do I put -Im and how do I fix this.
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
float a=0, b=0, c=0, root1=0, root2=0;
printf("Enter the value of a,b and c to determine the roots\n");
scanf("%f%f%f",&a,&b,&c);
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root1=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("The first roots of the quadratic equation are\nFirst root=%.1f\nSecond root=%.1f",root1,root2);
return 0;
}
You have a copy-paste bug here:
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root1=(-b-sqrt(b*b-4*a*c))/(2*a);
should be:
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root2=(-b-sqrt(b*b-4*a*c))/(2*a);
Also you may need to link with the math library, e.g.
$ gcc -Wall foo.c -o foo -lm
Two things: first you copy pasted "root1" twice so you will lose the "plus" value and root2 will be zero. Second, for the benefits of others, the problem is most probably at compile time and the googled answer is there:
http://www.cs.cf.ac.uk/Dave/C/node17.html
And you should test for imaginary values:
if(b*b-4*a*c < 0){
printf("error: complex solution unsupported, see http://en.wikipedia.org/wiki/Square_root\n");
exit(1);
}

Resources