LinuxQuestions.org
Help answer threads with 0 replies.
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 11-22-2011, 04:39 AM   #1
XiaoMei
LQ Newbie
 
Registered: Nov 2011
Posts: 7
Blog Entries: 1

Rep: Reputation: Disabled
Unhappy pipe


Hi,everyone, can somebody help me to solve the problem?
i be requested to sent out two ways pipe, and the signal is sending through the pipe, so what can i do?? HERE is my current coding:-

Code:
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>

#define MSGSIZE 100

char *msg1 = "tmn3013 SystemProgramming#1";
char *msg2 = "tmn3013 SystemProgramming#2";
char *msg3 = "tmn3013 SystemProgramming#3";


void sig_usr(int signo)
{
	if(signo == SIGINT)
	printf("Signal caught!");
	return;
}

int main(void)
{
	char inbuf[MSGSIZE];
	int p[2], i;
	pid_t pid,ppid;
	ppid = getpid();
	

	if((pid = fork()) == 0)
	{ //Child
		kill(ppid, SIGINT);
	}
	else{
		printf("%d %d ",ppid, pid);
		struct sigaction sig;
		sigemptyset(&sig.sa_mask);
		sig.sa_flags = 0;
		sig.sa_handler = sig_usr;
		if(sigaction(SIGINT,&sig,NULL) == 0)
		printf("Signal processed OKay ");
		sleep(10);
	}
	if(pipe(p) == -1)
	{
		perror("pipe call");
		exit(1);
	}
	
	switch(pid=fork())
	{
	case -1:
		perror("fork call");
		exit(2);
	case 0:
		close(p[0]);
		write(p[1], msg1, MSGSIZE);
		write(p[1], msg2, MSGSIZE);
		write(p[1], msg3, MSGSIZE);
		break;
	default:
		close(p[1]);
		for(i=0; i<3; i++)
		{
			read(p[0], inbuf, MSGSIZE);
			printf("%s\n", inbuf);
		}
		wait(NULL);
	}
	exit(0);
}

Last edited by XiaoMei; 11-26-2011 at 07:46 PM. Reason: Added code tags
 
Old 11-22-2011, 05:42 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Your message appears to have been cut off. You were just about to say what you expect the code to do and what it actually does and what you heave done to debug the problem.
 
1 members found this post helpful.
Old 11-22-2011, 09:41 PM   #3
XiaoMei
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
thanks for reply, but i not really understand what you trying to say. can has more declaration??? thanks
 
Old 11-23-2011, 02:11 AM   #4
XiaoMei
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
i wish can send the signal through pipe
 
Old 11-23-2011, 05:25 PM   #5
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Quote:
Originally Posted by XiaoMei View Post
i wish can send the signal through pipe
signals are sent to processes. pipes are I/O devices.
 
Old 11-24-2011, 11:16 PM   #6
XiaoMei
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
how about if i convert the signal to number, and pass the number through the pipe, is that logic????
 
Old 11-25-2011, 03:52 AM   #7
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
Upon receiving a signal, you can send a message through the pipe (you could do that in a signal handler); upon which the process on the other end acts:
As smallpond said: signals are sent to processes. A pipe is not a process, and as such, you cannot send a signal through a pipe... try to visualize it ;-)

But the sender can send a message which the listener has to interpret, so it acts like you wanted.
In pseudo code:

sender:
Code:
catch signal(SIGTERM)
{
   send_message("terminate");
}
listener:
Code:
receive_message(string)
{
   if(string == "terminate")
   {
       exit;
   }
   else
   {
       print(string);
   }
}
get the idea?
 
Old 11-25-2011, 10:05 PM   #8
XiaoMei
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
thank you, Ramurd, when i see it, i almost cry le, thank you so much!!!
 
Old 11-25-2011, 10:21 PM   #9
XiaoMei
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Ramurd, i try it, but it doesn't work....since now i dealing with parent and child process...the scenario likes this, a pipe will be establish between the parent and child, and the signal should be able to send through the pipe from parent and child...
 
Old 11-26-2011, 02:42 AM   #10
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
well; it's not that hard. If it's a real parent/child process, so the child is forked off the parent, you could use IPC calls as well...
You might want to read up on signals: what happens if a signal gets sent to the parent, what signal does the child get? Just remember: a pipe is an I/O stream, and you cannot send signals to that... you'll have to write a signal handler if what you want to happen on a certain signal is different from the default.

What process are you trying to send and catch? What code do you have?
 
Old 11-26-2011, 07:44 PM   #11
XiaoMei
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Question

Raymurd, thanks for ur patient, the above one is my code, sending kill signal from parent to child, and from child to parent
 
  


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
[SOLVED] How to handle a broken pipe exception (SIGPIPE) in FIFO pipe? zyroot998 Programming 5 03-03-2011 08:10 PM
PING .... pipe 2 vs pipe 3 hanen03 Linux - Networking 1 04-10-2009 10:50 AM

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

All times are GMT -5. The time now is 10:24 PM.

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