#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <libgen.h>
// argv[1] doit contenir le chemin complet du programme à lancer (/usr/bin/gvim par exemple)
int main(int argc, char *argv[]) {
char *newargv[5];
char *newenviron[5];
newargv[0] = basename(argv[1]);
newargv[1] = NULL;
newenviron[0] = "DISPLAY=:0.0";
newenviron[1] = NULL;
pid_t pid;
// Clonage du parent
pid = fork();
if (pid == 0) {
// On est dans le clone
// Remplacement du clone par la commande shell ou le programme
execve (argv[1],newargv,newenviron );
} else {
// On est dans le parent
// On attend la fin du programme
wait(NULL);
puts ("This is a message from the parent");
}
}
Hello,
Distro : Debian 10 - 64bits
I think it's the right list ; if not, please redirect me.
In order to launch a command from within the C programme in attachment, I use "fork - execve - wait" trio so that parent program waits until command has finished
Building the program : cc execve.c -o execve
Program syntax : ./execve <command full path>
Example : ./execve /usr/bin/gvim
The problem :
--- if I execute "./execve /usr/bin/gvim", gvim starts after a few seconds but without executing .vimrc, and parent program doesn't wait for gvim to end.
Output of "ps -auwx --forest" during the few wait seconds :
lgentis 5216 0.0 0.0 7124 3812 pts/0 Ss 14:47 0:00 \_ bash
lgentis 9368 0.0 0.0 2144 752 pts/0 S+ 16:44 0:00 | \_ ./execve /usr/bin/gvim
lgentis 9369 2.0 0.2 68916 16452 pts/0 S+ 16:44 0:00 | \_ gvim
lgentis 9392 2.5 0.3 71620 22444 ? Ss 16:44 0:00 | \_ gvim
Output of "ps -auwx --forest" once gvim started :
lgentis 5212 0.1 0.7 426212 43880 ? Rl 14:47 0:07 xfce4-terminallgentis 5216 0.0 0.0 7124 3812 pts/0 Ss+ 14:47 0:00 \_ bash
lgentis 9093 1.3 0.4 75804 26700 ? Ss 16:36 0:00 gvim
We can see that gvim is not a child process of the parent program (which has ended)
--- if I execute "./execve /usr/bin/vim.gtk", vim starts in text mode in the console ; however, parent program waits till vim is closed.
Output of "ps -auwx --forest" :
lgentis 5212 0.0 0.7 426212 43880 ? Rl 14:47 0:06 xfce4-terminal
lgentis 5216 0.0 0.0 7124 3812 pts/0 Ss 14:47 0:00 \_ bash
lgentis 9011 0.0 0.0 2144 748 pts/0 S+ 16:34 0:00 | \_ ./execve /usr/bin/vim.gtk
lgentis 9012 0.8 0.2 68932 17232 pts/0 S+ 16:34 0:00 | \_ vim.gtk
We can see that vim.gtk is a child process of the parent program
--- I did the same test with gedit and audacious, and all works fine : child program is executed and parent program waits as expected.
Output of "ps -auwx --forest" :
lgentis 5212 0.0 0.7 425664 43548 ? Sl 14:47 0:05 xfce4-terminal
lgentis 5216 0.0 0.0 7124 3812 pts/0 Ss 14:47 0:00 \_ bash
lgentis 8910 0.0 0.0 2144 748 pts/0 S+ 16:30 0:00 | \_ ./execve /usr/bin/gedit
lgentis 8911 4.2 0.7 477720 47812 pts/0 Sl+ 16:30 0:00 | \_ gedit
We can see that gedit is a child process of the parent program
No comments:
Post a Comment