• Advertisement


  • Content count

  • Joined

  • Last visited

Community Reputation

100 Neutral

About yitzle

  • Rank
  1. Two questions. I wrote this code for someone else. My personal objective was just to learn the ideas. What is does: - Get some shared memory. - Fork off n children - Wait for the n children to finish - The children: - use the shared memory (a Data structure) to take turns (round robin) - when its their turn, they print their number - when each child had a turn, increment the count and repeat until each child had m turns My problems: - If you got eg n = 3, m = 3, it ought to print: "0 \n 1 \n 2 \n 0 \n 1 \n 2 \n 0 \n 1 \n 2 \n " However, the last child during the last round doesn't print. I tried flush()ing it, too. - Did I do the wait()s right? - Oh. Also, when writing this, at first I tried moving the child code out of the loop: if (! cpid) break; and after the loop: if (! cpid) { // child code } But that code for some reason never got executed. Does that make sense? Why? CODE: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> typedef struct { int turn; int count; } Data; int main(int argc, char *argv[]) { // Declare int fdOut; pid_t cpid; key_t shmKey; int shmid; int myNumber; Data * data; int tmp = 99; int redirect = 1; int n,m; // The params int i; char * fileName; // Assert param count n = 3; m = 3; // Get shm shmKey = ftok(argv[0], 'R'); shmid = shmget(shmKey, sizeof(Data), IPC_CREAT | IPC_EXCL | 0600); data = (Data *) shmat(shmid, (void *)0, 0); if( (int)data == -1 ) return -1; // Init data (unneeded) data->count = 0; data->turn = 0; // Branch off n children for(myNumber = 0; myNumber <= n; myNumber++) { cpid = fork(); if (cpid == -1) return -1; // Children if (! cpid) { // Gotta write m times while(data->count < m) { // Wait for your number while ( data->turn != myNumber ) { ; } if (0) printf("%d %d\n", data->count, myNumber); // Write your number else printf("%d\n", myNumber); // Write your number data->turn++; // Next turn if (data->turn > n ) { //printf("Inc count. turn == %d\n", data->turn); data->count++; // Do this before data->turn = 0; // this } } return 0; } } // Wait for childs for(i = 0; i <= n; i++) wait(NULL); // Detach + remove if(! shmdt(data) ) shmctl(shmid, IPC_RMID, NULL); return 0; }
  2. Quote:Original post by rip-off I doubt there is any standard way to handle this. If you were more specific about the platforms you target maybe we could give you some advice. For example on linux you would use the pipe() call, followed by fork()ing and close()ing the child processes stdin and stdout, then calling dup() on the pipe read and write descriptors, before exec*()ing the process. The parent can read and write to its pipe descriptors for the communication. On windows there is probably a very similar process, create some kind of interprocess communication handle and passing its input and output ends via the "startup info" parameter... There may be ways of getting a std::i/o stream from a file pointer (Google tells me that the gnu c++ library has at least an extension tp obtain a std::stream_buf from FILE *). I don't know of any standard way, that said I haven't delved too deeply into some of c++'s stream stuff, there could well be. OS: Solaris (Unix-like) Someone else also told me to set it up with pipe(), fork(), execl?() etc. Getting a stream_buf from a FILE * is good. Converting stream_buf to a fstream is trivial. I'll Google. Thanks. EDIT: I think I'll be reading stdio.h ;)
  3. Goal: have my program run another program and be able to send data to its STDIN and read from its STDOUT. I would like to have control over the IO with a fstream opposed to a FILE* that popen() makes. I started with this: int runApp (char * dynCmd) { FILE * my_pipe; my_pipe = popen( dynCmd, "r" ); if (my_pipe == NULL) return -1; char buf[SIZE]; while ( fgets(buf, SIZE, my_pipe) != NULL ) cout << buf; return 0; } 1) popen() is unidirectional. I need biderectional 2) popen() gives a FILE* and not a fstream. How do I go about running an app such that I can get input and output with a fstream? (Or a ifstream + ofstream)
  4. Thanks for the idea. I probably should've mentioned the platform. Its Windows.
  5. Thanks. This is about 1,000X more powerfull than I need but the basic parts should be helpful.
  6. Background: I know the basics of C/C++ decently well. I am part of a university robots club. We are programming 'bots to play soccer. Our 'bot team has (I think) 5 'bots on it as does the opponent. The system output is a Bluetooth signal the the 'bots tells them where to move and what to do. System input is a video stream. I got to write a program that can analyze the video and locate the red/blue circles (the 'bots). What do I do? I may need to convert frames to normal image files eg bmp. I need to open the mage file or stream and look for the redest and bluest parts and determine their location. I have no clue how to do this. Can anyone point me in the right direction? Maybe articles that discuss this or open source projects that handle video streams or parse graphics? Thanks!
  7. No idea where I should be asking this or even if there is a better forum (if anyone knows of a forum that deals with this sort of stuff, please point me towards it). Anyhow, the U3 USBs are set up with two partitions, one marked as removable media the other as a CDROM How do I go about marking partitions?
  8. Unfortunately, the host I am using (my university) does not offer ASP/SSI/PHP. So I am learning Perl. I used it to run a few system tasks, but am using it for web dev like an online voting system. It works...
  9. I'm doing a bit of Perl/cgi dev and Perl learning (simultaneously) and I'm running into a few bits of trouble. Where would I get help? Can anyone suggest a forum that has many active Perl users? Does GameDev have any Perl users? Thanks!
  10. sed - simple question *SOLVED*

    Arg! Thanks! I think I needed the ' before and after and was missing it ;) It works without the -e, but not without the single quotes. Thank you!
  11. OK. Simple question. I need some help with runnning a RegEx. Using the Windows Notepad++, I managed to replace ( [A-Z]* ) ( [A-Z][a-z]* ) with \1,\2 So my CSV phone directory which had a coule LASTFirst was fixed to LAST,First But I couldn't manage to pull it off with the sed. And I don't think tr is what I wanted... I tried sed s/ ( [A-Z]* ) ( [A-Z][a-z]* ) / \1,\2 / and I tried putting a '\' in front of: the ',', the '()'s, the '[]'s. Can someone kindly show me how it is supposed to be done? Thanks! EDIT: > sed s / \( [A-Z]* \) \( [A-Z] [a-z]* \) / \1,\2 / > sed: -e expression #1, char 1: unterminated `s' command
  12. Boot Loader Help

    Yay! It works now. I thought I tried --root-directory /dev/hda3 at one point... So I guess I had the GRUB 1.5 image on hdb. Thank you so much for all your time, patience and expertise. I sincerely appreciate all the effort!
  13. Boot Loader Help

    I still have the /boot directory on the GRUB partition so my menu.lst looks like title Ubuntu, kernel 2.6.15-23-386 root (hd0,1) kernel /boot/vmlinuz-2.6.15-23-386 root=/dev/hdb1 ro quiet splash initrd /boot/initrd.img-2.6.15-23-386 savedefault boot So far so good. I modified the fstab - found in /dev/hdb1/etc or /etc on the Ubuntu partition I moved /boot to /_boot and made an empty /boot I rebooted. Boom. GRUB stage1.5 Error 15. Note: this error occurs before I get the menu of OSs I can boot into, so the fault probably does not lie with the menu.lst or fstab I reboot with a LiveCD and move /_boot back to /boot Reboot. Everything works. I'm convinced GRUB is using something in the /boot on the second hard drive (hdb1) even though GRUB stage1 works fine off hda3. So... what's up? GRUB stage 1 is working off /dev/hda/boot GRUB stage 1.5 needs /dev/hdb/boot
  14. Boot Loader Help

    I didn't add /boot to the fstab yet...
  • Advertisement