c++/unix fork(), fifo's, desperate need of help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2005
Posts: 7
Reputation: Funktar is an unknown quantity at this point 
Solved Threads: 0
Funktar Funktar is offline Offline
Newbie Poster

c++/unix fork(), fifo's, desperate need of help

 
0
  #1
May 18th, 2005
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:
  1. fdw = Open("syscallfifo", O_WRONLY);
  2.  
  3. fifomsg msg1;
  4. strcpy(msg1.message, (char*)pid);
  5. msg1.length = sizeof(msg1);
  6.  
  7. write(fdw, &msg1, sizeof(msg1));
  8. close(fdw);

parent code:
  1. fdr = Open("syscallfifo", O_RDWR);
  2.  
  3. fifomsg msg1;
  4.  
  5. read(fdr, &msg1.length, sizeof(msg1.length));
  6. read(fdr, &msg1.message, sizeof(msg1) - sizeof(msg1.length));
  7. 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.

  1. int MSGSIZE = 16;
  2. char* msgbuf;
  3. msgbuf = (char*)malloc(MSGSIZE+1);
  4. sprintf(msgbuf, "systemcall");
  5. strcat(msgbuf, (char*)pid);
  6.  
  7. int fdw = Open("syscallfifo", O_WRONLY);
  8. 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.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: c++/unix fork(), fifo's, desperate need of help

 
0
  #2
May 18th, 2005
>>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.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 7
Reputation: Funktar is an unknown quantity at this point 
Solved Threads: 0
Funktar Funktar is offline Offline
Newbie Poster

Re: c++/unix fork(), fifo's, desperate need of help

 
0
  #3
May 18th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: c++/unix fork(), fifo's, desperate need of help

 
0
  #4
May 18th, 2005
if msg is a struct, why not just have an an int inside the struct to hold the pid and set it equal?
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 7
Reputation: Funktar is an unknown quantity at this point 
Solved Threads: 0
Funktar Funktar is offline Offline
Newbie Poster

Re: c++/unix fork(), fifo's, desperate need of help

 
0
  #5
May 18th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: c++/unix fork(), fifo's, desperate need of help

 
0
  #6
May 19th, 2005
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:
  1. 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:
  1. msg1.message = malloc(ENOUGH_FOR_AN_INT + 1);
  2.  
  3. if (msg1.message == NULL)
  4. error("malloc failure");
  5.  
  6. sprintf(msg1.message, "%lu", (unsigned long)pid);
The same goes with your alternate solution:
  1. sprintf(msgbuf, "systemcall%lu", (unsigned long)pid);
  2. /* strcat(msgbuf, (char*)pid); */
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 8283 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC