1 进程控制开发
(1)getpid和getppid函数可获得当前进程的PID和PPID,调用方式如下:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
printf("The PID of this process is %d\n",getpid());
printf("The PPID of this process is %d\n",getppid());
return 0;
}
(2)利用fork()函数创建子进程,该子进程为父进程的一个复制品,调用方式如下:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
pid_t result;
result = fork();
if(result == -1)
{
perror("fork");
exit;
}
else if(result == 0)
{
printf("The return value is %d\nIn child process!!\nMy PID is%d,",result,getpid());
}
else
{
printf("The return value is %d\nIn father process!!\nMy PID is %d\n",result,getpid());
}
}
(3)利用execle()函数使用文件名的方式查找可执行文件,调用方式如下:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
if(fork() == 0){
if(execlp("ps","ps","-ef",NULL) < 0)
perror("execlp error!");
}
}
(4)用来终止进程的exit()函数和_exit()函数
exit()函数:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Using exit...\n");
printf("This is the content in bufffer");
exit(0);
}
_exit()函数:
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("Using _exit...\n");
printf("This is the content in buffer");
_exit(0);
}
(5)使父进程阻塞的waitpid()函数
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
pid_t pc,pr;
pc = fork();
if(pc < 0)
printf("Error fork.\n");
else if (pc == 0)
{
sleep(5);
exit(0);
}
else
{
do{
pr=waitpid(pc,NULL,WNOHANG);
if(pr==0)
{
printf("The child process has not exited\n");
sleep(1);
}
}while(pr == 0);
if(pr == pc)
{
printf("Get child %d\n",pr);
}
else
printf("some error occured.\n");
}
}
(6)创建守护进程实例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAXFILE 65535
int main()
{
pid_t pc;
int i,fd,len;
char *buf="This is a Dameon\n";
len = strlen(buf);
pc = fork();
if(pc<0)
{
printf("error fork\n");
exit(1);
}else if(pc>0)
exit(0);
setsid();
chdir("/");
umash(0);
for(i=0;i<MAXFILE;i++)
{
close(i);
}
while(1)
{
if((fd=open("/tmp/dameon.log",O_CREAT|O_WRONLY|O_APPEND,0600))<0){
perror("open");
exit(1);
}
write(fd,buf,len+1);
close(fd);
sleep(10);
}
}
2 进程控制开发相关实验
(1)编写多进程程序
该实验有3个进程,其中一个为父进程,其余为子进程,其中一个子进程运行“ls -l"命令,另一个子进程在暂停5s后退出
/*exc.c*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void)
{
pid_t child1,child2,child3;
child1 = fork();
child2 = fork();
if(child1 == -1)
{
perror("child1 fork");
exit(1);
}else if(child1 == 0)
{
printf("In child1: execute 'ls -l'\n");
if(execlp("ls","ls","-1",NULL)<0)
perror("child1 execlp");
}
if(child2 == -1)
{
perror("child2 fork");
exit(1);
}else if(child2 == 0){
printf("In child2: sleep for 5 seconds and then exit\n");
sleep(5);
exit(0);
}
else{
printf("In father process:\n");
do{
child1 = waitpid(child2,NULL,WNOHANG);
if(child1 == 0){
printf("The child2 process has not exited!\n");
sleep(1);
}
}while(child1 == 0);
if(child1 == child2)
printf("Get child2\n");
else
printf("Error occured!\n");
}
}
(2)编写守护进程
在该实验中,读者首先建立起一个守护进程,然后再新建一个子进程,子进程暂停10s后,自动退出。
/*exc2.c*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <syslog.h>
#define MAXFILE 65535
int main(void)
{
pid_t child1,child2;
int i;
child1 = fork();
if(child1 == -1)
{
perror("child1 fork");
exit(1);
}
else if(child1 > 0)
exit(0);
openlog("exc2_info",LOG_PID, LOG_DAEMON);
setsid();
chdir("/");
umask(0);
for(i = 0; i < MAXFILE; i++)
{
close(i);
}
child2 = fork();
if(child2 == -1)
{
perror("child2 fork");
exit(1);
}
else if(child2 == 0){
syslog( LOG_INFO,"child2 will sleep for 10s");
sleep(10);
syslog( LOG_INFO,"child2 is going to exit!");
exit(0);
}
else
{
waitpid(child2,NULL,0);
syslog(LOG_INFO,"child1 noticed that child2 has exited");
closelog();
while(1)
{
sleep(10);
}
}
}