Computer-Science

exec

1. exec : execve, execl, execlp, โ€ฆ.

exec ํ•จ์ˆ˜๋Š” ์ธ์ž๋กœ ๋ฐ›์€ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์„ ์ž์‹ ์„ ํ˜ธ์ถœํ•œ ํ”„๋กœ์„ธ์Šค์˜ ๋ฉ”๋ชจ๋ฆฌ์— ๋ฎ์–ด์“ด๋‹ค. ๋”ฐ๋ผ์„œ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ˆ˜ํ–‰ ์ค‘์ด๋˜ ๊ธฐ์กด ํ”„๋กœ๊ทธ๋žจ์€ ์ค‘์ง€๋˜์–ด ์—†์–ด์ง€๊ณ , ์ƒˆ๋กœ ๋ฎ์–ด์“ด ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋œ๋‹ค. exec ํ•จ์ˆ˜๊ตฐ์„ ํ˜ธ์ถœํ•œ ํ”„๋กœ์„ธ์Šค ์ž์ฒด๊ฐ€ ๋ฐ”๋€Œ๋ฏ€๋กœ, exec ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด ์„ฑ๊ณตํ•˜๋ฉด ๋ฆฌํ„ด๊ฐ’์ด ์—†์Šต๋‹ˆ๋‹ค. ๋น„์Šทํ•œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” fork๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ๋ณต์ œํ•˜๊ณ , exec์€ ํ”„๋กœ์„ธ์Šค๋ฅผ ๋Œ€์ฒดํ•œ๋‹ค.

1. exec์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  1. remove old body
  2. load new body
  3. adjust process descriptor

2. function prototype of execve

    y=execve(fname, argv, envp); // change to fname with additional arguments specified in argv[]
                                 // and additional environment variables specified in  envp[].
                                 // returns -1 if error.
    y=execve("/aa/bb", k, 0); // change to /aa/bb with additional arguments in k

2. Example

1-1) Try below and explain why the output is โ€œI am ex1โ€ when โ€œex2โ€ runs.

ex1.c:

#include <stdio.h>
int main(){
   printf("I am ex1\n");
}

ex2.c:

#include <stdio.h>
#include <unistd.h>
int main(){
   execve("./ex1",0 , 0); // change to ./ex1 with no additional argument
   printf("I am ex2\n");
}
$ gcc -o ex1 ex1.c
$ gcc -o ex2 ex2.c
$ ex2
I am ex1

1-2) Run myexec below. Explain the result.

myexec.c:

#include <stdio.h>
int main(){
char *k[10];
   k[0]="/bin/ls";
   k[1]=0;
   execve(k[0], k, 0); // change to /bin/ls with no additional argument
}

The above program will exec to /bin/ls and print the listing of files in the current directoy.

1-3) Run myexec below and explain the result.

myexec.c:

int main(){
   char *x[10];
   x[0]="/bin/cat";
   x[1]="f1";
   x[2]=0;              // argument list should end with 0
   execve(x[0], x, 0);  // change to /bin/cat with one argument f1
}

The above program will exec to โ€œ/bin/cat f1โ€ which will print the contents of f1.

2) Change myexec such that it execs to โ€œ/bin/ls -lโ€.

3) Change myexec such that it execs to โ€œ/bin/cp f1 f2โ€.

4) Change myexec such that it runs โ€œ/bin/ls -lโ€ and prints โ€œjob doneโ€ after โ€œ/bin/ls -lโ€ is finished.

5) Change myexec such that it reads a command and execs to the given command.

$ myexec
command> /bin/cat  f1

myexec execs to โ€œ/bin/cat f1โ€

$ myexec
command> /bin/cp f1 f2

myexec execs to โ€œ/bin/cp f1 f2โ€

6) Same as 5), but myexec will repeat the process 5 times.

to avoid system-down, use a fixed-iteration loop:

for(;;){โ€ฆ} -> for(i=0;i<5;i++){โ€ฆ}

$ myexec
command> /bin/cat f1 // display the contents of f1
command> /bin/ls     // display file names in the current directory
...

7) Same as 6), but change the prompt to the current location and $ as follows. You may need โ€œgetcwdโ€.

$ myexec
[/home/sp1/12345]$ /bin/cat f1   // display the contents of f1
[/home/sp1/12345]$ /bin/ls       // display file names in the current directory
...