[C] Problem with IPC


 
Thread Tools Search this Thread
Top Forums Programming [C] Problem with IPC
# 1  
Old 09-09-2008
[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


Code:
#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[i] = 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);
}

# 2  
Old 09-10-2008
...

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


Code:
#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[i] = 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);
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Questions about IPC

Hello and thanks in advance for any help anyone can offer to help me understand this I'm curious about a 30.000 ft view on how IPC works in Linux between parent-child processes...I understand there's multiple types of IPC's... But I'm currently trying to figure out if parent-child process IPC... (1 Reply)
Discussion started by: bodisha
1 Replies

2. HP-UX

IPC settings on HP-UX

Hi Experts, Need your help for checking te interprocess communications settings on HP-UX box. Using ipcs command I am able to view Message queue,semapohores etc, but from that output I m not able to understand how to determine if there is any issue with ipc settings and how to resolve that? (1 Reply)
Discussion started by: sai_2507
1 Replies

3. Programming

Best IPC mechanism to be used

Suppose I have 5 independent process divided in two imaginay sets: set1 set2 --------------------- p1 p3 | | p2 p4 | p5 The processes inside each set communicate mutually quite often. I mean p1 and p2 communicate mutually quite often Similarly p3, p4 and p5 communicate mutually... (2 Replies)
Discussion started by: rupeshkp728
2 Replies

4. Programming

kill() function problem in client-server ipc message using 2 FIFOs

I want to have a message send & receive through 2 uni-direction FIFO Flow of data FIFO1 stdin--->parent(client) writefd--->FIFO1-->child(server) readfd FIFO2 child(server) writefd2---->FIFO2--->parent(client) readfd2--->stdout I need to have boundary structed message... (3 Replies)
Discussion started by: ouou
3 Replies

5. Programming

please help a problem in client-server ipc message 2 pipes communication simple example

I want to have a message send & receive through 2 half-duplex pipes Flow of data top half pipe stdin--->parent(client) fd1--->pipe1-->child(server) fd1 bottom half pipe child(server) fd2---->pipe2--->parent(client) fd2--->stdout I need to have boundary structed message... (1 Reply)
Discussion started by: ouou
1 Replies

6. Programming

IPC Mechanisms

Hi! I wanted to know the advantages / disadvantages of different IPC mechanims such as sockets, pipes (unnamed) , shared memory & message queues. Pipes for example i hear are fast , but are difficult to debug as compared to sockets. Can you guys please name some situations where one is... (4 Replies)
Discussion started by: _korg
4 Replies

7. Solaris

errors on Netra-440: "IPC Warning: ipc: tcp_protocol: bad magic number"

I was asked to look into a problem with a Sun Netra 440 in another department. On the server in question, the relevant 'uname -a' information is, "SunOS host1 5.9 Generic_118558-16 sun4u sparc SUNW,Netra-440". That information aside, while the other admin is logged into the ALOM, these errors are... (0 Replies)
Discussion started by: Borealis
0 Replies

8. Programming

IPC with PIPE

Hi guys, I'm new to Linux and Unix I have just simple code . But I don't know why it doesn't work .. But, the outputfile is Blank.. I don't understand why.. Please help me.. Thank you very much P.S: sorry, I don't know how to edit this post clearly.. it's hard to read.. Please try.. (2 Replies)
Discussion started by: thanh_sam_khac
2 Replies

9. UNIX for Dummies Questions & Answers

Ipc

I have a parent that is passing data to child A and then child A has to process it and pass to child B. I am able to pass the data to child A but am not able to pass it to child B. Child B seems to only be receiving the last data instead of the whole data. I saw one example in a book but it uses... (1 Reply)
Discussion started by: scmay
1 Replies

10. Programming

fork() and IPC

Hey all. I recently ported a multithreaded program from WinNT to LINUX using pthreads. Everything works fine, except that it turns out the drivers for the hardware I'm working with aren't thread safe. So, now I have to find a way to make this multi-process instead of multi-threaded. I just... (1 Reply)
Discussion started by: TelePlayer
1 Replies
Login or Register to Ask a Question