implement the following problem using pipe and signal...
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
implement the following problem using pipe and signal...
nodes are coonected in the circular fashion.A token is passed from 1 node to another node.token is initialized to some integer value so when token is passed to next node, it shud decrement the value and pass token to nxt node.
whwn token value becomes 0 the particular node generates another token with initial value passing to next and kills itself giving a signal to adjacent nodes..
What is this a test for us all? More likely some exam problem or homework for you, eh? Well, some help, however given that I don't wish to edit, test, debug, etc code just for the fun of it, I'll at least give some pointers and help with organization.
Firstly, the problem is kind of ... off. If a node is going to kill itself, then the question would be, "How many nodes are you starting with?", because eventually you'll end up with one node and by the rules of this problems, you'll decrement and pass along a signal, but to no other node, so ... you stay stuck? You assume that "you" the last node are the sender and recipient.
Here are some basic thoughts that this is generating when I think about this:
- Signal is communications between one process and one or more other processes. For instance, a child process can send a SIGKILL and a parent process can be monitoring the child to see if it goes away. The other part of that is that the parent process typically uses fork() to create that child so that it knows the child's PID.
- Pipe is communications between one process and one or more other processes. This IMHO "has" to be done with the use of fork() because the first process creates the pipe[] to get two descriptors, and then uses fork() to create a child where each uses one side of the pipe for transmit and one side for receive.
You could perhaps do something like a ping-pong. One process, where the process starts, creates a pipe and a child process, they send the integer back and forth and decrement it, once one of the processes decrements to zero, it seeds the new integer value, forks a new child, and destroys itself via SIGKILL. I think you'd have to play with it a bit, and make sure you don't invalidate that pipe.
Remember that when you use fork() you make a complete copy of the process which you are at the time, the only indicator that you are the child is that the return from fork() is a positive non-zero value. A zero value means that you are the parent issued fork().
It would probably look something like this:
- Utility function to create_new_process() - invokes the fork() and sets up PID storage for who the opposing process is.
- Utility function (loop) to perform pipe receive, decrement and test of integer, and then either send over pipe or seeds a new integer value, creates a new process, and then signals it's own death, leaving the other process there to now be the peer to the newly created process. The only thing you have to cover there I think would be somehow communicate the newly created PID to the process that isn't going to kill but remain. You could use the pipe for that.
- The pipe stays around because you're never killing the final process which uses it.
- These processes stay around because they're sort of recursive, if that's the correct term. Never more than three total, typically two processes, except during transition.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.