This question already has answers here:
How to send a simple string between two programs using pipes?
(7 answers)
Closed 8 years ago.
I have created a named pipe (mkfifo) in process 1 and written something on it.
Now I can read the content written by process 1 in process 2.
Now I can to something (like listen) by which process 2 comes to know that process 1 has written some thing.
To complete the comment of Joachim.
You should consider using select or poll
What are the differences between poll and select?
I don't know if I really got your question but, if you want to process 2 know when process 1 write something, I suggest you use a signal from the process 1 to warn process 2 that things are ready to be read.(there are another ways to do this as well)
Since you got both process IDs(pid) you can use kill to send your ready message, so from the process that has done writting, it will be like that:
kill(pid, SIGINT)
And then handle Interrupt signal as you wish:
struct sigaction sigtohandle;
memset (&sigtohandle, 0, sizeof (sigtohandle));
sigtohandle.sa_handler = &read_process;
sigaction (SIGINT, &sigtohandle, NULL);
So you will have a function called read_process() on the signal receiver process that is supposed do the job you desire.
You can read more about processes and signals here:
http://advancedlinuxprogramming.com/alp-folder/alp-ch03-processes.pdf
Related
This question already has answers here:
printf anomaly after "fork()"
(3 answers)
Duplicated output using printf() and fork() in C
(1 answer)
Closed 1 year ago.
I'm having difficulty understanding the behavior of fork(). I thought the child process will execute the lines "after" the fork(). So I expected to see only one "Hello world!", but this code:
printf("Hello World!\n");
fork();
return 0;
outputs two "Hello World". Why is that?
I also noticed from online examples using pipe() that pipes are created before forking a child. How does the child also have a pipe when it was created after the creation of the pipe in the parent process?
The first question is more than likely related to your program's file buffering mode, it's likely using full buffering setup, meaning that the stream is only written once the buffer (stdout) is full, this will delay the output, and the child process will also output, because it has the same stdout.
If you use fflush(stdout) right after the printf, or if you change your buffering mode (setvbuf) to line buffered or not buffered at all, you will prevent the duplicate output because it will happen before the fork.
As for the second question, the child process duplicates the code of the parent process, after the fork, as you correctly mentioned, but it also duplicates the file table. By creating the unnamed pipes before the fork you will assure the child has the same file descriptors. This is commonly used to setup pipe comunication between child and parent processes.
You can check the /proc/<process id>/fd folder for each one of the two processes to confirm this.
Footnote
For future reference, these are two different subjects, albeit tangential, they belong in different questions, you can see one of the problems here, the question was close with duplicates related to the first question but not the second, though by chance it didn't remain unanswered, it's still burried in a different matter and virtually undiscoverable by other users.
When we do fork the child process get the exact copy of address space of parent process. So such behaviour should be expected. The pipe system call returns a file descriptor and both parent and child process have this descriptor .
This question already has answers here:
How to view thread id of a process which has opened a socket connection?
(2 answers)
Closed 3 years ago.
I have a process with 100 threads.
I know that only one thread is using a specific fd.
For example, this fd is a socket descriptor, and only one thread is using this socket with send() and receive().
How can I find out, with C, on Linux, the ID of this thread?
Is there a smarter way than attaching to each thread with ptrace and waiting until one of them will be detected?
File descriptors are part of the process. And since a file descriptor is just a nonnegative integer, and can be used by all threads of the same processes without explicit rebinding, asking "which thread holds an fd" is not a question applicable to the Linux process/threading model.
If you really want an answer then it would be: All the threads do!
This question already has an answer here:
Synchronising N sibling processes after fork
(1 answer)
Closed 3 years ago.
I have a shared variable set to 0 and then fork N processes. These processes then busy wait for the shared variable to become 1. The parent is responsible for setting this variable to 1. What's the best option here to eliminate busy waiting?
I was looking into semaphores (sem_open, etc) but my understanding is that parent will need sem_post N times so that all presses can proceed.
One way would be to create a pipe in the parent process. Each child process closes the write end of the pipe, then calls read (which blocks). When the parent process is ready, it closes both ends of its pipe. This makes all the children return from read and they can now close their read end of the pipe and proceed with their code.
For situations like this, I sometimes use atomic operation. You can use atomic flags to notify sub-process, but it creates busy-waiting. So you can use it on a very special cases.
Other method is to create an event with something like pthread_cond_broadcast() and pthread_cond_timedwait(). All sub-process wait on your condition. When you are ready to go, unlock it on parent process.
I have a shared memory X (array), 2 type of process and I have 3 semaphores, one for modifier X and the other one is for wait until I want to wake up one of these waited process.
Step by step:
1. I'm the process 1 and I ask for semaphore 1 (mutex).
2. OK I got it so I can add me in the array, now I wait (semaphore 2).
3. REPITE this for all the process
Now I have an array with 20 PID's for example, and I want wake up the process 5 (x[4]).
The question is: how can I wake up this process? How can I send a signal to a concret PID?
Excuse my english...
Thank you guys!!
You might use the kill(2) syscall to send a signal to some given process.
I am not sure it is the best way to synchronize (I believe it is not; read signal(7)). You could use Posix semaphores sem_overview(7) (or even old SysV semaphores svipc(7)) or pipes pipe(7) (or unix(7) sockets) with poll(2)
Read Advanced Linux Programming
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why is writing a closed TCP socket worse than reading one?
Why doesn't an erroneous return value suffice?
What can I do in a signal handler that I can't do by testing the return value for EPIPE?
Back in the old days almost every signal caused a Unix program to terminate. Because inter-process communication by pipes is fundamental in Unix, SIGPIPE was intended to terminate programs which didn't handle write(2)/read(2) errors.
Suppose you have two processes communicating through a pipe. If one of them dies, one of the ends of the pipe isn't active anymore. SIGPIPE is intended to kill the other process as well.
As an example, consider:
cat myfile | grep find_something
If cat is killed in the middle of reading the file, grep simply doesn't have what to do anymore and is killed by a SIGPIPE signal. If no signal was sent and grep didn't check the return value of read, grep would misbehave in some way.
As with many other things, my guess is that it was just a design choice someone made that eventually made it into the POSIX standards and has remained till date. That someone may have thought that trying to send data over a closed socket is a Bad Thing™ and that your program needs to be notified immediately, and since nobody ever checks error codes, what better way to notify you than to send a signal?