944,065 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9643
  • C++ RSS
May 18th, 2005
0

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

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Funktar is offline Offline
7 posts
since May 2005
May 18th, 2005
0

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

>>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.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
May 18th, 2005
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Funktar is offline Offline
7 posts
since May 2005
May 18th, 2005
0

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

if msg is a struct, why not just have an an int inside the struct to hold the pid and set it equal?
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
May 18th, 2005
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Funktar is offline Offline
7 posts
since May 2005
May 19th, 2005
0

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

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. sprintf(msgbuf, "systemcall%lu", (unsigned long)pid);
  2. /* strcat(msgbuf, (char*)pid); */
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Quick, Insertion, and Partition
Next Thread in C++ Forum Timeline: Micosoft Visual C++ streaming problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC