[C] Problem with IPC

Hi! I'm trying to write this program: in my intentions it should get a message and send it to a second process (pid_upost), then to a third process (pid1, pid2, pid3, depending on the choice made when a new message is inserted). This program should write the message in a file (message1, message2 or message3) and then send back a message to process pid_upost. My problem is that the program seems to work, but when i finished to type a message, which is correctly written in the file, the while cicle at the end doesn't go on. I thought maybe there is a problem with the waitpid functions, but i can't get the whole thing working... can anybody help me, please? And sorry for my english...
Thank you

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/types.h>

#define MSGSZ 128


main ()
{
FILE *fp1, *fp2, *fp3;
char filename[70];


int remove_res;
int msqid, msqid1, msqid2, msqid3, msqidK;
int status;
int msgflg = IPC_CREAT | 0666;
key_t key;
key_t key1, key2, key3, keyK, keyK2;
size_t buf_length;
int pid_upost, pid1, pid2, pid3;
int temp, temp1, temp2, temp3, temp4, temp5;
int ans;
int i;
int c;
int maxmsgsz;
int msgsz;
struct msgbuf {
	long mtype;
	char mtext[MSGSZ];
	long dest;
} *sbuf, *rbuf, *rbuf1, *rbuf2, *rbuf3, *rbufK, *rbufK2;

do
{

printf("\n\t\t1) Write a new message");
printf("\n\t\t2) View old messages");
printf("\n\t\t3) Delete old messages");
printf("\n\t\t4) Exit");

sbuf = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));

do 
  {
  scanf("%d", &ans);
  } while ((ans < 1) || (ans > 4));


key = ftok("/home/...../file.tmp", 'a');
if (msqid = msgget(key, msgflg) < 0)
	{
	perror("\nError (msgget)\n");
	exit(-1);
	}
	buf_length = MSGSZ;


switch(ans) {
	case 1:
	{
	sbuf->mtype = 1;
	printf("\nType destination number ");
	scanf("%d",&sbuf->dest);
	if ((sbuf->dest < 1) || (sbuf->dest > 3))
		{
		printf("\nError: type 1, 2 or 3\n");
		exit(-1);			
		}
	printf("\nType a message): ");
	while ((c = getchar()) != '\n' && c != EOF);
		for (i = 0; ((c = getchar()) != '\n'); i++){
			sbuf->mtext = c;
		}	
	sbuf->mtext[strlen(sbuf->mtext)] = '\0';
	break;
	}


	case 2:
	{
	sbuf->mtype = 2;
	printf("\nType destination number ");
	scanf("%d", &sbuf->dest);
	if ((sbuf->dest < 1) || (sbuf->dest > 3))
		{
		printf("\nError: type 1, 2 or 3\n");
		exit(-1);			
		}
	strcpy(sbuf->mtext, "View old messages ");
	break;
	}

	case 3:
	{
	sbuf->mtype = 3;
	printf("\nInsert file number : ");
	scanf("%d", &sbuf->dest);
	if ((sbuf->dest < 1) || (sbuf->dest > 3))
		{
		printf("\nError: type 1, 2 or 3\n");
		exit(-1);			
		}
	strcpy(sbuf->mtext, "Delete old messages ");
	break;
	}

	case 4:
	{
	exit(0);
	}
} 


if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0)
	{
	perror("\nError (msgsnd)\n");
	exit(-1);
	}


pid_upost = fork();
if (pid_upost == 0)
	{
	rbuf = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));

	temp = msgrcv(msqid, &rbuf, MSGSZ, 0, MSG_NOERROR);
	if (temp < 0)
		{
	        perror("\nError (msgrcv)\n");
		exit(-1);
		}

	switch (rbuf->dest) {
		case 1:
			{
			key1 = ftok("/home/...../file1.tmp", 'b');
			if (msqid1 = msgget(key1, IPC_CREAT | 0666) < 0)
				{
				perror("\nError (msgget)\n");
				exit(-1);
				}
			buf_length = MSGSZ;
			if (msgsnd(msqid1, &rbuf, buf_length, IPC_NOWAIT) < 0)
				{
				perror("\nError (msgsnd)\n");
				exit(-1);
				}

			pid1 = fork();
			if (pid1 == 0)
				{
				rbuf1 = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));
				temp1 = msgrcv(msqid1, &rbuf1, MSGSZ, 0, MSG_NOERROR);
				if (temp1 < 0)
					{
					perror("\nError (msgrcv)\n");
					exit(-1);
					}

				if ((fp1 = fopen("/home/...../Desktop/message1", "a")) == NULL)				
					{	
					printf("\nError (fopen)\n");
					}
				fprintf(fp1, "Destination: %d\n", rbuf1->dest);				
				fprintf(fp1, "Message: %s\n\n", rbuf1->mtext);
				fclose(fp1);

				rbufK = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));
				rbufK->mtype = 4;
				if (rbuf1->mtype == 3)
					{
					if (remove_res = remove("message1") != 0)
						{
						strcpy(rbufK->mtext, "File not deleted");
						}
					else 
						{
						strcpy(rbufK->mtext, "File message1 deleted");
						}
					}
				else
					{
					strcpy(rbufK->mtext, "Message inserted");
					}

				if (msgsnd(msqid1, &rbufK, buf_length, IPC_NOWAIT) < 0)
					{
					perror("\nError (msgsnd)\n");
					exit(-1);
					}

				}
			break;
			} 

		case 2:
			{
                        /*The same as above, but process pid2 is  
created and file message2 is written*/
			} 

		case 3:
			{
                        /*The same as above, but process pid3 is  
created and file message3 is written*/
			} 

	}


	waitpid(pid1, &status, 0);	
	rbufK2 = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));
	
	temp4 = msgrcv(msqid1, &rbufK2, MSGSZ, 4, MSG_NOERROR);
	if (temp4 < 0)
		{
		perror("\nError (msgrcv)\n");
		exit(-1);
		}
	else
		{
		printf("\nMESSAGE %s RECEIVED\n", rbufK2->mtext);
		}

	}

