Posted in software

Programming Tidbits – C

  • When using getchar function in C to read a characted from standard input, one should take the value to an integer and not a character. Because EOF is -1 which is an integer
  • getline is a handy function that can get a line of input from a given FILE stream (which means a file or standard input)
  • strtok can take different delimiter bytes in each invocation for the same string! Did not know this.
    • two or more delimiter bytes at the start and at the end, in between the tokens are ignored.
    • When we have multiple threads tokenizing strings, strtok does not work. For that strtok_r should be used instead which also takes another parameter to store the context
  • waitpid just doesn’t wait for the child to exit. It waits for the child’s state to change. A child can exit, can get paused/resumed by a signal. These can all be caught
    • By default, it waits for child termination, until one of the options passed overrides the default behavior
  • wait is a simpler version of waitpid where it waits until one of its children terminates
  • The pid value passed on has a lot of semantics. If the pid is:
    • > 0, then it is the pid of a child process
    • -1, then it is any child process
    • < -1, then it is any child process that belongs to this process group (absolute value)
    • 0, then it is any child process that belongs to the caller’s process group
  • WIFEXITED and WIFSIGNALED are two macros that can help in knowing if the child process terminated normally or interrupted via a signal

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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