日期:2014-05-16  浏览次数:20676 次

关于Linux进程问题求助
本人刚接触操作系统,用Linux。
请问各位老师:
“父进程每隔三秒重复建立两个子进程”用c语言应该怎么写?

------解决方案--------------------
fork()函数创建子进程;
sleep()函数用户休眠;

主要使用上述俩个函数

写的话还是自己写吧,只有自己写的才会有收获啊
------解决方案--------------------
探讨

fork()函数创建子进程;
sleep()函数用户休眠;

主要使用上述俩个函数

写的话还是自己写吧,只有自己写的才会有收获啊

------解决方案--------------------
探讨

fork()函数创建子进程;
sleep()函数用户休眠;

主要使用上述俩个函数

写的话还是自己写吧,只有自己写的才会有收获啊

------解决方案--------------------
探讨
fork()函数创建子进程;
sleep()函数用户休眠;

主要使用上述俩个函数

写的话还是自己写吧,只有自己写的才会有收获啊

------解决方案--------------------
给你一个例子吧,注意那个exit(0),很关键

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

static void Routine(){
printf("I am %ld\n",getpid());
}

int main(int argc, char **argv){
#define NUM 10
pid_t pids[NUM];
int cnt = 0;

for(cnt=0;cnt<NUM;cnt++){
sleep(3);
pids[cnt]=fork();
if(pids[cnt]<0){
perror("fork error\n");
exit(EXIT_FAILURE);
}else if(pids[cnt]==0){
Routine();
exit(0);
}
}

return 0;
}