I want use pointer to get the pid of parent process with child process of it:
int main(void){
pid_t childPid,*parentPid,pid;
childPid = fork();
if(childPid == 0 ){
printf("[Child] the parent pid is 0x%u\n", *parentPid);
}else if(childPid < 0){
printf("there is something wrong");
}else{
pid = getpid();
printf("[Parent] the pid is 0x%u\n",pid);
parentPid = &pid;
}
return (EXIT_SUCCESS);
}
the output is:
[Parent] the pid is 0x5756
[Child] the parent pid is 0x1
there must be something wrong with my code, any idea?
The child never modifies *parentPid, so it just contains random garbage. If you want your parent PID, call getppid.
Even if you fixed the race condition, the code still wouldn't work. Changing memory in the parent doesn't change that memory in the child unless the memory is shared. If all memory were shared by default, the child and parent would instantly obliterate each other's data and cause total chaos.
Related
I'm trying to assign my node value to a pointer, but gdb gives me segmentation fault when the code is ran. What can I do?
void biggerPotion(No* node, int bottleSize, int *aux){
if(node == NULL)
return;
maiorPocao(node>left, bottleSize, aux);
maiorPocao(node->right, bottleSize, aux);
if((node->value >= garra) && (node-> value < *aux))
*aux = node->value; //here is the issue
}
Other relevant parts of the code are:
for(i=0; i< nBottles;i++){
a = 1000; //i declared that
biggerPotion(potions,bottleSize[i],&a);
}
Okay, since the errant line is:
*aux = node->value;
then either aux is the problem or node is (because they're the only two pointers being dereferenced on that line).
I would print them both out before executing that if block just to be certain:
fprintf(stderr, "node is %p, aux is %p\n", node, aux);
Given the large use of node and small use of aux, it's probably the latter that's causing the issue, in which case you should examine what you're passing to the top-level call of biggerPortion. You should post that top-level call, including the declaration of whatever variable you're passing in.
In any case, you can test that by simply changing:
*aux = node->value;
into:
{
int temp = node->value;
}
If the problem disappears then it's definitely the aux pointer being wrong somehow. Make sure you are actually passing in a pointer, such as with:
int myVar;
biggerPotion(rootNodePtr, 42, &myVar);
Double Fork
In my understanding, double fork is used when a process wants to fork a background process, but 1. it does not want to wait for it AND 2. the background process should be reaped after it exits.
As a result, double fork only requires parent process waits for child process, which should exit immediately after forking grandchild process, and grandchild process is in charge of the real task as a background process.
Context
According to this excerpt from APUE, the grandchild sleeps 2 seconds to make sure its parent (the child) exits before it exits, so that it will be an orphan and init will take care of it and reap it when it exits.
#include "apue.h"
#include <sys/wait.h>
int
main(void)
{
pid_t pid;
if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid == 0) { /* first child */
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid > 0)
exit(0); /* parent from second fork == first child */
/*
* We're the second child; our parent becomes init as soon
* as our real parent calls exit() in the statement above.
* Here's where we'd continue executing, knowing that when
* we're done, init will reap our status.
*/
sleep(2);
printf("second child, parent pid = %ld\n", (long)getppid());
exit(0);
}
if (waitpid(pid, NULL, 0) != pid) /* wait for first child */
err_sys("waitpid error");
/*
* We're the parent (the original process); we continue executing,
* knowing that we're not the parent of the second child.
*/
exit(0);
}
Problem
Why does grandchild process need to sleep that 2 seconds? Assume it exits before child process exits, it will still be reaped when child process exits according to this question, and parent process still does not need take care of it.
Doesn't that fulfill the original goal of using double fork?
The purpose of the example is to demonstrate that the grandchild's parent becomes process 1 (init) after its original parent exits.
To demonstrate that the grandchild's parent becomes process 1, the grandchild calls getppid and prints the result.
If the grandchild calls getppid before its original parent has exited, then getppid returns something that's not pid 1.
If the grandchild calls getppid after its original parent has exited, then getppid returns 1.
The purpose of the example program is to make #2 happen. So it needs to make sure the original parent has exited before the grandchild calls getppid. It does this by calling sleep(2) in the grandchild.
The grandchild, in a real program, would not sleep(2) there. It would just do its work.
Since this is a toy program, there's no real work for the grandchild to do.
im learning about fork() but something is working wrong in my ubuntu. im running this code:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
printf("--beginning of program\n");
int counter = 0;
pid_t pid = fork();
if (pid == 0)
{
// child process
int i = 0;
for (; i < 5; ++i)
{
printf("child process: counter=%d\n", ++counter);
}
}
else if (pid > 0)
{
// parent process
int j = 0;
for (; j < 5; ++j)
{
printf("parent process: counter=%d\n", ++counter);
}
}
else
{
// fork failed
printf("fork() failed!\n");
return 1;
}
printf("--end of program--\n");
return 0;
}
i know the parent and the child should run their code in no specific order so the code should return something like this:
-beginning of program
parent process: counter=1
parent process: counter=2
parent process: counter=3
child process: counter=1
parent process: counter=4
child process: counter=2
parent process: counter=5
child process: counter=3
child process: counter=4
child process: counter=5
--end of program--
But every time i run the program, they run in what it seems to be the same order. here is a capture of what i get every single time i run this program:
user#user-VirtualBox:~/Documents/S-O$ ./sample
--beginning of program
parent process: counter=1
parent process: counter=2
parent process: counter=3
parent process: counter=4
parent process: counter=5
--end of program--
user#user-VirtualBox:~/Documents/S-O$
child process: counter=1
child process: counter=2
child process: counter=3
child process: counter=4
child process: counter=5
--end of program--
it seems like the parent finish first, and then the child starts. but i guess thats not true. please note that the second shell prompt is opened by the program itself. any ideas of what may be happening?
Edit:
if i put an sleep(1) it works just fine. i still think that with no delay it shouldn't always have the same execution order. even counting until 100, it giver the same thing
Yes parent and children run their code in an almost unpredictable order (remember that there is no randomness), but your scheduler probably choose, after forking, to schedule the parent first and then the child. As both have a very small code, the parent is able to run all of its code, and then the children. To experiment something else you must do something much much more longer after forking in both processes (You will be able to see different interleaving of outputs for example).
The prompt is not produced by your code, but is an effect of it. When the parent process dies, the shell that ran it get control back and prompt you for another command, while in the same time the children is working and pollutes your terminal. The child has become an orphan process. To prevent this, you must tell the parent to wait its child by calling...wait().
So, I've already read this article Counting machine instructions of a process using PTRACE_SINGLESTEP, and i understand that dynamically linking a testprogram to my ptrace program will return an instruction count that also counts the initialization of the run-time library. However, I'm trying to get a valid count for my test program, which is:
int main(){
return 0;
}
My ptrace program first also returned 90k+ values, so I changed it to statically linking the used testprogram. The counter is now less, but still over 12k. The program I used to count the instructions is:
#include <sys/ptrace.h>
#include <unistd.h>
#include <stdio.h>
int main() {
long long counter = 1; // machine instruction counter
int wait_val; // child's return value
int pid; // child's process id
int dat;
switch (pid = fork()) { // copy entire parent space in child
case -1: perror("fork");
break;
case 0: // child process starts
ptrace(PTRACE_TRACEME,0,NULL,NULL);
/*
must be called in order to allow the
control over the child process and trace me (child)
0 refers to the parent pid
*/
execl("./returntestprog","returntestprog",NULL);
/*
executes the testprogram and causes
the child to stop and send a signal
to the parent, the parent can now
switch to PTRACE_SINGLESTEP
*/
break;
// child process ends
default: // parent process starts
wait(&wait_val);
if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0) != 0)
perror("ptrace");
/*
switch to singlestep tracing and
release child
if unable call error.
*/
wait(&wait_val);
// parent waits for child to stop at next
// instruction (execl())
while (wait_val == 1407) {
counter++;
if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0) != 0)
perror("ptrace");
/*
switch to singlestep tracing and
release child
if unable call error.
*/
wait(&wait_val);
// wait for next instruction to complete */
}
/*
continue to stop, wait and release until
the child is finished; wait_val != 1407
Low=0177L and High=05 (SIGTRAP)
*/
}
printf("Number of machine instructions : %lld\n", counter);
return 0;
} // end of switch
Any help would be really appreciated as I'm not quite sure if it's working right, or not at all. Once I get this thing started, I want to work on timing analysis with ptrace, but first things first and try to count the number of executed instructions
thanks!
I've kind of figured it out by now. So even when statically linking your code, you try to prevent the libraries to be dynamically linked to your program and thus also included into your count. The other side of it however is that executing your file, or basically invoking under the operating system also takes an enormous amount of instructions. So basically, as long as the instruction count is constant and the same under the same conditions, you can subtract this count (when using a return 0 program for instance) from your original program, to count the real number of instructions.
I have to use fork() recursively, but limit the number of forked processes (including children and descendants) to (for example) 100. Considering this code snippet:
void recursive(int n) {
for(int i=0; i<n; i++) {
if(number_of_processes() < 100) {
if(fork() == 0) {
number_of_processes_minus_one();
recursive(i);
exit(0);
}
}
else
recursive(i);
}
}
How to implement number_of_processes() and number_of_processes_minus_one()? Do I have to use IPC? I tried to pre-create a file, write PROC_MAX into it and lock-read-write-unlock it in number_of_processes() but it still eat all my pids.
I suspect that the simplest thing to do is to use a pipe. Before you fork anything create a pipe, write 100 bytes into the write side, and close the write side. Then, try to read one byte from the pipe whenever you want to fork. If you are able to read a byte, then fork. If not, then don't. Trying to track the number of total forks with a global variable will fail if children are allowed to fork, but the pipe will persist across all descendants.