LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 08-19-2005, 11:36 AM   #1
greghua
LQ Newbie
 
Registered: Apr 2005
Posts: 19

Rep: Reputation: 0
IPC problem


Here is a simple code to test of using IPC to terminate a program.
The program is supposed to print what it receives form pipe, but it doesn't.
And if I comment out the break in line 40, the programm terminates normally. In my point of view, it should not.

So what 's problem? Thanks!

Code:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#include <errno.h>
static void signal_handler();
static int pipefd[2];


int main(void)
{
	fd_set rset;
	int n,maxfd;
	char add[20];
	pipe(pipefd);
	maxfd = pipefd[0]+1;
	FD_ZERO(&rset);
	alarm(5);
	for( ; ; )
	{
		printf("loop\n");
		sleep(1);
		FD_SET(pipefd[0],&rset);
		if((n=select(maxfd,&rset,NULL,NULL,NULL))<0)
		{
			if(errno == EINTR)
			{
			   printf("interrupt received!\n");
		        }
			else
			{
				printf("select error\n");
			}			
		}
		if(FD_ISSET(pipefd[0],&rset))
		{
			bzero(add,sizeof(add));
			read(pipefd[0],add,sizeof(add));
			printf("%s\n",add);
			//break;
		}
	}
    return 0;
}

static void signal_handler()
{
	char data[]="Received from pipe";
	write(pipefd[1],data,sizeof(data));
	return;
}

Last edited by greghua; 08-19-2005 at 11:47 AM.
 
Old 08-19-2005, 04:12 PM   #2
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Hi,

There are a number of things that I see wrong with this code.

1) You have a function called "signal_handler", where is that function being called and what is it supposed to actually do? If that function is supposed to be called every 5 seconds because of your call to alarm, then you need to first tell the OS that you want to use the function as the default SIGALRM signal handler. Add the following line of code into your main function before the call to pipe:

signal( SIGALRM, signal_handler );

Also add that same line of code as the FIRST statement in your signal handler in order to rearm the signal handler for subsequent execution.

2) I see that your main function goes into an infinite loop. Without the break statement how is the program supposed to terminate?
 
Old 08-19-2005, 08:43 PM   #3
greghua
LQ Newbie
 
Registered: Apr 2005
Posts: 19

Original Poster
Rep: Reputation: 0
Yes, you are right, I forget to add
signal( SIGALRM, signal_handler );

Thanks.

Quote:
Originally posted by rstewart
Hi,

There are a number of things that I see wrong with this code.

1) You have a function called "signal_handler", where is that function being called and what is it supposed to actually do? If that function is supposed to be called every 5 seconds because of your call to alarm, then you need to first tell the OS that you want to use the function as the default SIGALRM signal handler. Add the following line of code into your main function before the call to pipe:

signal( SIGALRM, signal_handler );

Also add that same line of code as the FIRST statement in your signal handler in order to rearm the signal handler for subsequent execution.

2) I see that your main function goes into an infinite loop. Without the break statement how is the program supposed to terminate?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
IPC question... climbingmerlin Programming 4 11-03-2005 04:30 AM
about IPC iclinux Programming 1 01-15-2005 12:16 AM
Semaphores and IPC gsu_790 Programming 2 09-29-2004 10:29 AM
using IPC::Open2 acid_kewpie Programming 7 09-18-2002 11:24 AM
ipc problem raven Programming 3 02-04-2002 11:53 AM


All times are GMT -5. The time now is 05:23 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration