《UNIX环境高级编程》笔记 第八章 进程控制

1. 简介

本章介绍UINIX系统的进程控制,包括创建新进程,执行程序和进程终止。还将说明进程属性的各种ID-----实际、有效和保存的用户ID和组ID。

2.函数原型

pid_t getpid(void);
pid_t getppid(void);
返回调用进程和调用进程父进程的进程ID
uid_t getuid(void);
uid_t geteuid(void);
返回调用进程的实际用户ID和有效用户ID
 gid_t getgid(void);
 gid_t getegid(void);
返回调用进程的实际组ID和有效组ID
pid_t fork(void);

创建一个新进程

返回值:子进程返回0,父进程返回子进程ID;若出错,返回-1

pid_t vfork(void);

创建一个新进程,保证子进程先运行,返回值与fork相同

fork和vfork最大区别是vfork在调用exec(或exit)之前共享父进程的数据、堆和栈空间,因此如果子进程运行不立即调用exec(或exit),可能会产生未知的结果

1. pid_t wait(int *wstatus);

2. pid_t waitpid(pid_t pid, int *wstatus, int options);

3. int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);

4. pid_t wait3(int *wstatus, int options,struct rusage *rusage);

5. pid_t wait4(pid_t pid, int *wstatus, int options,  struct rusage *rusage);

等待进程改变状态

返回值:若成功,返回进程ID;若出错,返回0或-1

1. int execl(const char *pathname, const char *arg, ...);

2. int execlp(const char *file, const char *arg, ...);
3. int execle(const char *pathname, const char *arg, ...);
4. int execv(const char *pathname, char *const argv[]);
5. int execvp(const char *file, char *const argv[]);
6. int execvpe(const char *file, char *const argv[],char *const envp[]);

7. int fexecve(int fd, char *const argv[], char *const envp[]);

执行一个新程序,替换当前进程的正文段、数据段、堆段和栈段

返回值:若成功,不返回;若出错,返回-1

int setuid(uid_t uid);

int setgid(gid_t gid);

设置用户ID和组ID

返回值:若成功,返回0;若出错,返回-1

 int setreuid(uid_t ruid, uid_t euid);
 int setregid(gid_t rgid, gid_t egid);

设置实际和(或)有效用户ID和组ID

返回值:若成功,返回0;若出错,返回-1

int seteuid(uid_t euid);
int setegid(gid_t egid);

设置有效用户和组ID

返回值:若成功,返回0;若出错,返回-1

int system(const char *command);

执行一个shell命令

返回值:若成功,返回shell终止状态;若失败返回-1或127

char *getlogin(void);返回值:若成功,返回登录名;若失败,返回NULL
 int nice(int inc);

改变进程调度优先级

返回值:若成功,返回新的nice值NZERO;若出错,返回-1

int getpriority(int which, id_t who);
int setpriority(int which, id_t who, int prio);
获取/设置进程调度优先级
clock_t times(struct tms *buf);

获取进程时间

返回值:若成功,返回流逝的墙上时钟时间;若出错,返回-1

3. 实例

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>

char *argv[] = {"ls", "/usr/", "/proc", NULL};

int main()
{
	printf(" pid: %d\n", getpid());
	printf("ppid: %d\n", getppid());
	printf(" uid: %d\n", getuid());
	printf("euid: %d\n", geteuid());
	printf(" gid: %d\n", getgid());
	printf("egid: %d\n", getegid());

	int n = 10;

#if 0
	pid_t pid = fork();
	if (pid < 0) {
		printf("fork error: %s\n", strerror(errno));
	} else if (pid == 0) {
		printf("I am the child process\n");
		n++;
	} else {
		printf("I am the parent process\n");
	}
#else
#if 1
	pid_t pid = fork();
#else
	pid_t pid = vfork();
#endif
	if (pid < 0) {
		printf("fork error: %s\n", strerror(errno));
	} else if (pid == 0) {
		printf("I am the child process\n");
		n++;
		int *sn = 100; // SIGSEGV==11
		printf("sn: %d\n", *sn);
		//_exit(128);
	} else {
		printf("I am the parent process\n");
		int status;
		waitpid(pid, &status, 0);

		if (WIFEXITED(status)) {
			printf("Normal termination: %d\n", WEXITSTATUS(status));
		} else if (WIFSIGNALED(status)) {
			printf("Abnormal termination: %d\n", WTERMSIG(status));
		} else {
			printf("Job control\n");
		}
	}
#endif
	printf("n = %d\n", n);

	pid_t pid2 = fork();
	if (pid2 == 0) {
#if 0
		if(execl("/usr/bin/ls", "ls", "/proc", "/mnt", NULL) < 0) {
			printf("execl error\n");
		}
#else
		if(execvp("ls", argv) < 0) {
			printf("execl error\n");
		}
#endif
	}
	printf("pid2 = %d\n", pid2);

	waitpid(pid2, NULL, 0);

	return 0;
}

4. 小结

1. 熟练掌握fork、exec函数族、_exit、wait和waitpid这些函数

2. 理解各种不同的用户ID(实际、有效和保存)

  • 21
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UNIX环境高级编程笔记是关于在UNIX系统中进行高级编程的一些笔记和技巧的记录。这些笔记主要涉及文件I/O和进程管理等方面的内容。在UNIX系统中,文件I/O是通过文件描述符来进行操作的。文件描述符是一个整数,用来标识打开的文件。为了实现跨平台的兼容性,可以使用POSIX标准来进行文件操作。POSIX是一个操作系统接口的标准,它以UNIX为基础,但并不限于UNIX类系统。此外,Single UNIX Specification简称SUS,它是POSIX.1标准的一个超集,定义了UNIX系统的实现标准。在UNIX系统中,进程的初始化是由init进程来完成的。init进程会读取文件/etc/ttys,并根据其中定义的终端设备进行处理。对于每个允许登录的终端设备,init进程会调用fork函数生成一个子进程,并通过exec函数执行getty程序来处理该终端设备。通过这些技巧和方法,可以实现在UNIX环境下进行高级编程的需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [UNIX环境高级编程笔记](https://blog.csdn.net/qq_55537010/article/details/127837953)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [《UNIX环境高级编程》学习笔记](https://blog.csdn.net/qq_42526420/article/details/123143423)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值