Saturday, March 3, 2012

Fork & execl - System Software Lab - Program 9



Program for Fork:



#include stdio.h
#include stdlib.h
#include unistd.h
#include sys/types.h
int main(void)
{
        pid_t pid;
          char cwdir_cld[100];
          char cwdir_prt[100];

          if ((pid = fork()) < 0)
      {
                  perror("\nfork error : Cannot Create Fork\n");
                     exit(1);
          }

          if (pid == 0)
      {
                printf("\nInside child process\n");
/*   Now we are in the childs thread let us issue a chdir syscall :)     */
                  if(chdir("/tmp") < 0) {
                          perror("chdir error");
                   }
/*  Let us now print the current working directory of the child   */
                   if (getcwd(cwdir_cld, 100) != NULL) {
printf("\nCurrent working directory of the child is : %s\n", cwdir_cld);
                   } else {
                           perror("getcwd error");
                   }
             printf("\n End of Child Process\n");
                  
           /* end of the child */
          }
        else
      {
               printf("\n Inside Parent Process \n");
/* Now we are in the parent thread Let us check the current working directory */
                  if (getcwd(cwdir_prt, 100) != NULL)
            {
printf("\nCurrent working directory of the parent is : %s\n", cwdir_prt);
                   } else
            {
                           perror("getcwd error");
                   }
          }

        printf("\nEnd of Fork Program\n");
          return(0);
}
Program for Execl :

#include
int main()
{
  execl("/bin/ls","/bin/ls","-r","-t","-l", (char *) 0);
}

No comments:

Post a Comment