免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 991 | 回复: 0
打印 上一主题 下一主题

Beginning Linux Programming (1) (process) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-20 11:57 |只看该作者 |倒序浏览
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);
}

  





本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/94518/showart_1933536.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP