A Unix Process by zwj23860

VIEWS: 16 PAGES: 20

									   A Unix Process

                                   Resources
        Data                        Resources
Text
        Stack
Process Status
                           File
                            File




                 UNIX kernel
    pid = 25
                      How Fork Works
                                  Resources
           Data                    Resources
 Text
           Stack
                          File
 Process Status            File


<…>
int cpid = fork( );
if (cpid = = 0) {
   <child code>
   exit(0);
}
<parent code>
wait(cpid);


                            UNIX kernel
    pid = 25
                      How Fork Works                pid = 26
                                  Resources
           Data                    Resources               Data
 Text                                            Text
           Stack                                           Stack
                          File
 Process Status            File                  Process Status


<…>                                            <…>
int cpid = fork( );                            int cpid = fork( );
if (cpid = = 0) {                              if (cpid = = 0) {
   <child code>                                   <child code>
   exit(0);                                       exit(0);
}                                              }
<parent code>                                  <parent code>
wait(cpid);                                    wait(cpid);


                            UNIX kernel
    pid = 25
                      How Fork Works                                   pid = 26
                                          Resources
           Data                            Resources                          Data
 Text                                                               Text
           Stack                                                              Stack
                                  File
 Process Status                    File                             Process Status


<…>                   cpid = 26                        cpid = 0   <…>
int cpid = fork( );                                               int cpid = fork( );
if (cpid = = 0) {                                                 if (cpid = = 0) {
   <child code>                                                      <child code>
   exit(0);                                                          exit(0);
}                                                                 }
<parent code>                                                     <parent code>
wait(cpid);                                                       wait(cpid);


                                    UNIX kernel
    pid = 25
                      How Fork Works                                   pid = 26
                                          Resources
           Data                            Resources                          Data
 Text                                                               Text
           Stack                                                              Stack
                                  File
 Process Status                    File                             Process Status


<…>                   cpid = 26                        cpid = 0   <…>
int cpid = fork( );                                               int cpid = fork( );
if (cpid = = 0) {                                                 if (cpid = = 0) {
   <child code>                                                      <child code>
   exit(0);                                                          exit(0);
}                                                                 }
<parent code>                                                     <parent code>
wait(cpid);                                                       wait(cpid);


                                    UNIX kernel
    pid = 25
                      How Fork Works                                   pid = 26
                                          Resources
           Data                            Resources                          Data
 Text                                                               Text
           Stack                                                              Stack
                                  File
 Process Status                    File                             Process Status


<…>                   cpid = 26                        cpid = 0   <…>
int cpid = fork( );                                               int cpid = fork( );
if (cpid = = 0) {                                                 if (cpid = = 0) {
   <child code>                                                      <child code>
   exit(0);                                                          exit(0);
}                                                                 }
<parent code>                                                     <parent code>
wait(cpid);                                                       wait(cpid);


                                    UNIX kernel
    pid = 25
                      How Fork Works                                   pid = 26
                                          Resources
           Data                            Resources                          Data
 Text                                                               Text
           Stack                                                              Stack
                                  File
 Process Status                    File                             Process Status


<…>                   cpid = 26                        cpid = 0   <…>
int cpid = fork( );                                               int cpid = fork( );
if (cpid = = 0) {                                                 if (cpid = = 0) {
   <child code>                                                      <child code>
   exit(0);                                                          exit(0);
}                                                                 }
<parent code>                                                     <parent code>
wait(cpid);                                                       wait(cpid);


                                    UNIX kernel
    pid = 25
                      How Fork Works                                   pid = 26
                                          Resources
           Data                            Resources                          Data
 Text                                                               Text
           Stack                                                              Stack
                                  File
 Process Status                    File                             Process Status


