How to fork only with a specific child? - c

I am trying to build a process tree like this:
P1
/ \
P3 P2
\
P4
\
....
But i couldn't make it work. I tried this but it only forks with P1.
for(int i = 0; i < depth; i++) {
int _pid = fork();
if(_pid == 0) {
printf("[son] pid %d from [parent] pid %d\n", getpid(), getppid());
exit(0);
}
}
wait(NULL);

The requirement to use only one fork call makes the solution cumbersome and ugly, but still doable like this:
Step 1: create an inner loop that will run twice if i is 0, and once for any i > 0.
Step 2: fork only in the child process after the first iteration of the outer loop.
Step 3: exit the child that was forked on the second iteration of the inner loop.
int cur_pid = 0;
printf("Root PID %d started by %d\n", getpid(), getppid());
for (int i = 0; i < depth && cur_pid == 0; i++) {
for (int j = 0; j < (i ? 1 : 2); j++) {
cur_pid = fork();
if (cur_pid == 0) {
printf("[son] pid %d from [parent] pid %d\n", getpid(), getppid());
if (j == 1) {
exit(0);
} else {
break;
}
} else if (cur_pid < 0) {
printf("Error forking at PID %d\n", getpid());
}
}
}
wait(NULL);
Remember to wait on the child process of every iteration if you want getppid to return valid values!

Related

Implementation of multiple pipelines in C

First, I Saw already the all posts before. I took already a code from here https://stackoverflow.com/a/8439286/14888108
I have a problem I didn't know how to solve: when I do fork the pid is not 0 no matter what.
its a random number started like : 4013,4014 if I give [input: echo atb | grep "b"]
Here is my code:
EDITED AFFTER MISTAKE IN THE CODE SAME PROBLEM:
void Pipeline(char *input) {
int numPipes = 2 * countPipes(input);
int k = commends(input);
int j = 0;
for (int i = 0; i < k; i++) {
int pid = fork();
if (pid == 0) {
if (i != k-1) {
if (dup2(pipefds[j + 1], 1) < 0) {
perror("dup2");
exit(EXIT_FAILURE);
}
}
//if not first command&& j!= 2*numPipes
if (j != 2*numPipes && i != 0) {
if (dup2(pipefds[j - 2], 0) < 0) {
perror(" dup2");
exit(EXIT_FAILURE);
}
}
for (i = 0; i < 2 * numPipes; i++) {
close(pipefds[i]);
}
if (execvp(vec[i], vec) < 0) {
perror(vec[i]);
exit(EXIT_FAILURE);
}
} else if (pid < 0) {
prev++;
perror("error");
exit(EXIT_FAILURE);
}
j += 2;
}
for (int i = 0; i < 2 * numPipes; i++) {
close(pipefds[i]);
}
for (int i = 0; i < numPipes + 1; i++) {
wait(NULL);
}
printf("DONE!\n");
}
These lines:
int pid = fork();
prev = pid;
if (pid == prev+1) {
don't seem to make a lot of sense. If you copy pid into prev first, how can pid then ever be equal to prev + 1?
Also, you seem to be expecting a particular sequence of process id:s, that is not very likely or portable (or even nice). Other processes are busy creating and destroying processes in the background, I don't think you can assume that your particular process has a private pid space to fill.

fork() and pipe() in C goes wrong

I am learning about fork and pipe but have a problem with the following: My aim was to build a program with 3 processes and I did that but my question is: Why does printf("Sum of first half: %d\n", sum); get executed twice?
I checked the code for any logical errors that I made but couldn't find anything.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int size = sizeof(arr) / sizeof(int);
int sum;
int fd[2];
pipe(fd);
int id = fork();
if (id == 0) // Child process
{
for (int i = 0; i < size / 2; i++)
sum = sum + arr[i];
int j = fork();
if (j == 0)
{
printf("Hello I'm the grandchild\n");
}
}
else // Parent process
{
for (int i = size / 2; i < size; i++)
sum = sum + arr[i];
}
if (id == 0) // Child process writing to the pipe
{
close(fd[0]);
write(fd[1], &sum, sizeof(sum));
close(fd[0]);
printf("Sum of first half: %d\n", sum);
}
else // Parent process reading from the pipe
{
int x;
close(fd[1]);
read(fd[0], &x, sizeof(sum));
close(fd[1]);
printf("Sum of second half: %d\n", sum);
printf("Total sum: %d\n", x + sum);
}
}
Your code simplified:
int main()
{
int id = fork();
if (id == 0)
fork();
if (id == 0)
printf("Sum of first half\n");
else
printf("Sum of second half\n");
}
And the explanation:
code
Parent
Child
Granchild
fork()
fork
N/A
N/A
id value
id != 0
id==0
N/A
if (id == 0) fork()
then not executed
fork
N/A
id value
id != 0
id == 0
id == 0
if (id == 0) printf("sum first")
then not executed
printf
printf
else printf("sum second half")
printf
else not executed
else not executed

