相关讨论
#include
#include
#include"apue.h"
#include"unix_err.h"
int glob = 6;
char buf[] = "a write to stdout\n";
int main()
{
int var;
pid_t pid;
pid_t pid_child;
var = 88;
if(write(STDOUT_FILENO,buf,sizeof(buf)-1) != sizeof(buf)-1)
{
err_sys("write error");
}
printf("before fork\n");
if( (pid=fork()) < 0 )
{
err_sys("fork error");
}
else if(pid == 0)
{
if( (...
我试了一下可以,想确认一下。。。
如果这样做的话有没有什么问题,需不需要注意什么?
谢谢
#include
#include
#include
#include
int glob =6;
int main(){
int var;
pid_t pid;
var =88;
if((pid=fork()<0))
printf("fork error");
else if(pid==0){
glob++;
var++;
}else{
sleep(2);
}
printf("pid=%d,glob=%d,var=%d\n",getpid(),glob,var);
}
执行结果,怎么和书上说的严重不一样呀
[lgl@jlu work]$ gcc -o 8 8...
by
redog
-
C/C++
-
2005-11-05 15:57:22 阅读(1533) 回复(10)
主函数main()里面有一个while(1)的循环,每隔1秒fork()一次,子进程打印一串字符之后调用exit(0),父进程继续loop。但是从ps看系统的进程状态,发现有好多同名字的进程,这是为什么阿,子进程应该如何退出阿?谢谢!
#include ;
#include ;
#include ;
#include ;
void main()
{
if (fork() == 0)
sleep(10);
else
exit(0);
}
test15:/usr/tux/zp/tmp $ cc -o zomb test.c
test15:/usr/tux/zp/tmp $ ./zomb
test15:/usr/tux/zp/tmp $ ps -ef|grep zomb
tux 8160 8030 0 15:15:13 pts/ti 0:00 grep zomb
tux 8158 1 0 15:15:...
c++写了一个程序,生成了一个线程,而后在该线程中调用fork生成一个子进程来执行
perl脚本(该脚本执行正确),父进程在一个循环中调用waitpid非阻塞的查看子进程状态,等待子进程退出。
结果发现父进程无法正常的得到子进程的结束状态,waitpid开始返回是0,在子进程结束后,
父进程返回的是-1.
请问这是什么原因,在线程中再生成进程会有什么问题么?
先 fork 子进程然后 execl 另一个可执行程序, 如何得到这个可执行程序结束的返回值?
pid_t pid;
int stat_loc;
/*父进程*/
if ((pid=fork())>0) {
waitpid(pid,&stat_loc,0);
/*父进程等待进程号为pid的子进程的返回*/
}
else {
/*子进程的处理过程*/
exit(1);
}
/*父进程*/
printf("stat_loc is [%d]\n",stat_loc);
这是【FreeBSD操作系统设计与实现】的习题5.5,原文如下:
5.5 Explain why the vfork system call will always be more efficient than a clever implementation of the fork system call.
by
雨丝风片
-
BSD
-
2006-03-29 20:58:05 阅读(4079) 回复(8)
apue中8-1的例子,(第一版与第二版 皆可)
myfork.c
---------------------------------------------------------------------------
#include "apue.h"
int glob=6;
char buf[]=" a write to stdout\n";
int main(int argc, char *argv[])
{
int var;
pid_t pid;
var=88;
if(write(STDOUT_FILENO,buf,sizeof(buf)-1)!=sizeof(buf)-1)
err_sys("write error");
prin...