SDP 课下总结

Process:

A sequence of operations performed by a program in execution on a given set of input data

Process identifier: PID unique

  • Non negative integer

  • Although a PID is unique,UNIX reuses the number of terminate processes

  • No mater in what kinds of computer systems,after the processed terminate all the system should release the resources which is occupied by the processes.Otherwise the resources of the system will be running out.

#include<unistd.h>

pid_t getpid(); //process id

Pid_t getppid(); //parent process id

Uid_t getuid(); //get the real user id

Gid_t geteuid(); //get the effective id

Process creation:

System call fork() create a new child process

  • The child is a copy of the parent excluding the Process ID return by fork

The parent process receives the child PID.A process may have more than one child that can identify on the basis of its PID

The child process receives the value of 0. It can identify its parent by means of the system call getppid()

  • Fork is issued once in the parent process,but returns in two different processes,and returns different values to the parent ,and to the child.

#include <unistd.h>

...

Pid_t pid;

...

Pid=fork();

Switch(pid){

Case -1: //fork failure

...

Exit(-1);

Case 0: //child Zero in the child process

...

Default: //parent Child PID in the parent process

...

}

  • Write a concurrent program that, given has its argument, generates n children processes Each child process outputs its PID and terminates

System call wait() and waitpid()

When a process terminates normally or not

  • The kernal sends a signal (SIGCHLD)to it parent

  • For the parent this is an asynchronous event

  • The parent process may

Manage the child termination and or the signal

Ignore the default event

#include <sys/wait.h>

*Pid_t wait(int statLoc);

  • Blocks the calling process if all its children are running(none is already terminated)

Wait will return as soon as one of its children terminates

  • Returns an error if the calling process has not children

#include<sys/wait.h>

Pid_t waitpid(

Pid_t pid;

Int statLoc;*

Int option);

If a paremt want to wait a specific child it is better to use waitpid,which suspends execution of the calling process until a child,specified by pid argument,has changed state.By default,waitpid() waits only for terminated children.

System call exec()

System call exec substitutes the process code with the executable code of another program

The new program begins its execution as usual from main

Exec:

  • Does not creat new process

  • Substitutes the calling process image with the image of another program

  • The process PID does not change

Fork->duplicates an existent process

Exec->executes a new program

  • 6 versions of exec system call

int execl(const char *path, const char *arg, ...);

int execlp(const char *file, const char *arg, ...);

int execle(const char *path, const char *arg, ..., char * const envp[]);

int execv(const char *path, char *const argv[]);

int execvp(const char *file, char *const argv[]);

int execvpe(const char *file, char *const argv[],char *const envp[]);

参数

L arguments are a list of strings

V arguments is a vector of strings char **arguments

path参数表示你要启动程序的名称包括路径名

arg参数表示启动程序所带的参数

返回值:成功返回0,失败返回-1

image.png

System call system()

#include<stdlib.h>

*Int system(const char string)

System cal system

  • Forks a shell,which execute the string command,while the [parent process waits the termination of the shell command

  • Returns -1 or 127 on error

The exit value of the shell that executed the command (with the format of waitpid)

Reference:
Linux下进程终止过程
http://blog.csdn.net/tuzhutuzhu/article/details/30029877
进程与线程的一个简单解释
http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html
Linux系统编程:fork函数的使用【循环创建N个子线程】
http://blog.csdn.net/qq_29542611/article/details/79513845
创建N个子进程
http://blog.csdn.net/sum_tw/article/details/53671227
linux中fork()函数详解(原创!!实例讲解)
http://blog.csdn.net/jason314/article/details/5640969
linux下的僵尸进程处理SIGCHLD信号
http://www.cnblogs.com/wuchanming/p/4020463.html
linux系统编程之进程(三):exec系列函数和system函数
https://blog.csdn.net/jnu_simba/article/details/8930400

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天上班了,感觉好没有意思的说。无休的劳作,我会感到厌烦。虽然我干活的时间并不多,但还是会有厌烦。今天是我姐的生日...
    旅人未安阅读 210评论 0 0
  • 从西藏回来一直有朋友在问我:你怎么敢一个人去西藏?你怎么有钱去西藏?我想去西藏但是不敢,我该怎么办?我该注意哪些,...
    op_do阅读 376评论 1 1
  • 1.教练技术的BIA反馈 2.表扬一定要及时、具体、明确 3.当孩子进步时,一定要及时表扬 如孩子从害怕到勇敢面对...
    喜乐妈妈阅读 226评论 0 0
  • 1. 本来是打算在来工场一年的时候写的,只是因为很多琐事儿所以搁浅。比如每天都有好多人问你要礼包,每天要和很多人聊...
    62dc958fa705阅读 411评论 0 3