Unix™ Systems Programming [Electronic resources] : Communication, Concurrency, and Threads نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Unix™ Systems Programming [Electronic resources] : Communication, Concurrency, and Threads - نسخه متنی

Prentice Hall

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید










3.6 Background Processes and Daemons


The shell is a command interpreter that prompts for commands, reads the commands from standard input, forks children to execute the commands and waits for the children to finish. When standard input and output come from a terminal type of device, a user can terminate an executing command by entering the interrupt character. (The interrupt character is settable, but many systems assume a default value of Ctrl-C.)

Exercise 3.27

What happens when you execute the following commands?


cd /etc
ls -l

Now execute the ls -l command again, but enter a Ctrl-C as soon as the listing starts to display. Compare the results to the first case.

Answer:

In the first case, the prompt appears after the directory listing is complete because the shell waits for the child before continuing. In the second case, the Ctrl-C terminates the ls.

Most shells interpret a line ending with & as a command that should be executed by a background process. When a shell creates a background process, it does not wait for the process to complete before issuing a prompt and accepting additional commands. Furthermore, a Ctrl-C from the keyboard does not terminate a background process.

Exercise 3.28

Compare the results of Exercise 3.27 with the results of executing the following command.


ls -l &

Reenter the ls -l & command and try to terminate it by entering Ctrl-C.

Answer:

In the first case, the prompt appears before the listing completes. The Ctrl-C does not affect background processes, so the second case behaves in the same way as the first.

A daemon is a background process that normally runs indefinitely. The UNIX operating system relies on many daemon processes to perform routine (and not so routine) tasks. Under the Solaris operating environment, the pageout daemon handles paging for memory management. The in.rlogind handles remote login requests. Other daemons handle mail, file transfer, statistics and printer requests, to name a few.

The runback program in Section 11.5.) The runback parent does not wait for its child to complete.

Program 3.7 runback.c

The runback program creates a child process to execute a command string in the background.


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include "restart.h"
int makeargv(const char *s, const char *delimiters, char ***argvp);
int main(int argc, char *argv[]) {
pid_t childpid;
char delim[] = " \t";
char **myargv;
if (argc != 2) {
fprintf(stderr, "Usage: %s string\n", argv[0]);
return 1;
}
childpid = fork();
if (childpid == -1) {
perror("Failed to fork");
return 1;
}
if (childpid == 0) { /* child becomes a background process */
if (setsid() == -1)
perror("Child failed to become a session leader");
else if (makeargv(argv[1], delim, &myargv) == -1)
fprintf(stderr, "Child failed to construct argument array\n");
else {
execvp(myargv[0], &myargv[0]);
perror("Child failed to exec command");
}
return 1; /* child should never return */
}
return 0; /* parent exits */
}

Example 3.29

The following command is similar to entering ls -l & directly from the shell.


runback "ls -l"


    / 276