Find the answer to your Linux question:
Results 1 to 5 of 5
How does one do a synchronous shell in c. I want to run another application from mine and wait until it is finished before I continue. I did look at ...
  1. #1
    Just Joined!
    Join Date
    May 2010
    Posts
    3

    synchronously shelling in c

    How does one do a synchronous shell in c.

    I want to run another application from mine and wait until it is finished before I continue.

    I did look at exec but it appears to kill your process??

    James

  2. #2
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    Use fork and then exec in the child process - I think.
    In a world without walls and fences, who needs Windows and Gates?

  3. #3
    Just Joined!
    Join Date
    May 2010
    Posts
    3
    I tried that but no-go. The After never prints
    James


    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    
    int
    main (int argc, char **argv)
    
    {
    	pid_t pID = fork();
    	int ret;
    	printf("Before\n");
    	
    	ret = execlp ("/bin/ls", "ls", "-1", (char *)0);
    	printf("After\n");
    	return 0;
    }

  4. #4
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    You have to do the exec in the child process. Also the parent process should wait for the child process to end.


    man fork
    man -s 2 wait
    In a world without walls and fences, who needs Windows and Gates?

  5. #5
    Just Joined!
    Join Date
    May 2010
    Posts
    3

    [solved]

    Got it:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    
    int
    main (int argc, char **argv)
    
    {
    	pid_t pID,status;
    	int ret;
    
    	printf("Before\n");
    	pID=fork();
    	if (pID == 0)
    	ret = execlp ("/bin/ls", "ls", "-1", (char *)0);
    
    	while (wait(&status) != pID)
            /* empty */ ;	
    	printf("After\n");
    	return 0;
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...