Chinaunix首页 | 论坛 | 博客
  • 博客访问: 988571
  • 博文数量: 200
  • 博客积分: 5011
  • 博客等级: 大校
  • 技术积分: 2479
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-27 15:07
文章分类

全部博文(200)

文章存档

2009年(12)

2008年(190)

我的朋友

分类:

2008-12-08 12:12:40

10.9 kill and raise function

#include

 

int kill(pid_t pid, int signo);

 

int raise(int signo);

 

Both return: 0 if OK, 1 on error

(一)Kill函数可以像一个进程,或者process group,甚至系统中所有的进程发送信号,这取决于你的权限和pid的取值。

其权限规则如下:

1The superuser can send a signal to any process.超级用户可以向任何进程发送信号

2For other users, the basic rule is that the real or effective user ID of the sender has to equal the real or effective user ID of the receiver. If the implementation supports _POSIX_SAVED_IDS (as POSIX.1 now requires), the saved set-user-ID of the receiver is checked instead of its effective user ID. 普通用户需要保证你的real/effective user id与接受进程的real/effective user id匹配,如果定义了_POSIX_SAVED_IDS,那么就不管effective user id了,而是看saved set-user-id

3There is also one special case for the permission testing: if the signal being sent is SIGCONT, a process can send it to any other process in the same session.如果是发送SIGCONT信号,那么一个进程可以将其发送给与她同session的所有其他进程。

如下是摘自linux suse man pageman 2 kill)的一段话:

For a process to have permission to send a signal it must either be privileged  (under  Linux:  have the  CAP_KILL  capability),  or  the real or effective user ID of the sending process must equal the real or saved set-user-ID of the target process.  In the case of SIGCONT it suffices when the  sending and receiving processes belong to the same session.

 

下面是用pid的不同取值来指定接受进程的方法:

There are four different conditions for the pid argument to kill.

Pid > 0

The signal is sent to the process whose process ID is pid.

Pid == 0

The signal is sent to all processes whose process group ID equals the process group ID of the sender and for which the sender has permission to send the signal. Note that the term all processes excludes an implementation-defined set of system processes. For most UNIX systems, this set of system processes includes the kernel processes and init (pid 1).

Pid < 0

The signal is sent to all processes whose process group ID equals the absolute value of pid and for which the sender has permission to send the signal. Again, the set of all processes excludes certain system processes, as described earlier.

Pid == 1

The signal is sent to all processes on the system for which the sender has permission to send the signal. As before, the set of processes excludes certain system processes.

 

(二)kill 函数可以用来测定一个进程是否还存在

如果进程退出了,但还是zombia的话,也算存在。所以这个功能本身就比较恶心,而且由于判断是否存在之后再进行操作之间不是原子的,有可能在这之间该进程就退出了。此外,由于pid是可重复利用的,因此你判断的结果不一定是你当初想判断的进程。

0 signal就是null signal,用来干这个事情。当返回-1,且errno=ESRCH时,表明进程不在了。

下面是一个测试kill来检测某进程是否存在的例子:

include

#include

#include

#include

#include

 

void nullhandler( int num )

{

       puts( "child received 0 signal" );

}

int main()

{

       setbuf( stdout, NULL );

       int pid = fork();

       if( pid == 0 )

       {

              //child

              puts("child started");

              pause();

//            exit(0);

       }

       sleep(2);

//     waitpid( pid,0,0 );

       puts( " father before to send 0 signal" );

       int ret = kill( pid, 0 );

       printf( " father after send 0 signal, kill returned %d\n", ret );

 

}

 

1. //child exit and father have waited it, the child

//disappeared definitely

孩子调用exit,不调用pause, 并且父亲调用waitpid。检查结果程序不存在

shaoting@desktopbj-LabSD:/home/shaoting/mytest> ./a.out

child started

father before to send 0 signal

father after send 0 signal, kill returned -1

 

2. //child exit and father have not waited it, there is

//a zombia of the child

孩子调用exit,不调用pause,父亲没有调用waitpid,检查结果程序存在

shaoting@desktopbj-LabSD:/home/shaoting/mytest> ./a.out

child started

father before to send 0 signal

father after send 0 signal, kill returned 0

 

3. //child do not exit, it is still there

孩子调用pause,没调用exit,父亲也不调用waitpid。检查结果程序存在

shaoting@desktopbj-LabSD:/home/shaoting/mytest> ./a.out

child started

father before to send 0 signal

father after send 0 signal, kill returned 0

(三)raise函数

Raise函数没有提供pid,因为raise函数是ISO C中的函数,ISO C中没有进程操作,所以也就不会关心进程。她用来对本进程发送信号。Posix后来加入了对raise的支持,并有所扩展,所谓扩展主要是针对thread的支持。

 

阅读(735) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~