Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
  int c, flags;
  mqd_t mqd;
  
  flags = O_RDWR | O_CREAT;
  while( (c = Getopt(argc, argv, "em:z:")) !=-1){/*Getopt命令行参数解析*/
    switch(c){
    case 'e':
      flags |= O_EXCL;
      break;
    case 'm':
      attr.mq_maxmsg = atol(optarg);
      break;
    case 'z':
      attr.mq_msgsize = atol(optarg);
      break;
    }
  }

  if(optind != argc -1)
    err_quit("usage; mqcreate [-e] [-m maxmsg -z msgsize] <name>");

  if((attr.mq_maxmsg != 0 && attr.mq_msgsize == 0) ||
     (attr.mq_maxmsg == 0 && attr.mq_msgsize != 0))	
    err_quit("must specify both -m maxmsg and -z msgszie");

  mqd = Mq_open(argv[optind], flags, FILE_MODE, (attr.mq_maxmsg != 0)? &attr:NULL);
  Mq_close(mqd);
  exit(0);
}
Ejemplo n.º 2
0
int
main(int argc, char **argv)
{
	int		i, nloop;
	mqd_t	mq1, mq2;
	char	buff[MSGSIZE];
	pid_t	childpid;
	struct mq_attr	attr;

	if (argc != 2)
		err_quit("usage: lat_pxmsg <#loops>");
	nloop = atoi(argv[1]);

	attr.mq_maxmsg = MAXMSG;
	attr.mq_msgsize = MSGSIZE;
	mq1 = Mq_open(Px_ipc_name(NAME1), O_RDWR | O_CREAT, FILE_MODE, &attr);
	mq2 = Mq_open(Px_ipc_name(NAME2), O_RDWR | O_CREAT, FILE_MODE, &attr);

	if ( (childpid = Fork()) == 0) {
		for ( ; ; ) {			/* child */
			if (Mq_receive(mq1, buff, MSGSIZE, NULL) != 1)
				err_quit("mq_receive error");
		    Mq_send(mq2, buff, 1, 0);
		}
		exit(0);
	}
		/* 4parent */
	doit(mq1, mq2);

	Start_time();
	for (i = 0; i < nloop; i++)
		doit(mq1, mq2);
	printf("latency: %.3f usec\n", Stop_time() / nloop);

	Kill(childpid, SIGTERM);
	Mq_close(mq1);
	Mq_close(mq2);
	Mq_unlink(Px_ipc_name(NAME1));
	Mq_unlink(Px_ipc_name(NAME2));
	exit(0);
}
Ejemplo n.º 3
0
int main(int argc, char **argv)
{
	if (argc != 2)
		err_quit("Usage nft <name>");
	mqd = Mq_open(argv[1], O_RDONLY | O_NONBLOCK);
	Mq_getattr(mqd, &attr);

	sigev.sigev_notify = SIGEV_THREAD;
	sigev.sigev_value.sival_ptr = NULL;
	sigev.sigev_notify_function = notify_thread;
	sigev.sigev_notify_attributes = NULL;
	Mq_notify(mqd, &sigev);

	for ( ; ; )
		pause();
	exit(0);
}
Ejemplo n.º 4
0
/* $$.bp$$ */
int
main(int argc, char **argv)
{
	int		nfds;
	char	c;
	fd_set	rset;
	mqd_t	mqd;
	void	*buff;
	ssize_t	n;
	struct mq_attr	attr;
	struct sigevent	sigev;

	if (argc != 2)
		err_quit("usage: mqnotifysig5 <name>");

		/* 4open queue, get attributes, allocate read buffer */
	mqd = Mq_open(argv[1], O_RDONLY | O_NONBLOCK);
	Mq_getattr(mqd, &attr);
	buff = Malloc(attr.mq_msgsize);

	Pipe(pipefd);

		/* 4establish signal handler, enable notification */
	Signal(SIGUSR1, sig_usr1);
	sigev.sigev_notify = SIGEV_SIGNAL;
	sigev.sigev_signo = SIGUSR1;
	Mq_notify(mqd, &sigev);

	FD_ZERO(&rset);
	for ( ; ; ) {
		FD_SET(pipefd[0], &rset);
		nfds = Select(pipefd[0] + 1, &rset, NULL, NULL, NULL);

		if (FD_ISSET(pipefd[0], &rset)) {
			Read(pipefd[0], &c, 1);
			Mq_notify(mqd, &sigev);			/* reregister first */
			while ( (n = mq_receive(mqd, buff, attr.mq_msgsize, NULL)) >= 0) {
				printf("read %ld bytes\n", (long) n);
			}
			if (errno != EAGAIN)
				err_sys("mq_receive error");
		}
	}
	exit(0);
}
Ejemplo n.º 5
0
int main(int argc, char **argv)
{
	if (argc != 2)
		err_quit("Usage: nts1 <name>");

	mqd = Mq_open(argv[1], O_RDONLY);
	Mq_getattr(mqd, &attr);
	buff = (void *)Malloc(attr.mq_msgsize);

	Signal(SIGUSR1, sig_usr1);
	sigev.sigev_notify = SIGEV_SIGNAL;
	sigev.sigev_signo = SIGUSR1;
	Mq_notify(mqd, &sigev);

	for ( ; ; )
		pause();
	exit(0);
}
Ejemplo n.º 6
0
int main(int argc, char **argv)
{
	mqd_t mqd;
	void *buff;
	ssize_t n;
	sigset_t zeromask, newmask, oldmask;
	struct mq_attr attr;
	struct sigevent sigev;

	if (argc != 2)
		err_quit("Usage: nfs3 <name>");

	mqd = Mq_open(argv[1], O_RDONLY | O_NONBLOCK);
	Mq_getattr(mqd, &attr);
	buff = (void *)Malloc(attr.mq_msgsize);

	Sigemptyset(&zeromask);
	Sigemptyset(&newmask);
	Sigemptyset(&oldmask);
	Sigaddset(&newmask, SIGUSR1);

	Signal(SIGUSR1, sig_usr1);
	sigev.sigev_notify = SIGEV_SIGNAL;
	sigev.sigev_signo = SIGUSR1;
	Mq_notify(mqd, &sigev);

	for ( ; ; )
	{
		Sigprocmask(SIG_BLOCK, &newmask, &oldmask);
		while (mqflag == 0)
			sigsuspend(&zeromask);
		mqflag = 0;

		Mq_notify(mqd, &sigev);
		while ((n = mq_receive(mqd, buff, attr.mq_msgsize, NULL)) >= 0)
		{
			printf("read %ld bytes\n", (long)n);
		}
		if (errno != EAGAIN)
			err_sys("mq_receive error");
		Sigprocmask(SIG_UNBLOCK, &newmask, NULL);
	}
	exit(0);
}
Ejemplo n.º 7
0
int
main(int argc, char **argv)
{
	mqd_t	mqd;
	void	*buff;
	ssize_t	n;
	sigset_t	zeromask, newmask, oldmask;
	struct mq_attr	attr;
	struct sigevent	sigev;

	if (argc != 2)
		err_quit("usage: mqnotifysig2 <name>");

		/* 4open queue, get attributes, allocate read buffer */
	mqd = Mq_open(argv[1], O_RDONLY);
	Mq_getattr(mqd, &attr);
	buff = Malloc(attr.mq_msgsize);

	Sigemptyset(&zeromask);		/* no signals blocked */
	Sigemptyset(&newmask);
	Sigemptyset(&oldmask);
	Sigaddset(&newmask, SIGUSR1);

		/* 4establish signal handler, enable notification */
	Signal(SIGUSR1, sig_usr1);
	sigev.sigev_notify = SIGEV_SIGNAL;
	sigev.sigev_signo = SIGUSR1;
	Mq_notify(mqd, &sigev);

	for ( ; ; ) {
		Sigprocmask(SIG_BLOCK, &newmask, &oldmask);	/* block SIGUSR1 */
		while (mqflag == 0)
			sigsuspend(&zeromask);
		mqflag = 0;		/* reset flag */

		Mq_notify(mqd, &sigev);			/* reregister first */
		n = Mq_receive(mqd, buff, attr.mq_msgsize, NULL);
		printf("read %ld bytes\n", (long) n);
		Sigprocmask(SIG_UNBLOCK, &newmask, NULL);	/* unblock SIGUSR1 */
	}
	exit(0);
}
Ejemplo n.º 8
0
int
main(int argc, char **argv)
{
	mqd_t	mqd;
	void	*ptr;
	size_t	len;
	uint_t	prio;

	if (argc != 4)
		err_quit("usage: mqsend <name> <#bytes> <priority>");
	len = atoi(argv[2]);
	prio = atoi(argv[3]);

	mqd = Mq_open(argv[1], O_WRONLY);

	ptr = Calloc(len, sizeof(char));
	Mq_send(mqd, ptr, len, prio);

	exit(0);
}
Ejemplo n.º 9
0
int
main(int argc, char **argv)
{
	mqd_t	mqd;
	struct mq_attr	attr;

	if (argc != 2)
		err_quit("usage: mqgetattr <name>");

	mqd = Mq_open(argv[1], O_RDONLY);

	Mq_getattr(mqd, &attr);
	printf("max #msgs = %ld, max #bytes/msg = %ld, "
		   "#currently on queue = %ld\n",
		   attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs);

	Mq_close(mqd);
	
	exit(0);
}
Ejemplo n.º 10
0
int
main(int argc, char **argv)
{
	int		signo;
	mqd_t	mqd;
	void	*buff;
	ssize_t	n;
	sigset_t	newmask;
	struct mq_attr	attr;
	struct sigevent	sigev;

	if (argc != 2)
		err_quit("usage: mqnotifysig4 <name>");

		/* 4open queue, get attributes, allocate read buffer */
	mqd = Mq_open(argv[1], O_RDONLY | O_NONBLOCK);
	Mq_getattr(mqd, &attr);
	buff = Malloc(attr.mq_msgsize);

	Sigemptyset(&newmask);
	Sigaddset(&newmask, SIGUSR1);
	Sigprocmask(SIG_BLOCK, &newmask, NULL);		/* block SIGUSR1 */

		/* 4establish signal handler, enable notification */
	sigev.sigev_notify = SIGEV_SIGNAL;
	sigev.sigev_signo = SIGUSR1;
	Mq_notify(mqd, &sigev);

	for ( ; ; ) {
		Sigwait(&newmask, &signo);
		if (signo == SIGUSR1) {
			Mq_notify(mqd, &sigev);			/* reregister first */
			while ( (n = mq_receive(mqd, buff, attr.mq_msgsize, NULL)) >= 0) {
				printf("read %ld bytes\n", (long) n);
			}
			if (errno != EAGAIN)
				err_sys("mq_receive error");
		}
	}
	exit(0);
}
Ejemplo n.º 11
0
int
main(int argc, char **argv)
{
	int		c, flags;
	mqd_t	mqd;

	flags = O_RDWR | O_CREAT;
	while ( (c = Getopt(argc, argv, "em:z:")) != -1) {
		switch (c) {
		case 'e':
			flags |= O_EXCL;
			break;

		case 'm':
			attr.mq_maxmsg = atol(optarg);
			break;

		case 'z':
			attr.mq_msgsize = atol(optarg);
			break;
		}
	}
	if (optind != argc - 1)
		err_quit("usage: mqcreate [ -e ] [ -m maxmsg -z msgsize ] <name>");

	if ((attr.mq_maxmsg != 0 && attr.mq_msgsize == 0) ||
		(attr.mq_maxmsg == 0 && attr.mq_msgsize != 0))
		err_quit("must specify both -m maxmsg and -z msgsize");

	printf("about to sleep for 30 seconds before mq_open\n");
	sleep(30);

	mqd = Mq_open(argv[optind], flags, FILE_MODE,
				  (attr.mq_maxmsg != 0) ? &attr : NULL);

	printf("mq_open OK, about to sleep for 30 more seconds\n");
	sleep(30);

	Mq_close(mqd);
	exit(0);
}
int main (int argc, char *argv[]) {
    struct mq_attr attr, old_attr, new_attr;     // To store queue attributes
    mqd_t mqdes;     // Message queue descriptors
    int ret;

    if( argc < 2){
        printf("Usage: ./a.out < string to be send through message queue >\n");
        exit(0);
    }

    strcpy(buf_send,argv[1]);

    mqdes = Mq_open();
    if(mqdes < 0){
        exit(-1);
    }

    /* Function used to get the attributes of the posix message queue */
    ret = mq_getattr(mqdes, &attr);
    if(mqdes == -1){
        printf("Error in getting attribute from 'Prakash' message queue\n");
        printf("Error number = %d\n",errno);
        exit(errno);;
    } else {
        printf("\n============================================================\n\n");
        printf("Success in getting attribute from 'Prakash' message queue\n");
        printf("Maxmsg = %ld\n",attr.mq_maxmsg);
        printf("Maxsize = %ld\n",attr.mq_msgsize);
        printf("flag = %ld\n",attr.mq_flags);
        printf("\n============================================================\n");
    }


    /* Function used to send the message on the message queue */
    ret = mq_send (mqdes, buf_send, sizeof(buf_send), 1);
    if(ret == -1){
        printf("Error in sending the buffer\n");
        printf("Error number = %d\n",errno);
        exit(errno);
    } else {
        printf(" Message send : [ %s ]\n",buf_send);
    } 


    /* Function used to receive the message from the message queue 
       The 3rd argument of the mesasage queue must be greater than mq_msgsize 
       value. If not we will get a error " Message too long" */
    ret = mq_receive(mqdes, buf_recv, attr.mq_msgsize + 1 , 0);
    if(ret == -1){
        perror("mq_receive");
        printf("Error in receiving the buffer\n");
        printf("Error number = %d\n",errno);
        exit(errno);
    } else {
        printf(" Message received : [ %s ]\n",buf_recv);
    } 

    /* Trying to change the value of the mq_attr structure using 
       mq_setattr() function call */ 

    new_attr.mq_maxmsg = 11;   // These feilds will get innored as they can't be changed.
    new_attr.mq_msgsize = 4096; // These feilds will get innored as they can't be changed.
    new_attr.mq_flags = O_NONBLOCK;   // The only flag which can be changed by mq_setattr fucntion

    ret = mq_setattr(mqdes, &new_attr,&old_attr);
    if(mqdes == -1){
        printf("Error in setting attribute to 'Prakash' message queue\n");
        printf("Error number = %d\n",errno);
        exit(errno);;
    } 

    /* Verifying the set value in the above step */
    ret = mq_getattr(mqdes, &attr);
    if(mqdes == -1){
        printf("Error in getting attribute from 'Prakash' message queue\n");
        printf("Error number = %d\n",errno);
        exit(errno);;
    } else {
        printf("\n============================================================\n\n");
        printf("Success in getting attribute from 'Prakash' message queue\n");
        printf("Maxmsg = %ld\n",attr.mq_maxmsg);
        printf("Maxsize = %ld\n",attr.mq_msgsize);
        printf("flag = %ld\n",attr.mq_flags);
        printf("mq_curmsgs %ld\n",attr.mq_curmsgs);
        printf("\n============================================================\n");
    }

    /* Closing the message queue discriptor
       Note:  Only closing the discriptor, not removing the message queue */

    ret = mq_close(mqdes);
    if(ret == -1){
        printf("Error in closing the  'Prakash' message queue\n");
        printf("Error number = %d\n",errno);
        exit(errno);
    }
    

    /* removing the message queue from the file system */
    ret = mq_unlink (NAME);
    if(ret == -1){
        printf("Error in removng the 'Prakash' message queue\n");
        printf("Error number = %d\n",errno);
        exit(errno);
    }  
    return 0;
}