Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to know can I run some compiled C program for example main.exe with option, that make sleep it after program work? Or can I run immediatly second program and using process, which running with main.exe?
I need to calculate memory of process, when I do it in background /proc/[pid]/status and ps aux show me incorrect values, because main.exe works so fast.
Or may be I can exec 2 programs in C using one fork()? Or can make option for execv or execl?
In Linux, time command can be used to calculate execution time, memory usage, etc
Refer http://linux.die.net/man/1/time
Usage ::
$ time main.exe
If you have access to source code of your application just insert code to send signal SIGSTOP to itself at the point you want to measure:
raise( SIGSTOP );
then you can measure whatever you want and let program go by sending SIGCONT from outside. If you do not have such access you may try to run this process and send signal outside, for example by shell script, but you would need to play with delay:
#!/bin/bash
./main.exe &
sleep 0.1 # this may not work on your distro, you will need to find how to sleep subsecond
kill -SIGSTOP %1
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I used named semaphore to synchronize multiple processes in my system. One of the process acquired the lock and exits without releasing the lock.Now none of the process able to acquire the semaphore lock.
named semaphore file present in /dev/shm/ directory (i.e)
/dev/shm/sem.XXXX.
I am trying to find the culprit process by adding debug logs in the code. Is there any other way we can use to find the process id associated with the named semaphore?
You can just use the command lsof /dev/shm/sem.XXXX (lsof = list of open files) to find which process has the file. fuser is also an equivalent for what you are trying to achieve. You can just call those commands from your c program via system() call or fork()/exec(). Then you have to analyse the output of the command to take the proper actions.
For more details on portability issues, have a look at https://unix.stackexchange.com/questions/18614/alternatives-for-lsof-command
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
As we know that exec family function calls eventually calls execve() which is a system call and remaining are library functions. So, typically, whatever implications applies on execve() are true for other exec*() functions as well.
I would like to know if, during execution of execve(), all signals are blocked until it succeed, or if there is a way to pass signal to that pid which corresponds to exec? (I know it does not return on success and further execution of calling function does not happen)
I am not sure I got your question right, so feel free to correct me if I am wrong.
But, basically, yes, system calls can be considered as 'atomic' from the process point of view. So, once the execve() system call is started, only the kernel has the hand on it and it won't release the process until running the new command or failing with an error code or raise the SIGKILL signal (as SIGKILL is the only unblockable signal).
But, once the execve() spawned a new process (and returned from the kernel), it is perfectly interruptible with any signal.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to call a program in C on Windows and immediately close the main program. I've tried using system() like this:
system("SecondaryProgram.exe");
return 0;
But the "caller" program always waits for SecondaryProgram.exe to finish. I would like to avoid this and immediately return 0 before the "called" program closes, something like opening it in "another thread". Is there any other function that does this?
Usually Windows compilers have support for execXXX and spawnXXX functions. So something like this should work:
#include <process.h>
...
spawnl(P_NOWAIT, "prog.exe", "prog.exe", NULL);
exit(0);
Use the CreateProcess() API. When it successfully launches another process, it will return a handle to you that you can use to wait for the process to complete, or not, as you like.
system("cmd.exe /C SecondaryProgram.exe");
But you may have a DOS box floating around until SecondaryProgram finishes.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
man for int kill(pid_t pid, int sig); says:
If pid equals -1, then sig is sent to every process for which the
calling process has permission to send signals, except for process 1
(init), but see below.
Does this mean that if my program is run with root permissions and it accidentally (due to memory corruption or a hack) provides -1 as pid argument - this will cause a complete DoS for the entire system?
If so, is it recommended to always perform a double check for the pid argument value before calling this potentially disasterous call? (just sayin')
Does this mean that if my program is run with root permissions and it
accidentally (due to memory corruption or a hack) provides -1 as pid
argument - this will cause a complete DoS for the entire system?
Yes, such a scenario is possible. But the chances of it happening is very less. Because no program that run with root permissions would do such a thing. If a malicious user/binary has somehow got gained root privilege, then sending signals is just one of the problems.
If so, is it recommended to always perform a double check for the pid
argument value before calling this potentially disastrous call?
That's just super paranoid thinking. There are thousands of ways to do disastrous activities.You might as well worry about:
What if there's not malicious daemon, ran at system startup, does:
kill(-1, SIGKILL);
How do you know if library function you make wouldn't call reboot(2) and reboot your system?
etc.
Besides, PIDs are not just user provided values that need to be sanitized. PIDs are mostly values acquired within the program using system calls or library calls. So the chances of "accidentally" using -1 is zero. Basically, you someone/program has root privilege and decided to screw your system then there's not much you can do.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to move a process to foreground. I know the bash I have to use fg but in c how can I implement this ? any suggestion as a starting point for me?
Background and foreground is just a matter of who receives the input the user types into the terminal. The processes are still scheduled by the operating system.
If you put a process into background from within your shell, you must disconnect the standard input file descriptor of this process from the terminal of the shell.
The outputs can still go to the terminal (depends on your expected behaviour of the shell).
To put the process back into foreground you have to reconnect the standard input back to the terminal, so that, the process can receive input from it, i.e, from the user again.
The best entry point is tcsetpgrp, a function that let you set a process group as the receiver of controls from terminal and be authorized to get inputs a do outputs to and from the terminal.
For all of this to work properly, you also need to have a look at setsid to set a session.
Subsequent interesting function is setpgid to build process groups.