Saturday, August 4, 2018

Notes on fork()

  • We can avoid orphans by Parent process calling wait( ) call.
wait(int *p)
*  p will have the return code of the child. So we can find
   if process was terminated normally or not. 
  • Child process has its own copy of globals too.
  • The file descriptor table of the parent is shared with child. It has FDs of all open files in the parent.
  • fread is buffered read (defaults to 1024 bytes at a time). We call fflush to empty buffer. This is more efficient as write/reads are in-memory till 1024 bytes.
  • read is a low level byte wise read op. There is no buffering.

The following code writes the string twice to the file. Why?

int main()
{
  char *p = "hello world";
  FILE *fp;
  fp = fopen("test", "w");
  fwrite(p, sizeof(p), 1, fp);
  fork();
  • fork() also copies environment variables of the parent to the child. Type $set to access env vars.

Written with StackEdit.

No comments:

Post a Comment