The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
unable to log into gdm, urgent help needed!!!pls wrapster Shell Programming and Scripting 0 04-25-2008 12:48 AM
urgent needed manish1 Shell Programming and Scripting 0 03-13-2008 05:09 AM
Urgent help needed skyineyes UNIX for Dummies Questions & Answers 5 06-19-2007 03:31 AM
urgent help needed. Batch Shell Programming and Scripting 4 12-08-2006 11:31 AM
urgent help needed !! guhas Shell Programming and Scripting 0 09-10-2005 01:01 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-19-2005
mgchato's Avatar
mgchato mgchato is offline
Registered User
  
 

Join Date: May 2005
Posts: 7
Message queues and coordination programs...again

Hey all!

I'm supposed to design and implement a set of programs that allow users logged on to a host to send messages to each other.

I already started with a messaging program which should be made of a server and a client(Thanx a lot for your help Blowtorch). I'm supposed to add this program to a coordination program. The coordinator should accept messages from the clients and forward them to all attached clients. I'm also expected to add a command to the client that allows it to log to a specified text file for all received messages.

I'm thinking that a good way to handle his would be to encapsulate the message queue in a module that the clients and coordinator may call, but the thing is that I have not even the slightest idea where to start! Do u guys know any books or websites where I can get sample code and explanations from? I think the only way for me to learn is through examples...

P.D. How many .c files would I need?

Thanx a lot for your help!!!

Oh, and here are my messenger programs:

Code:
/*q.h*/
#ifndef Q_H
	#define Q_H
	#define QKEY ftok("q.h",1)
	#define MSGSZ 128

	#ifdef DEBUG
		#define DEBUG_PRINT(...) fprintf(stderr,__VA_ARGS__)
		/* this __VA_ARGS__ thing allows a macro to
		take a varialble number of arguments */
	#else
		#define DEBUG_PRINT(...) /* __VA_ARGS__ */
	#endif

	int init_queue();
	int exchange_pids();
	void remove_queue();
	int send_mesg(char*,int);
	int send_mesg_signal(char*,int);
	int recv_mesg(char*,int);
#endif



/*signalmsg.c*/
#include<signal.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<errno.h>
#include"q.c"
#include"q.h"

int cnt;
void hndlr(int);

main()
{
        struct sigaction buf;
        char msg[MSGSZ];
        int stop=0,n;
        sigfillset(&buf.sa_mask);
        buf.sa_flags=0;
        buf.sa_handler=hndlr;
        sigaction(SIGUSR1,&buf,NULL);
        sigaction(SIGINT,&buf,NULL);
        init_queue();
        exchange_pids();
        
while(!stop)
        {
                printf("\nEnter message: ");
                while (-1 == (n=read(0,msg,MSGSZ-1) ))
                {
                        if (errno != EINTR )
                        {
                           perror("read");
                           send_mesg("bye\n",0);
                           exit(1);
                        }
                        for(;cnt>0;cnt--)
                        {
                           recv_mesg(msg,0);
                           printf("\nReceived Message: %s\n", msg);
                           if (0==strcmp(msg,"bye\n"))
                           {
                                remove_queue();
                                exit(0);
                           }
                        }
                        printf("\nEnter message: ");
                }
                msg[n]=0;
                send_mesg_signal(msg,0);
                if (0==strcmp(msg,"bye\n"))
                        stop=1;
        }
        return 0;
}

void hndlr(int sig)
{
        DEBUG_PRINT("Caught signal: %d\n",sig);
        switch(sig)
        {
        case SIGUSR1:   DEBUG_PRINT("USR1 branch\n");
                        cnt++;
                        break;
        case SIGINT:    DEBUG_PRINT("SIGINT branch\n");
                        send_mesg_signal("bye\n",0);
                        exit(0);
                        break;
        default:        fprintf(stderr,"Yikes how did I get here?!!!\n");
                        exit(1);
        }

}



/*q.c*/
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<unistd.h>
#include<signal.h>
#include<errno.h>
#include"q.h"

struct mymsg
{
        long mtype;
        char mtext[MSGSZ];
};

static int qid;
static int channel_no;
static pid_t remote_pid;

void remove_queue()
{
        msgctl(qid,IPC_RMID,0);
}

int init_queue()
{
        char  msg[MSGSZ];
        int rv;
        if( -1 != ( qid = msgget(QKEY,IPC_CREAT|IPC_EXCL|0600) ))
        {
                /* am entity A using channel 1 */
                channel_no=1;
                DEBUG_PRINT("Am entity A\n");
                return 0;
        }
        if ( errno != EEXIST )
        {
                perror("MSGGET:");
                return -1;
        }
        if (-1 != ( qid = msgget(QKEY,IPC_CREAT|0600) ))
        {
                /* am entity B using channel 2 */
                 channel_no=2;
                DEBUG_PRINT("Am entity B\n");
                return 0;
        }

        perror("MSGGET:");
        return -1;
}

int exchange_pids()
{
        char msg[MSGSZ];
        if(channel_no==1)
        {
                /* sending PID to other end */
                sprintf(msg,"%d",getpid());
                send_mesg(msg,0);
                DEBUG_PRINT("My pid is %s\n",msg);
                /* grab pid from other end */
                recv_mesg(msg,0);
                sscanf(msg,"%d",&remote_pid); 
                DEBUG_PRINT("Received PID is %s\n",msg);
        }
        else
        {
                /* grab pid from other end */
                recv_mesg(msg,0);
                sscanf(msg,"%d",&remote_pid);
                DEBUG_PRINT("Received PID is %s\n",msg);
                
                /* sending PID to other end */
                sprintf(msg,"%d",getpid());
                send_mesg(msg,0);
                DEBUG_PRINT("My pid is %s\n",msg);
        }
        return remote_pid;
}

int recv_mesg(char *msg, int nb)
{
        struct mymsg m;
        int options=0,len;
        int recv=((channel_no==1)?2:1);
        DEBUG_PRINT("Receiving on %d\n",recv);
        if (nb) options|=IPC_NOWAIT;
        if( -1==(len=msgrcv(qid,&m,MSGSZ,recv,options)))
                return -1;
        strcpy(msg,m.mtext);
        return len;
}

int send_mesg(char* msg,int nb)
{
        struct mymsg m;
        int options,len;
        m.mtype=channel_no;
        strcpy(m.mtext,msg);
        len=strlen(msg)+1;
        options=0;
        if (nb) options|=IPC_NOWAIT;
        DEBUG_PRINT("Sending type %d\n",m.mtype);
        return msgsnd(qid,&m,len,options);
}

int send_mesg_signal(char*msg,int nb)
{
        int rv;

        rv=send_mesg(msg,nb);
        kill(remote_pid,SIGUSR1);
        return rv;
}

Last edited by mgchato; 05-19-2005 at 03:34 AM..
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0