waitpid(pid_upost, &status, 0);

}
while (ans != 4);
}


This way the program works, but how can i send a message from process pid1 to his parent? I must open a new queue? Any suggestions? Thanks

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/types.h>

#define MSGSZ 128


main ()
{
FILE *fp1, *fp2, *fp3;
char filename[70];


int remove_res;
int msqid, msqid1, msqid2, msqid3, msqidK;
int status;
int msgflg = IPC_CREAT | 0666;
key_t key;
key_t key1, key2, key3, keyK, keyK2;
size_t buf_length;
int pid_upost, pid1, pid2, pid3;
int temp, temp1, temp2, temp3, temp4, temp5;
int ans;
int i;
int c;
int maxmsgsz;
int msgsz;
struct msgbuf {
	long mtype;
	char mtext[MSGSZ];
	long dest;
} *sbuf, *rbuf, *rbuf1, *rbuf2, *rbuf3, *rbufK, *rbufK2;

do
{

printf("\n\t\t1) Write a new message");
printf("\n\t\t2) View old messages");
printf("\n\t\t3) Delete old messages");
printf("\n\t\t4) Exit");

sbuf = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));

do 
  {
  scanf("%d", &ans);
  } while ((ans < 1) || (ans > 4));


key = ftok("/home/...../file.tmp", 'a');
if (msqid = msgget(key, msgflg) < 0)
	{
	perror("\nError (msgget)\n");
	exit(-1);
	}
	buf_length = MSGSZ;


switch(ans) {
	case 1:
	{
	sbuf->mtype = 1;
	printf("\nType destination number ");
	scanf("%d",&sbuf->dest);
	if ((sbuf->dest < 1) || (sbuf->dest > 3))
		{
		printf("\nError: type 1, 2 or 3\n");
		exit(-1);			
		}
	printf("\nType a message): ");
	while ((c = getchar()) != '\n' && c != EOF);
		for (i = 0; ((c = getchar()) != '\n'); i++){
			sbuf->mtext = c;
		}	
	sbuf->mtext[strlen(sbuf->mtext)] = '\0';
	break;
	}


	case 2:
	{
	sbuf->mtype = 2;
	printf("\nType destination number ");
	scanf("%d", &sbuf->dest);
	if ((sbuf->dest < 1) || (sbuf->dest > 3))
		{
		printf("\nError: type 1, 2 or 3\n");
		exit(-1);			
		}
	strcpy(sbuf->mtext, "View old messages ");
	break;
	}

	case 3:
	{
	sbuf->mtype = 3;
	printf("\nInsert file number : ");
	scanf("%d", &sbuf->dest);
	if ((sbuf->dest < 1) || (sbuf->dest > 3))
		{
		printf("\nError: type 1, 2 or 3\n");
		exit(-1);			
		}
	strcpy(sbuf->mtext, "Delete old messages ");
	break;
	}

	case 4:
	{
	exit(0);
	}
} 


if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0)
	{
	perror("\nError (msgsnd)\n");
	exit(-1);
	}


pid_upost = fork();
if (pid_upost == 0)
	{
	rbuf = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));

	temp = msgrcv(msqid, &rbuf, MSGSZ, 0, MSG_NOERROR);
	if (temp < 0)
		{
	        perror("\nError (msgrcv)\n");
		exit(-1);
		}

	switch (rbuf->dest) {
		case 1:
			{
			key1 = ftok("/home/...../file1.tmp", 'b');
			if (msqid1 = msgget(key1, IPC_CREAT | 0666) < 0)
				{
				perror("\nError (msgget)\n");
				exit(-1);
				}
			buf_length = MSGSZ;
			if (msgsnd(msqid1, &rbuf, buf_length, IPC_NOWAIT) < 0)
				{
				perror("\nError (msgsnd)\n");
				exit(-1);
				}

			pid1 = fork();
			if (pid1 == 0)
				{
				rbuf1 = (struct msgbuf*)malloc((unsigned)(sizeof(struct msgbuf) - sizeof sbuf->mtext + 128));
				temp1 = msgrcv(msqid1, &rbuf1, MSGSZ, 0, MSG_NOERROR);
				if (temp1 < 0)
					{
					perror("\nError (msgrcv)\n");
					exit(-1);
					}

				if ((fp1 = fopen("/home/...../Desktop/message1", "a")) == NULL)				
					{	
					printf("\nError (fopen)\n");
					}
				fprintf(fp1, "Destination: %d\n", rbuf1->dest);				
				fprintf(fp1, "Message: %s\n\n", rbuf1->mtext);
				fclose(fp1);

				}
                                      exit(0);
			break;
			} 

		case 2:
			{
                        /*The same as above, but process pid2 is  
created and file message2 is written*/
			} 

		case 3:
			{
                        /*The same as above, but process pid3 is  
created and file message3 is written*/
			} 

	}
}
while (ans != 4);
}