Re: why printf() don't work?

2009-01-05 Thread Patrick Lamaizière
Le Mon, 05 Jan 2009 15:29:23 +0800,
Edward King  a écrit :

> I use FreeBSD7.0,and use signal,like follows:
> signal(SIGHUP,sig_hup);
> signal(SIGIO,sig_io);
> 
> when I run call following code,it can run,but I find a puzzled
> question,it should print some information,such as printf("execute
> main()") will print execute main(),but in fact,printf fuction print
> none!!! Why printf function do not go work?

Just to be sure :
You should not use printf() in a signal handler, it is not
considered 'signal safe'.
(See man sigaction)

Regards.
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: why printf() don't work?

2009-01-05 Thread Polytropon
On Mon, 5 Jan 2009 15:12:12 + (UTC), Tom Marchand  
wrote:
> trying putting newlines in the strings like this: 
> 
>    "receive overflow\n" 

You can add

fflush(stdout);

to force the output, even if no \n is appended. But as it has
been mentioned before, don't forget to

#include 

:-)


-- 
Polytropon
>From Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: why printf() don't work?

2009-01-05 Thread Tom Marchand


trying putting newlines in the strings like this: 



   "receive overflow\n" 






- Original Message - 
From: "Edward King"  
To: freebsd-questions@freebsd.org 
Sent: Monday, January 5, 2009 2:29:23 AM GMT -05:00 US/Canada Eastern 
Subject: why printf() don't work? 

I use FreeBSD7.0,and use signal,like follows: signal(SIGHUP,sig_hup); 
signal(SIGIO,sig_io); when I run call following code,it can run,but I find a 
puzzled question,it should print some information,such as printf("execute 
main()") will print execute main(),but in fact,printf fuction print none!!! Why 
printf function do not go work? my code is follows: #include "sys/ioctl.h" 
#include "unp.h" static int sockfd; #define QSIZE 8 #define MAXDG 4096 typedef 
struct{ void *dg_data; size_t dg_len; struct sockaddr *dg_sa; socklen_t 
dg_salen; }DG; static DG dg[QSIZE]; static long cntread[QSIZE+1]; static int 
iget; static int iput; static int nqueue; static socklen_t clilen; static void 
sig_io(int); static void sig_hup(int); int main(int argc,char **argv){ 
printf("execute main()"); int sockfd; struct sockaddr_in servaddr,cliaddr; 
sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); 
servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); 
servaddr.sin_port=htons(SERV_PORT); bind(sockfd,(SA 
*)&servaddr,sizeof(servaddr)); dg_echo(sockfd,(SA *)&cliaddr,sizeof(cliaddr)); 
} void dg_echo(int sockfd_arg,SA *pcliaddr,socklen_t clilen_arg){ 
printf("called dg_echo"); int i; const int on=1; sigset_t 
zeromask,newmask,oldmask; sockfd=sockfd_arg; clilen=clilen_arg; for(i=0;i 
=QSIZE) iget=0; sigprocmask(SIG_BLOCK,&newmask,&oldmask); nqueue--; } } static 
void sig_io(int signo){ printf("sig_io called"); ssize_t len; int nread; DG 
*ptr; for(nread=0;;){ if(nqueue>=QSIZE) err_quit("receive overflow"); 
ptr=&dg[iput]; ptr->dg_salen=clilen; 
len=recvfrom(sockfd,ptr->dg_data,MAXDG,0,ptr->dg_sa,&ptr->dg_salen); if(len<0){ 
if(errno==EWOULDBLOCK) break; else err_sys("recvfrom error"); } 
ptr->dg_len=len; nread++; nqueue++; if(++iput>=QSIZE) iput=0; } 
cntread[nread]++; } static void sig_hup(int signo){ printf("sig_hup called"); 
int i; for(i=0;i<=QSIZE;i++) printf("cntread[%d]=%ld\n",i,cntread[i]); } 
---
 Confidentiality Notice: The information contained in this e-mail and any 
accompanying attachment(s) is intended only for the use of the intended 
recipient and may be confidential and/or privileged of Neusoft Corporation, its 
subsidiaries and/or its affiliates. If any reader of this communication is not 
the intended recipient, unauthorized use, forwarding, printing, storing, 
disclosure or copying is strictly prohibited, and may be unlawful.If you have 
received this communication in error,please immediately notify the sender by 
return e-mail, and delete the original message and all copies from your system. 
Thank you. 
---
 
