So I've been working on this today and I'm pretty sure I'm close, but I'm still a bit confused on how to terminate child processes and if I'm doing this assignment correctly. Here's the problem description:
Write a UNIX program that creates a child process that
prints a greeting, sleeps for 20 seconds, then exits.
The parent process should print a greeting before creating
the child, and another after the child has terminated. It
should then terminate.
And here's the code that I have:
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int child;
printf("Parent Greeting\n");
child = fork();
if(child >= 0)
{
if(child == 0)
{
printf("Child process\n");
sleep(2);
printf("Child exiting\n");
exit(0);
}
}
else
{
printf("Failed\n");
}
printf("End");
exit(1);
return 0;
}
The issue I'm running into is how to properly terminate the child process. If I have the exit statements commented out, then the child will run, wait, and then the "End" statement will print. If I have the exit statements in, then the child process will say that it's exiting and the program will just sit until I ctrl+c out of it. Any help would be appreciated, as I'm interested in the topic but am a bit confused :) Thank you!
You don't have to terminate the child process from the parent; it should terminate itself (and does after the sleep(), printf() and exit()). The parent process should wait() or waitpid() for the child to die before it prints the "End" message. Also, your "End\n" message should include a newline.
The exit(1); (at the end of the first program) is not wanted; it indicates failure. The exit() function does not return, so as written the return is redundant. But it would be better to remove the exit() and leave the return 0; indicating success.
(Note that the child should include a call to exit(), probably with the value 0 rather than 1 as in the revised code. After all, it has done its job successfully.)
The problem is that you have forced the parent process to exit before the child process , in that case the child process ceases to become a zombie process and in some time a ghost process , add a wait() at the end before the final exit .
I think this is exactly what you wanted to achieve.
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
void err_sys(const char* x)
{
perror(x);
exit(1);
}
int main()
{
pid_t childPid;
printf("Parent greeting\n");
childPid =fork();
if (childPid >=0)
{
if(childPid == 0)
{
printf("Child process\n");
sleep(20);
printf("child exiting\n");
}
else
{
waitpid(-1,NULL,0);
}
}
else
{
err_sys("fork error\n");
}
printf("END\n");
return 0;
}
Related
I have this assignment where we are supposed to create a specific amount of child processes, lets say 3, and make the parent wait for each child to finish. Also we're supposed to have a pipe that all processes write to so that once the parent is done waiting, it would use the pipe's to output the sum of all the children's results.
This is my code so far but it seems that wait(NULL) isn't working as expected. I am not sure what I'm doing wrong.
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
for (int i=0; i<3; i++) {
pid_t child = fork();
if (child > 0) {
printf("Child %d created\n", child);
wait(NULL);
printf("Child %d terminated\n", child);
}
}
printf("Parent terminated\n");
return 0;
}
First of all, it's better to first run all child processes and then wait for all of them, instead of waiting for each one sequentially.
In addition, the child processes should exit immediately and not keep running the forked code.
Thirdly, you must pay attention and wait for all children after the loop, and not only for the first one that terminates:
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
for (int i=0; i<3; i++) {
pid_t child = fork();
if (child > 0) {
printf("Child %d created\n", child);
}
else if (child == 0) {
printf("In child %d. Bye bye\n", i);
return 0; // exit the child process
}
}
while (wait(NULL) > 0); // wait for all child processes
printf("Parent terminated\n");
return 0;
}
EDIT:
The code above is just an improvement to the example given in the question. In order to implement the pipe of information from the child processes to the parent, a pipe can be created (using pipe()) and the write-end file descriptor would be accessible from child processes.
Here's a good example to do so.
this program is used to create maximum no. of processes system is allowed to create
Ok fine but i didn't get the else part what is going in there
When i execute it my system hangs boot itself automatically??
Can someone please explain it how the following code works??
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
int i = 1;
for(;;)
{
pid = fork();
if(pid < 0)
printf("MAX no of concurrent process are %d\n",i);
if(pid == 0)
i++;
else
{
wait(0);
exit(0);
}
}
return 0;
}
On entering the for loop, the process is tried to be forked.
On success, in the parent process, fork() returns something > 0, the PID of the child. The child returns 0.
On failure, fork() returns < 0. This case should be handled appropriately.
In your code, the child process increments the "inherited" i and goes on with the next loop run, but the parent waits for its child and exits.
This all goes well up to the point where fork() fails. Then you get an output, but the code is continued nevertheless, up to wait(0). There it hangs, and so do all its parents.
If you would do
if(pid < 0) {
printf("MAX no of concurrent process are %d\n",i);
exit(0); // or return 0
}
the child which was unable to create another child would exit properly and so would do all of its parents.
I have two process. Their names are parent and child process. I want parent process wait child process without wait() function. How can I do this?
My code is here.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
intmain(int argc, char *argv[]) {
printf("hello world (pid:%d)\n", (int) getpid());
int rc = fork(); if (rc < 0) { // fork failed; exit
fprintf(stderr, "fork failed\n");
exit(1);
} else if (rc == 0) { // child (new process)
printf("hello, I am child (pid:%d)\n", (int) getpid());
} else { // parent goes down this path (original process)
printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", rc, wc, (int) getpid());
}
return 0; }
You could write a loop that keeps checking if the child is running. You can use kill function to check if a process is alive. (This won't work, you can send a signal to a zombie process)
You could define a signal handler for SIGCHLD that sets a variable, and check that variable in a loop.
If you don't call some form of wait the child will become a zombie process though.
I'm trying to understand what the following code does:
#include <stdio.h>
#include <stdlib.h>
#include <syscall.h>
#include <unistd.h>
int main(void) {
int pid;
for(;;) {
pid = fork();
printf(getpid());
if(pid == -1) {
printf("fork failed");
exit(1);
}
if(pid == 0) {
execv("MYFORKAPP", NULL);
exit(2);
}
wait();
}
return 0;
}
The program itself is called MYFORKAPP. I'm learning about these 3 functions and I saw this code on the internet and I don't understand what it does.
I tried to run it (Fedora GCC) but the terminal is empty.
Should it at-least print the first getpid()?
Can you please explain me this code?
printf(getpid());
This may crash the program. printf() expects its first argument be a string, but getpid() will return a integer, so this integer will be used as a pointer to an array of character, this very likely leads to a crash, i.e. segmentation fault.
Besides that, what this program does is
fork() a child process and wait()
this child process will execuate the same program again
so it will fork() and wait()
and so on, until your system does not have enough resource to create new process
then fork() will fail, these different level child processes will exit one by one
finally, the first process created by your shell will exit, and the program ends.
What the following code does - segmentation fault.
Must be:
#include <stdio.h>
#include <stdlib.h>
#include <syscall.h>
#include <unistd.h>
int main(void) {
int pid;
for(;;) {
pid = fork();
if(pid == -1) {
printf("fork failed");
exit(1);
}
if(pid == 0) {
printf("child has pid %d\n", getpid());
execv("MYFORKAPP", NULL);
exit(2);
}
wait();
}
return 0;
}
This is a loop:
1. Parent creates a child process.
2. Child turnes to MYFORKAPP.
3. Parent wait for the child process.
4. Child terminates (may be).
5. goto 1
I'm having trouble with a process forking exercise. I want to fork a child process and have it hang after announcing it has been forked, and wait for a signal to terminate, after which the parent process must announce it is terminating and then exit.
I can get the processes forked and have the parent wait for the hanging child to be killed by the signal, but it seems to kill the parent as well. I tried killing the child process specifically by its PID, but with no success.
Thanks for any help!
Code:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
void catchInt (int signum)
{
printf("\nMy sincerest apologies, master\n");
/*kill(0, SIGINT);*/
exit(0);
}
void ignoreInt (int signum)
{
wait(NULL);
}
int main () {
pid_t pid;
/* fork process */
pid = fork();
if (pid < 0) /* error handler */
{
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) /* child */
{
printf("Child reporting in\n");
signal(SIGINT, catchInt);
for ( ;; )
pause();
}
else /* parent */
{
/* parent will wait for the child to complete */
signal(SIGINT, ignoreInt);
wait(NULL);
printf("You're welcome\n");
exit(0);
}
}
Even assuming you fix the code so it compiles (you've not defined tempPID), there are problems:
You set the child to go to sleep until a signal arrives.
You set the parent to wait until the child dies.
So, you have a state where neither process is going to do anything more.
You probably need the parent to send a signal to the child:
kill(pid, SIGINT);
It is not clear that you need the parent to set a signal handler.
You probably want the child to set a signal handler.
You probably don't want the infinite loop in the child.
Oh, and void main() is incorrect - int main() or int main(void) or int main(int argc, char **argv) are the approved declarations for main().
And it is tidier if you return a value (0) from main(). The C99 standard does permit you to drop off the end of main() and will treat that as returning zero, but only if the function is properly declared as returning an int.
The header for wait() and relatives in POSIX is <sys/wait.h>.
And, because I'm a sucker, here's code that compiles and might even do what you want:
#include <stdio.h>
#include <signal.h>
#include <unistd.h> /* getpid() */
#include <stdlib.h>
#include <sys/wait.h>
void catchInt(int signum)
{
printf("Child's PID is %d\n", (int)getpid());
printf("My sincerest apologies, master\n");
exit(1);
}
int main()
{
pid_t pid = fork();
if (pid < 0) /* error handler */
{
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) /* child */
{
printf("Child reporting in\n");
signal(SIGINT, catchInt);
pause();
}
else /* parent */
{
sleep(1);
kill(pid, SIGINT);
wait(NULL);
printf("You're welcome\n");
}
return(0);
}
Just figured out what I was doing wrong, I should have realized SIGINT is sent to every process, and so the parent was simply being sent an unhandled SIGINT, causing it to exit. Thanks for all the help (my apologies on the sloppy coding, I really shouldn't wait until the program is completed to clean that up), the code's been edited above and works as intended.
Thanks again.