fork() and pipe() in C goes wrong - c

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

Related

C: multiple child process each generate different random number

I am trying to let children to process different random value EVERYTIME. If there is 3 children, I want them to generate 3 different random value each, so in total 3*3=9 different number. I've tried many srand(), if I go srand(time(NULL)) , all the children produces three same random number(123, 123, 123), and if I put getpid() into srand(), each child produces a random number three times(111,222,333). But I can't find something that produces 9 different random values. Here are my codes.
int main(int argc, char const *argv[]){
int pid[6];
for (int i = 0; i < 3; i++) {
pid[i] = fork();
if (pid < 0) {
printf("Fork Failed\n");
exit(0);
}
if (pid[i] == 0) {
time_t t;
srand((int)time(&t) % getpid());
int r = rand() % 30;
int count = 0;
while (count < 3){
printf("Child %d: %d\n",i+1,r);
count++;
}
exit(0);
}
}
return 0;
}
You only call rand() once in each child (outside the while (count < 3) loop, and then you print out that same number three times in a loop. Note that you do not assign any new value to r between loop iterations, so naturally its value hasn't changed when you print it again.
If you want three different numbers, call rand() three times (inside the loop). Replace
int r = rand() % 30;
int count = 0;
while (count < 3){
printf("Child %d: %d\n",i+1,r);
count++;
}
with
int count = 0;
while (count < 3){
int r = rand() % 30;
printf("Child %d: %d\n",i+1,r);
count++;
}

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?

How to fork only with a specific child?

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!

MPI Broadcast and Reduce

I have this code below..
When I run this with mpiexec -n 2
I get output in the console log like The input sequence is as follows: 0 1 2 3
I'm trying to get an output sequence of 0 2 6 12
which is an addition of PartialResult in the forloop
This is the result of the forloop, and I get this for each process, so when I run with 2 processes, this gets printed out twice.
Partial result in forloop: 0
Partial result in forloop: 1
Partial result in forloop: 3
Partial result in forloop: 6
I want to add them so that the output sequence (when rank == 0), this return 0 2 6 12. Can this be achieved after MPI_Reduce step?
Below is the entire C code.
int main(void)
{
int my_rank, comm_sz;
int i;
int Count = 4;
int Number[4];
int PartialResult = 0;
int Result;
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if(my_rank == 0)
{
printf("The input sequence is as follows: \n");
for (i = 0; i < Count; i++)
{
Number[i] = i;
printf("%d ", Number[i]);
}
printf("\n");
}
// Process 0 sends data to all of the processes
MPI_Bcast(Number, Count, MPI_INT, 0, MPI_COMM_WORLD);
for (i = 0; i < Count; i++)
{
Number[i] += my_rank;
PartialResult += Number[i];
printf("\n");
printf("Partial result in forloop: %d \n", PartialResult);
}
MPI_Reduce(&PartialResult, &Result, 1, MPI_INT, MPI_PROD, 0, MPI_COMM_WORLD);
printf("partial result after reduce: %d \n", PartialResult);
// Print out the result
if (my_rank == 0) {
printf("\n");
for (i = 0; i < Count; i++) {
printf("\n");
printf("\n");
printf("Result[i]: %d \n", Result[i]);
}
}
MPI_Finalize();
return 0;
}

passing 2D Array to recursion

I am trying to make calculations with using fork in C.
I have a 2D array with members {1,2,3,4}, I have a function to get square for each member in the array. I will call this function with child processes.
For example for 3 child process output will be like this;
1.Child
1 4
9 16
----------
2.Child
1 16
81 256
----------
3.Child
1 256
6561 65536
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int matrix[2][2] = {1, 2, 3 ,4 } ;
int main(){
forkFork(3);
}
void forkFork(int counter)
{
pid_t pid;
if(counter == 0)
{
exit(0);
}
if(counter > 0)
{
if ((pid = fork()) < 0)
{
perror("fork error :(");
}
else if (pid == 0)
{
matrixPower(matrix);
print(matrix);
}
else if(pid > 0)
{
forkFork(counter - 1);
}
}
}
void matrixPower(int m[2][2])
{
int i,j,k;
int result = 0;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
result = matrix[i][j]*matrix[i][j];
m[i][j] = result;
result = 0;
}
}
}
void print(int a[2][2])
{
int row, col ;
for( row = 0; row < 2; row++ )
{
for( col = 0; col < 2 ; col++ )
{
printf("%d\t", a[row][col]);
}
puts("");
}
printf("-------------------------------\n");
}
I could not find a way to pass new array to recursion function...
The problem with your solution is that all forked processes are created by the main/parent process. Therefore, each forked process just inherits the address space of the parent whose matrix values are always {1, 2, 3, 4}. The solution is for each modifying process to create its child process. Thus a new child process inherits the modified matrix.
See my solution below for forkFork function which works as expected.
void forkFork(int counter)
{
pid_t pid;
if(counter == 0)
{
exit(0);
}
if(counter > 0)
{
if ((pid = fork()) < 0)
{
perror("fork error :(");
}
else if (pid == 0)
{
matrixPower(matrix);
print(matrix);
forkFork(counter - 1);
}
}
}

Resources