User:Gregory.Szorc/EECS 338 Homework 1

[edit] hw1.c

 
//Gregory Szorc <gregory.szorc@case.edu>
//EECS 338 HW 1
//2005-09-15
 
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
 
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <unistd.h>
#include <wait.h>
 
int printArguments(int argc, char *argv[]);
int printHostname();
int printUsername();
int printCWD();
char* getHomeEnv();
 
int main(int argc, char *argv[])
{
  pid_t homeLsPid = 0;
  pid_t child1 = 0;
  pid_t child2 = 0;
  pid_t lsPid = 0;
  char* home = 0;
  pid_t parent = 0;
 
  printArguments(argc, argv);
  printHostname();
  printUsername();
  printCWD();
 
  lsPid = vfork();
  if (lsPid == 0) {
    execlp("ls", "ls", "-l", NULL);
    exit(EXIT_SUCCESS);
  }
  else if (lsPid < 0) {
    printf("failed to fork\n");
    exit(EXIT_FAILURE);
  }
  else {
    //code that gets executed by parent process only
  }
  
  //wait for the child to finish
  wait(&lsPid);
  printf("\n\n");
  
  home = getHomeEnv();
  if (home) {
    if (chdir(home) == -1) {
      printf("Failure changing directory\n");
      exit(EXIT_FAILURE);
    }
    printCWD();
    
    homeLsPid = vfork();
    
    if (homeLsPid == 0) {
      execlp("ls", "ls", "-l", NULL);
      exit(EXIT_SUCCESS);
    }
    else if (homeLsPid < 0) {
      printf("failed to fork\n");
      exit(EXIT_FAILURE);
    }
    else {
      //code that gets executed by parent process only
    }
    
    wait(&homeLsPid);
    printf("\n\n");
    
  }
  else {
    printf("Failed to get home folder\n");
  }
  
  parent = getpid();
 
  sleep(1);
  
  printf("        PID     PPID     GID\n");
  
  child1 = fork();
  
  if (child1 == 0) {
    child2 = fork();
    
    if (child2 == 0) {
       sleep(1);
       
       printf("child2:  %d    %d    %d\n\n", getpid(), getppid(), getpgid(child2));
       
       sleep(2);
       printf("child2:  Who's there?\n");
       sleep(2);
       printf("child2:  Scold who?\n");
       
       exit(EXIT_SUCCESS);
    }
    else if (child2 == -1) {
      printf("Child2 didn't go well\n");
      exit(EXIT_FAILURE);
    }
    else {
      //executed by child 1
 
      printf("child1:  %d    %d    %d\n", getpid(), getppid(), getpgid(child1));
      exit(EXIT_SUCCESS);
    }
    
  }
  else if (child1 == -1) {
    printf("child 1 fork failed\n");
  }
  else {
    //executed by parent process
    wait(&child1);
    
    printf("parent:  %d    %d    %d\n", getpid(), getppid(), getpgid(getpid()));
    
    sleep(2);
    
    printf("parent:  Knock knock\n");
    sleep(2);
    printf("parent:  Scold\n");
    sleep(2);
    printf("parent:  Scold outside\n");
  }
  
  sleep(2);
  
  return EXIT_SUCCESS;
}
 
int printArguments(int argc, char* argv[]) {
  int i = 0;
  
  printf("Command Line Parameters:\n");
  
  for (i = 0; i < argc; i++) {
    printf("%i: %s\n", i, argv[i]);
  }
  
  printf("\n\n");
  
  return 0;
}
 
int printHostname() {
  char hostname[256] = "";
  int iError = 0;
  
  if (0 == (iError = gethostname(hostname, 256))) {
    printf("Hostname: %s\n", hostname);
    return iError;
  }
  else {
    printf("Hostname retrieval failed");
  }
  
  return iError;
  
}
 
int printUsername() {
  char *username = cuserid(NULL);
  
  printf("Username: %s\n", username);
  
  return 0;
}
 
int printCWD() {
  char cwd[1024] = "";
  
  getcwd(cwd, 1024);
  
  printf("Current Directory: %s\n", cwd);
  
  return 0;
}
 
char* getHomeEnv() {
  return getenv("HOME");
}
 
This page has been accessed 1,281 times.
This page was last modified 15:16, October 20, 2005 by Gregory Szorc.
About | Disclaimers