Pass a value between processes using pipes in a loop

I am trying to build a program that forks 4 child processes. The first child takes a value declared before forking, decrements it then passes it through a tube to the second process. The latter increments the value and passes it to the third one. 3 -> 4, 4 - 1 and so on. The program will end when 0 is reached and exit with the PID of the child that got that 0.
This is what I tried:
volatile sig_atomic_t end = 1;
First I installed a signal handler:
void sighandler(int sig)
{
(void)sig;
end = 0;
}
Then I assigned the action to SIGUSR1:
void prepare()
{
struct sigaction s;
s.sa_handler = sighandler;
s.sa_flags = 0;
sigemptyset(&s.sa_mask);
sigaddset(&s.sa_mask, SIGUSR1);
sigaction(SIGUSR1, &s, NULL);
}
This is my main:
int main()
{
pid_t cpid, first;
int status;
int COUNT = 10;
int pipes[NPROC];
prepare();
for (int i = 0; i < 2; i++)
{
pipe(pipes + 2 * i);
}
for (int i = 0; i < NPROC; i++)
{
switch (first = fork())
{
case -1:
perror("fail");
case 0:
if (i % 2 == 0)
{
while (end)
{
dup2(pipes[i], 0);
read(pipes[i], &COUNT, sizeof(COUNT));
COUNT--;
if (COUNT < 0)
kill(getppid(), SIGUSR1);
dup2(pipes[i + 1], 1);
write(pipes[i + 1], &COUNT, sizeof(COUNT));
for (int j = 0; j < NPROC; j++)
close(pipes[j]);
}
}
else
{
while (end)
{
dup2(pipes[i - 1], 0);
read(pipes[i - 1], &COUNT, sizeof(COUNT));
COUNT--;
if (COUNT < 0)
kill(getppid(), SIGUSR1);
dup2(pipes[(i + 2) % NPROC], 1);
write(pipes[(i + 2) % NPROC], &COUNT, sizeof(COUNT));
for (int j = 0; j < NPROC; j++)
close(pipes[j]);
}
}
exit(i);
}
printf("Start %d \n", first);
}
for (int i = 0; i < NPROC; ++i)
{
cpid = wait(&status);
if (!WIFEXITED(status))
printf("Error in child %d\n", (int)cpid);
else
printf("Finished %d\n", (int)cpid);
}
return EXIT_SUCCESS;
}
The idea is, the program runs, installs the signal handler. The signal handler will, when triggered, stop all children processes and allow them to exit.
Then I fork 4 processes. Each will open and close certain tubes in order to communicate with each other and at the same time, each decrements the value and pass it along. When the value hits 0 a signal will be raised and we will get the process id that sent the signal.
When I run the program, it starts and then it just waits indefinitely...
What am I missing?

Creating multiple processes in C

