UNIX System Overview

UNIX Architecture

  • a kernel is a software that contrals the hardware resources of the computer and provides an environment under which programs can run.
  • system calls:a layer of software,is the interface to the kernel.libraries of common functions are built on top of the system call.
  • the architecture:
    这里写图片描述
  • shell:is a special application that provides an interface for running other applications
  • two filenames are automatically created whenever a new directory is created: . and .. ,in thr root, the dot-dot is the same as dot
  • a code that shows a bare-bones impplementation of the ls command:
#include  "apue.h"
#include <dirent.h>
#include "myerr.h"

int main(int argc,char *argv[])
{

    DIR *dp;
    struct dirent *dirp;
    for(int i=0;i<argc;i++)
    {
        printf("%d-%s\n",i,argv[i]);
    }
    if(argc != 2)
    {
        err_quit("usage:ls directory_name");
    }
    if((dp = opendir(argv[1])) == NULL)
    {
        err_sys("can't open %s",argv[1]);
    }
    while((dirp = readdir(dp)) != NULL)
    {
        printf("%s\n",dirp->d_name);
    }

    closedir(dp);
    exit(0);
}
  • a code that copy standard input to standard output
#include "apue.h"
#include "myerr.h"
#define BUFFSIZE 4096
int main() {
    int n;
    char buf[BUFFSIZE];

    while((n = read(STDIN_FILENO,buf,BUFFSIZE)) > 0)
    {
        if(write(STDOUT_FILENO,buf,n) != n)
        {
            err_sys("write err");
        }
    }
    if(n < 0)
    {
        err_sys("read err");
    }
    exit(0);
}
  • copy standard input to output,using standard I/O
#include "apue.h"
#include "myerr.h"

int main() {
    //use standard IO
    int c;
    while((c = getc(stdin)) != EOF)
    {
        if(putc(c,stdout) == EOF)
        {
            err_sys("putc err");
        }
    }
    if(ferror(stdin))
    {
        err_sys("getc err");
    }
    exit(0);
}
  • a code that reads the standard input and execute it
#include "apue.h"
#include <sys/wait.h>
#include "myerr.h"
int main() {
    char buf[MAXLINE];
    pid_t pid;
    int status;

    //print prompt
    printf("%%");
    while(fgets(buf,MAXLINE,stdin) != NULL)
    {
        if(buf[strlen(buf)-1] == '\n')
        {
            buf[strlen(buf)-1] = 0;
        }

        if((pid = fork()) < 0)
        {
            err_sys("fork error");
        }
        else if(pid == 0)
        {
            execlp(buf,buf,(char*)0);
            err_ret("couldn't execute:%s",buf);
            exit(127);
        }

        if((pid = waitpid(pid,&status,0)) < 0)
        {
            err_sys("waitpid error");
        }
        printf("%%");
    }
    exit(0);
}

thread

  • all threads within a process share the same address space,file descriptors,stacks,and process-related attributes.Each thread executes on its own stack,although any thread can access the stacks of other threads in the same process.
  • thread IDs are local to a process

error handling

  • when a error occurs,a negtive value is often returned
  • some functions return a pointer to an object or return a null pointer to indicate an error.

user Identification

user id

  • the user id from our entry in the password is a number that identifies us to the system,we can’t change it and it is unique for every user
  • root/superuser’s user id is 0.

group id

  • the group id is assigned by the system administrator when our login name is assigned
  • the groups are used to collcet users together,allows the sharing of resources(files…),
  • the group file is usually /etc/group.
  • user id and group id usually used 2 bytes.
  • there has reflect between the login name and the numeric id.
  • a demo code
#include "apue.h"

int main()
{
    printf("uid=%d,gid=%d",getuid(),getgid());
    exit(0);
}

supplementary group ids

  • the group id specified in the password file for a login name,we alse need a user to belong to other groups
  • the 4.2BSD allows a user to belong to up to 16 additional groups,these supplementary group ids are stored at /etc/group

signals

  • signals are a technique used to notify a process that some condition has occurred
  • the process has three choices for dealing with the signal
    – Ignore the signal.
    – let the default action occur.
    – provide a function that is called when the signal occurs(called catching the signal)

time values

  • unix systems have maintained two different time values:
    – calendar time.this value counts the number of secondssince the Epoch(00:00:00 1970/1/1),used to record the time when a file was last modified.
    – Process time.is also called CPU time and measures the central processor resources used by a process
  • when we measure the execution time of a process,we use three values:
    – clock time,the amount of time the process takes to run.
    – user cpu time,the cpu time attributed to user instructions.
    – system cpu time,the cpu time attributed to kernel.
  • cpu time = system cpu time + user cpu time

system calls and library functions

-for unix system,each system call has a function of the same name in the standard C library.the user process calls this function,this function then invokes the kernel service.example,the printf() will invoke the write system call to output a string,but the strcpy or the atoi function don’t involve the kernel at all.
- a picture demo:
这里写图片描述
这里写图片描述

summary

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值