| | |
c++/unix fork(), fifo's, desperate need of help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2005
Posts: 7
Reputation:
Solved Threads: 0
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:
parent code:
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.
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.
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)
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:
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
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.
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:
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:
The same goes with your alternate solution:
C++ Syntax (Toggle Plain Text)
sprintf(msg1.message, "%lu", (unsigned long)pid);
C++ Syntax (Toggle Plain Text)
msg1.message = malloc(ENOUGH_FOR_AN_INT + 1); if (msg1.message == NULL) error("malloc failure"); sprintf(msg1.message, "%lu", (unsigned long)pid);
C++ Syntax (Toggle Plain Text)
sprintf(msgbuf, "systemcall%lu", (unsigned long)pid); /* strcat(msgbuf, (char*)pid); */
![]() |
Similar Threads
- Unix, C++, pipe and fork (C++)
- How can i do unix c programs in turbo c editor in windows (Computer Science)
- Making a UNIX Shell, so inexperienced at it (C++)
- i want help about the Simulate a Linux/UNIX shell, called MASH in the C (C)
Other Threads in the C++ Forum
- Previous Thread: Quick, Insertion, and Partition
- Next Thread: Micosoft Visual C++ streaming problems
Views: 8283 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






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.