银行家算法实现教程与bank.cpp源码解析

版权申诉
0 下载量 29 浏览量 更新于2024-11-11 收藏 2KB RAR 举报
资源摘要信息:"银行家算法是一种避免死锁的著名算法,用于多进程资源分配系统。它通过预先分配资源给进程,并保证系统始终处于安全状态,来确保不会发生死锁。银行家算法的核心思想是模拟银行家放贷的过程,即在分配资源之前预先评估系统是否能进入一个安全状态,如果可以,则分配资源;否则,拒绝请求,保持系统当前状态。" 银行家算法的概念最早由艾兹-格兰奇(E. W. Dijkstra)提出,目的是防止操作系统中因资源分配不当而导致的死锁现象。它要求操作系统在分配资源之前,必须先检查此次分配后系统是否仍然处于安全状态。安全状态是指系统能够按照某种进程顺序(安全序列),为每个进程分配其所需资源,直到最大需求,使每个进程都可以顺利完成,而不会陷入等待状态。 银行家算法主要涉及以下几个关键概念: 1. 可用资源向量(Available):表示系统中当前每种类型资源的可用数量。 2. 最大需求矩阵(Max):表示每个进程在运行过程中最多需要的每种类型资源的数量。 3. 分配矩阵(Allocation):表示系统已分配给每个进程的每种类型资源的数量。 4. 需求矩阵(Need):表示每个进程当前还需要的每种类型资源的数量,计算方式为 Max - Allocation。 算法的执行过程通常如下: 1. 当进程请求资源时,系统首先计算该进程如果获得所需资源后,它还需要的最大资源数(Need = Max - Allocation)。 2. 系统检查此次分配后是否超过可用资源(Available >= Need),如果超出,则拒绝此次分配,进程必须等待。 3. 如果资源可以分配,系统模拟分配该资源(Available -= Need),并假定进程开始执行。 4. 系统运行安全性检查算法,尝试找到一个安全序列。如果存在一个安全序列,则系统认为此次资源分配后系统仍然是安全的,确认资源分配;否则,进程必须等待,系统回滚此次资源分配。 安全性检查算法通常是一个寻找安全序列的过程,它会使用一个工作向量(Work)来表示系统剩余的可用资源,初始时Work = Available。接着,它会遍历每个进程,检查Need <= Work的情况。如果找到这样的进程,系统会假设该进程获得了所需的全部资源,执行完毕并释放所有资源,更新Work和Available,然后继续此过程,直到所有进程都被检查。如果所有进程都能完成这样的模拟过程,则系统存在一个安全序列,可以进行实际的资源分配。 实现银行家算法的关键在于维护Max, Allocation, Need和Available这几个矩阵和向量,并正确实现安全性检查算法。错误的实现可能导致系统无法准确评估资源分配后是否安全,进而导致死锁或者拒绝合理的资源分配请求,影响系统的效率。 从提供的文件信息来看,"bank.cpp"是一个用C++语言编写的简单银行家算法实现程序。虽然功能不多,但它可能是一个很好的学习和教学工具,帮助理解银行家算法的工作原理和实现细节。对于学习操作系统、并发编程或者需要处理资源分配问题的开发者来说,这个程序可能具有一定的参考价值。

