Chapter 4

The Linux Environment

When you write a program for Linux (or UNIX and UNIX-like systems), you have to take into account that the program will run in a multitasking environment. This means that there will be multiple programs running at the same time and sharing machine resources such as memory, disk space, and CPU cycles. There may even be several instances of the same program running at the same time. It’s important that these programs don’t interfere with one another, are aware of their surroundings, and can act appropriately to avoid conflicts such as trying to write the same file at the same time as another program.

This chapter considers the environment in which programs operate, how they can use that environment to gain information about operating conditions, and how users of the programs can alter their behavior. In particular, this chapter looks at

  • Passing arguments to programs
  • Environment variables
  • Finding out what the time is
  • Temporary files
  • Getting information about the user and the host computer
  • Causing and configuring log messages
  • Discovering the limits imposed by the system

Program Arguments

When a Linux or UNIX program written in C runs, it starts at the function main. For these programs, main is declared as

int main(int argc, char *argv[])

where argc is a count of the program arguments and argv is an array of character strings representing the arguments themselves.

You might also see C programs for Linux simply declaring main as

main()

This will ...

Get Beginning Linux Programming, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.