___ freebsd-questions@freebsd.org 
mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To 
unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: why printf() don't work?

2009-01-05 Thread Mike Jeays
On January 5, 2009 02:29:23 am Edward King wrote:
> I use FreeBSD7.0,and use signal,like follows:
> signal(SIGHUP,sig_hup);
> signal(SIGIO,sig_io);
>
> when I run call following code,it can run,but I find a puzzled question,it
> should print some information,such as printf("execute main()") will print
> execute main(),but in fact,printf fuction print none!!! Why printf function
> do not go work?
>
> my code is follows:
>
> #include "sys/ioctl.h"
> #include "unp.h"
> static int sockfd;
> #define QSIZE 8
> #define MAXDG 4096
> typedef struct{
>   void *dg_data;
>   size_t dg_len;
>   struct sockaddr *dg_sa;
>   socklen_t dg_salen;
> }DG;
> static DG dg[QSIZE];
> static long cntread[QSIZE+1];
> static int iget;
> static int iput;
> static int nqueue;
> static socklen_t clilen;
> static void sig_io(int);
> static void sig_hup(int);
>
> int main(int argc,char **argv){
>   printf("execute main()");
>   int sockfd;
>   struct sockaddr_in servaddr,cliaddr;
>   sockfd=socket(AF_INET,SOCK_DGRAM,0);
>   bzero(&servaddr,sizeof(servaddr));
>   servaddr.sin_family=AF_INET;
>   servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
>   servaddr.sin_port=htons(SERV_PORT);
>   bind(sockfd,(SA *)&servaddr,sizeof(servaddr));
>   dg_echo(sockfd,(SA *)&cliaddr,sizeof(cliaddr));
> }
> void dg_echo(int sockfd_arg,SA *pcliaddr,socklen_t clilen_arg){
>   printf("called dg_echo");
>   int i;
>   const int on=1;
>   sigset_t zeromask,newmask,oldmask;
>   sockfd=sockfd_arg;
>   clilen=clilen_arg;
>   for(i=0;i  dg[i].dg_data=malloc(MAXDG);
>  dg[i].dg_sa=malloc(clilen);
>  dg[i].dg_salen=clilen;
>   }
>   iget=iput=nqueue=0;
>   signal(SIGHUP,sig_hup);
>   signal(SIGIO,sig_io);
>   fcntl(sockfd,F_SETOWN,getpid());
>   ioctl(sockfd,FIOASYNC,&on);
>   ioctl(sockfd,FIONBIO,&on);
>   sigemptyset(&zeromask);
>   sigemptyset(&oldmask);
>   sigemptyset(&newmask);
>   sigaddset(&newmask,SIGIO);
>   sigprocmask(SIG_BLOCK,&newmask,&oldmask);
>   for(;;){
> while(nqueue==0)
>   sigsuspend(&zeromask);
> sigprocmask(SIG_SETMASK,&oldmask,NULL);
>
> sendto(sockfd,dg[iget].dg_data,dg[iget].dg_len,0,dg[iget].dg_sa,dg[iget].dg
>_salen); if(++iget>=QSIZE)
> iget=0;
> sigprocmask(SIG_BLOCK,&newmask,&oldmask);
> nqueue--;
>   }
> }
> static void sig_io(int signo){
>   printf("sig_io called");
>   ssize_t len;
>   int nread;
>   DG *ptr;
>   for(nread=0;;){
>  if(nqueue>=QSIZE)
>err_quit("receive overflow");
>  ptr=&dg[iput];
>  ptr->dg_salen=clilen;
>  len=recvfrom(sockfd,ptr->dg_data,MAXDG,0,ptr->dg_sa,&ptr->dg_salen);
>  if(len<0){
>if(errno==EWOULDBLOCK)
>   break;
>else
>   err_sys("recvfrom error");
>  }
>  ptr->dg_len=len;
>  nread++;
>  nqueue++;
>  if(++iput>=QSIZE)
> iput=0;
>}
>cntread[nread]++;
> }
> static void sig_hup(int signo){
>   printf("sig_hup called");
>   int i;
>   for(i=0;i<=QSIZE;i++)
> printf("cntread[%d]=%ld\n",i,cntread[i]);
> }
> ---
> Confidentiality Notice: The information contained
> in this e-mail and any accompanying attachment(s) is intended only for the
> use of the intended recipient and may be confidential and/or privileged of
> Neusoft Corporation, its subsidiaries and/or its affiliates. If any reader
> of this communication is not the intended recipient, unauthorized use,
> forwarding, printing,  storing, disclosure or copying is strictly
> prohibited, and may be unlawful.If you have received this communication in
> error,please immediately notify the sender by return e-mail, and delete the
> original message and all copies from your system. Thank you.
> ---
>