将下面这条sql语句简化一下:SELECT * FROM ( SELECT fpd.fpd_id AS fpdId, fpd.fpd_budget_project AS fpdBudgetProject, fpd.fpd_budget_project_id AS fpdBudgetProjectId, fpd.fpd_pf_name AS fpdPfName, fpd.fpd_pf_id AS fpdPfId, fpd.fpd_expend_subject AS fpdExpendSubject, fpd.fpd_expend_subject_id AS fpdExpendSubjectId, fpd.fpd_payee_name AS fpdPayeeName, fpd.fpd_payee_account AS fpdPayeeAccount, fpd.fpd_payee_opening_bank AS fpdPayeeOpeningBank, fpd.fpd_payee_opening_bank_num AS fpdPayeeOpeningBankNum, fpd.fpd_dept_pay_economics_classify_num AS fpdDeptPayEconomicsClassifyNum, fpd.fpd_dept_pay_economics_classify_name AS fpdDeptPayEconomicsClassifyName, fpd.fpd_applied_amount AS fpdAppliedAmount, fpd.fpd_capital_use AS fpdCapitalUse, fpd.fpd_postscript AS fpdPostscript, fpd.fpd_budget_item_name AS fpdBudgetItemName, fpd.fpd_budget_item_number AS fpdBudgetItemNumber, fpd.fpd_dr_name AS fpdDrName, dbec.dbec_parent_num AS dbecParentNum FROM financial_payment_details fpd LEFT JOIN dept_budget_economy_class dbec ON fpd.fpd_dept_pay_economics_classify_num = dbec.dbec_num WHERE fpd.fpd_is_tax = "0" UNION ALL SELECT fpd.fpd_id AS fpdId, fpd.fpd_budget_project AS fpdBudgetProject, fpd.fpd_budget_project_id AS fpdBudgetProjectId, fpd.fpd_pf_name AS fpdPfName, fpd.fpd_pf_id AS fpdPfId, fpd.fpd_expend_subject AS fpdExpendSubject, fpd.fpd_expend_subject_id AS fpdExpendSubjectId, fpd.fpd_payee_name AS fpdPayeeName, fpd.fpd_payee_account AS fpdPayeeAccount, fpd.fpd_payee_opening_bank AS fpdPayeeOpeningBank, fpd.fpd_payee_opening_bank_num AS fpdPayeeOpeningBankNum, fpd.fpd_dept_pay_economics_classify_num AS fpdDeptPayEconomicsClassifyNum, fpd.fpd_dept_pay_economics_classify_name AS fpdDeptPayEconomicsClassifyName, sum( fpd.fpd_applied_amount ) AS fpdAppliedAmount, fpd.fpd_capital_use AS fpdCapitalUse, fpd.fpd_postscript AS fpdPostscript, fpd.fpd_budget_item_name AS fpdBudgetItemName, fpd.fpd_budget_item_number AS fpdBudgetItemNumber, fpd.fpd_dr_name AS fpdDrName, dbec.dbec_parent_num AS dbecParentNum FROM financial_payment_details fpd LEFT JOIN dept_budget_economy_class dbec ON fpd.fpd_dept_pay_economics_classify_num = dbec.dbec_num WHERE fpd.fpd_is_tax = "1" GROUP BY fpd_pay_num ) t1

2023-04-20 上传

将下面这条sql语句简短一些:SELECT * FROM ( SELECT fpd.fpd_id AS fpdId, fpd.fpd_budget_project AS fpdBudgetProject, fpd.fpd_budget_project_id AS fpdBudgetProjectId, fpd.fpd_pf_name AS fpdPfName, fpd.fpd_pf_id AS fpdPfId, fpd.fpd_expend_subject AS fpdExpendSubject, fpd.fpd_expend_subject_id AS fpdExpendSubjectId, fpd.fpd_payee_name AS fpdPayeeName, fpd.fpd_payee_account AS fpdPayeeAccount, fpd.fpd_payee_opening_bank AS fpdPayeeOpeningBank, fpd.fpd_payee_opening_bank_num AS fpdPayeeOpeningBankNum, fpd.fpd_dept_pay_economics_classify_num AS fpdDeptPayEconomicsClassifyNum, fpd.fpd_dept_pay_economics_classify_name AS fpdDeptPayEconomicsClassifyName, fpd.fpd_applied_amount AS fpdAppliedAmount, fpd.fpd_capital_use AS fpdCapitalUse, fpd.fpd_postscript AS fpdPostscript, fpd.fpd_budget_item_name AS fpdBudgetItemName, fpd.fpd_budget_item_number AS fpdBudgetItemNumber, fpd.fpd_dr_name AS fpdDrName, dbec.dbec_parent_num AS dbecParentNum FROM financial_payment_details fpd LEFT JOIN dept_budget_economy_class dbec ON fpd.fpd_dept_pay_economics_classify_num = dbec.dbec_num WHERE fpd.fpd_is_tax = "0" UNION ALL SELECT fpd.fpd_id AS fpdId, fpd.fpd_budget_project AS fpdBudgetProject, fpd.fpd_budget_project_id AS fpdBudgetProjectId, fpd.fpd_pf_name AS fpdPfName, fpd.fpd_pf_id AS fpdPfId, fpd.fpd_expend_subject AS fpdExpendSubject, fpd.fpd_expend_subject_id AS fpdExpendSubjectId, fpd.fpd_payee_name AS fpdPayeeName, fpd.fpd_payee_account AS fpdPayeeAccount, fpd.fpd_payee_opening_bank AS fpdPayeeOpeningBank, fpd.fpd_payee_opening_bank_num AS fpdPayeeOpeningBankNum, fpd.fpd_dept_pay_economics_classify_num AS fpdDeptPayEconomicsClassifyNum, fpd.fpd_dept_pay_economics_classify_name AS fpdDeptPayEconomicsClassifyName, sum( fpd.fpd_applied_amount ) AS fpdAppliedAmount, fpd.fpd_capital_use AS fpdCapitalUse, fpd.fpd_postscript AS fpdPostscript, fpd.fpd_budget_item_name AS fpdBudgetItemName, fpd.fpd_budget_item_number AS fpdBudgetItemNumber, fpd.fpd_dr_name AS fpdDrName, dbec.dbec_parent_num AS dbecParentNum FROM financial_payment_details fpd LEFT JOIN dept_budget_economy_class dbec ON fpd.fpd_dept_pay_economics_classify_num = dbec.dbec_num WHERE fpd.fpd_is_tax = "1" GROUP BY fpd_pay_num ) t1

