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.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.