Operating System/System Programming(Arif Butt)

Lec19) Process Management-III

Tony Lim 2021. 7. 11. 22:14

exec system call

completely replaced by the new prorgam, and the new program starts executing its main function.

there is no return after a successful exec call. The exec() function return only if an error has occurred. The return value is -1, and errno is set to indicate the error.

 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <wait.h>
int main(){
   int status;
   pid_t cpid = fork();
   if (cpid == 0){
      execlp("ls", "myls", "-l", "/home/", NULL);
      printf("This line will not be printed\n");
   }
   else{
      wait(&status);
      printf("Hello I m Parent.\n");
   }
   return 0;
}

wait system call make parent process to wait until child process finishes. 

child process is exchanged with "ls" if it success executing , printf will not be printed.

 

 

 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <wait.h>
void exit_handler(){
   printf("Exit handler\n");
}
int main(){
   atexit(exit_handler);

pid_t cpid = fork();
   if (cpid == 0){
      printf("This is child process.\n");
      exit(0);
   }
   else{
      wait(NULL);
      printf("This is parent process.\n");
      exit(0);
   }
}

exit handler is inherited child is also calling same exit handler

 

 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <wait.h>
void exit_handler(){
   printf("Exit handler\n");
}
int main(){
   atexit(exit_handler);

pid_t cpid = fork();
   if (cpid == 0){
      execlp("/bin/ls", "myls", "/home", '\0');
      printf("This is child process.\n");
      exit(0);
   }
   else{
      wait(NULL);
      printf("This is parent process.\n");
      exit(0);
   }
}

notice that exit handler is not inherited.  

 

Process Group 

Process group is set of one or more processes sharing the same PGID. Every process group has a Process Group Leader, which is the process that creates the group and whose PID becomes the PGID of that group.

a child process inherits its parent's PGID. life time of a Process Group starts when the leader creates the group an dends when the last member process leave the group.

 

Session

A session is a collection of one or more proces groups. A process's session membership is determined by its SID(Session ID).

every sesion ahs a session leader, which is the process that creates a new session and whose PID becomes the SID.

At any point in time, one of the process groups in a session is the foreground process group & others are background process groups.

 

Terminal

All processes in a session shares a single controlling terminal, which is established when a sesion leader first opens a terminal device. A session leader is the controlling process for the terminal. If a terminal disconnect occurs, the kernel sends teh session leader SIGHUP signal.

every process group has same SID.

3 process group = 1 foreground process group + 2 background process group