O'Reilly logo

Beginning Linux Programming, 4th Edition by Richard Stones, Neil Matthew

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required