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);
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 5 years ago.
Improve this question
For instance if I call
if (!fork())
does this create a new process like calling
if (fork())
or does it not?
It is the same, fork is evaluated before check is made.
if (!fork()) is the same as if (fork()) for function itself but not the same for if statement check.
Of course it does. In order to execute
if (! <expression>)
it first has to evaluate <expression>, so that it can then invert the boolean value of the expression and test it with if. When the expression is fork(), it calls the fork function, which creates a new process, and then returns a value in both the child and parent processes.
It's essentially just short for:
pid_t pid = fork();
if (!pid)
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 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 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.
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 have three modules(2 C executable+ 1 python script). I need three processes-one parent, two child processes. All of them won't stop until they see an End of file. They just keep running after execution.
Problem just arises here. Parent process needs the output of child process 1, and child process 2 needs the output of parent process. The order is: child process 1 outputs result and sleep for 5 seconds before next round of output-> parent process gets that output as input and output its own result -> child process 2 gets its parent's result and output final result to standard output. This is only one iteration. This loop will be repeated again and again until seeing an End of file.
I don't know how to make parent process wait for child process 1's each iterations output and forward it to child process 2. So as child process waits for parent process. I considered wait(), but that needs child process 1 to stop. But actually, none of those three process stops before End of file.
So, is there any good mechanism to implement this scenario?
You need to implement a pipe mechanism between the processes. Suggest you to go though the man pages of pipe(), dup() and also the link here for more info