Hello i have such problem
pid_t pid1;
pid_t pid2;
void switch_files(int sig_type)
{
printf("%d %d\n", pid1, pid2);
}
int main(int argc, char **argv)
{
pid_t lpid1,lpid2;
if ((lpid1 = fork()) == 0)
{
signal(SIGUSR1, switch_files);
//Some work
} else {
pid1 = lpid1;
}
if ((lpid2 = fork()) == 0)
{
signal(SIGUSR2, switch_files);
//Some work
} else {
pid2 = lpid2;
}
while(scanf("%s", input) > 0)
{
write(pipe1[1], input, strlen(input) + 1);
kill(pid1, SIGUSR1);
}
waitpid(pid1, 0, 0);
waitpid(pid2, 0, 0);
}
So i need to have value of pid1 and pid2 in my callback for signals, and at printf i have 0 0 but in main i have normal value of pids. How i can fix this, thank you for any help.
If you want the child to have the pid, simply ask for it:
if ((lpid1 = fork()) == 0)
{
pid1 = getpid();
pid2 = -1; // other child doesn't even exist yet
signal(SIGUSR1, switch_files);
//Some work
exit(0); // you don't want the child to go executing parent code, do you?
}
And
if ((lpid2 = fork()) == 0)
{
//pid1 already set with pid of 1st child
pid2 = getpid();
signal(SIGUSR2, switch_files);
//Some work
exit(0); // you don't want the child to go executing parent code, do you?
}
If you need to the 1st child to have the pid of the 2nd child, then you need to use some form of IPC, so you can communicate to the the 1st child after also 2nd child has been started and its pid is known.
If you want the children to have information, you have to communicate that information to them somehow. You can use a file, a pipe, shared memory, or any mechanism you want. But you have to pass the information somehow.
Related
I try to get the exit code from a child of a child process in the parent process.
If the process goes in the while loop to fork again i don't get the exit code.
I tried some options for waitpid like WNOHANG but then the program hangs.
Maybe what i wan't is not possible because it's some like a zombie child?
This is my code.
void parrent_process(t_token *token, t_info *info)
{
pid_t pid;
int wstatus;
pid = fork();
if (pid == -1)
return (print_error_msg(FORK_FAIL, NULL, NULL));
if (pid == 0)
{
child_process(token);
}
if (info->in)
close(info->in);
waitpid(pid, &wstatus, 0);
if (WIFEXITED(wstatus))
info->exit_code = WEXITSTATUS(wstatus);
}
void child_process(t_token *token)
{
t_token *cmd_token;
pid_t pid;
pid = 0;
cmd_token = token;
while (token->next && ((token->next)->type == GREAT))
{
pid = fork();
if (pid == -1)
return (print_error_msg(FORK_FAIL, NULL, NULL));
if (pid == 0)
{
redirect_output(token);
break;
}
token = token->next->next;
}
if (execve(cmd_token->path, cmd_token->args, cmd_token->envp) == -1)
{
print_error_msg(CMD_NOT_FOUND, "minishell", cmd_token->args[0]);
exit(127);
}
}
In most POSIX-like systems, the answer is "No — a process can only wait on its own children, those it created directly with fork()1".
However, on Linux, there is the prctl(2) system call. The option PR_SET_CHILD_SUBREAPER allows a process to wait for more distant descendants (grandchildren, great-grandchildren, …) too. However, if the direct parent of a process waits for it, the ancestral process will not get the exit status information.
1 Or posix_spawn()
I'm writing a Unix program where the parent process has to send signals to children and a grandson. How could I know if all processes have been already created before sending signals? Because sometimes they don't exist yet. Thanks a lot!
void t(int sig)
{
kill(SIGKILL, pidc1);
kill(SIGKILL, pidc2);
kill(SIGKILL, pidg2);
kill(SIGKILL, pidc3);
}
void handler()
{
write(1, "Signal SIGUSR1\n", 15);
}
pid_t pidc1, pidc2, pidc3, pidg2;
int main(int argc, char **argv)
{
struct sigaction action;
int status;
action.sa_flags = 0;
action.sa_handler = handler;
sigaction(SIGUSR1, &action, NULL);
pidc1 = fork();
if(pidc1 == 0)
{
printf("Child 1\n");
}
pidc2 = fork();
if(pidc2 == 0)
{
printf("Child 2\n");
pidg2 = fork();
if(pidg2 == 0)
{
printf("Grandson 2\n");
}
wait(&status);
}
pidc3 = fork();
if(pidc3 == 0)
{
printf("Child 3\n");
}
kill(pidg2, SIGUSR1);
kill(pidc3, SIGUSR1);
signal(SIGALRM, t);
alarm(10);
wait(&status);
}
Preliminary note: The child code parts in your example program fall through to their parent's code, which is certainly not intended; I'll assume something like return sleep(5); at the end of each block. Also note that the printf()s may malfunction with fork()s and buffered output.
Barmar wrote:
If you need to wait for the grandchild processes to be created, you need some kind of communication from the child to the parent, so it can send the grandchild's PID. Shared memory and a mutex would be a way to do this.
That's absolutely correct. (The direct children are no problem, since the parent knows their PIDs.) Another way to communicate the grandchild's PID is a pipe; your example main() could become:
int main(int argc, char **argv)
{
int status;
sigaction(SIGUSR1, &(struct sigaction){.sa_handler = handler}, NULL);
setbuf(stdout, NULL); // printf() may malfunction without this
pidc1 = fork();
if (pidc1 == 0)
{
printf("Child 1\n"); return sleep(5);
}
int pipefd[2];
pipe(pipefd); // for communicating the grandson's PID
pidc2 = fork();
if (pidc2 == 0)
{
printf("Child 2\n");
pidg2 = fork();
if (pidg2 == 0)
{
printf("Grandson 2\n"); return sleep(5);
}
write(pipefd[1], &pidg2, sizeof pidg2); // write pidg2 to write end
wait(&status); return sleep(5);
}
pidc3 = fork();
if(pidc3 == 0)
{
printf("Child 3\n"); return sleep(5);
}
read(pipefd[0], &pidg2, sizeof pidg2); // read pidg2 from pipe's read end
kill(pidg2, SIGUSR1);
kill(pidc3, SIGUSR1);
}
I'm struggling to understand this concept. Let's say I want to run 3 concurrent processes (threads are not an option, this is an assignment).
I would do:
int main(){
fork();
if (getPid() == -1) { //this is the parent
fork(); //make a third process
} else if (getPid() == 0) { //child process
//do child things
}
So from what I've learned the parent pid is -1. And there's two children, both with PID 0?
So then the "parent" can spawn as many children as possible, correct? They will all do child things.
What if I want to do 3 different things? How do I track the PID's so that I have 3 unique
ones?
as per the comments - is this how it's done?
pid_t child2Pid;
pid_t child1Pid = fork();
switch /*if getPid is wrong what do I put here?*/ {
case -1: //parent
child2Pid = fork(); //create another child
case child1Pid :
//do what child1 would do
case child2Pid :
//do what child2 would do
pid_t child1, child2, child3;
if ((child1 = fork()) == 0)
{
// first child stuff goes here
_exit(0);
}
if ((child2 = fork()) == 0)
{
// second child stuff goes here
_exit(0);
}
if ((child3 = fork()) == 0)
{
// third child stuff goes here
_exit(0);
}
// we are in the parent, we have the PIDs of our three
// children in child1, child2, and child3
The whole idea is that you enter fork() once, but leave it twice - once in the parent, once in the child. In the parent, fork() returns the child's PID, in the child, fork() returns 0. -1 means error and there is no child.
So when you call fork(), look at the return value. Like this:
if(fork() > 0) //We're in the parent, and a child was spawned
fork(); //Spawn another.
This will create three processes. Error handling omitted.
**
int main()
{
pid_t pid;
int i;
for(i = 0;i < 3;i++)
{
pid = fork();
}
if(pid == 0)
{
printf("child\n");
}
else if(pid > 0)
{
printf("parent\n");
}
return 0;
}
you can use pid_t = fork() to record the value,and select what you do by the value.**
I am trying to learn programming on and I don’t understand why I can't get child 2 to print hello. I have also closed both read and write ends of the pipe for the parent because I don’t want the parent to communicate. Any help is much appreciated.
int main ()
{
int fd[2];
pid_t child_pid, child_pid1;
if (pipe(fd) < 0)
{
printf("Pipe error");
}
child_pid = fork (); // child 1
if (child_pid != 0)
{
printf("im child 1");
dup2 (fd[1], STDIN_FILENO);
printf("greetings");
child_pid1 = fork (); //child 2
if (child_pid1 != 0)
{
printf("im child 2");
dup2(fd[0], STDOUT_FILENO);
printf("hello");
}
else if (child_pid1 > 0) //Parent Code
{
close (fd[0]);
close (fd[1]);
}
}
return 0;
}
Your first if clause is wrong.
child_pid = fork (); // child 1
if (child_pid != 0)
{
printf("im child 1");
}
This is not the child but the parent. If it is the child, then pid will be 0, only on the parent you have child_pid != 0.
The same happens in the second if clause as well.
You should do if(child_pid == 0) in both cases, if you want that code to be executed in the child process.
can someone help me about how to create multiple child processes which have the same parent in order to do "some" part of particular job?
for example, an external sorting algorithm which is applied with child processes; each child process sorts a part of data and finally the parent merges them..
EDIT: Maybe I should mention the forking multiple child processes with loop..
Here is how to fork 10 children and wait for them to finish:
pid_t pids[10];
int i;
int n = 10;
/* Start children. */
for (i = 0; i < n; ++i) {
if ((pids[i] = fork()) < 0) {
perror("fork");
abort();
} else if (pids[i] == 0) {
DoWorkInChild();
exit(0);
}
}
/* Wait for children to exit. */
int status;
pid_t pid;
while (n > 0) {
pid = wait(&status);
printf("Child with PID %ld exited with status 0x%x.\n", (long)pid, status);
--n; // TODO(pts): Remove pid from the pids array.
}
If you want to launch several forks, you should do it recursively. This is because you must call fork from the parent process. Otherwise, if you launch a second fork, you will duplicate both parent and first child process. Here's an example:
void forker(int nprocesses)
{
pid_t pid;
if(nprocesses > 0)
{
if ((pid = fork()) < 0)
{
perror("fork");
}
else if (pid == 0)
{
//Child stuff here
printf("Child %d end\n", nprocesses);
}
else if(pid > 0)
{
//parent
forker(nprocesses - 1);
}
}
}
I think it would be worth pointing out why threads are more appropriate here:
As you are trying to do a "part" of the job in parallel i assume that your program needs to know about the result of the computation. fork()s of a process don't share more then the initial information after fork(). Every change in one process is unknow to the other and you would need to pass the information as a message (e.g. through a pipe, see "man pipe").
Threads in a process share the same adress space and therefor are able to manipulate data and have them visible toeach other "immediatly". Also adding the benefits of being more lightweight, I'd go with pthreads().
After all: You will learn all you need to know about fork() if you use pthreads anyway.
You can do this with fork. A given parent can fork as may times as it wants. However, I agree with AviD pthreads may be more appropriate.
pid_t firstChild, secondChild;
firstChild = fork();
if(firstChild > 0)
{
// In parent
secondChild = fork();
if(secondChild > 0)
{
// In parent
}
else if(secondChild < 0)
{
// Error
}
else
{
// In secondChild
}
}
else if(firstChild < 0 )
{
// Error
}
else
{
// In firstChild
}