Chinaunix首页 | 论坛 | 博客
  • 博客访问: 47864
  • 博文数量: 25
  • 博客积分: 1415
  • 博客等级: 上尉
  • 技术积分: 270
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-13 05:50
文章分类

全部博文(25)

文章存档

2011年(5)

2010年(1)

2009年(19)

我的朋友

分类: LINUX

2009-05-20 11:57:05

1.a special set of "files" in directory called /proc which allows you to "look inside" processes while they are running as if they are files in directories.
 
2.In Linux, Processes with high priority get to run more often, while ithers, such as low-priority background tasks, run less frequently.(preemptive, and suspend or resume without cooperation).(windows 3.1要明确指出悬挂和恢复进程)
 
LINUX 总的调度策略:
the Linux scheduler decides which process it will allow to run on the basis of priority. Exact implementation vary, of course , but higher priority processes run more often. In some cases, low priority processes don't run at all if higher processes are ready to run . P451
 
c语言中:
system("ps -ax"); 执行命令
 
Uisng "systen" is a far from ideal way to start other process, because it invokes the disired program using a shell. This is both efficient because a shell is started before the program is started, and also dependent on the installation for the shell and environment that are used.
 
Repalcing a Process Image
 
#include
 
char ** environ ;
-------------------------
execl( . . . );
execlp( . . . ) ;
execle( . . . ) ;
-------------------------
execv
execvp
execve
------------------------
 
the global variable environment is available to pass a value for the new environment.
 
the new process started by exec inherits many features from the original. In particular, open file decriptor remains open in te new process unless their "close on flag" has been set(refer the system function call fcntl). Any open directory stream in the original process are closed.
 
--------------------------
duplicating a process image (fork system call)
 
#include
#include
 
pid_t fork(void) ;
 
 
initial process ---> fork() --- return a new PID---> original process continue   
                       |
                       |_ _ _ _ return zero -------> new process
 
 
a typical code fragment using fork is
pid_t new_pid;
new_pid = fork() ;
 
switch(new_pid)
case -1 : /*Error*/
break ;
case 0:   /*Child process*/
break ;
default : /*parent process*/
break ;
 
 
waiting for a Process
#include 
#include  
 
pid_t wait(int *stat_loc) ;
 
arrange one process to wait until one of its child processes is stopped.
Tbe call return the child process.
 
stat_loc records the status imformation of child process. Theses include:
 
Macro                          Definition
WIFEXITED(stat_val)     Non zero if the child is terminated normally.
WEXITSTATUS(stat_val)   If WIFEXITED is non    zero, this returns child exit code.
WIFSIGNALED(stat_val)   Non zero if the child is terminated on an uncaught signal.
WTERMSIG(stat_val)      If WIFSIGNALED is non zero, this returns a signal number.
WIFSTOPPED(stat_val)    Non zero if the child has stopped.
WSTOPSIG(stat_val)      If WIFSTOPPED is non zero, this returns a signal number.
 
/*  This section of the program waits for the child process to finish.  */
    if(pid) {
        int stat_val;
        pid_t child_pid;
        child_pid = wait(&stat_val);
        printf("Child has finished: PID = %d\n", child_pid);
        if(WIFEXITED(stat_val))
            printf("Child exited with code %d\n", WEXITSTATUS(stat_val));
        else
            printf("Child terminated abnormally\n");
    }
    exit (exit_code);
 
--------------------------------------------------------
★Zombie process
F S   UID PID  PPID  C PRI  NI ADDR    SZ WCHAN  TTY          TIME CMD
0 S   0  5892  4088  0  77   0    -   336 schedu pts/1    00:00:00 fork2
1 Z   0  5893  5892  0  80   0    -     0 do_exi pts/1    00:00:00 fork2
0 R   0  5895  3327  0  75   0    -   783 -      pts/0    00:00:00 ps
if parent process terminates abnormally, the child process automatically gets the process with PID 1(init) process as its parent. The process is now a zombie that is no longer running but has been inherited by init process.
 
There is another call you can use to wait for process, it's called waitpid and you can use it to wait for a specific process to terminate.
 
#include
#include
 
pid_t waitpid (pid_t pid, int *stat_loc , int options );
 
pid: specifies the PID if a particular process  to wait for .
options : allows us to modify the behavior of waitpid.
最有用的选项值是 WNOHANG, which prevents the call to waitpid from suspending execution of the caller. 使用它可以找出是否所有子进程都终止。
waitpid(child_pid, (int *) 0 , WNOHANG) ;
return 0 ---> child has not terminated
      - 1 ---> set error (no child process)
 
----------------------------------------------------------
★I/O redirection
 
/*upper.c*/
#include
#include
int main()
{
    int ch;
    while((ch = getchar()) != EOF) {
        putchar(toupper(ch));
    }
    exit(0);
}
/*useupper.c*/
/*  This code, useupper.c, accepts a file name as an argument
    and will respond with an error if called incorrectly.  */
#include
#include
int main(int argc, char *argv[])
{
    char *filename;
    if(argc != 2) {
        fprintf(stderr, "usage: useupper file\n");
        exit(1);
    }
    filename = argv[1];
/*  That done, we reopen the standard input, again checking for any errors as we do so,
    and then use execl to call upper.  */
    if(!freopen(filename, "r", stdin)) {
        fprintf(stderr, "could not redirect stdin to file %s\n", filename);
        exit(2);
    }
    execl("./upper", "upper", 0);
/*  Don't forget that execl replaces the current process;
    provided there is no error, the remaining lines are not executed.  */
    fprintf(stderr, "could not exec upper!\n");
    exit(3);
}
 
 
 
 
 
阅读(576) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~