0

I'm new to C, and trying to figure out how I achieve the following:

./mycprog uname -r
5.3.0-42-generic
1234,5678

Where 1234 is the pid of mycprog and 5678 is the pid of uname

Thanks, Zvi

anastaciu
  • 23,467
  • 7
  • 28
  • 53
Zvi
  • 312
  • 1
  • 3
  • 10
  • 1
    You need to give us a bit more to work with by telling us more specifically which part you can do and which you have difficulty with.Do you know how to use `fork` and `exec`? Do you know how to wait for a child process? Do you know how to get current process's id? – kaylum Apr 26 '20 at 12:06

1 Answers1

1

argv is an array of strings each one contains a command line argument, including the name of the program, in your case:

argv[0]  ./mycprog 
argv[1]  uname 
argv[2]  -r

argc counts the number of arguments, in your case, 3.

You can use them as you wish. In case you have trouble you can find extensive documentation on the site on how to use these, e.g. Regarding 'main(int argc, char *argv[])'

To achieve what you need I would point you to:

getpid() to get the program's pid.

getuid() to get the user id.

getopt() is also a nice option to parse command line arguments.

anastaciu
  • 23,467
  • 7
  • 28
  • 53