Unix Processes(1)

来源:互联网 发布:win10无法连接windows 编辑:程序博客网 时间:2024/05/22 04:38

  1.All processes are provided with a set of memoru addresses, called a virtual address space;

  2.A process's prcess control block(PCB) is maintained by the kernel in a protected region of memory taht user processes cannot access

  3.A Unix PCB stores: the contents of the processeor registers, Process ID(pid), the program counter and the system stack,all processes are listed in the process table.

  4.All processes interact woth OS via system calls. To spawn a child process ,using the <a>fork</a> system, which creates a copy of the parent process (child receives a copy of the parent's resources as well)

  5.Process priorities are integers b/t -20 and 19(inclusive), a lower numerical priority value indicates a higher scheduling priority

  6.Unix provides inter-process communication(IPC) mechanisms ,such as pipes ,to allow unrelated process transger data


 System calls


7. All processes run in separate address spaces;e.g. process A and B has same address 520,when B rites to address 520, the

   contents of 520 for A is not changed.

e.g.--create two child processes

/* This program creates two new children processes. */#include <sys/types.h>       /* file of data types needed for many compilers */#include <unistd.h>          /* needed for fork, getpid procedures */#include <stdio.h>#include <signal.h>void procont(){   printf("Process 1 paused. It will resume soon.\n");   }int main (void){      pid_t pid1;                /* variable to record process id of child 1 */    pid_t pid2;                /* variable to record process id of child 2 */    pid_t pid;    /* fork system call creates a new process by duplicating the parent      process's address space stack and open file descriptors */    pid1 = fork();               if (pid1 == -1)           /* check for error in spawning child process */    { perror ("error in fork");  exit (1);    }        if (pid1 == 0)             /* check if this is the new child process */    { /* processing for child 1 */        signal(SIGUSR1,procont);        pause();        sleep(2);printf ("This output comes from the 1st child process\n");    }     else     {     pid2 = fork();        /* create child 2 */if (pid2 == -1)       /* check for error in spawning child process */{     perror ("error in fork");      exit (1);}if (pid2 == 0)  {        /* check if this is the new child process */              sleep(2);    printf ("This output comes from the 2nd child process\n");            kill(pid1,SIGUSR1);}else {    /* processing for parent */             int s;       pid = wait(&s);       pid = wait(&s);       printf ("This output comes from the parent process.\n");    printf ("Parent report:  my pid = %d   child1's pid = %d, child2's pid = %d\n", getpid(), pid1, pid2);     }    }        exit (0);                 /* quit by reporting no error */}


wait(stat_loc) : allows the parent process to ait for its children to terminate;returns the pid of the child rpocess that is being traced; stores status info. about the child at the location pointed to by the stat_loc param.

 



原创粉丝点击