LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-21-2013, 11:35 AM   #1
siya25
LQ Newbie
 
Registered: Jan 2013
Posts: 8

Rep: Reputation: Disabled
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..

i tried the following code but its nt wrking..


Code:
#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
#include<sys/types.h>

int token=5;
int ar[5]={0,0,0,0,0};

void alrmhandler(int signal)
{	if(signal!=SIGALRM)
	{ return;  }
	else
	{ printf("node killed");
	}
}

void main()
{	printf("\n1\n");
	int pipefd[2],i;
	struct sigaction alrm;
	alrm.sa_handler = &alrmhandler;
        alrm.sa_flags = 0;
        sigaction(SIGALRM, &alrm, NULL);
	printf("2\n");

	if(pipe(pipefd)<0)
	  printf("pipe error");
	for(i=0;i<4;i++)
	 { 
	if(write(pipefd[1],ar[i],1)!=1)
	  printf("write error");
	  }
	printf("\n3\n");

//-------------------------------------------------------------------------------------------
	while(1)
	{ 
	   if(token==5)
	   { //printf("inside 5"); 
		token--;
	     for(i=0;i<4;i++)
	     { //if(n=read(pipefd[0],ar[i],5)>0)
		if(ar[i]==1){
		alarm(2);
	      	printf("node [%d] killed",ar[i]);}		}
	         if(token==0)
		  token=5;
		}
	    printf("\nleave 5\n");
	}
	   
//----------------------------------------------------------------------------------------------
	    if(token==4)
	   { token--;
	     for(i=0;i<4;i++)
	     { //if(n=read(pipefd[0],ar[i],5)>0)
		if(ar[i]==1){
		alarm(2);
	        printf("node [%d] killed",ar[i]);}
	      }
	        if(token==0)
		  token=5;
	 
		}

//-----------------------------------------------------------------------------------------------
           if(token==3)
	   { token--;
	     for(i=0;i<4;i++)
	     { //if(n=read(pipefd[0],ar[i],5)>0)
		if(ar[i]==1){
		alarm(2);
		printf("node [%d] killed",ar[i]);}
	      }
	 	if(token==0)
		  token=5;
		}

//----------------------------------------------------------------------------------------------
            if(token==2)
	   { token--;
	     for(i=0;i<4;i++)
	     { //if(n=read(pipefd[0],ar[i],5)>0)
		if(ar[i]==1){
		alarm(2);
		printf("node [%d] killed",ar[i]); }
	      }
	 	if(token==0)
		  token=5;
		}

//--------------------------------------------------------------------------------------------
	   if(token==1)
	   { token--;
	     for(i=0;i<4;i++)
	     { //if(n=read(pipefd[0],ar[i],5)>0)
		if(ar[i]==1){
		alarm(2);
		printf("node [%d] killed",ar[i]); }
	      }
	 	if(token==0)
		  token=5;
		}
//--------------------------------------------------------------------------------------------
      }

  }

Last edited by colucix; 01-22-2013 at 02:37 PM. Reason: Added CODE tags to improve readability and preserve indentation.
 
Old 01-21-2013, 02:52 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 01-22-2013, 02:38 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SFTP - CyberDuck doesn't work, but WinSCP does? Server exited on signal "PIPE"? dragos19 Other *NIX 1 10-04-2008 04:52 PM
implement pipe using C chrislam Programming 3 09-17-2007 09:33 AM
Program received signal SIGPIPE, Broken pipe. grupoapunte Programming 1 06-03-2005 05:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration