我一直花費大量時間瞭解在linux
中使用clone()
系統調用的方法。我寫了下面的程序並沒有與正常工作....clone()系統調用不能正常工作
#define _GNU_SOURCE
#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
#include<sched.h>
#include<sys/types.h>
int sched2()
{
printf("it is working\n");
}
int main(int argc, char *argv[])
{
printf("in the main\n");
void *stack=malloc(65536);
int Pid=clone(sched2, stack, 0, NULL);
printf("%d\n",Pid);
if(Pid==-1)
{
printf("clone error\n");
exit(1);
}
sleep(100);
printf("yea2");
exit(0);
}
正如我所料,串it is working
應印... ...一起
in the main
2534
它WASN打印it is working
字符串。此外,當我把新創建的內核線程的進程ID,併發出
kill -9 2534
,我使用ps命令檢查終端上的進程,它應該是這樣的...
PID TTY TIME CMD
2209 pts/2 00:00:00 bash
2533 pts/2 00:00:00 a.out
2534 pts/2 00:00:00 a.out <defunct>
2535 pts/2 00:00:00 ps
過程其實並沒有被殺死!它仍然在運行...請拋出一些光...謝謝你...
'wait(50)'(in'sched2')可能不會做你認爲它的做法... – twalberg
其實你是對的......這就是我編輯和刪除它的原因......反正非常感謝.. :) – nitish712