Thread: Aargh mailslots and the like!!

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    Aargh mailslots and the like!!

    Hi guys,

    i just registered, and since there is no point in lying.... ill tell you straight off. Im a inforatics student from holland, and i have an home work assignment... BUT i dont want you guys making it for me but help me with... So here goes:

    I need to write a C++ program which starts 3 (with help from createprocess) ms-dos boxes each printing a, b, c when it their turn. So first box 1 prints a then box 2 prints a then box three prints a and after this box 1 prints b, and so forth and so on...
    But when only one ms-box is running it will print: abcabcabcabc etc, etc.

    This process needs to be synchronized with mailslots...

    Ok im just beginnen but this is what i have so far:

    Code:
    void main()
    
    {
      // Set the startup information
      STARTUPINFO startup_info = {0};
      startup_info.cb = sizeof startup_info;
      PROCESS_INFORMATION pi = {0};
    
      // Create the process
     CreateProcess("D:\\ipc\\opdracht\\debug\\printer.exe", "D:\\ipc\\opdracht\\debug\\printer.exe", NULL, NULL, FALSE, 0, NULL, NULL, &startup_info, &pi);
    }
    Where printer.exe is the program which prints abcabcbabc....
    But i need three of those boxes, simply copying the piece of code i have three times doesn't work.... anybody wiling to help a lost soul here?
    Last edited by Frietbakker; 03-01-2004 at 08:49 AM.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Hi, welcome to the boards. For future reference a question about windows api function can elict more responses on the windows board.

    In the processes you create(output processes) you need to:
    - Create a mailslot.
    - Listen on that mailslot.
    - Write to output when told to.

    In the master process you need to:
    - Create the output processes.
    - Connect to their mailslots.
    - Write to their mailslots to tell them to continue.

    Sample for output process:
    Code:
    int main(void)
    {
    	HANDLE hSlot = CreateMailslot("\\\\.\\mailslot\\msname", 0,  MAILSLOT_WAIT_FOREVER,  NULL);
    
    	while (TRUE)
    	{
    		ReadFile(hSlot, buf, sizeof(buf), &dwRead, NULL); // Receive message from master.
    		printf("%s", buf); // print what the master has told us to.
    	}
    }
    Sample for master process:
    Code:
    int main(void)
    {
    
    	// Create Processes
    
    	// Connect to mailslots.
    	hSlot1 = CreateFile("\\\\.\\mailslot\\msname", ...);
    
    	// Write to mailslots what you want them to output.
    	WriteFile(hSlot1, "test", 4, ...);
    }
    Things to think about:
    - You need a different name for each mailslot (maybe pass the name you want the output process to use on the command line).
    - You need to make sure that the master process does not try to connect to a mailslot before it is created.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    4
    Hey man thanks for your help, really! And thanks for the tip, i posted my updated problems in the other section of the board, so if anyone is willing to take a risk and a look its over here: http://cboard.cprogramming.com/showt...threadid=50080

Popular pages Recent additions subscribe to a feed