Connecting between to brothers (at C language)

At C language:

I have a father that make two sons at a loop by fork(), I want that each son will have (at pid_t type) the process ID of his brother.

I try do this via pipe but I didn't success. Do you have any idea how can I do this?

I did this via (write(pos[1],getpid(),sizeof(pid_t)) (this son send to his brother his pid) but it doesn't work...

Thank you!
The problem is passing the pid to the second child.

Well, there is a set of IPC mechanisms available in Unix. They're listed here.
https://en.wikipedia.org/wiki/Inter-process_communication

Choose a suitable one and try. But I take it you've tried with pipes. If you want help with that code, you'll have to post it.
What about something like this:

1
2
3
4
5
6
7
8
9
10
11
12
pid_t first_son;
pid_t second_son;
bool first_son_is_set = 0;
// Also need a mutex
// spawn the threads

lock_mutex();
if(first_son_is_set)
second_son = getpid();
else
{first_son = getpid();first_son_is_set=1;}
unlock_mutex();


You'll also need to make sure the pid_t's have been set when reading them.
Last edited on
I solve it!

I had to do:
1
2
pid_t me=getpid();
(write(pos[1],&me,sizeof(pid_t))



Thank you!
Last edited on
Topic archived. No new replies allowed.