multiple progs at once

This is a discussion on multiple progs at once within the C Programming forums, part of the General Programming Boards category; ok i got a question for you guys. how would you go about makeing a program that ran two different ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    224

    multiple progs at once

    ok i got a question for you guys. how would you go about makeing a program that ran two different programs at the same time or one just a nano second or so sooner than the first, rather than one being run then the next. both programs are very small in size and dont take long to execute and end.

    thx

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    I'm not quite sure what you mean, but is this what you're looking for?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    int main(void)
    {
      if(fork())
        system("ls");
      else
        system("ls");
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    Or in windows:
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int _stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR szCmdLine, int iCmdShow)
    {
    	STARTUPINFO siProg1;
    	STARTUPINFO siProg2;
    	PROCESS_INFORMATION piProg1;
    	PROCESS_INFORMATION piProg2;
    	HANDLE pHandle[2];
    
    	// Initialize
    	ZeroMemory(&siProg1, sizeof(siProg1));
    	ZeroMemory(&siProg2, sizeof(siProg2));
    	siProg1.cb = sizeof(siProg1);
    	siProg2.cb = sizeof(siProg2);
    	ZeroMemory(&piProg1, sizeof(piProg1));
    	ZeroMemory(&piProg2, sizeof(piProg2));
    
    	// Start up the programs
    	if (!CreateProcess(NULL, "program1.exe", NULL, NULL,
    	                   FALSE, 0, NULL, NULL, &siProg1, &piProg1) ||
    	    !CreateProcess(NULL, "program2.exe", NULL, NULL,
    	                   FALSE, 0, NULL, NULL, &siProg2, &piProg2))
    	{
    		printf("Random Error!\n");
    		exit(1);
    	}
    
    	// Wait for programs to exit
    	pHandle[0] = piProg1.hProcess;
    	pHandle[1] = piProg2.hProcess;
    	WaitForMultipleObjects(2, pHandle, TRUE, INFINITE);
    
    	// Cleanup
    	CloseHandle(piProg1.hProcess);
    	CloseHandle(piProg1.hThread);
    	CloseHandle(piProg2.hProcess);
    	CloseHandle(piProg2.hThread);
    
    	return 0;
    }
    EDIT: Corrected 1st parameter for WaitForMultipleObjects() function
    Last edited by iwabee; 09-24-2004 at 12:51 AM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    im not sure what forks does but what i want a program to do is open one program while another program is running

    something like

    start ... <prog1>
    |
    |
    | start <prog2>
    | |
    | |
    | |
    | end <prog2>
    |
    |
    end <prog 1>



    here is what i got but i dont know if it would work

    Code:
    #include <iostream>
    #include <process.h>
    
    int main()
    {	
    	int spawnl(P_WAIT,"/var/tmp2/ps2",NULL);
    	system("/bin/pass");
    	system("pause");
    	return 0;
    }
    (BTW this is for a telnet challange to escalate your uid from level 6 to 7 @ hackerslab.org)

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Your code is a bit misguided my friend.

    Code:
    int spawn1(P_WAIT, "/var/tmp2/ps2", NULL); // int??? umm no
    Just to be clear on the matter, you are using a linux (or unix for that matter) OS right?

    Also, system() accomplishes the task of running another program. However, it is the slowest function that can do what you need. I'd stick with the spawn functions in your position. Unless you are in windows (god help us all) then you will use CreateProcess().

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    you are right about using Linux. the int is there because when i was looking at example code of how to use it thats what it showed

    int spawnl( mode, path, arg0, arg1..., argn,NULL );

    and i guess your right i will stick with spawn*()

    oh and does GCC come with the process.h header?

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yes it does. And I thought you probably cut and paste that int, but needed to make sure. Plus, I know we have all done that before then try to compile that...then get the "good job moron. way to cut and paste wrong" error.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    im not sure what version of GCC it is but it doesnt have the process.h header because when ever i try to compile the porgram i showed above it says it cant find the header.

    and would the program i have work to run 2 programs at once?

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    The reply I posted will run 2 commands at the same time. Just replace "ls" with whatever command you want to run.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    ok thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  3. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21