I think you need #include 

-- 
Mike Jeays
http://www.jeays.ca
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


why printf() don't work?

2009-01-04 Thread Edward King
I use FreeBSD7.0,and use signal,like follows:
signal(SIGHUP,sig_hup);
signal(SIGIO,sig_io);

when I run call following code,it can run,but I find a puzzled question,it 
should print some information,such as printf("execute main()") will print 
execute main(),but in fact,printf fuction print none!!! Why printf function do 
not go work?

my code is follows:

#include "sys/ioctl.h"
#include "unp.h"
static int sockfd;
#define QSIZE 8
#define MAXDG 4096
typedef struct{
  void *dg_data;
  size_t dg_len;
  struct sockaddr *dg_sa;
  socklen_t dg_salen;
}DG;
static DG dg[QSIZE];
static long cntread[QSIZE+1];
static int iget;
static int iput;
static int nqueue;
static socklen_t clilen;
static void sig_io(int);
static void sig_hup(int);

int main(int argc,char **argv){
  printf("execute main()");
  int sockfd;
  struct sockaddr_in servaddr,cliaddr;
  sockfd=socket(AF_INET,SOCK_DGRAM,0);
  bzero(&servaddr,sizeof(servaddr));
  servaddr.sin_family=AF_INET;
  servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
  servaddr.sin_port=htons(SERV_PORT);
  bind(sockfd,(SA *)&servaddr,sizeof(servaddr));
  dg_echo(sockfd,(SA *)&cliaddr,sizeof(cliaddr));
}
void dg_echo(int sockfd_arg,SA *pcliaddr,socklen_t clilen_arg){
  printf("called dg_echo");
  int i;
  const int on=1;
  sigset_t zeromask,newmask,oldmask;
  sockfd=sockfd_arg;
  clilen=clilen_arg;
  for(i=0;i=QSIZE)
iget=0;
sigprocmask(SIG_BLOCK,&newmask,&oldmask);
nqueue--;
  }
}
static void sig_io(int signo){
  printf("sig_io called");
  ssize_t len;
  int nread;
  DG *ptr;
  for(nread=0;;){
 if(nqueue>=QSIZE)
   err_quit("receive overflow");
 ptr=&dg[iput];
 ptr->dg_salen=clilen;
 len=recvfrom(sockfd,ptr->dg_data,MAXDG,0,ptr->dg_sa,&ptr->dg_salen);
 if(len<0){
   if(errno==EWOULDBLOCK)
  break;
   else
  err_sys("recvfrom error");
 }
 ptr->dg_len=len;
 nread++;
 nqueue++;
 if(++iput>=QSIZE)
iput=0;
   }
   cntread[nread]++;
}
static void sig_hup(int signo){
  printf("sig_hup called");
  int i;
  for(i=0;i<=QSIZE;i++)
printf("cntread[%d]=%ld\n",i,cntread[i]);
}
---
Confidentiality Notice: The information contained in this e-mail and any 
accompanying attachment(s) 
is intended only for the use of the intended recipient and may be confidential 
and/or privileged of 
Neusoft Corporation, its subsidiaries and/or its affiliates. If any reader of 
this communication is 
not the intended recipient, unauthorized use, forwarding, printing,  storing, 
disclosure or copying 
is strictly prohibited, and may be unlawful.If you have received this 
communication in error,please 
immediately notify the sender by return e-mail, and delete the original message 
and all copies from 
your system. Thank you. 
---
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"