Chinaunix首页 | 论坛 | 博客
  • 博客访问: 893574
  • 博文数量: 299
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2493
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-21 10:07
个人简介

Linux后台服务器编程。

文章分类

全部博文(299)

文章存档

2015年(2)

2014年(297)

分类: LINUX

2014-07-08 12:25:12

(1)wait()
wait函数用于父进程和子进程的同步,让父进程等待子进程结束。wait函数不需要带任何参数,适合于等待所有的子进程。相关信息如下:
pid_t wait(int* status);
如果函数执行成功,返回等待到结束的子进程的PID和结束状态status,否则返回-1.
状态status有2种情况:一种是正常的退出,另一种是接受到了信号(eg.kill)而退出。
 
(2)kill
2.1 kill命令:
kill命令可以给指定进程发送消息,命令格式:kill -信号类型 进程号
可以通过kill -l命令查看所有信号类型的值:

 kill -l
 1) SIGHUP  2) SIGINT  3) SIGQUIT  4) SIGILL
 5) SIGTRAP  6) SIGABRT  7) SIGBUS  8) SIGFPE
 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT
17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU
25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN
35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4
39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6
59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX 

 
2.2 kill函数:
Linux提供的kill函数可以实现与kill命令相同的功能,调用格式为:int kill(pid_t pid,int sig);
 
【程序示例】
(1) systemcall2.c
#include
#include
#include
#include
int main(int argc,char* argv[])
{
 pid_t pid;
 int exit_code;
 pid = getpid();
 exit_code = (int)(rand()%256);    //生成随机返回值<256以内
 sleep(3);
//根据入参决定进程退出方式
 if(atoi(argv[1])%2)   
 {
  printf("The child process %d receive a signal SIGKILL.\n",pid); 
  kill(pid,9);    //向本进程发送SIGKILL信号中断
 }
 else
 {
  printf("The child process %d normally exit, exit code is %d.\n",pid,exit_code);
  exit(exit_code);    //以生成随机值为退出状态
 }
}
(2) file.c
#include
#include
#include
#include
int main(int argc,char* argv[])
{
 pid_t pid,wait_pid;
 int status;
 int i;
 if(argc < 4)
 {
  printf("Usage: %s para1 para2 para3\n",argv[0]);
  return 1;
 }
//for循环创建三个子进程,对于子进程,用execl函数,执行systemcall2.c编译生成的可执行程序(execl:完整路径、变长参数)
 for(i=1;i<4;i++)
 {
  if((pid=fork())==-1)
  {
   printf("Creat child process failed.\n");
   return 1;
  }
  else if(pid==0)
  {
   //NULL表示参数结束,"kill",argv[i],NULL为kill可执行函数的参数列表
   execl("./kill","kill",argv[i],NULL); 
   }
  else
  {
   printf("Creat child process ID: %d\n",pid);  //打印本次创建的子进程ID
  }
 }
//使用wait,直到子进程全部结束,while结束
 while((wait_pid=wait(&status))&&(wait_pid != -1))
 {
  printf("Process ID: %d exit,exit_code is %d.\n",wait_pid,status);
 }
 return 0;
}
执行结果:

 gcc -o kill systemcall2.c
 gcc -o file file.c

 ./file 4 2 1    //参数4,2可以让子进程正常退出,1接收结束信号退出
Creat child process ID: 5889
Creat child process ID: 5890
Creat child process ID: 5891
The child process 5889 normally exit, exit code is 103.
Process ID: 5889 exit,exit_code is 26368.
The child process 5890 normally exit, exit code is 103.
Process ID: 5890 exit,exit_code is 26368.
The child process 5891 receive a signal SIGKILL.
Process ID: 5891 exit,exit_code is 9.
阅读(1259) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~