CIS307: HMW 2: From TA: Pradeep Pappachan

CIS307: Homework 2: More from Pradeep Pappachan

For homework2 I expect each process to print out its ID before it prints anything else. For example if child1 prints out its process id it should do so as follows: CHILD1 : My process id is 13789

The problem with it is if you use selfid() to print out the process id there is no way for the calling process to identify itself to selfid() by passing an argument because selfid() cannot have input arguments. (Remember that functions whose address gets passed to atexit() cannot have input arguments.)

To get around this declare a global array of char called name (say) i.e., char name[10]; Before a process does anything else it should copy its own name into its copy of this array. Then in selfid you should print out the contents of this array and the process will be correctly identified.

e.g.
In the parent you should have :
main() {
  ...
  ...
  strcpy(name,"PARENT");
  ...
  ...
  if (fork()==0) {
	/* child1 */
    strcpy(name,"CHILD1");
    selfid();
    ...
    ...
  }
  ...
  ...
}

selfid() {
  printf("%s : My process id is %d\n",name, getpid());
  ...
}
Pradeep Pappachan