Sponsored Content
Top Forums Programming why printf() function don't go work? Post 302273506 by konvalo on Monday 5th of January 2009 01:39:45 AM
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..
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
SENDMMSG(2)						     Linux Programmer's Manual						       SENDMMSG(2)

NAME
sendmmsg - send multiple messages on a socket SYNOPSIS
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <sys/socket.h> int sendmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, int flags); DESCRIPTION
The sendmmsg() system call is an extension of sendmsg(2) that allows the caller to transmit multiple messages on a socket using a single system call. (This has performance benefits for some applications.) The sockfd argument is the file descriptor of the socket on which data is to be transmitted. The msgvec argument is a pointer to an array of mmsghdr structures. The size of this array is specified in vlen. The mmsghdr structure is defined in <sys/socket.h> as: struct mmsghdr { struct msghdr msg_hdr; /* Message header */ unsigned int msg_len; /* Number of bytes transmitted */ }; The msg_hdr field is a msghdr structure, as described in sendmsg(2). The msg_len field is used to return the number of bytes sent from the message in msg_hdr (i.e., the same as the return value from a single sendmsg(2) call). The flags argument contains flags ORed together. The flags are the same as for sendmsg(2). A blocking sendmmsg() call blocks until vlen messages have been sent. A nonblocking call sends as many messages as possible (up to the limit specified by vlen) and returns immediately. On return from sendmmsg(), the msg_len fields of successive elements of msgvec are updated to contain the number of bytes transmitted from the corresponding msg_hdr. The return value of the call indicates the number of elements of msgvec that have been updated. RETURN VALUE
On success, sendmmsg() returns the number of messages sent from msgvec; if this is less than vlen, the caller can retry with a further sendmmsg() call to send the remaining messages. On error, -1 is returned, and errno is set to indicate the error. ERRORS
Errors are as for sendmsg(2). An error is returned only if no datagrams could be sent. See also BUGS. VERSIONS
The sendmmsg() system call was added in Linux 3.0. Support in glibc was added in version 2.14. CONFORMING TO
sendmmsg() is Linux-specific. NOTES
The value specified in vlen is capped to UIO_MAXIOV(1024). BUGS
If an error occurs after at least one message has been sent, the call succeeds, and returns the number of messages sent. The error code is lost. The caller can retry the transmission, starting at the first failed message, but there is no guarantee that, if an error is returned, it will be the same as the one that was lost on the previous call. EXAMPLE
The example below uses sendmmsg() to send onetwo and three in two distinct UDP datagrams using one system call. The contents of the first datagram originates from a pair of buffers. #define _GNU_SOURCE #include <netinet/ip.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> int main(void) { int sockfd; struct sockaddr_in addr; struct mmsghdr msg[2]; struct iovec msg1[2], msg2; int retval; sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd == -1) { perror("socket()"); exit(EXIT_FAILURE); } addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); addr.sin_port = htons(1234); if (connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) { perror("connect()"); exit(EXIT_FAILURE); } memset(msg1, 0, sizeof(msg1)); msg1[0].iov_base = "one"; msg1[0].iov_len = 3; msg1[1].iov_base = "two"; msg1[1].iov_len = 3; memset(&msg2, 0, sizeof(msg2)); msg2.iov_base = "three"; msg2.iov_len = 5; memset(msg, 0, sizeof(msg)); msg[0].msg_hdr.msg_iov = msg1; msg[0].msg_hdr.msg_iovlen = 2; msg[1].msg_hdr.msg_iov = &msg2; msg[1].msg_hdr.msg_iovlen = 1; retval = sendmmsg(sockfd, msg, 2, 0); if (retval == -1) perror("sendmmsg()"); else printf("%d messages sent ", retval); exit(0); } SEE ALSO
recvmmsg(2), sendmsg(2), socket(2), socket(7) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. Linux 2018-02-02 SENDMMSG(2)
All times are GMT -4. The time now is 09:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy