进程理论基础:
进程是一个具有一定独立功能的程序的一次运行活动。
进程的状态:
就绪态、执行态、阻塞态
程序开始执行时首先从外存调入内存,进入就绪态,等待进程调度后进行执行,执行过程中如果发生I/O请求就进入阻塞态,如果I/O完成再次进入就绪态等待下一次调度。正在执行的程序如果时间片完也会重新进入就绪态等待下一次时间轮转的调度。
进程ID(PID):标识进程的唯一数字
父进程的ID(PPID)
启动进程的用户ID(UID)
进程互斥是指当有若干进程都要使用某一共享资源时,任何时刻最多允许一个进程使用,其它要使用该资源的进程必须等待,直到占用该资源者释放了该资源为止。
操作系统中将一次只允许一个进程访问的资源称为临界资源。
进程中访问临界资源的那段程序代码称为临界区。为实现对临界资源的互斥访问,应保证进程互斥地进入各自的临界区。
一组并发进程按一定顺序执行的过程称为进程间的同步。具有同步关系的一组并发进程称为合作进程,合作进程间相互发送的信息称为消息或事件。
进程调度
概念:按一定算法,从一组待运行的进程中选出一个来占有cpu运行。
调度方式: 抢占式 非抢占式
调度算法:先来先服务调度算法、短进程优先调度算法、高优先级优先调度算法、时间片轮转法。
死锁:多个进程因竞争资源而形成一种僵局,若无外力作用,这些进程都将永远不能再向前推进。
多进程程序设计:
获取ID
# include <sys/types.h>
# include <unistd.h>
pid_t getpid(void)
获取本进程ID。
pid_t getppid(void)
获取父进程ID。
例:getpid.c
# include <stdio.h>
# include <unistd.h>
# include <stdlib.h>
int main(void)
{
printf("PID = %d\n", getpid());
printf("PPID = %d\n", getppid());
return 0;
}
运行结果:
[[email protected] test]$ ./getpid
PID = 26307
PPID = 4477
进程创建-fork
# include <unistd.h>
pid_t fork(void)
功能:创建子进程
fork的奇妙在于它被调用一次,却返回两次,它可能有三种不同的返回值:
1、在父进程中,fork返回新创建的子进程的PID;
2、在子进程中,fork返回0;
3、如果出现错误,fork返回一个负值
例:fork1.c
# include <sys/types.h>
# include <unistd.h>
int main(void)
{
pid_t pid;
pid = fork();
if(pid < 0)
printf("error in fork!");
else if(pid == 0)
printf(" i am the child process, ID is %d\n", getpid());
else
printf("i am the parent process, ID is %d\n", getpid());
return 0;
}
运行结果显示:
[[email protected] test]$ ./fork1
i am the child process, ID is 26712
i am the parent process, ID is 26711
在pid = fork()之前,只有一个进程在执行,但在这条语句执行之后,就会变成两个进程在执行了,这两个进程的共享代码段,将要执行的下一条语句都是if(pid == 0)。两个进程中,原来就存在的那个进程被称为“父进程”,新出现的进程就是“子进程”,父子进程的区别在于进程标识符(PID)不同。
例:fork2.c
# include <unistd.h>
# include <stdio.h>
int main(void)
{
pid_t pid;
int count = 0;
pid = fork();
count++;
printf("count = %d\n", count);
return 0;
}
运行结果:
[[email protected] test]$ ./fork2
count = 1
count = 1
分析结果:子进程的数据空间、堆栈空间都会从父进程得到一个拷贝,而不是共享。在子进程中对count进行加1的操作,并没有影响到父进程中的count值,父进程中的count值仍然为0.
进程创建-vfork
# include <sys/types.h>
# include <unistd.h>
pid_t vfork(void)
功能:创建子进程。
frok与vfork的区别:
1、fork:子进程拷贝父进程的数据段,堆栈。
vfork:子进程与父进程共享数据段,堆栈。
2、fork:父、子进程的执行次序不确定。
vfork:子进程先执行,父进程后运行。
例:vfork.c
# include <unistd.h>
# include <stdio.h>
int main(void)
{
pid_t pid;
int count = 0;
pid = vfork();
count++;
printf("count = %d\n", count);
return 0;
}
我在运行这个程序时出现了Segmentation fault,目前还不能解决,留给以后再解释。
exec函数族
exec用被执行的程序替换调用它的程序。
区别:
fork创建一个新的进程,产生一个新的PID。
exec启动一个新的程序,替换原有的进程,因此进程的PID不会改变。
# include <unistd.h>
int execl(const char *path, const char *arg1, ....)
参数:
path:被执行的程序名(含完整路径)。
arg1-argn:被执行程序所需的命令行参数,含程序名。以空指针(NULL)结束。
例:execl.c
# include <unistd.h>
int main(void)
{
execl("/bin/ls", "ls", "-al", "/etc/passwd", (char *)0);
return 0;
}
注释:/bin/ls:bin中存放了常用的命令,其中也包含了ls,大家可以亲自查看一下。
运行结果:
[[email protected] test]$ ./execl
-rw-r--r-- 1 root root 1548 Oct 26 15:47 /etc/passwd
# include <unistd.h>
int execlp(const char *path, const char *arg1,...)
参数:
path:被执行程序名(不含路径,将从path环境变量中查找该程序)。
arg1-argn:被执行程序所需的命令行参数,含程序名。以空指针(NULL)结束。
例:execlp.c
# include <unistd.h>
int main(void)
{
execlp("ls", "ls", "-al", "/etc/passwd", (char *)0);
return 0;
}
# include <unistd.h>
int execv(const char *path, char *const argv[])
参数:
path:被执行程序名(包含完整路径)。
argv[]:被执行程序所需的命令行参数数组。
例:execv.c
# include <unistd.h>
int main(void)
{
char *argv[] = {"ls", "-al", "etc/passwd", (char*)0};
execv("/bin/ls", argv);
return 0;
}
system 函数
# include <stdlib.h>
int system(const char *string)
功能:
调用fork产生子进程,由子进程来调用/bin/sh -c string来执行参数string所代表的命令。
例:system.c
# include <stdlib.h>
int main(void)
{
system("ls -al /etc/passwd");
return 0;
}
进程等待:
# include <sys/types.h>
# include <sys/wait.h>
pid_t wait(int *status)
功能:
阻塞该进程,直到其某个子进程退出。
注释:wait()函数使父进程暂停执行,直到它的一个子进程结束为止,该函数的返回值是终止运行的子进程的PID. 参数status所指向的变量存放子进程的退出码,即从子进程的main函数返回的值或子进程中exit()函数的参数。如果status不是一个空指针,状态信息将被写入它指向的变量。
例:wait.c
# include <sys/types.h>
# include <sys/wait.h>
# include <unistd.h>
# include <stdlib.h>
int main(void)
{
pid_t pc, pr;
pc = fork();
if(pc == 0)
{
printf("this is child process with pid of %d \n", getpid());
sleep(10);//10s
}
else if(pc > 0)
{
pr = wait(NULL);
printf(" i catched a child process with pid of %d \n", pr);
}
return 0;
}
运行结果:
[[email protected] test]$ ./wait
this is child process with pid of 31262
i catched a child process with pid of 31262