LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1414|回复: 4

fork调用返回值的问题,请大家帮忙看一下.

[复制链接]
发表于 2005-1-13 13:40:58 | 显示全部楼层 |阅读模式
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main()
{
        int data_processed;
        int file_pipes[2];
        const char some_data[]="123";
        char buffer[BUFSIZ+1];
        int fork_result;

        memset(buffer,'\0',sizeof(buffer));

        if(pipe(file_pipes)==0) {
                fork_result=fork();
                if(fork_result==-1) {
                        fprintf(stderr,"fork failure");
                        exit(EXIT_FAILURE);
                }

                /*here we wil jugde the parent and the child process*/
                if(fork_result==0) {
                        /*child process*/
                #ifdef DEBUG
                        printf("see here child id:%d\n",getpid());
                #endif

                data_processed=read(file_pipes[0],buffer,BUFSIZ);
                printf("read %d bytes:%s\n",data_processed,buffer);
                exit(EXIT_SUCCESS);
                } else {
                        /*parent process*/
                #ifdef DEBUG
                        printf("see here fork_result:%d\n",fork_result);
                        printf("compare it with parent id:%d\n",getpid());
                #endif

                data_processed=write(file_pipes[1],some_data,strlen(some_data));                printf("wrote %d bytes\n",data_processed);
                }
        }
        exit(EXIT_FAILURE);
}


--------------------------------------------------------------------------
运行结果是:
[asnoka@bdolphin:prog]$ ./pipe2
see here child id:3327
see here fork_result:3327
compare it with parent id:3326
wrote 3 bytes
[asnoka@bdolphin:prog]$ read 3 bytes:123
--------------------------------------------------------------------------
现在我想问的是,为什么那个child id和fork_result的关系,他们是相等的,于是我觉得是不是因为那用蓝色标出的那一行,在那里通过分别fork_result的取值,而进入子进程的,但是我总觉得那么既然进入了子进程,那么fork_result的值当是0,或者说如果这个语句不在父进程中执行的话,那么应当是一个随机值,为什么还和fork_result的值一样呢?是不是进入了子进程后,fork_result又被赋值为子进程的pid呢?
请大家帮我看一下,谢谢先!!!
 楼主| 发表于 2005-1-13 13:43:22 | 显示全部楼层
对不起呀,上边的那个红色的笑脸是%
而那个蓝白色的是            :p

第一次插入UBB代码,于是出现了这些问题,不便之处,还请兄弟们见谅.再谢!!!
发表于 2005-1-13 15:17:17 | 显示全部楼层
fork后父进程的确得到了子进程和PID。参见glibc的说明:


  1. pid_t fork (void)  Function
  2. The fork function creates a new process.
  3. If the operation is successful, there are then both parent and child processes and both see
  4. fork return, but with different values: it returns a value of 0 in the child process and returns
  5. the child's process ID in the parent process.

  6. If process creation failed, fork returns a value of -1 in the parent process. The following errno
  7. error conditions are defined for fork:

  8. EAGAIN
  9. There aren't enough system resources to create another process, or the user already has too
  10. many processes running. This means exceeding the RLIMIT_NPROC resource limit, which can
  11. usually be increased; see Limits on Resources.

  12. ENOMEM
  13. The process requires more space than the system can supply.  

复制代码

实际上fork之后,这段代码在两个进程中执行。子进程走forkresult==0的一段。父进程走
forkresult==childpid的一段。
发表于 2005-1-13 19:12:36 | 显示全部楼层
fork在英语中就是叉子,表示程序执行到这里分叉了。
fork()函数调用的返回值在父子进程中也不一样,子进程中返回值为0,父进程中返回值为子进程的pid。
 楼主| 发表于 2005-1-14 00:05:23 | 显示全部楼层
原来如此,谢谢了,呵呵,原来从英文里边还可以学到一些函数功能呢.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表