I'm trying to send a request to a server, and fork it each time beforehand:
int main(void)
{
// request send
fork();
// errx() error handling
return 0;
}
This however only causes 1 fork, then the program crashes. I've been browsing through some docs online and cannot figure out what I'm doing wrong and why it isn't forking each time it attempts to query a server.
Your main problem is execution path. Both parent and child process continue to errx, which apparently kills the active process, meaning no iterations in the parent or child. You don't check the result of fork, which is purposely there to help you tell if the function (a) worked, and (b) the current process is the parent or just-forked child.
You're better off setting up the parent process to be a watchdog, and launching the child processes to be guarded. A simple shell example is below, which sequentially launches child processes, waiting for each to terminate.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
for (int i=0; i<10; ++i)
{
pid_t pid = fork();
if (pid == 0)
{
// handle child process here
sleep(1);
return EXIT_SUCCESS;
}
if (pid < 0)
{
perror("Failed to launch child process");
exit(EXIT_FAILURE);
}
else
{
printf("child %d started\n", (int)pid);
int res = 0;
pid = wait(&res);
printf("child %d exited, result = %d\n", (int)pid, WEXITSTATUS(res));
}
}
return EXIT_SUCCESS;
}
Example Output
child 72916 started
child 72916 exited, result = 0
child 72917 started
child 72917 exited, result = 0
child 72919 started
child 72919 exited, result = 0
child 72920 started
child 72920 exited, result = 0
child 72921 started
child 72921 exited, result = 0
child 72923 started
child 72923 exited, result = 0
child 72924 started
child 72924 exited, result = 0
child 72925 started
child 72925 exited, result = 0
child 72926 started
child 72926 exited, result = 0
child 72927 started
child 72927 exited, result = 0
Monitoring Batches
For a more complicated version, the following launches child processes in batches of three, then waits for all three to terminate before launching the next batch. This is repeated three times for a total of nine processes total (ten, including the parent watchdog).
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
for (int i=0; i<3; ++i)
{
// batch launch loop
int n_children = 3;
for (int j=0; j<3; ++j)
{
pid_t pid = fork();
if (pid == 0)
{
// handle child process here
sleep(1);
return EXIT_SUCCESS;
}
else if (pid < 0)
{
perror("Failed to launch child process");
--n_children;
}
else
{
printf("child %d started\n", (int)pid);
}
}
// batch wait loop
for (int j=0; j<n_children; ++j)
{
int res;
pid_t pid = wait(&res);
printf("child %d exited, result = %d\n", (int)pid, WEXITSTATUS(res));
}
}
return EXIT_SUCCESS;
}
Example Output
child 73190 started
child 73191 started
child 73192 started
child 73190 exited, result = 0
child 73191 exited, result = 0
child 73192 exited, result = 0
child 73193 started
child 73194 started
child 73195 started
child 73194 exited, result = 0
child 73193 exited, result = 0
child 73195 exited, result = 0
child 73196 started
child 73197 started
child 73198 started
child 73196 exited, result = 0
child 73197 exited, result = 0
child 73198 exited, result = 0
Hopefully you'll find some of that helpful.
You have to keep in mind that both the main (first) process and the child (second one) will execute the same code, namely the errx function call. This thing is done usually with and if/else checking for the return value of fork. That would be something like this:
if (fork() == 0) {
// you are in the child thread here
}
else {
// and here you are in the parent
}
and you split your logic between the 2 threads. If not, the threads will both go on executing the same code until the quit the execution.
Related
I have the following code in my main function
pid_t pid;
pid = fork(); //Two processes are made
if (pid > 0 && runBGflag==0) //Parent process. Waits for child termination and prints exit status
{
int status;
if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
{
printf("Exitstatus [");
for (int i = 0; i < noOfTokens; i++)
{
printf("%s ", commands[i]);
}
printf("\b] = %d\n", WEXITSTATUS(status));
}
}
else if (pid == 0) //Child process. Executes commands and prints error if something unexpected happened
{
if (runBGflag==1) insertElement(getpid(),ptr);
execvp(commands[0], commands);
printf ("exec: %s\n", strerror(errno));
exit(1);
}
In a nutshell, a child process is made and if the runBackGround flag is set, the parent process will not wait for the child process to exit, but rather continue running. If a background process is made, the PID of the background process is stored in a list. At a later point, this function is called
void delete_zombies(void)
{
pid_t kidpid;
int status;
char buffer[1337];
while ((kidpid = waitpid(-1, &status, WNOHANG)) > 0)
{
removeElement(kidpid,buffer,1337);
printf("Child %ld terminated\n", kidpid);
printf("its command was %s\n",buffer);
}
}
This function simply checks if any child processes have died and in that case deletes them. It will then search for the childs PID in the list, remove it and print it out.
The problem is, the delete_zombies function will find that a child has died and will then try to remove it from the list, but it only finds an empty list, as if the child process never inserted its PID into the list.
This is really strange, because delete_zombies only finds a dead child process, when there was one created with the background flag set, so we know insertElement must have been called, but strangely when the parent checks in the list nothing is there
Is the cause for that, that child process and parent process have seperate lists, or is the PID maybe wrong?
I am confused about fork(). For example, what will be the output of the following code?
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int value = 5;
int main() {
pid_t pid;
pid = fork();
if (pid == 0) { value += 15; return 0; }
else if (pid > 0>) { wait (NULL); printf (“Value = %d”, value); return 0}
}
The function fork() creates a new process that is a complete copy of the original process. The new process has its own memory and its own copy of all variables.
In the new child process the returned pid value is zero. The child adds 15 to its variable value and exits in the line:
if (pid == 0) { value += 15; return 0; }
The value is 5 in the original process. The original parent process has pid greater than zero and it goes to:
else if (pid > 0) { wait (NULL); printf("Value = %d", value); return 0; }
This line prints: Value = 5
The output will be "Value = 5".
The fork function will create a new process (child process) with its own address space. The child process will receive a copy of the parents process data region, heap and stack. Therefore, modifying the variable value in the child process won't affect the variable value in the parent process.
Probably you don't know or don't quite understand what fork does. Like Orest Hera and reffox both said, fork() spans a new process.
You should also know that the parent process (the once actually calling fork) will get the pid of the child process as a result from fork.
The child process starts at the point, where fork finished and returns 0 instead, thus giving the processes the chance to check, who they are:
var x = 7;
pid = fork();
if(pid < 0)
{
perror("failing to create a child process");
return SOME_ERROR;
}
if(pid == 0)
{
/* I'm the child process */
x = 9;
/* only I see that, my dad doesn't even notice that this happened */
...
} else {
/* I'm the parent process */
...
/* waiting for my child to die,
otherwise a zombie will be created,
and I DO mean a zombie */
wait(0);
/* the child is gone, now I can do whatever I want to */
}
The program initially asks the user to input the number of child processes to create. After creating the children, the parent sleeps and waits for all its children to terminate via a signal handler function 'handle_child' registered with SIGCHLD.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
int to_kill;
void handle_child(int sig)
{
pid_t id = wait(NULL);
printf("Reaped child with PID = %d\n",id);
--to_kill;
if(to_kill == 0){
printf("~All children have been reaped successfully.. parent will now exit~\n");
exit(0);
}
}
int main()
{
pid_t id, parent_id = getpid();
int children, i;
signal(SIGCHLD, handle_child);
printf("Enter number of child processes to spawn: ");
scanf("%d",&children);
to_kill = children;
for(i = 0; i < children; ++i){
id = fork();
if(id == 0){
printf("New child with pid = %d\n",getpid());
sleep(2);
return 0;
}
sleep(1);
}
sleep(30);
return 0;
}
The problem I'm facing is that the parent often exits without reaping all of its children. Some of the times the program runs perfectly well, and at other times it ends abruptly. What is exactly happening here?
One of the instances of the erroneous output:
Enter number of child processes to spawn: 4
New child with pid = 6458
New child with pid = 6459
Reaped child with PID = 6458
New child with pid = 6461
Reaped child with PID = 6459
New child with pid = 6462
Reaped child with PID = 6461
nishad#nishad-Lenovo-B575:~$
I think the call to sleep(30) is interrupted by the SIGCHLD interrupt so it will not sleep for 30s but return immediately after the signal handler is called.
To get it right you will need to sleep in a loop:
tosleep = 30;
while (tosleep > 0) {
tosleep = sleep(tosleep);
}
I wanna create spare process (child?) in specific function called eg. void process(). I want just to create that child process and do nothing with it. I just wanna it alive and do nothing while main() of my app will be working as I want.
In some point of my app's main() I will be killing child process and then respawn it again. Any ideas how to do that ?
I have something like that but when I'm using this function to create process I get everything twice. Its like after initiation of process() every statement is done twice and i dont want it. After adding sleep(100) after getpid() in child section seems working fine but I cannot kill it.
int process(int case){
if(case==1){
status=1;
childpid = fork();
if (childpid >= 0) /* fork succeeded */
{
if (childpid == 0) /* fork() returns 0 to the child process */
{
printf("CHILD PID: %d\n", getpid());
}
/* fork() returns new pid to the parent process *//* else
{
}*/
}
else
{
perror("fork");
exit(0);
}
}
else{
if(status!=0){
status=0;
//kill!!!!
system(a); //getting kill -9 PID ; but PID is equal 0 here...
printf("\nkilling child");
}
}
}
how to just spawn new child process and let it just exist, like some sort of worker in C#?
Assuming you are in Linux, here's an example that might clarify your view: parent process spawns a child, the child calls pause() which suspends it until a signal is delivered, and finally parent process kill's the child with SIGKILL.
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
int main()
{
pid_t pid;
pid = fork();
if (pid < 0) { perror("fork"); exit(0); }
if (pid == 0) {
printf("Child process created and will now wait for signal...\n");
pause(); //waits for signal
}
else {
//do some other work in parent process here
printf("Killing child (%ld) from parent process!", (long) pid);
kill(pid, SIGKILL);
}
return 0;
}
Please note that fork() returns:
<0 on failure
0 in child process
the child's pid in parent process.
I'm starting to learn some C and while studying the fork, wait functions I got to a unexpected output. At least for me.
Is there any way to create only 2 child processes from the parent?
Here my code:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main ()
{
/* Create the pipe */
int fd [2];
pipe(fd);
pid_t pid;
pid_t pidb;
pid = fork ();
pidb = fork ();
if (pid < 0)
{
printf ("Fork Failed\n");
return -1;
}
else if (pid == 0)
{
//printf("I'm the child\n");
}
else
{
//printf("I'm the parent\n");
}
printf("I'm pid %d\n",getpid());
return 0;
}
And Here is my output:
I'm pid 6763
I'm pid 6765
I'm pid 6764
I'm pid 6766
Please, ignore the pipe part, I haven't gotten that far yet. I'm just trying to create only 2 child processes so I expect 3 "I'm pid ..." outputs only 1 for the parent which I will make wait and 2 child processes that will communicate through a pipe.
Let me know if you see where my error is.
pid = fork (); #1
pidb = fork (); #2
Let us assume the parent process id is 100, the first fork creates another process 101. Now both 100 & 101 continue execution after #1, so they execute second fork. pid 100 reaches #2 creating another process 102. pid 101 reaches #2 creating another process 103. So we end up with 4 processes.
What you should do is something like this.
if(fork()) # parent
if(fork()) #parent
else # child2
else #child1
After you create process , you should check the return value. if you don't , the seconde fork() will be executed by both the parent process and the child process, so you have four processes.
if you want to create 2 child processes , just :
if (pid = fork()) {
if (pid = fork()) {
;
}
}
You can create n child processes like this:
for (i = 0; i < n; ++i) {
pid = fork();
if (pid > 0) { /* I am the parent, create more children */
continue;
} else if (pid == 0) { /* I am a child, get to work */
break;
} else {
printf("fork error\n");
exit(1);
}
}
When a fork statement is executed by the parent, a child process is created as you'd expect. You could say that the child process also executes the fork statement but returns a 0, the parent, however, returns the pid.
All code after the fork statement is executed by both, the parent and the child.
In your case what was happening was that the first fork statement created a child process. So presently there's one parent, P1, and one child, C1.
Now both P1 and C1 encounter the second fork statement. The parent creates another child (c2) as you'd expect, but even the child, c1 creates a child process (c3). So in effect you have P1, C1, C2 and C3, which is why you got 4 print statement outputs.
A good way to think about this is using trees, with each node representing a process, and the root node is the topmost parent.
you can check the value as
if ( pid < 0 )
process creation unsuccessful
this tells if the child process creation was unsuccessful..
fork returns the process id of the child process if getpid() is used from parent process..
You can create a child process within a child process. This way you can have 2 copies of the original parent process.
int main (void) {
pid_t pid, pid2;
int status;
pid = fork();
if (pid == 0) { //child process
pid2 = fork();
int status2;
if (pid2 == 0) { //child of child process
printf("friends!\n");
}
else {
printf("my ");
fflush(stdout);
wait(&status2);
}
}
else { //parent process
printf("Hello ");
fflush(stdout);
wait(&status);
}
return 0;
}
This prints the following:
Hello my friends!