昆哥吧 关注:15贴子:242
  • 7回复贴,共1

昆哥

收藏回复

  • 61.157.97.*
/*课堂验证实验*/

#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<errno.h>
                                                                                
main(argc,argv)
 int argc;
 char *argv[];
 {
  int pid,stat;
  if(argc!=1){
  if((pid=fork())==0){
  printf("Child pid = %d\n",getpid());
  execl(argv[1],argv[1],0);
  exit(5);
  }
  }
 pid=wait(&stat);
 printf("pid=%d,H_stat=%d,L_stat=%d\n",pid, stat>>8,stat&0xff);
 }



1楼2007-10-08 10:31回复
    • 61.157.97.*
    /*实验3.4.1*/

    #include<unistd.h>
    #include<sys/types.h>
    #include<stdio.h>
    #include<errno.h>
     
    /*
    forkdemo.c
    */
    #include <stdio.h>
     
    main()
    {
     int ret_from_fork,mypid;
     mypid = getpid();
     printf("Before:my pid is %d\n" , mypid);
     
     ret_from_fork = fork();
     
     sleep(1);
     printf("After:my pid is %d, fork() said %d" , getpid(), ret_from_fork);
     
    }


    2楼2007-10-08 10:40
    回复
      • 61.157.97.*
      /*第二个*/

      /*
      execdemo.c
      */
       
      #include<unistd.h>
      #include<sys/types.h>
      #include<stdio.h>
      #include<errno.h>
       
      #include <stdio.h>
      main()
      {
       char *arglist[3];
       arglist[0] = "ls";
       arglist[1] = "-1";
       arglist[2] = "0";
       printf("***About to exec ls 1\n");
       execvp("ls",arglist);
       printf("***ls is done .bye!");
      }


      3楼2007-10-08 10:53
      回复
        • 61.157.97.*
        execvp("ls",arglist)

        取代当前进程的内容,把"***ls is done .bye!"替换到上面的"ls"


        4楼2007-10-08 11:08
        回复
          • 61.157.97.*
          arglist[2] = "0"; 创建进程失败
          结果:
          ***About to exec ls 1
          ***ls is done .bye!

          改成arglist[2] = NULL ;创建进程成功,运行第二句,覆盖第三句.
          结果:
          ***About to exec ls 1
          1
          11
          11.c
          1.c
          anaconda-ks.cfg
          execdemo
          execdemo.c
          forkdemo
          forkdemo.c
          install.log
          install.log.syslog
          xx
          xx.c
          xx.o


          5楼2007-10-08 11:28
          回复
            • 61.157.97.*
            7楼2007-12-27 18:26
            回复
              • 221.200.63.*
              哈哈` 
              `
              ` ` 仰慕我狠久乐八 
              `
               ` ` 
               ` 各位`
               、` ?


              8楼2009-02-10 09:20
              回复
                阿姨妈妈妈妈


                禁言 |9楼2009-12-23 09:59
                回复