Operating System: Linux processes and threads

Here is part of the content, but also to make changes.

One: the purpose and content

Learning fork (), exec, using the pthread library functions, read the source code, analyze the mechanism of fork, exec, pthread_create function

Code:

A process create a child process B

Parent child process B and A respectively correspond to different executable: A print Hello world, B accumulated sum implemented

B has three process threads, the main thread creates a new thread to achieve a cumulative sum (from 1 to the parameter x), the thread 2 monitors the input parameter x

If the input x is a non-negative integer, a thread counted; if the thread 1 are calculated previous program, the input thread and 2 non-negative integer and different from the last thread 1 should be recalculated; if the same is not recomputed

If you enter the letters p, then 1 thread suspended.

Enter the letter e 1 print after exiting the thread information of the entire process of A and B exit

If you enter other characters, the thread 1 to print an input error, the task continues

Basic information processor usage, memory usage, etc. Analysis of each of the executable run

Two step

reference

Linux C Programming --fork () Detailed - Zhang Qin, a - CSDN blog 

[Concurrent parallel] _ [pthread] _ [of worker threads simple control - pause - continued - Stop] - calm - CSDN blog

Linux multitasking programming (c) --- exec family of functions and basic experiments - Wang Zhuang's blog - CSDN blog

Linux multi-threaded programming and pass multiple parameters instances - blankqdb - blog Park

Pausing and resuming discussions about pthread thread - a small step for one day - CSDN blog

The subject of the request requires two execution found A and B. The modular design principle of the first A, then B. Design

1. moderation, according to the questions asked, to write the code for the process A

#include provided fork (), execlp () function

#include <sys / wait.h> provided wait () function

#include provide a definition of the type of pid_t

Code is relatively simple, mainly to create a child process, the child process replaces the current process image with a new process image.


 

2. Write the child process B after the code

Take full advantage of the modular design approach.

First, the header file

#include <pthread.h> provide pthread library functions

The note, plus -lpthread gcc compile time, because Linux is not the default library pthread


 

After a few global variables, reduce trouble passing a parameter between threads.


 

1. The main thread

Function is to create two sub-thread, the thread 2 monitoring inputs, thread 1 print information and calculate cumulative.

When the thread exits are 1,2 emperor thread exits.

Use pthread_create create child threads.

Use pthread_join waiting thread end


 

2. Thread 2

Because Thread 2 To monitor input, so the first to write thread 2.

Thread 2 Part I:

The main processing input.

For details, see pictures and comments of the code.

Processing the input string used here c ++, because there is no limit input type


 

Thread 2 Part II:

Func determined according to the input value. func adjustment state of the thread 1 as a global variable

The recent cal and simultaneously determines whether to recalculate. target is the thread 1 from the beginning of the accumulation destination.


 

Thread 2 Part 3:

1 func adjusted according to the operating state the second portion of the thread obtained.

func == 2, the use of pthread_join () waits for Thread 1 to print an error message, then exit.

pthread_suspend () pthread_resume () function is a function of their own definition, to achieve a thread suspended. Suspend implementation of detailed explanation, see the subsequent part of the experiment report.


 

3. Thread 1

The first part: to pause thread 1.

Use

pthread_mutex_lock (), pthread_mutexunlock (), pthread_cond_wait (), pthread_cond_broadcast () to pause the thread.

Source embodied suspend implementation process more clearly.


 

 

 

Part II: thread 1 accumulates the operation information output

p sleep in order to reflect the operation, two identical inputs, different operation settings. Otherwise, the speed may not as good as people enter the computer calculates the speed, leading to the second input has not arrived yet, we come to the last result.

 


 

In summary, the process of the following structure B

void pthread_suspend(void)

void pthread_resume(coid)

void thread1(void* arg)

void thread2(void*arg)

int main ()

three. Basic information processor usage, memory usage, etc. Analysis of each of the executable run

First of all processes running in Terminal A.


 

pgrep 是通过程序的名字来查询进程的工具,一般是用来判断程序是否正在运行。在服务器的配置和管理中,这个工具常被应用,简单明了;

然后打开另一个终端,pgrep -l A查看进程名和pid


 

pstree -p 26638。以树状图显示进程,并显示进程pid。可见父子进程和线程的关系。


 

ps -T -p 26639 可以查看进程B的线程信息。


 

使用top查看进程cpu和内存信息。

下图为示例:


 

top命令的第三行,cpu状态:

依次对应:

us:user 用户空间占用cpu的百分比

sy:system 内核空间占用cpu的百分比

ni:niced 改变过优先级的进程占用cpu的百分比

id:空闲cpu百分比

wa:IO wait IO等待占用cpu的百分比

hi:Hardware IRQ 硬中断 占用cpu的百分比

si:software 软中断 占用cpu的百分比

st:被hypervisor偷去的时间

top命令第四行,内存状态:

total,free ,used ,buff/cache

依次对应:物理内存总量,空闲内存总量,使用中的内存总量,缓冲内存量

 

进入top后,交互时,输入s,系统提示更改刷新间隔。输入0则不断刷新。


 

top -p 26638查看pid为26638的进程(即A)的信息


 

top -p - H 26639

top命令可以实时显示各个线程情况。要在top输出中开启线程查看,请调用top命令的“-H”选项,该选项会列出所有Linux线程。在top运行时,你也可以通过按“H”键将线程查看模式切换为开或关。


 

四 遇到的问题及解决:

1.编译时出现c++11标准库未定义错误,因为编译器使用的库版本不同。


 

 

解决:使用-l链接 stdc++


 

2. gcc编译时报错


 

解决:

在32位模式时,int 和指针类型变量都占32位在64位模式下,int占32位,指针变量占64位


 

3.如图


 

又是-lstdc++解决

五.实验结果记录:

执行程序A。程序A 父进程fork 出子进程,之后打印Hello, world!然后执行wait(),等待子进程结束。

子进程调用execlp,执行另一个执行体B。

执行体B中线程2开始监控输入。

输入15,是非负整数,之后线程1计算1累加到15.结果为120。120后跟着的15表明计算的是1-15累加。

输入16,是非负整数,之后线程1计算1累加到16.结果为136。136后跟着的16表明计算的是1-16累加。

输入18,之后输入20.线程2监控到由于20与18不同,故线程1不再进行1-18累加,而进行1-20累加。

输出210.

输入25,再输入25.线程2监控到25相同,不需要重新计算,打印信息“输入相同,不需要重新计算”。线程1计算第一个25,输出325.

输入afd。线程2监控,属于其他字符,线程1打印错误信息“input wrong”。

输入fg。线程2监控,属于其他字符,线程1打印错误信息“input wrong”。

输入p,线程1暂停执行。打印信息“pause”表明线程1已暂停。

输入123.线程2检测到是非负整数,先调用恢复函数解除线程1的暂停。之后打印信息“resume”表示线程1已经恢复。之后线程1执行1-123累加,输出7626.

输入e。线程2检测到要退出。之后线程1打印信息“A and B exited”表示主进程,子进程都要退出。之后主进程和子进程都退出。

结束。

Guess you like

Origin www.cnblogs.com/lqerio/p/11117626.html