Kill a process group - c

I have a process tree as P1 ⟶ P2 ⟶ P3 ⟶ P4 ⟶ P5 (so P2 is child for P1 and P3 is child for P2 and so on).
Process P1 and P2 belong to same process group.
Process P3 and P4 and P5 belong to other process group.
In process P1, we know the process group of P3, P4 and P5 (it is the value P3) and we are sending the SIGKILL to this process group. using kill(-P3, SIGKILL).
The expectation is this will kill P3, P4 and P5 but not P2 but the observation is that P2
also got killed.
I have a two questions here:
Why is P2 getting killed?
What would be the exit status of P2 we will be getting in P1.

Related

Fork how many processes created confused

Say i have the following program
pid_t pid = fork(); // fork #1
pid = fork(); // fork #2
So now what we have:
Fork #1 creates an additional processes. so now we have two
processes.
Fork #2 is executed by two processes, creating two
processes, for a total of four.
My confusion is after the first fork we will have two processes P1(parent) and C1 (child). each process will execute the second fork once. so shouldn't we have 6 processes since P1 will create two more and C1 also? or is it that only the P1 can execute the second fork creating P2 C2
A good general rule is that one process calls fork but two return from it (assuming it works, of course).
Both returning processes continue executing after the return from the fork, something I suspect you man not have fully understood based on your confusion.
That means you go from one process to two in the first fork, then each of those processes calls fork again, so the process count doubles then doubles again (1 -> 4). Basically:
1 -> fork#1 -+-> 1 -> fork#2 -+-> 1
| |
| +-> 3
|
+-> 2 -> fork#2 -+-> 2
|
+-> 4
C1 won't execute the fork twice, only once.
P1 will execute it twice which will result in C1 and C2.
C1 will execute the second fork only.
the end result is 4 processes.
here is a visualization:
P1 -> C2 second fork
| first
V fork
C1 -> C3 second fork of child process

Reading an input file in C

I have an assignment to create a CPU scheduler by reading in process information from an input file like this:
q 1 tq 4 p1 30 p2 10 p3 24 p4 20 p5 17 p6 4 p7 7 p8 11 p9 8 p10 9 p11 5 p12 6 p13 3 p14 2 p15 1
q 2 tq 5 p1 1 p2 2 p3 2 p4 9 p5 8 p6 5 p7 12 p8 11 p9 15 p10 1 p11 4 p12 8 p13 22 p14 21 p15 30
q 3 tq 30 p1 30 p2 10 p3 24 p4 20 p5 17 p6 4 p7 7 p8 11 p9 8 p10 9 p11 5 p12 6 p13 3 p14 2 p15 1
where q x is "ready queue x", tq y is time quantum y and the rest in the line is in the format pv w, where pv is p1, p2, p3,..., p15 and are the process names and w is the CPU burst time. each queue are to be read from the same input file and stored in separate arrays of struct that contains the time quantum, process name and the CPU burst time:
struct process{
char name[4];
int cputime;
int timequantum;
};
How would I accomplish this?
I should mention that I have only just started with this C course and have never been taught how to read input files in C.

Concurrent Processes using fork()

