omg i need help so bad. I've been working on a school project for what seems like an eternity and i'm close to deadline. Using FIFO's (i ahve to) to communicate between parent and child proc's. Right now I'm stuck on a read/write.

fifomsg is a struct with int length and char[16] message fields. This is what I'm trying to send between the processes.

child code:

fdw = Open("syscallfifo", O_WRONLY);

fifomsg msg1;        
strcpy(msg1.message, (char*)pid);
msg1.length = sizeof(msg1);

write(fdw, &msg1, sizeof(msg1));
close(fdw);

parent code:

fdr = Open("syscallfifo", O_RDWR);
	
fifomsg msg1;
	
read(fdr, &msg1.length, sizeof(msg1.length));
read(fdr, &msg1.message, sizeof(msg1) - sizeof(msg1.length));
close(fdr);

At this point in execution, the process halts and I have to suspend/kill. I'm begging for help. As soon as possible would be ideal.

If I can't get the struct data to work, I'm going to have to resort to a fixed-sized character array to pass to the FIFO. I'm working on this as a backup because I can't progress any further with my msg struct. I'm getting a freeze when I use strcat.

int MSGSIZE = 16;
char* msgbuf;
msgbuf = (char*)malloc(MSGSIZE+1);
sprintf(msgbuf, "systemcall");
strcat(msgbuf, (char*)pid);

int fdw = Open("syscallfifo", O_WRONLY);
int written = write(fdw, msgbuf, sizeof(msgbuf));

I'm desperate. Thank anyone for any consideration.

also I wanted to mention I searched through the forum and couldn't find a similar problem.

>>strcat(msgbuf, (char*)pid);
If you have to cast to char * then chances are good you're doing something wrong. ;) What type is pid? I'm guessing it's an int, and that would be a problem because typecasting doesn't make an integer into a string.

pid is a pid_t, pretty much like an int, just holds the process id, is there any other way to add this onto the end of my message?

by the way, thanks for the fast reply, i will be checking for response about every 10 minutes or so.

if msg is a struct, why not just have an an int inside the struct to hold the pid and set it equal?

ya i guess i could add another int field, but wouldn't make a difference right now I can't get the message struct to send anyway. my main problem is with read/write.

pid_t is integral, so you need to actually convert it to a string. Type casting doesn't cut it, but sprintf is a good easy fix:

sprintf(msg1.message, "%lu", (unsigned long)pid);

This is assuming that message is an array or pointer with memory allocated to it and sized enough to hold the value. If it's just a pointer, you need to allocate some memory:

msg1.message = malloc(ENOUGH_FOR_AN_INT + 1);

if (msg1.message == NULL)
  error("malloc failure");

sprintf(msg1.message, "%lu", (unsigned long)pid);

The same goes with your alternate solution:

sprintf(msgbuf, "systemcall%lu", (unsigned long)pid);
/* strcat(msgbuf, (char*)pid); */
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.