370 likes | 756 Views
Introduction to Unix Operating System. Introduction: Unix System. Operating System: a system that manages the resources of a computer. Resources: CPUs, Memory, I/O devices, Network Kernel: the memory resident portion of Unix system File system Process Control System.
E N D
Introduction: Unix System • Operating System: a system that manages the resources of a computer. • Resources: CPUs, Memory, I/O devices, Network • Kernel: the memory resident portion of Unix system • File system • Process Control System
System Calls • System calls instruct the kernel to do various operations for the calling program (such as ls, who, date etc) and exchange data between kernel and the program • The set of system calls and the internal algorithms that implement them form the body of the kernel and the study of Unix OS.
Two modes of Process Execution • User Mode • Processes in user mode can access its own instructions and data but not kernel instruction and data. • E.g. if user tries to execute privileged machine instruction, it results in an error • Kernel Mode • Processes in kernel mode can access kernel instruction as well as user instruction.
File sub-system • System calls : creat(), open(), read(), write() and close(). • Process control subsystem • System calls: fork(), exec(), wait(), exit()
struct student { int idno; char name[10]; float marks; }s[10]; • int main(intargc,char *argv[]) • { • inti=0; • FILE *fin,*fout; • fin = fopen(argv[1],"r"); • if(fin == NULL) • { • printf("Can not open file!...\n"); • exit(0); • } • fout = fopen(argv[2],"w"); • if(fout == NULL) • { • printf("Can not open file!...\n"); • exit(0); • } • while(!feof(fin)) • { • fscanf(fin,"%d %s %f",&s[i].idno,s[i].name,&s[i].marks); • printf("%d %s %f\n",s[i].idno,s[i].name,s[i].marks); • fprintf(fout,"%d %s %f",s[i].idno,s[i].name,s[i].marks); • } • fclose(fin); • fclose(fout); • }
char buffer[2048]; copy(int old, int new) { int count; while((count = read(old, buffer, sizeof(buffer))) > 0) write(new, buffer, count); } main(intargc, char *argv[]) { intfdold, fdnew; if(argc != 3) { printf("need 2 arguments for copy program \n"); exit(1); } if((fdold = open(argv[1], O_RDONLY)) == -1) { printf("cannot open file %s \n", argv[1]); exit(1); } if((fdnew = creat(argv[2], 0666)) == -1) { printf("cannot create file %s \n", argv[2]); exit(1); } copy(fdold, fdnew); exit(0); }
File Subsystem • A file system is a collection of files and directories on • a disk or tape in standard UNIX file system format. • Inode: when a file is newly created an unused inode • assigned to that file. it contains info for that file. • Each UNIX file system contains four major parts: • A. boot block: • B. superblock: • C. i-node table: • D. data block: file storage
Boot Block • A boot block may contains several physical blocks. • Note that a physical block contains 512 bytes • (or 1K or 2KB) • A boot block contains a short loader program for • booting
Superblock • Superblock contains key information about a file system • Superblock information: • A. Size of a file system and status: • label: name of this file system • size: the number of logic blocks • date: the last modification date of super block. • B. information of i-nodes • the number of i-nodes • the number of free i-nodes • C. information of data block: free data blocks. • The information of a superblock is loaded into memory.
I-node structure • Type: file, directory, pipe, symbolic link • Access: read/write/execute (owner, group,) • owner: who own this I-node (file, directory, ...) • timestamp: creation, modification, access time • size: the number of bytes • block count: the number of data blocks • direct blocks: pointers to the data
Data Block • A data block has 512 bytes. • Some FS has 1K or 2k bytes per blocks. • A data block may contains data of files or data of • a directory.
In-core inode table • UNIX system keeps regular files and directories on block • devices such as disk or tape, • Such disk space are called physical device address space. • The kernel deals on a logical level with file system • (logical device address space) rather than with disks. • Disk driver can transfer logical addresses into physical • device addresses. • In-core (memory resident) inode table stores the • inode information in kernel space.
In-core inode table • An in-core inode contains • all the information of inode in disks. • status of in-core inode • inode is locked, • inode data changed • file data changed. • the logic device number of the file system. • inode number • reference count
File table • The kernel have a global data structure, called file table, • to store information of file access. • Each entry in file table contains: • A. a pointer to in-core inode table • B. the offset of next read or write in the file • C. access rights (r/w) allowed to the opening process. • D. reference count.
User File Descriptor table • Each process has a user file descriptor table to identify • all opened files. • An entry in user file descriptor table points to an entry • of kernel’s global file table. • Entry 0: standard input • Entry 1: standard output • Entry 2: error output
System Call: open • open: A process may open a existing file to read or write • syntax: • fd = open(pathname, mode); • A. pathname is the filename to be opened • B. mode: read/write • Example
$ cc openEx1.c -o openEx1 $ openEx1 Before open ... fd1=3 fd2=4 fd3=5 $ main() { int fd1, fd2, fd3; printf("Before open ...\n"); fd1 = open("/etc/passwd", O_RDONLY); fd2 = open("./openEx1.c", O_WRONLY); fd3 = open("/etc/passwd", O_RDONLY); printf("fd1=%d fd2=%d fd3=%d \n", fd1, fd2, fd3); }
User file descriptor table in-core inodes file table U area 0 ... … 1 Pointer to Descriptor table 2 CNT=1 R CNT=2 /etc/passwd 3 ... 4 ... CNT=1 W 5 ... 6 CNT=1 ./openEx2.c 7 . . . CNT=1 R ... ...
System Call: read • read: A process may read an opened file • syntax: • fd = read(fd, buffer, count); • A. fd: file descriptor • B. buffer: data to be stored in • C. count: the number (count) of byte • Example
$ cc openEx2.c -o openEx2 $ openEx2 ======= fd1=3 buf1=root:x:0:1:Super-Us fd1=3 buf2=er:/:/sbin/sh daemo ======= $ main() { int fd1, fd2, fd3; char buf1[20], buf2[20]; buf1[19]='\0'; buf2[19]='\0'; printf("=======\n"); fd1 = open("/etc/passwd", O_RDONLY); read(fd1, buf1, 19); printf("fd1=%d buf1=%s \n",fd1, buf1); read(fd1, buf2, 19); printf("fd1=%d buf2=%s \n",fd1, buf2); printf("=======\n"); }
#include <stdio.h> #include <sys/types.h> #include <fcntl.h> main() { int fd1, fd2, fd3; char buf1[20], buf2[20]; buf1[19]='\0'; buf2[19]='\0'; printf("======\n"); fd1 = open("/etc/passwd", O_RDONLY); fd2 = open("/etc/passwd", O_RDONLY); read(fd1, buf1, 19); printf("fd1=%d buf1=%s \n",fd1, buf1); read(fd2, buf2, 19); printf("fd2=%d buf2=%s \n",fd2, buf2); printf("======\n"); } $ cc openEx3.c -o openEx3 $ openEx3 ====== fd1=3 buf1=root:x:0:1:Super-Us fd2=4 buf2=root:x:0:1:Super-Us ====== $
User file descriptor table in-core inodes file table U area 0 ... … 1 Descriptor table 2 CNT=1 R CNT=2 /etc/passwd 3 ... 4 ... ... 5 ... 6 ... 7 . . . CNT=1 R ... ...
main() { int fd1, fd2, fd3; char buf1[20], buf2[20]; buf1[19]='\0'; buf2[19]='\0'; printf("======\n"); fd1 = open("/etc/passwd", O_RDONLY); fd2 = dup(fd1); read(fd1, buf1, 19); printf("fd1=%d buf1=%s \n",fd1, buf1); read(fd2, buf2, 19); printf("fd2=%d buf2=%s \n",fd2, buf2); printf("======\n"); char buf1[20], buf2[20]; } $ cc openEx4.c -o openEx4 $ openEx4 ====== fd1=3 buf1=root:x:0:1:Super-Us fd2=4 buf2=er:/:/sbin/sh daemo ====== $
User file descriptor table in-core inodes file table U area 0 ... … 1 Descriptor table 2 CNT=2 R CNT=1 /etc/passwd 3 ... 4 ... ... 5 ... 6 ... 7 . . . ... ... ...
System Call: creat • creat: A process may create a new file by creat system • call • syntax: • fd = creat(pathname, access permissions); • A. pathname: file name • B. e.g. 0666
System Call: close • close: A process may close a file by close system • call • syntax: • close(fd); • A. fd: file descriptor • Example
System Call: write • write: A process may write data to an opened file • syntax: • fd = write(fd, buffer, count); • A. fd: file descriptor • B. buffer: data to be stored in • C. count: the number (count) of byte • Example