sun.com docs.sun.com My Sun Worldwide Sites

Previous Previous     Contents     Index     Next Next

Code Examples for Accessing project Database Entries

Example 2-1 Printing the First Three Fields of Each Entry in the project Database

The key points for this example include the following:

  • setprojent() rewinds the project database to start at the beginning.

  • getprojent() is called with a conservative maximum buffer size that is defined in project.h.

  • endprojent() closes the project database and frees resources.

#include <project.h>

struct project projent;
char buffer[PROJECT_BUFSZ]; /* Use safe buffer size from project.h */
	...
struct project *pp;

setprojent();  /* Rewind the project database to start at the beginning */

while (1) {
   pp = getprojent(&projent, buffer, PROJECT_BUFSZ);
	  if (pp == NULL)
          break;
    printf("%s:%d:%s\n", pp->pj_name, pp->pj_projid, pp->pj_comment);
		...
};

endprojent();   /* Close the database and free project resources */

Example 2-2 Getting a project Database Entry That Matches the Caller's Project ID

The following example calls getprojbyid() to get a project database entry that matches the caller's project ID. The example then prints the project name and the project ID.

#include <project.h>

struct project *pj;
char buffer[PROJECT_BUFSZ]; /* Use safe buffer size from project.h */

main()
{
   projid_t pjid;
   pjid = getprojid();
   pj = getprojbyid(pjid, &projent, buffer, PROJECT_BUFSZ);
   if (pj == NULL) {
       /* fail; */
   }
   printf("My project (name, id) is (%s, %d)\n", pp->pj_name, pp->pj_projid);
}

Programming Issues Associated With Projects and Tasks

Consider the following issues when writing your application:

  • No function exists to explicitly create a new project.

  • A user cannot log in if no default project for the user exists in the project database.

  • A new task in the user's default project is created when the user logs in.

  • When a process joins a project, the project's resource control and pool settings are applied to the process.

  • setproject() requires privilege. The newtask command does not require privilege if you own the process. Either can be used to create a task, but only newtask can change the project of a running process.

  • No parent/child relationship exists between tasks.

  • Finalized tasks can be created by using newtask -F or by using setproject() to associate the caller with a new project. Finalized tasks are useful when trying to accurately estimate aggregate resource accounting.

  • The reentrant functions, getprojent(), getprojbyname(), getprojbyid(), getdefaultproj(), and inproj(), use buffers supplied by the caller to store returned results. These functions are safe for use in both single-threaded applications and multithreaded applications.

  • Reentrant functions require these additional arguments: proj, buffer, and bufsize. The proj argument must be a pointer to a project structure allocated by the caller. On successful completion, these functions return the project entry in this structure. Storage referenced by the project structure is allocated from the memory specified by the buffer argument. bufsize specifies the size in number of bytes.

  • If an incorrect buffer size is used, getprojent() returns NULL and sets errno to ERANGE.

Previous Previous     Contents     Index     Next Next
Company Info Contact Terms of Use Privacy Copyright 1994-2007 Sun Microsystems, Inc.