I am given the following code:
main()
{
int i, rc;
for (i = 0; i<=1; i++)
{
if( (rc=fork()) == 0)
{
printf("Child %d executing\n",i);
} /*end if*/
} /*end for*/
}
printf("All children created\n");
I am also given the solution of the possible permutations of which outputs may occur.
Child 0 executing |
Child 1 executing | Child 1
All children created |
Child 1 executing | Child 2
All children created |
Child 1 executing | Grand child
All children created |
All children created | Parent
I know that these outputs are created by each process but I am just having trouble tracing them to understand HOW these outputs occur. I know fork() creates a process and if (fork() == 0) means it is a child process, but if anyone can help me understand where the answers beyond Child 0 executing | come about thanks. I believe the | is just a description of which process is currently being ran. How come child 1 can create a "grand child" but child 0 can not?
First, the code and behavior will be easier to understand if the loop is unrolled. The code then becomes:
int rc;
if ((rc = fork()) == 0)
printf("Child 0 executing\n");
if ((rc = fork()) == 0)
printf("Child 1 executing\n");
printf("All children created\n");
Then, to help understand what's going, the best is to draw the process hierarchy as a tree. Here is an ASCII version of it:
main
/|
/ |
/ |\
child0 | \
| | \
| | child1
/| | |
/ | | |
/ | | end
/ end |
/ |
child1 end
|
|
end
In the graph, child0 is the printf statement displaying "Child 0 executing", child1 is the statement "Child 1 executing" and "end" is the printf statement displaying "All children created".
As you can see from the graph, you'll get 1x child0, 2x child1 and 4x "All children created".
UPDATE #bkennedy
Here is another view that shows the process view only, with P0 being the main (original) process and "end" indicating each process' completion:
P0
/|
/ |
/ |\
P1 | \
| | \
| | P2
/| | |
/ | | |
/ | | end
/ end |
/ |
P3 end
|
|
end
There are really 4 processes: P0 (main), P1, P2 and P3.
P1 is the first child of P0; it displays "Child 0 executing".
P2 is the second child of P0; it displays "Child 1 executing". P2 never creates any children, it just finishes with the printf statement.
P3 is the first (and only) child of P1.
Each process displays "All children created" when they finish.
Remember:
P0 (main) goes through 2 fork calls, hence the 2 children.
P1 goes through 1 fork call, hence the single child (P3).
P2 never goes through a fork call.
That's it, there is no other process creation. I'm not sure how to explain this better to you.
After the first fork and the if statement that it's in, the "child 0 executing" process and its silent parent process main go on to the next if statement. There they both produce a "child 1 executing" process while remaining silent. All of those four processes, ie main, the "child 0 executing" process, main's "child 1 executing" process" and the "child 0 executing" process's "child 1 executing process", go on after the second fork and the if statement that it's in to print "all children created".
One of "Child 0 executing" is output, two of "Child 1 executing" are output, and four of "All children created" are output.
How come child 1 can create a "grand child" but child 0 can not?
No "child 1" creates a child. Main and "child 0" each create a "child 1". The output apparently calls "child 0"'s "child 1" the "Grand child" of main. So no "child 1" creates a child, and both main and "child 0" produce a "child 1". So your question is wrong about both "child 1" (of which there are two) and "child 0".
HOW these outputs occur
The quoted "output" seems to call main Parent, the "child 0" process Child 0, main's "child 1" process Child 2 and the "child 0" process's "child 1" process Grand child. So you need to distinguish between what the "output" is naming the processes and what those processes are outputing. It also has three lines outputing "Child 1 executing", which is wrong. But you should be telling us what that "output" is supposed to be.

Understanding MPI group communication

I am trying to understand the concept of MPI group communication through the following example:
int new_rank, rank, size, ranks1[4]={0,1,2,3}, ranks2[4]={4,5,6,7};
MPI_Group Original_Group, New_Group;
MPI_Comm comm1;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_group(MPI_COMM_WORLD, &Original_Group);
if (rank < 4) {
MPI_Group_incl(Original_Group, 4, ranks1, &New_Group);
}
else {
MPI_Group_incl(Original_Group, 4, ranks2, &New_Group);
}
MPI_Comm_create(MPI_COMM_WORLD, New_Group, &comm1);
MPI_Group_rank(New_Group, &new_rank);
printf("rank= %d newrank= %d \n",rank,new_rank);
In fact, I want to divide n*n matrix to a set of processors, say 9 processors, such that each row is divided into 3 blocks and sent to 3 processors in a group to perform some calculation. For example: The following figure shows n*n matrix is divided into sqrt(n) blocks and assigned to different processors such that each row forms one group, i.e. P1,P2,P3 is group 1, P4,P5,P6 is group 2 and so on.
| P1 | P2 | P3 |
| P4 | P5 | P6 |
| P7 | P8 | P9 |
I tried to play with the previous code by creating 3 groups (each group for each row processors) but my program crashed. Does it mean I don't need to make 3 groups? If so, how P1, for example, knows its neighbor processors in its group?

How to find all equal attributes from another relation using Relational Algebra

I have a relation A:
Store Product
---------- ----------
store A P1
store A P4
store B P1
store B P2
store B P3
store B P4
store C P2
store C P3
store C P4
and another relation B:
Product
----------
P1
P2
P3
How to find all stores (store B only in this case) that sell every product from relation B using Relational Algebra query?

Resources