2023-04-20 上传

SELECT t1.supplier_id, t1.ky_count, t1.ky_amount, IFNULL(t2.ky_refund_count,0) as ky_refund_count, IFNULL(t2.ky_refund_amount,0) as ky_refund_amount FROM ( SELECT a.supplier_id, count( DISTINCT c.order_no ) AS ky_count, SUM( IFNULL( c.record_amount, 0 )) / 100 AS ky_amount FROM settle_order a INNER JOIN settle_order_receipt b ON a.order_id = b.order_id INNER JOIN cash_withdrawal_record c ON b.third_trade_no = c.order_no WHERE a.is_del = 0 AND a.order_time >= '2023-05-28 00:00:00' AND a.order_time < '2023-05-29 00:00:00' AND a.order_type in (70,75) AND a.supplier_id IN (78,63,58,57,64,72,71,74,83,77,70,69,67,82,65,87,73,59,66,60,86,85,79,80,84,90) AND b.channel_code = 61 AND c.con_bank_account_no IN ( 247, 325 ) AND c.record_status = 1 AND c.record_time > '2023-05-01 00:00:00' GROUP BY a.supplier_id ) t1 LEFT JOIN ( SELECT a.supplier_id, count( DISTINCT b.order_no ) AS ky_refund_count, SUM( IFNULL( b.record_amount, 0 )) / 100 AS ky_refund_amount FROM settle_order_refund a INNER JOIN cash_withdrawal_record b ON a.third_refund_id = b.order_no WHERE a.is_del = 0 AND a.order_type in (70,75) AND a.apply_time >= '2023-05-28 00:00:00' AND a.apply_time < '2023-05-29 00:00:00' AND a.supplier_id IN (78,63,58,57,64,72,71,74,83,77,70,69,67,82,65,87,73,59,66,60,86,85,79,80,84,90) AND a.channel_code = 61 AND b.con_bank_account_no IN ( 247, 325 ) AND b.record_status = 5 AND b.record_time > '2023-05-01 00:00:00' GROUP BY a.supplier_id ) t2 ON t1.supplier_id = t2.supplier_id order by t1.supplier_id asc

2023-06-10 上传

insert overwrite table discountdw.dwd_sd_adds_order_bill_inc partition(dt = '2023-06-06') select t1.order_bill_id, t1.counterfoil_no, t1.acceptor, date_format(to_utc_timestamp(cast(t1.expiry_date as bigint) ,'GMT-8'),'YYYY-MM-dd'), t2.company_id, t1.cert_no, t1.company_name, t1.third_order_id, t1.counterfoil_amt/10000, t1.transaction_amt/10000, t1.rate, '3bp' as service_tariffing, ((DATEDIFF(to_utc_timestamp(t1.expiry_date ,'GMT-8'),to_utc_timestamp(t1.transaction_date ,'GMT-8') ) + adjust_days)* 0.0003 *(counterfoil_amt))/ 360 as service_fee, 360 as total_days, DATEDIFF(to_utc_timestamp(t1.expiry_date ,'GMT-8'),to_utc_timestamp(t1.transaction_date ,'GMT-8') ) + adjust_days as modulation_date, t3.channel_type, t3.bank_name, date_format(to_utc_timestamp(cast(t1.transaction_date as bigint) ,'GMT-8'),'YYYY-MM-dd'), t1.order_status_code, t1.order_status_msg, t4.fee_amt, t4.status, t1.tenant_id, t5.revenue, to_utc_timestamp(cast(t1.create_date as bigint) ,'GMT-8'), to_utc_timestamp(cast(t1.update_date as bigint) ,'GMT-8') from (select * from discountdw.ods_adds_order_bill_inc where dt ='2023-06-06' and channel_id=101110004 )t1 left join (select * from mecdw.ods_company_full where platform_id='sdpjw')t2 on t1.cert_no=t2.cert_no and t1.tenant_id=t2.tenant_id left join discountdw.dim_adds_product_full t3 on t1.partner_id=t3.partner_id and t1.product_id=t3.product_id left join (select * from mecdw.dwd_sc_fee_record_full where dt='2023-06-06' and biz_type=2 ) t4 on t1.order_bill_id=t4.third_id left join (select * from discountdw.ods_sd_order_ext_inc where dt='2023-06-06') t5 on t1.order_bill_id=t5.order_bill_id left join sdpjwdw.dim_holiday_info_full t6 on date_format(to_utc_timestamp(t1.expiry_date ,'GMT-8'),'YYYY-MM-dd') = t6.civic_holiday ;

2023-06-09 上传