2013-07-04 46 views
-2

好吧,我試圖做一個簡單的功能不斷地改變其PID, 但我得到這個:錯誤:「SIGKILL」未申報(在一次使用此功能)

error: ‘SIGKILL’ undeclared (first use in this function) 

這裏是我的代碼:

#include <stdio.h> 

int changePID(void) { 
     int pid = fork(); 
     printf(pid); 
     sleep(3); 
     kill(pid, SIGKILL); 
} 

    int main(void) { 
     while (1) { 
      changePID(); 
      } 
} 
+1

這也可能幫助,http://stackoverflow.com/questions/6501522/how-to-kill-a-child-process-by-the-parent-process – D4zk1tty

+6

#include http://unixhelp.ed.ac.uk/ CG I/man-cgi?signal + 2 – Alexis

回答

4

你缺少行#include <signal.h>爲SIGKILL

printf(pid);是行不通的,因爲printf的需要char*,你給他一個pid_t

什麼的在一個循環中殺死一個孩子呢?

的printf會在父親SEGV所以你永遠不會達到殺死

,並在兒子你要殺死

every process in the process group of the current process with a pid of 0

+0

'pid_t'隱式與'@ include'ing''形成'fork()'和'sleep()'原型。 – alk

2

您需要添加:

#include <sys/types.h> 
#include <signal.h> 

看看man頁面,當你有一個問題,與作爲系統調用。

+0

現在就開始工作,雖然我不得不修復其他一些錯誤,但是很好,謝謝! – D4zk1tty

+0

爲什麼包括''? – alk

+0

用於討論聯機幫助頁。似乎沒有人在2013年看過手冊頁...... –

0

嘗試包括

#define <sys/signal.h> 
相關問題