16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Intro to System Concepts<br />

! File Subsystem<br />

User File Descriptor Table<br />

File Table<br />

Inode Table<br />

- allocated per process<br />

- global kernel structure<br />

- index node , describes disk<br />

layout file data, file owner,<br />

access permissions, access times<br />

Introduction to kernel<br />

user fd table file table i-node table<br />

When a process creates a new file, <strong>the</strong> kernel assigns it an unused inode. Inodes are stored in <strong>the</strong> file system, but<br />

<strong>the</strong> kernel reads <strong>the</strong>m into an in-core inode table.<br />

The file table keeps track of <strong>the</strong> byte offset in <strong>the</strong> file where <strong>the</strong> user's next read or write will start, and <strong>the</strong> access<br />

rights allowed to <strong>the</strong> opening process.<br />

The user file descriptor table identifies all open files for a process. The kernel returns a file descriptor for <strong>the</strong> open<br />

system call, which is an index into <strong>the</strong> user fd table.<br />

! File System Layout<br />

boot block - occupies <strong>the</strong> beginning of <strong>the</strong> file system: first sector, bootstrap code<br />

super block - describes state of file system: size, number of files, free space<br />

inode list - kernel references inodes by index, <strong>the</strong> root inode is used by mount<br />

data blocks - an allocated data block can belong to one and only one file in <strong>the</strong> file system<br />

Processes<br />

A process is <strong>the</strong> execution of a program and consists of bytes that <strong>the</strong> CPU interprets as machine instructions.<br />

Processes communicate <strong>with</strong> o<strong>the</strong>r processes and <strong>with</strong> <strong>the</strong> rest of <strong>the</strong> world via system calls.<br />

A process on a UNIX system is created by <strong>the</strong>" fork" system call. Every process except process 0 is created by<br />

"fork". Process 0 is <strong>the</strong> swapper, process 1, known as init is <strong>the</strong> parent of all o<strong>the</strong>r processes.<br />

Executable File contents:<br />

- set of headers that describe <strong>the</strong> attributes of <strong>the</strong> file<br />

- <strong>the</strong> program text<br />

- machine language representation of data initial values when much memory space for uninitialized data (bss =<br />

block started<br />

- o<strong>the</strong>r sections, such as a symbol table<br />

The kernel loads an executable file into memory during an "exec" system call. The three regions are: text, data and<br />

stack.<br />

The stack region is automatically created and its size is dynamically adjusted by t_ kernel at run time.<br />

#include /* program to copy a file */<br />

char buffer[2048];<br />

int version = 1;<br />

main(int argc, char *argv[])<br />

{<br />

int fdold, fdnew;<br />

if (argc != 3)<br />

{<br />

printf("need 2 arguments for copy program\n");<br />

exit(1);<br />

}<br />

fdold = open(argv[l], O_RDONLY); /* open source file */<br />

if (fdold == -1)<br />

{<br />

printf("can't open file %s\n", argv[l]);<br />

exit(1);<br />

}<br />

fdnew = creat(argv[2], 0666); /* create target file */<br />

if (fdnew == -1)<br />

83

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!