I have very simple code that should run on background and at 1 am shut down the computer:
#include <ctime>
#include <cstdlib>
#include <unistd.h>
int main() {
time_t t;struct tm * now;
daemon(0,0);
while(1){
t = time(0);
now = localtime( & t );
if(now->tm_hour==1){
system("shutdown -P");
break;
}
sleep(10);
}
return 0;
}
The code works without sleep(10) but uses whole free memory so I need sleep function there to stop loop and recheck time each ten seconds, but with sleep function program stops immediately after I run it.
If you are writing C code, don't use C++ headers (ctime, cstdlib). Replace those #includes with #include <stdlib.h> and #include <time.h>. If the behavior of this code is really as you describe (which I would find surprising), then this is probably the source of the error.
Of course it immediately exits. Thats the whole point of using daemon. Check with ps and you will see that your proram is still running as a seperate process now.
Check the man page for a desription how daemon works.
Related
I am looking for a ptrace() call to observe a process until the process exits.
I have this which compiles with gcc / cc on OSX:
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <stdio.h>
#include <sys/ptrace.h>
int main(int argc, char *argv[]) {
pid_t pidx = atoi(argv[1]);
printf("pid = %jd\n", (intmax_t) pidx);
ptrace(PT_ATTACHEXC, pidx, 0, 0);
wait(NULL);
}
However, even with a valid/existing pid, this program will still exit immediately. I am trying to only exit this program after pidx dies.
Is this possible somehow?
Ideally I want something that works on both OSX and Linux.
Your problem is probably that the wait call returns immediately, because the traced "inferior" process is suspended, you know, waiting for you to debug it. You're going to need some kind of loop in which you make ptrace requests to inspect the child and then resume execution, and then call wait again to wait for it to suspend on the next breakpoint or whatever. Unfortunately the debugger API is extremely non-portable; you will have to write most of this program twice, once for OSX and once for Linux.
I recently tried Sysdig for a project.
My main goal is to catch syscalls from the kernel and Sysdig seems to be a good option. Actually, I had done some tests with different techniques and compared the overhead for each one.
I come here with a simple test: a C program which just opens, writes and then close a file 100000 times.
#include "stdio.h"
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
int main(void)
{
int i;
char puffer[100];
FILE * handle;
for (i=0; i<100000; i++) {
strcpy (puffer, "Sysdig c'est trop fort !\n");
handle = fopen("test.txt", "w");
fputs(puffer, handle);
fgets(puffer, 80, handle);
fclose(handle);
}
return 0;
}
I ran 5 tests without supervision and the average duration is:
real: 30,29s
user:1.068
sys:13.098
I ran other 5 tests with small LKM with kprobes. It took about 3 more seconds to complete the same test.
At this point, everything seems normal but when I ran tests with Sysdig:
sudo sysdig proc.name="ctest" > ctestlog
Where ctest is my C program, I had those results on average:
real: 17,1108
user: 0,6336
sys: 7,3752
So the result from this tests is that Sysdig enhanced my process quite by twice.
I precise that standard deviation from my tests is less than 1 sec.
Has someone already seen something like that or have an explanation?
Can it be related with linux task scheduler because of Sysdig job?
This is my code. I'm completely aware that an endless loop results when executing directly. What I did was I compiled this code and then executed twice in the linux command line via ./a.out & twice. The first time the program executes, it runs fine and gives a decent file handle. When the first instance of the program is running in the background and I execute the second instance one minute later (via ./a.out &), the file handle returned is the exact same. I was expecting a negative return value for the second instance to indicate the first instance is using the file.
How do I solve this issue? I don't want to use buffered file functions like fopen/fread because the file I want to make is small and must be made at the beginning of the program before anything else happens in the code.
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
char* pidf="/testpid.del";
int lfp=open(pidf,O_WRONLY|O_CREAT|0x700);
printf("File handle = %d\n",lfp);
if (lfp ==-1){printf("Can't use PID file: %s\n",pidf);return -1;}
while(1){
sleep(1);
}
close(lfp);
}
How to write current time in printf on Minix 3.2.1?
I try to use gmtime like below but it gives error on time(&nowtime).
#include <sys/time.h>
#include <time.h>
struct tm *now;
time_t nowtime;
time(&nowtime);
now=gmtime(&nowtime);
printf("TIME is NOW %s",now);
Moreover, I try to recall that in kernel (/usr/src/kernel/main.c) because I need that time on the booting of minix to say when the kernel process is finished and switch to user.
I take some errors on above code like when rebuild the kernel like below;
Not that familiar with minix, but it is similar to Unix & Linux, so maybe something from that platform may be present on minix... so A couple of approaches
Run a man on ctime
the man page on Linux's time() command contains this example code (which you may have to modify for minix, but it shows how to use asctime() localtime() and time() ):
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t result;
result = time(NULL);
printf("%s%ju secs since the Epoch\n",
asctime(localtime(&result)),
(uintmax_t)result);
return(0);
}
I am having a problem with a program of mine, as I cannot see the output display. Using a Dev C++ compiler to compile my C program, I debug it to see the output. However my program immediately terminates, so I can't see the output properly.
I ended my program with return 0, and Aldo tried getch(), but even with both endings my program terminates quick.
I want to know if my program endings are wrong, and if so what is the correct way to end a program?
you need the window stop to view the output, is it right?
if yes, include this library
#include <stdlib.h>
then add this line at the end of code:
system("PAUSE");
e.g
#include <stdlib.h>
#include <stdio.h>
int main()
{
/* do/print some thing*/
system("PAUSE");
}