Kill all child processes of a parent but leave the parent alive
By : user6658566
Date : March 29 2020, 07:55 AM
I hope this helps . One way to accomplish this is to deliver some signal that can be caught (not SIGKILL). Then, install a signal handler that detects if the current process is the parent process or not, and calls _exit() if it is not the parent process. You could use SIGUSR1 or SIGUSR2, or perhaps SIGQUIT. code :
signal(SIGQUIT, SIG_IGN);
kill(-parent_pid, SIGQUIT);
|
communication between child and parent processes in C linux: parent process not blocking
By : Flo74
Date : March 29 2020, 07:55 AM
Does that help I want parent and child processes to communicate in C linux using pipes. First I want parent to pass a string and then child to acknowledge it. I have created two file descriptors. one for parent to child i.e. readpipe and other writepipe for viceversa. The problem is its not taking my data as input. Also I want the printf statements such as "Enter your data" to be printed once but since after fork, there are two processes so they are being displayed twice. Any alternative to that?? , There are a few problems in your code: 1. code :
close(writepipe[1]);
read(readpipe[0],ch,sizeof(ch));
ch1="YES";
write(writepipe[1],ch1,sizeof(ch1));
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
pid_t pid;
int r;
/* Hope this is big enough. */
char buf[1024];
char *cp;
int readpipe[2];
int writepipe[2];
int a;
int b;
a=pipe(readpipe);
b=pipe(writepipe);
// check a and b
pid=fork();
// check pid
if(pid==0)
{ //CHILD PROCESS
close(readpipe[1]);
close(writepipe[0]);
read(readpipe[0],buf,sizeof(buf));
printf("\nREAD = %s",buf);
close(readpipe[0]);
cp="YES\n";
write(writepipe[1],cp,strlen(cp)+1);
close(writepipe[1]);
}
else
{ //PARENT PROCESS
close(readpipe[0]);
close(writepipe[1]);
cp="HI!! YOU THERE";
write(readpipe[1],cp,strlen(cp)+1);
close(readpipe[1]);
read(writepipe[0],buf,sizeof(buf));
printf("\nACK RECEIVED %s",buf);
close(writepipe[0]);
}
return 0;
}
|
C: Parent and Child Processes
By : Toz Fik
Date : March 29 2020, 07:55 AM
it should still fix some issue As already stated above, you're not assigning pid to anything, so it's always zero. You should also change your condition to be pid instead of calling another fork(). code :
int main() {
int i;
pid_t pid=0;
pid = fork(); /* Add this */
/** use fork() system call to create a new process */
/** if the pid returned by fork() is negative, it indicates an error */
if(pid<0) { /* Change this */
perror("fork");
exit(1);
}
|
How to create 4 child processes for the same parent and wait for the 4 child's to finish?
By : Amit Basak
Date : March 29 2020, 07:55 AM
I wish this helpful for you I want one parent and 4 childs and after creating them I print something like: , A solution could be: code :
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
void childFunction(){
printf("Child : %d\n", getpid());
// do stuff
}
int main(){
int childLimit = 3; // number of children wanted
int childrenPids[childLimit]; // array to store children's PIDs if needed
int currentPid, i;
for(i=0; i<childLimit; i++){
switch(currentPid = fork()){
case 0:
// in the child
childFunction();
// exit the child normally and prevent the child
// from iterating again
return 0;
case -1:
printf("Error when forking\n");
break;
default:
// in the father
childrenPids[i] = currentPid; // store current child pid
break;
}
}
printf("Father : %d childs created\n", i);
// do stuff in the father
//wait for all child created to die
waitpid(-1, NULL, 0);
}
|
One parent with 2 child processes
By : Roberto Lopez Rito
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I am trying to create a single parent process with 2 childs. When I run my code i get 3 different child process ID. , The line:
|