Visit Our UNIX and Linux User Community


why printf() function don't go work?


 
Thread Tools Search this Thread
Top Forums Programming why printf() function don't go work?
# 1  
Old 01-05-2009
Question why printf() function don't go work?

I use FreeBSD,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?

Another question:
I use a udp client to send message to this code,this code can run well,when upd client quit,this code should call sig_hup(),but when the client quit,this code don't call sig_hup(),why? How to correct my code to call sig_hup() when client quit?

my code is follows:

Code:
#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;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]);
}


Last edited by konvalo; 01-05-2009 at 04:22 AM..
# 2  
Old 01-05-2009
I'm not sure ( I didn't look closely at the code), but calling an IO function while handling an IO signal might be a problem. Also, when printing within signals, it's a good idea to call flush().

Also, why would you expect the program to call SIGHUP on itself when exiting? That's only the case if you're connected to a TTY which is closed on you.
# 3  
Old 01-05-2009
Quote:
Originally Posted by otheus
I'm not sure ( I didn't look closely at the code), but calling an IO function while handling an IO signal might be a problem. Also, when printing within signals, it's a good idea to call flush().
One could also print to the stderr stream instead of stdout, since stderr never needs flush.
Code:
fprintf(stderr, "This is a printf call %d\n", 1234);


Previous Thread | Next Thread
Test Your Knowledge in Computers #361
Difficulty: Medium
The rmdir command remove directories, even if they are not empty.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Equivalence classes don't work

Hello: I can't get equivalence classes to work in globs or when passing them to tr. If I understood correctly, matches e, é, è, ê, etc. But when using them with utilities like tr they don't work. Here's an example found in the POSIX standard: I decided to create the aforementioned files in... (9 Replies)
Discussion started by: Cacializ
9 Replies

2. UNIX for Dummies Questions & Answers

printf function

#include<stdio.h> int counter; int fibonacci(int n) { counter += 1; if ( n <= 2 ) return 1; else return fibonacci(n-1) + fibonacci(n-2); } int main(void) { int i; int sum ; for( i = 1 ; i<= 10; i++) { counter = 0; sum... (1 Reply)
Discussion started by: vincent__tse
1 Replies

3. HP-UX

awk don't work in hp-ux 11.11

Hello all! I have problem in hp-ux 11.11 in awk I want to grep sar -d 2 1 only 3 column, but have error in awk in hp-ux 11.11 Example: #echo 123 234 | awk '{print $2}' 123 234 The situattions in commands bdf | awk {print $5}' some... In hp-ux 11.31 - OK! How resolve problem (15 Replies)
Discussion started by: ostapv
15 Replies

4. Programming

Internals of the printf function?

hey all, im a new programmer. i was wondering how you would go about writing the printf function yourself? it is my understanding that when you call printf you are calling an already written function and just providing an argument? if this is the case, is it possible to write that function... (8 Replies)
Discussion started by: Christian.B
8 Replies

5. Programming

why printf don't work?

I use Solaris 10, I use following code: #include <signal.h> int main(void){ printf("----------testing-----------"); if(signal(SIGUSR1,sig_usr)==SIG_ERR) err_sys("can't catch SIGUSR1"); for(;;) pause(); sig_user(int signo){ ..... } when I run above code,it print nothing... (3 Replies)
Discussion started by: konvalo
3 Replies

6. Programming

why daytime don't work?

Following code is detecting solaris daytime,when I run it,I can't get any result,code is follows: #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define BUFFSIZE 150 int main(){ ... (2 Replies)
Discussion started by: konvalo
2 Replies

7. Shell Programming and Scripting

Use variable in sed don't work.

Hi all. I have a script as below: cutmth=`TZ=CST+2160 date +%b` export cutmth echo $cutmth >> date.log sed -n "/$cutmth/$p" alert_sbdev1.log > alert_summ.log My purpose is to run through the alert_sbdev1.log and find the 1st occurence of 'Jan' and send everything after that line to... (4 Replies)
Discussion started by: ahSher
4 Replies

8. UNIX for Dummies Questions & Answers

Things in tutorials that don't work.

I am thankful for this site and for the many links provided. I have been going through one of the tutorials, but as I try some things, they don't seem to work. I am wondering if there is something I need first before being able to use a tutorial (like version number (HP-UX) or how I am getting... (1 Reply)
Discussion started by: arungavali
1 Replies

9. Post Here to Contact Site Administrators and Moderators

How come sigs don't work?

They appear to be turned on, I entered mine in. The check boxes are all checked. And yet, no sigs? (4 Replies)
Discussion started by: l008com
4 Replies

Featured Tech Videos