Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

930 Solutions to Selected Exercises Appendix C


The results depend on the platform. Recall thatdaemonizecloses all open file
descriptors and then reopens the first three to/dev/null.This means that the
process won’t have a controlling terminal, sogetloginwon’t be able to look in
theutmpfile for the process’s login entry.Thus, on Linux 3.2.0 and Solaris 10,
we find that a daemon has no login name.
Under FreeBSD 8.0 and Mac OS X 10.6.8, however,the login name is maintained
in the process table and copied across afork.This means that the process can
always get the login name, unless the parent didn’t have one to start out (such as
initwhen the system is bootstrapped).

Chapter 14


14.1 The test program is shown in FigureC.15.
#include "apue.h"
#include <fcntl.h>
#include <errno.h>

void
sigint(int signo)
{
}

int
main(void)
{
pid_t pid1, pid2, pid3;
int fd;

setbuf(stdout, NULL);
signal_intr(SIGINT, sigint);

/*
*Create a file.
*/
if ((fd = open("lockfile", O_RDWR|O_CREAT, 0666)) < 0)
err_sys("can’t open/create lockfile");

/*
*Read-lock the file.
*/
if ((pid1 = fork()) < 0) {
err_sys("fork failed");
}else if (pid1 == 0) { /* child */
if (lock_reg(fd, F_SETLK, F_RDLCK, 0, SEEK_SET, 0) < 0)
err_sys("child 1: can’t read-lock file");
printf("child 1: obtained read lock on file\n");
pause();
printf("child 1: exit after pause\n");
Free download pdf