fork ,vfork,该怎么解决

fork ,vfork
1. fork vfork创建的进程名字是多少? 

linux为什么 不像widows那样 : 一个进程有名字? 

难道我创建一个进程就不能有名字?

进程不应该是exe 可执行文件吗?

真神奇


2.  fork的执行顺序

pid_t pid = fork();
if(pid <0)
{

}
else if(pid  ==0) //child process  为什么子进程会直接执行这里的代码呢? 
// 为什么子进程,不执行 pid_t pid = fork(); ? 既然是复制了一个进程,
//那么就应该执行 啊? 
{

}
else  //parent pid
{

}
------解决方案--------------------
回复4楼:
(1)pid_t可以当做windows下的进程句柄看,但是句柄是一个广义的概念,在Windows中,任何通过操作系统API创建的东西都返回一个句柄,句柄就是一个指针(在我的vs中);pid_t只有当创建一个进程时才能得到,在UNIX系统中,描述符使用的比较广泛,比如打开一个文件(open),会得到一个文件描述符,描述符通常是一个int类型的变量。

(2)我提供的命令没问题,是你用错了,命令ps的参数我可没有加横线
ps aux 
------解决方案--------------------
 grep ‘Test'


(3)system('pause'),pause是Windows下的命令,Linux是没有这个命令的,但是有一个函数,pause(),在头文件unistd.h中

稍稍修改后,代码如下:


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int gval = 0;

int main ()
{
  int nval = 7;

  pid_t pid = fork();

  if (pid < 0)
    return -1;

  if (0 == pid) { // child
    gval++;
    nval++;

    printf("In child process %u\n", (unsigned int)getpid());
    printf("[c] %d %d\n", gval, nval);
    pause();
    exit(0);
  } else {
    printf("In parent process %u\n", (unsigned int)getpid());
    printf("[p] %d %d\n", gval, nval);
    wait(NULL);
  }

  return 0;
}


输出:
[root@localhost src]# ./Test
In child process 5015
[c] 1 8
In parent process 5014
[p] 0 7

命令输出:
[root@localhost test_env]# ps aux 
------解决方案--------------------
 grep 'Test'
root      5014  0.0  0.0   1608   344 pts/2    S+   10:13   0:00 ./Test
root      5015  0.0  0.0   1608   220 pts/2    S+   10:13   0:00 ./Test
root      5025  0.0  0.0   4016   672 pts/3    R+   10:14   0:00 grep Test