I am writing a program that needs to create multiple processes. Let's say this number is 3. I want each of these processes to count and output from 1 to 5, and to sleep for 1 second between each count/output. I tried to do that in a following manner, but my sleep(1) did not work like it was being interrupted. I would appreciate some background on the topic, what i did wrong, and how to resolve this. Here is my code so far.
/*
* Creates N > 2 processes.
*/
int main(int argc, const char * argv[])
{
pid_t pid;
for(int i = 0; i < N_PROC; i++) {
pid = fork();
if(pid == 0) {
ProcessWork();
exit(0);
}
}
}
/*
* Work of a single process.
*/
void ProcessWork() {
char buffer[BUF_SIZE];
for (int i = 1; i <= 5; i++) {
sleep(1);
sprintf(buffer, "PID = %d, count = %d\n", getpid(), i);
write(1, buffer, strlen(buffer));
}
}
Your sleep() works exactly as it should work. However, your problem seems to be that the parent process does not wait for the termination of the child processes, so the parent process terminates before the children have performed the work. Thus, it looks like this on a Unix system:
% ./a.out
% PID = 41431, count = 1
PID = 41430, count = 1
PID = 41432, count = 1
PID = 41431, count = 2
PID = 41430, count = 2
PID = 41432, count = 2
PID = 41430, count = 3
PID = 41431, count = 3
PID = 41432, count = 3
PID = 41430, count = 4
PID = 41431, count = 4
PID = 41432, count = 4
PID = 41431, count = 5
PID = 41430, count = 5
PID = 41432, count = 5
You should take a look at the manual page of the wait() system call. You can call that system call in a loop, and it returns the pid of the terminated child as long as there are children, and -1 with errno == ECHILD once you've run out of children. That ECHILD can be used as the loop termination criterion.
Wait for forked process termination, if I understood your problem...
int main(int argc, const char * argv[])
{
pid_t pid[N_PROC];
for(int i = 0; i < N_PROC; i++) {
pid_t cur_pid = fork();
if(cur_pid == 0) {
pid[i] = cur_pid;
ProcessWork();
exit(0);
}
}
for(int i = 0; i < N_PROC; i++)
wait(&pid[i]);
}

Forking in loop results in weird behavior

I have the following snippet of C code:
int i;
printf("ncams: %d\n", ncams);
for (i = 0; i < ncams; i++) {
int *pips_fds = malloc(2 * sizeof(int));
pid_t pid;
pipe(pips_fds);
printf("cproc_count in parent: %d, counter i: %d\n", cproc_count, i);// cproc_count is a variable declared above in code
if ( (pid = fork())== -1) {
logerr_r("cannot fork");
} else if (pid == 0) {
if ( close(pips_fds[1]) < 0) {
logerr_r("cannot close pipe");
}
printf("cproc_count in child: %d, counter i: %d\n", cproc_count, i);
int j;
for (j = 0; j < i; j++) {
free_cproc_id(cprocs[i]);//I don't need this in child process.
}
free(cprocs);// I don't need it also here in child process.
} else {
CProcID *cproc = malloc(sizeof(CProcID));
cproc->id = ++cproc_count;
cproc->pipes = pips_fds;
if (close(pips_fds[0]) < 0) {
logerr_r("cannot close pipe");
}
cprocs[i] = cproc;
}
}
Now, the output from this is:
ncams: 2
cproc_count in parent: 0, counter i: 0
cproc_count in parent: 1, counter i: 1
cproc_count in child: 1, counter i: 1
cproc_count in child: 0, counter i: 0
cproc_count in parent: 0, counter i: 1
cproc_count in child: 0, counter i: 1
As you can see, I have i = 1 for two times in parent. Can anybody tell me what I'm doing wrong?
The reason you have cproc_count in parent: 0, counter i: 1 being printed twice is because it is printed once by the parent, and then printed again by the child after the child has exited it's else if branch and looped around. You probably want to break out of the outer for within the child else if branch so that the child process doesn't also continue looping and forking new children.

Resources