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

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.

Related

Redirection ambiguity [duplicate]

This question already has answers here:
printf anomaly after "fork()"
(3 answers)
Closed 4 years ago.
I have a file named test.c (Its contents are given below)
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("T\n");
fork();
printf("F\n");
}
The expected result is:
T
F
F
Compiling with gcc and executing ./a.out ,the output is:
T
F
F
which matches the expected answer.
But ambiguity arises when I redirect the output to another file.
$ ./a.out > Output.txt
The Output.txt has the following data:
T
F
T
F
Why is there an additional T in my Output.txt file when I use redirectors?
1) I've check this in multiple PC's running on ubuntu with gcc installed.
2) Tried deleting the Output.txt and moving all the files to a different location, but this still persists.
P.s. this works fine without the redirector.
I think it's because of the buffer, give a try with the follow code:
#include <stdio.h>
#include <unistd.h>
int main() {
printf("T\n");
fflush(stdout);
fork();
printf("F\n");
}

What does the "exit code 10" mean in C? [duplicate]

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 .

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

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.

Cygwin showing error iostream.h is unable to locate [duplicate]

This question already has answers here:
fatal error: iostream.h no such file or directory [duplicate]
(3 answers)
Closed 8 years ago.
I wanted to run C programs on windows in order to achieve this I downloaded cygwin(Linux like environment for windows) made a program and kept it on a directory called ..\cygwin\home\Computer
Code goes here
#include<iostream.h>
void main(){
printf("Hai");
}
When i am trying to execute this program using command prompt.
$ g++ hai.c
Its throwing out an error
hai.c:1:21: fatal error: iostream.h: No such file or directory
#include<iostream.h>
^
compilation terminated.
What is going on any idea?
change to
#include <iostream>
#include <cstdio>
int main(){
printf("Hai");
}
or with g++ -x c hai.c or gcc hai.c
#include <stdio.h>
int main(){
printf("Hai");
}

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;
}

Resources