<…>                   cpid = 26                        cpid = 0   <…>
int cpid = fork( );                                               int cpid = fork( );
if (cpid = = 0) {                                                 if (cpid = = 0) {
   <child code>                                                      <child code>
   exit(0);                                                          exit(0);
}                                                                 }
<parent code>                                                     <parent code>
wait(cpid);                                                       wait(cpid);


                                    UNIX kernel
    pid = 25
                      How Fork Works                                   pid = 26
                                          Resources
           Data                            Resources                          Data
 Text                                                               Text
           Stack                                                              Stack
                                  File
 Process Status                    File                             Process Status


<…>                   cpid = 26                        cpid = 0   <…>
int cpid = fork( );                                               int cpid = fork( );
if (cpid = = 0) {                                                 if (cpid = = 0) {
   <child code>                                                      <child code>
   exit(0);                                                          exit(0);
}                                                                 }
<parent code>                                                     <parent code>
wait(cpid);                                                       wait(cpid);


                                    UNIX kernel
    pid = 25
                      How Fork Works
                                          Resources
           Data                            Resources
 Text
           Stack
                                  File
 Process Status                    File


<…>                   cpid = 26
int cpid = fork( );
if (cpid = = 0) {
   <child code>
   exit(0);
}
<parent code>
wait(cpid);


                                    UNIX kernel
    pid = 25
                      How Fork Works
                                          Resources
           Data                            Resources
 Text
           Stack
                                  File
 Process Status                    File


<…>                   cpid = 26
int cpid = fork( );
if (cpid = = 0) {
   <child code>
   exit(0);
}
<parent code>
wait(cpid);


                                    UNIX kernel
    pid = 25
                      How Fork Works
                                          Resources
           Data                            Resources
 Text
           Stack
                                  File
 Process Status                    File


<…>                   cpid = 26
int cpid = fork( );
if (cpid = = 0) {
   <child code>
   exit(0);
}
<parent code>
wait(cpid);


                                    UNIX kernel
    pid = 25
                      How Fork Works
                                          Resources
           Data                            Resources
 Text
           Stack
                                  File
 Process Status                    File


<…>                   cpid = 26
int cpid = fork( );
if (cpid = = 0) {
   <child code>
   exit(0);
}
<parent code>
wait(cpid);
<…>

                                    UNIX kernel
How Exec Works                           pid = 26
            Resources
             Resources                          Data
                                      Text
                                                Stack
    File
     File                             Process Status


                         cpid = 0   <…>
                                    int cpid = fork( );
                                    if (cpid = = 0) {
                                       <child code>
                                       exit(0);
                                    }
                                    <parent code>
                                    wait(cpid);


      UNIX kernel
      How Exec Works                           pid = 26
                  Resources
                   Resources                          Data
                                            Text
                                                      Stack
          File
           File                             Process Status


                               cpid = 0   <…>
                                          int cpid = fork( );
                                          if (cpid = = 0) {
                                             exec(“foo”);
                                             exit(0);
                                          }
                                          <parent code>
                                          wait(cpid);


foo         UNIX kernel
      How Exec Works                           pid = 26
                  Resources
                   Resources                          Data
                                            Text
                                                      Stack
          File
           File                             Process Status


                               cpid = 0   <…>
                                          int cpid = fork( );
                                          if (cpid = = 0) {
                                             exec(“foo”);
                                             exit(0);
                                          }
                                          <parent code>
                                          wait(cpid);


foo         UNIX kernel
      How Exec Works           pid = 26
                  Resources
                   Resources

          File
           File




foo         UNIX kernel
      How Exec Works                pid = 26
                  Resources
                   Resources               Data
                                 Text
                                          Stack
          File
           File                  Process Status


                               <line first of foo>
                               <…>
                               <…>
                               <…>
                               exit(0);




foo         UNIX kernel
How Exec Works                pid = 26
            Resources
             Resources               Data
                           Text
                                    Stack
    File
     File                  Process Status


                         <line first of foo>
                         <…>
                         <…>
                         <…>
                         exit(0);




      UNIX kernel
How Exec Works                pid = 26
            Resources
             Resources               Data
                           Text
                                    Stack
    File
     File                  Process Status


                         <line first of foo>
                         <…>
                         <…>
                         <…>
                         exit(0);




      UNIX kernel

								
To top