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.
Related
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 4 years ago.
Improve this question
Create child process by using fork() function.
The parent process runs change content of the process by execl() function which run cat f1.c command.
child process runs a traceroute www.google.com command.
Before asking questions here, please try it on your own and post what you have tried so far so we can guide you in the right direction. Also, it would be nice if you put more effort in asking better question. But to give you some guidance:
you can create child process by using fork. It returns an integer. If it is zero, that means you are in the child process. so you can do something like:
int pid;
if((pid=fork())==0){
// you are in child process
//use execl(constant char *path, constant char *commands); to run your commands
}
else {
//whatever you need to do in the parent process
}
You can find about execl() here :https://www.systutorials.com/docs/linux/man/3-execl/ It is basically a way to run a command. The first argument is a constant char pointer which points to the shell that you want to run the command in ("/bin/sh" etc.). The next arguments are the command it self ("cd", "mydir" etc.) terminated with null.
execl("/bin/sh","cd","mydir",NULL);
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 7 years ago.
Improve this question
I am building a very tiny shell in c,
I have the option on running programs in the background.
I keep a list of all my jobs meaning all the ones in the bg.
Now if i want to go and update this list, how can i check is a process is finished or if its still running.
ps
if i waited with waitpid for some process, will i still be able to check if the process is done? (i mean if i used waitpid it took the process of zombie state.
You should be able to call waitpid, passing it the process id and the WNOHANG option and call the WIFEXITED macro on the integer returned through status argument. See Just check status process in c.
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How do i know that a process [not called by c] has exited and based on that do something in C?
For eg. there is a running application , say notepad. I create an application to delete the text file created by it. I cannot do that while it is open . So how do I know when notepad exits and based on that take a decision in C.,.,
If you know the PID of the process, you can use the kill() function. Sending signal 0 to the process is guaranteed not to kill it, but merely to report on whether it is running.
If you don't know the PID of the process, there is often a pidfile on disk for the process concerned (e.g. /var/run/processname.pid) - look at the file that starts it for more information. If this PID file is not there, the process should not be running. If it is there, the file contains (as text) the PID of the process, which you can check using the method above.
If you do not know the PID of the process and it does not have a PID file, then you will have to walk /proc, or shell out to pgrep or similar. Avoid this if possible.