-1

Hey I have to write a small process launcher for uni.

#include  <stdio.h>
#include  <sys/types.h>
#include <unistd.h>

int main(int argc, char* argv[]){
    pid_t pid;


    if((pid = fork()) < 0){
        return 0;
    }
    else if(pid == 0){
        if(execvp(*argv, argv) < 0){
            return 0;
        }
    }
    return 0;
}

This is my program. I want to call it like ./process-launcher firefox --browser to start a new firefox process.

I think when I start the programm like this there should be a process of firefox in my system monitoring but it isn't

How do I get this?

Compiling with:

clang -o process-launcher process-launcher.c

Jan Wolfram
  • 145
  • 8

1 Answers1

1

What is in *argv? Your launcher name...

So when executing your code as is, you only relaunch your launcher...

Solution: ++argv;, it will pass the second parameter to execvp:

#include  <stdio.h>
#include  <sys/types.h>
#include <unistd.h>

int main(int argc, char* argv[]){
    pid_t pid;

    if((pid = fork()) < 0){
        return 0;
    }
    else if(pid == 0){

        ++argv;

        printf("execvp(%s, ...)\n", *argv);
        if(execvp(*argv, argv) < 0){
            return 0;
        }
    }
    return 0;
}
Mathieu
  • 8,840
  • 7
  • 32
  • 45
  • Thx but I have one question left. Will it also execute it with the parameters after the name of the programm like '--browser' – Jan Wolfram Nov 28 '19 at 13:27
  • 1
    Yes, this is the point of `++argv;`: At program startup, `argv` is `["./launcher", "firefox", "--browser"]`and after the `++argv;`, is becomes `["firefox", "--browser"]` – Mathieu Nov 28 '19 at 13:37
  • You can try with simpler command like `echo`: try `./launcher echo hello` – Mathieu Nov 28 '19 at 13:42