当前位置: 技术问答>linux和unix
some problems about C in linux
来源: 互联网 发布时间:2015-10-31
本文导语: recently, I am studying C programming in Linux.There are some problems bothering me: 1> fork(..) can start a process which is the same with his parent,but where do the child process star to run,from the first line of 'main' or from the line in...
recently, I am studying C programming in Linux.There are some problems bothering me:
1> fork(..) can start a process which is the same with his parent,but where do the child process star to run,from the first line of 'main' or from the line including 'fork'?
2>if i call 'waitpid(child,&status,WNOHANG)',it will return if no process exits.What's the effect of calling this function(with WNOHANG)?Is it necessary to call 'wait' or 'waitpid' to every child process to avoid zombie process?
3>i wrote a programme which used the function of pthreadcreate and pthreadjoin.It was OK when I compiled it.But when I linked it to be excutable programme,gcc prints these words:
: undefined reference to `pthread_vreate'
/tmp/ccJVYm5F.o(.text+0x60): In function `main':
: undefined reference to `pthread_join'
collect2: ld returned 1 exit status
whatis the problem?how can i solve this problem?
1> fork(..) can start a process which is the same with his parent,but where do the child process star to run,from the first line of 'main' or from the line including 'fork'?
2>if i call 'waitpid(child,&status,WNOHANG)',it will return if no process exits.What's the effect of calling this function(with WNOHANG)?Is it necessary to call 'wait' or 'waitpid' to every child process to avoid zombie process?
3>i wrote a programme which used the function of pthreadcreate and pthreadjoin.It was OK when I compiled it.But when I linked it to be excutable programme,gcc prints these words:
: undefined reference to `pthread_vreate'
/tmp/ccJVYm5F.o(.text+0x60): In function `main':
: undefined reference to `pthread_join'
collect2: ld returned 1 exit status
whatis the problem?how can i solve this problem?
|
1> fork(..) can start a process which is the same with his parent,but where do the child process star to run,from the first line of 'main' or from the line including 'fork'?
if fork return 0,it means child process. the child process runs from here
pid=fork();
if(pid==0)
{
}
if fork return 0,it means child process. the child process runs from here
pid=fork();
if(pid==0)
{
}
|
偶的E文不好,只好用中文写了。看不懂,偶就帮不了你了。
第3个问题:
要在“链接”的时候加入-lpthread选项
第3个问题:
要在“链接”的时候加入-lpthread选项
|
我还以为这年头流行用E文问问题呢……
楼主应该多动手,多看文档,这样就不至于要问第一个问题了。确切地说,新的进程在你的可见返回是从fork的return来运行的(所以child process可以得到一个0作为返回值)
2、还是那句话,你应该多看文档……不论是wait还是waitpid,你没有child process,它们都会因为错误而返回(且errno为ECHILD)。对于任何一个进程来说,wait总是由父进程调用的,就算你自己不掉用,只要父进程结束了,init会帮你调用wait来回收僵尸进程的(前提是你的父进程要结束)。
3、是pthread_create不是vreate……部分POSIX的函数也不需要额外的链接库,譬如fork、read等等。一般来说gcc默认链接libc,只有libc里面的函数不需要额外的lib
楼主应该多动手,多看文档,这样就不至于要问第一个问题了。确切地说,新的进程在你的可见返回是从fork的return来运行的(所以child process可以得到一个0作为返回值)
2、还是那句话,你应该多看文档……不论是wait还是waitpid,你没有child process,它们都会因为错误而返回(且errno为ECHILD)。对于任何一个进程来说,wait总是由父进程调用的,就算你自己不掉用,只要父进程结束了,init会帮你调用wait来回收僵尸进程的(前提是你的父进程要结束)。
3、是pthread_create不是vreate……部分POSIX的函数也不需要额外的链接库,譬如fork、read等等。一般来说gcc默认链接libc,只有libc里面的函数不需要额外的lib