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 ...
- 05-21-2010 #1Just 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
- 05-21-2010 #2Linux 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?
- 05-21-2010 #3Just 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; }
- 05-21-2010 #4Linux 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?
- 05-21-2010 #5Just 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; }


Reply With Quote