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;
}
Related
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.
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
This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 7 years ago.
I am using Sublime to program in C but when the code is compiled it keeps returning [Finished in 1.1s with exit code 10]. What does that mean? What is going on?
#include <stdio.h>
void main()
{
int x;
scanf("%d", &x);
printf("%d", x);
}
#include <stdio.h>
int main()
{
int x;
scanf("%d", &x);
printf("%d", x);
return 0;
}
main( ) does not take void as a return type.In C programming language we use int as a return type of main .Exit codes are nothing but Windows System Error Codes .I think you are using windows Operating system and for windows Operating system
exit code 10 means :The environment is incorrect.
May be your installation process was not correct .
Reinstall it and run the program .
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.
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.