msg q


 
Thread Tools Search this Thread
Top Forums Programming msg q
# 1  
Old 01-14-2006
msg q

hi...
i posted the code earlier but it wasnt quite readbale... ive added a few comments and made the indentation problem right...
the problem is that it doent seem to be working... ive almost pulled out my hair tryin to fix the prob
another wierd thing is that it worksthe 1st time when i reboot the system then stops workin
also im getting the msgqid=0...

here is the code:


HTML Code:
/*heres what the codes does... it spawns 3 new threads, so in all there r 4 threads. from 3 of the threads i send a message to the 4th thread, and i am using 1 message queue to hold msgs from different senders. using the type variable to distinduish the msgs.*/


#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdlib.h>
#include<errno.h>

//this is the structure used to hold the message 
struct message
{
        int type;
        int msg;
};
 
int main(void)
{
        pid_t pid0,pid1,pid_p,pid_c1,pid_c2,pid_c3;
        key_t key;
        int msgqid;
        
        struct message *msgs,*msgr;
        pid_p=getpid();
 
        //used to generate the key for msg queue
	if((key=ftok("abc.txt",'c'))==-1)
                perror("Error in ftok\n");
        else
                printf("Ftok executed.Key=%d\n",key);
 	
	//get the msg queue
        if((msgqid=(msgget(key,0666|IPC_CREAT))==-1))
                perror("Error creating queue\n");
        else
                printf("Queue created.MSGQID=%ld\n",msgqid);
 	
	//using the forks so that i have 4 separate threads
        pid0=fork();
        if(pid0<0)
        {
                perror("Error in fork\n");
        }
 
        pid1=fork();
        if(pid1<0)
        {
                perror("Error in fork\n");
        }
 
	/*checks pid as entry level condition. in each of the thread i set a different type
	  to distinguish between the messages.*/
        if(pid0==0 && pid1==0)
        {
                pid_c1=getpid();
                printf("In child.Process ID=%d\n",pid_c1);
                msgs->type=1;
                msgs->msg=1;
                if(msgsnd(msgqid,&(msgs->type),sizeof(struct message),1)==-1)
                {       printf("%d\n",errno);perror("Message not sent.Type 1\n");}
 
                else
                        printf("Message sent.Type 1\n");
        }
 
        if(pid0==0 && pid1!=0)
        {
                pid_c2=getpid();
                printf("In child.Process ID=%d\n",pid_c2);
                msgs->type=2;
                msgs->msg=2;
                if(msgsnd(msgqid,&(msgs->type),sizeof(struct message),2)==-1)
                        perror("Message not sent.Type 2\n");
                else
                        printf("Message sent.Type 2\n");
        }
 
        if(pid0!=0 && pid1==0)
        {
                pid_c3=getpid();
                printf("In child.Process ID=%d\n",pid_c3);
                msgs->type=3;
                msgs->msg=3;
                if(msgsnd(msgqid,&(msgs->type),sizeof(struct message),3)==-1)
                        perror("Message not sent.Type 3\n");
                else
                        printf("Message sent.Type 3\n");
        }
 

	//in the 4th thread i read the message from the message queue based on the type
        if(pid0!=0 && pid1!=0)
        {
                wait(NULL);
                wait(NULL);
                wait(NULL);
                printf("In parent.Process ID=%d\n",pid_p);
                                 
                if(msgrcv(msgqid,&(msgr->type),sizeof(struct message),1,0)==-1)
                        perror("Message not recvd.Type 1\n");
                else
                        printf("Message revcd.Type 1\n");
 
                if(msgrcv(msgqid,&(msgr->type),sizeof(struct message),2,0)==-1)
                        perror("Message not recvd.Type 2\n");
                else
                        printf("Message revcd.Type 2\n");
 
                if(msgrcv(msgqid,&(msgr->type),sizeof(struct message),3,0)==-1)
                        perror("Message not recvd.Type 3\n");
                else
                        printf("Message revcd.Type 3\n");
 
		//destroying the queue

                if(msgctl(msgqid,IPC_RMID,NULL)==-1)
                        perror("Error removing queue\n");
                else
                        printf("Queue removed\n");
        }
 
        exit(0);
}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Error msg

When i run errpt -a, the output was below: --------------------------------------------------------------------------- LABEL: DMPCHK_NOSPACE IDENTIFIER: F89FB899 Date/Time: Tue Jan 17 15:00:02 BEIS Sequence Number: 28998 Machine Id: 0058C0CE4C00 Node Id: ... (2 Replies)
Discussion started by: Ivanku
2 Replies

2. Shell Programming and Scripting

My script is working but why do I get this msg?

Hi, I have a script to determine the max memory to set a worker to start up at. The variable will need to end up being "-Xmx512m" or "-Xmx256m" or "-Xmx768m." Everything works fine BUT when I set the "MEMORY" variable I get the "Cmmand not foundine 40: -Xmx512m" msg though the variable is the... (5 Replies)
Discussion started by: vsekvsek
5 Replies

3. UNIX for Dummies Questions & Answers

viewing the *.msg files

Is there any way we can view the *.msg files in unix / linux ? Example cat aa.msg Or Do we need to do any decryption ? Kindly provide the solution? (0 Replies)
Discussion started by: kingganesh04
0 Replies

4. UNIX for Dummies Questions & Answers

msg input box

Hi! I need a command to ask for input a project name and store the input in a var like in VB a: r = msgbox blablabla but for unix shell I know that is a gdialog --inputbox but then how do I store in the var? and how do I use the text proprity Thanks (0 Replies)
Discussion started by: ruben.rodrigues
0 Replies

5. UNIX for Advanced & Expert Users

Getting 'Killed' msg

Can anyone explain? I start my unix session on AIX, run tcsh move to a particular directory, let say: cd /test/bin and next i run a command like: grep "test string" /test/bin/* to look for the string in any files in the directory. I am getting a response of Killed. Why is that... (16 Replies)
Discussion started by: gio001
16 Replies

6. Shell Programming and Scripting

How to display an error msg?

Hi friends, Please suggest a solution for this. A script has following things. echo "Enter P for PML" echo "Enter V for VVL" echo "Enter L for LNL" echo "Enter G for GDL" read choice echo ${choice} >> LOG_Daily.txt if operator enters anything other that P/V/L/G it should show an... (8 Replies)
Discussion started by: anushree.a
8 Replies

7. Shell Programming and Scripting

msg??

I want to display a message when the script is done running, but I do not want to use echo. Is there another command I can use besides echo? If so, show me how it is done. (1 Reply)
Discussion started by: dshea0001
1 Replies

8. Programming

msg q again!

is it possible to use a msg queue with multiple threads spawned from the same app? (1 Reply)
Discussion started by: strider
1 Replies

9. UNIX for Dummies Questions & Answers

sendmail alert msg- How to

Sorry for such a simple question, but being new cannot find any info.. I am trying to ftp a file and checking if the ftp was successful or not. Either way I want to send an e-mail to myself to left me know if good ftp or ftp was not sent. I have the code in the program to do this and am going... (1 Reply)
Discussion started by: roncayenne
1 Replies

10. IP Networking

Netstat msg

What do you think of the netstat msg about : udp 2320 0 *:xdmcp *:* The 2320? Does that mean an error? I used netstat -l | grep 'xdmcp' to get that, but does the 2320 indicate an error? I'm trying to figure out why, after i've enabled xdmcp to true, it is still not showing up... (2 Replies)
Discussion started by: kymberm
2 Replies
Login or Register to Ask a Question