A shell is a command line interpreter that reads user input and executes commands.

Chapter 5: Standard I/O Library

Opening a Stream:

  • FILE *fopen(const char *pathname, const char *type);
  • FILE *freopen(const char *pathname, const char *type, FILE *fp);

Reading and Writing a Stream:

  • int getc(FILE *fp);
  • int putc(FILE *fp);
  • char *fgets(char *buf, int n, FILE *fp);
  • int *fputs(const char *str, FILE *fp);

Example:

\#include "ourhdr.h"

int main(void) {

int c;
while ( (c = getc(sdtin)) != EOF )
if (putc(c, stdout) == EOF)
err_sys("ooutput error");
if (ferror(stdin))
err_sys("input error");
exit(0);

}

int main(void) {

char buf[MAXLINE];
while (fgets(buf, MAXLINE, sdtin)) != NULL)
if (fputs(buf, stdout) == NULL)
err_sys("ooutput error");
if (ferror(stdin))
err_sys("input error");
exit(0);

}

Chapter 8: Process Control

The only way a new process is created by the Unix kernel is when an existing process calls the fork function.

The new process created by fork is called the child process. This function is called once but returns twice. The only difference in the returns is that the return value in the child is 0 while the return value in the parent is the process ID of the new child. The reason the child's process ID is returned to the parent is because a process can have more than one child, so there is no function that allows a process to obtain the process IDs of its children. The reason fork returns 0 to the child is because a process can have only a single parent, so the child can always call getppid to obtain the process ID of its parent. (Process ID 0 is always in use by the swapper, so it's not possible for 0 to be the process ID of a child.)

Both the child and parent continue executing with the instruction that follows the call to fork. The child is a copy of the parent. For example, the child gets a copy of the parent's data space, heap, and stack. Note that this is a copy for the child--the parent and child do not share these portion of memory. Often the parent and child share the text segment, if it's read only.

In general, we never know if the child starts executing before the parent of vice versa. This depends on the scheduling algorithm used by the kernel. If it's required that the child and parent synchronize with each other, some form of interprocess communication is required.

When we redirect the standard output of the parent, the child's standard output is also redirected.

Example:

\#include <sys/types.h>
\#include "ourhdr.h"

int glob = 6;
char buf[] = "a write to stdout\n";
int main(void) {

int var;
pid_t pid;
var = 88;
if (write(STDOUT_FILENO, buf, sizeof(buf)-1) != sizeof(buf)-1)
err_sys("write error");
printf("before fork\n");
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0) {
glob++;
var++;
} else
sleep(2);
printf("pid = d, var = %d\n", getpid(), glob, var);
exit (0);

}

There are two uses for fork:

  1. When a process wants to duplicate itself so that the parent and child can each execute different sections of code at the same time. This is common for network servers--the parent waits for a service request from a client. When the request arrives, the parent calls fork and lets the child handle the request. The parent goes back to waiting for the next service request to arrive.
  2. When a process wants to execute a different program. This is common for shells. In this case, the child does and exec right after it returns from the fork.

HIGHLIGHTED POSTS

My wedding photos (17/09/08)

Friendly match - Austria 3 - 4 Netherlands (26/03/08)

5 days in Greece (Day 0 1 2 3 4 5) (15/03/08)

Lần đầu tiên trượt băng (28/02/08)

Uhrenmuseum Wien (24/02/08)

Wien Museum Karlsplatz Part 1 2 3 4 5 (17/02/08)

Slam Dunk (06/02/08)

Nem rán mừng xuân (05/02/08)

Comparisons between West and East's cultures (31/01/08)

Captain Tsubasa (27/01/08)

Bò xào, thật là đơn giản! (24/01/08)

Rambling in the center of Vienna (16/01/08)

The last night of the year 2007 in Vienna (01/01/08)

TOEFL Score (27/12/07)

Snowing in Vienna (16/11/07)

Locations of visitors to this page

page counter