Linux System Programming (1)

1. procedures and processes, parallelism and concurrency

Concurrency: to refer to a period of time there are several programs have been started in the run to the finish between running, and these programs are running on the same processor, but any one point in time only one program running on the processor.

Parallel: a plurality of processors simultaneously processed.

The picture shows the three processes in turn is cpu processing, processing each time a fragment of time.

2. PCB (process control block)

We know that each process in the kernel has a process control block (PCB) to maintain process-related information, Linux kernel process control block is task_struct structure.

/usr/src/linux-headers-3.16.0-30/include/linux/sched.h file can be viewed struct task_struct structure is defined. Whose members have a lot of, we can focus on mastering the following sections:

* Process id. System, each process has a unique id, type in C language with pid_t said, in fact, a non-negative integer.

* State of the process, there is ready to run, suspend, stop and other states.

* The need to preserve and restore some CPU registers when the process of switching.

* Description of the virtual address space.

* Description of the control terminal.

* Current working directory (Current Working Directory).

* Umask mask.

* File descriptor table, a pointer to the file structure contains many body.

* Signal and related information.

* User id and group id.

* Session (Session) and process group. (The parent process, the child process and brothers in the same process group, process group consisting of a multiple session)

* Resource Capping (Resource Limit) process can be used.

3. The process of switching state

4. fork function

Example:

5. The process of analysis-related issues

Example:

Results of the:

Repeatedly executed, sometimes you get the following results:

This is because:

6. loop to create multiple sub-processes

If you are creating multiple sub-processes following this way, it will create more unwanted processes:

So to determine the return value of the function fork, when the return value is 0, indicating that the current process is a child, would not have created the process, so break.

When i equals 0 to 4, the current process is a child process, when i is equal to 5, the current process parent process.

7. The process of data sharing between

8. exec family of functions Application

9. exec函数族函数的使用

(1)使用execl函数,让子进程执行ls命令。

执行结果:

可以看到:++++++++++ i 这里的for循环只输出了一次,这是父进程输出的,子进程并没有输出。这是由于execl函数执行后,子进程的代码段就被替换为ls的代码了,所以执行ls输出后,子进程就结束了。

(2)使用execl函数,让子进程执行自己写的程序

执行结果:

(3)使用execlp函数,让子进程执行PATH环境变量能够搜索到的程序

(4)exec函数是有返回值的,但是没有用。因为如果execlp函数执行成功,那么子进程的代码段会被替换,所以后面的perror函数执行不到。而如果execlp函数执行失败,那么子进程的代码段没有被替换,那么后面的perror函数会输出错误信息,然后exit函数退出当前进程。代码如下:

10. 孤儿进程和僵尸进程

(1)孤儿进程例子:

执行结果:

可以看到子进程被1492号进程(ubuntu下的init进程)领养了。

(2)僵尸进程例子:

该例子中父进程一直进行while循环,没有回收子进程,所以此时子进程成为僵尸进程。ps aux一下也可以看到:

40763号进程为父进程,还在进行while循环,40764号为僵尸进程。

如果想让僵尸进程消失,用kill直接杀它是不行的,因为它已经死了。我们可以将该僵尸进程的父进程用kill命令杀死,那么僵尸进程就会被init进程领养,然后init进程会释放僵尸进程的pcb,然后就会看到父进程和僵尸进程都不存在了。

11. wait函数回收子进程资源

例1:父进程等待子进程正常退出后,回收子进程。

执行结果:

例2:父进程等待子进程正常退出后,回收子进程,并打印子进程的退出状态。

    

执行结果:

例3:父进程等待子进程被杀死后,回收子进程,并打印杀死子进程的信号的编号。

    

 

执行结果:

 

Guess you like

Origin blog.csdn.net/mengyujia1234/article/details/90763360