越秀區(qū)網(wǎng)站建設寧波seo搜索引擎優(yōu)化公司
目錄
- 1. 代碼
- (1)啟動進程execvp
- (2)替換的新進程new_proc
- 2. 驗證
- (1)new_proc與execvp源文件同一目錄
- (2)new_proc與execvp軟鏈接同一目錄
- 3. 總結(jié)
- 4. errno.h
用execvp軟鏈接啟動進程,execvp中進行進程替換執(zhí)行進程new_proc,new_proc如果用./表示的當前相對路徑執(zhí)行,那么new_proc應該放在execvp源文件的同一目錄,還是execvp的軟鏈接所在目錄?
1. 代碼
(1)啟動進程execvp
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>int main(void)
{int pid = 0;pid = fork();if (pid < 0){printf("[%s,%d] fork failed, ret=%d\n", __FUNCTION__, __LINE__, pid);}else if (pid == 0){char *const argv[] = {"./new_proc", NULL};if (-1 == execvp(argv[0], argv)){printf("[%s,%d] execvp failed!, error: %s (%d)\n", __FUNCTION__, __LINE__, strerror(errno), errno);exit(1);}}for (;;){printf("pid = %d\n", pid);sleep(1);}return 0;
}
(2)替換的新進程new_proc
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main(int argc, char **argv)
{for (;;){printf("new_proc\n");sleep(1);}return 0;
}
2. 驗證
(1)new_proc與execvp源文件同一目錄
[exec]$ pwd
/test/exec
[exec]$ ls -l *
lrwxrwxrwx 1 test test 14 Aug 17 13:44 execvp -> package/execvp
package:
total 24
-rwxrwxr-x 1 test test 8808 Aug 17 13:49 execvp
-rwxrwxr-x 1 test test 8496 Aug 17 11:27 new_proc[exec]$ ./execvp
pid = 13487
[main,21] execvp failed!, error: No such file or directory (2)
pid = 13487
pid = 13487[~]$ ps x | grep execvp
13486 pts/4 S+ 0:00 ./execvp
13487 pts/4 Z+ 0:00 [execvp] <defunct>
[~]$ cat /proc/13486/environ
...
PWD=/test/exec
執(zhí)行會報錯“No such file or directory”,查看/proc/pid/environ,其中包含了進程環(huán)境變量的列表,發(fā)現(xiàn)包含了軟鏈接路徑,但是沒有源文件路徑。
(2)new_proc與execvp軟鏈接同一目錄
[exec]$ ls -l *
lrwxrwxrwx 1 test test 14 Aug 17 13:44 execvp -> package/execvp
-rwxrwxr-x 1 test test 8496 Aug 17 11:27 new_proc
package:
total 12
-rwxrwxr-x 1 test test 8808 Aug 17 14:51 execvp[exec]$ ./execvp
pid = 25910
new_proc
pid = 25910
new_proc
可進行進程替換運行。
3. 總結(jié)
用軟鏈接啟動進程時,進程的環(huán)境變量列表會包含軟鏈接的路徑,但是沒有源文件所在路徑,可通過/proc/pid/environ查看。
所以進程替換需要考慮軟鏈接路徑,而不是源文件路徑。
4. errno.h
使用strerror(errno)轉(zhuǎn)換錯誤碼為字符串的時候,需要加上頭文件#include <string.h>,不然好像沒有輸出。
#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EIO 5 /* I/O error */
#define ENXIO 6 /* No such device or address */
#define E2BIG 7 /* Argument list too long */
#define ENOEXEC 8 /* Exec format error */
#define EBADF 9 /* Bad file number */
#define ECHILD 10 /* No child processes */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* Out of memory */
#define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */
#define ENOTBLK 15 /* Block device required */
#define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define ENFILE 23 /* File table overflow */
#define EMFILE 24 /* Too many open files */
#define ENOTTY 25 /* Not a typewriter */
#define ETXTBSY 26 /* Text file busy */
#define EFBIG 27 /* File too large */
#define ENOSPC 28 /* No space left on device */
#define ESPIPE 29 /* Illegal seek */
#define EROFS 30 /* Read-only file system */