CN112015525B - Task process blocking management method - Google Patents

Task process blocking management method Download PDF

Info

Publication number
CN112015525B
CN112015525B CN201910454326.4A CN201910454326A CN112015525B CN 112015525 B CN112015525 B CN 112015525B CN 201910454326 A CN201910454326 A CN 201910454326A CN 112015525 B CN112015525 B CN 112015525B
Authority
CN
China
Prior art keywords
task
file
identification file
identification
blocking
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201910454326.4A
Other languages
Chinese (zh)
Other versions
CN112015525A (en
Inventor
刘湛
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Shanghai Bilibili Technology Co Ltd
Original Assignee
Shanghai Bilibili Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Shanghai Bilibili Technology Co Ltd filed Critical Shanghai Bilibili Technology Co Ltd
Priority to CN201910454326.4A priority Critical patent/CN112015525B/en
Publication of CN112015525A publication Critical patent/CN112015525A/en
Application granted granted Critical
Publication of CN112015525B publication Critical patent/CN112015525B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/48Program initiating; Program switching, e.g. by interrupt
    • G06F9/4806Task transfer initiation or dispatching
    • G06F9/4843Task transfer initiation or dispatching by program, e.g. task dispatcher, supervisor, operating system
    • G06F9/4881Scheduling strategies for dispatcher, e.g. round robin, multi-level priority queues

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The invention relates to a task process blocking management method, and belongs to the technical field of Internet. When the second process of the task is started, the method of the invention judges whether the identification file exists or not; if yes, writing the second process number into the identification file and entering a blocking state; after the first process monitors that the identification file is modified, the progress is saved and the first process exits; the second process starts to run, and the further second process can open the lock file in an exclusive mode in the blocking state, so that the subsequent process is prevented from entering the blocking state. The method ensures that only one process is processing data at the same time of the timing trigger task, the later process can cover the operation of the former process, and in the process handover process, the progress interruption of the former process is not caused, the problem of coexistence of multiple processes of the timing trigger process and the progress interruption problem in the handover process are effectively solved, so that the process handover process is smoother, resources are saved, and the method is beneficial to realizing the efficient and safe operation of the server.

Description

Task process blocking management method
Technical Field
The invention relates to the technical field of Internet, in particular to the technical field of server process management methods, and specifically relates to a task process blocking management method.
Background
In the existing internet technology, two server background task processing modes exist: one is resident memory and the other is a timed task. When code or configuration is updated, the process of the resident memory cannot be automatically loaded, and the risk of dead running process or memory overflow is high. And the process triggered by the timing task can be automatically loaded when the codes or the configuration is updated, so that the requirement on the memory is smaller.
However, the main problem of the timing task is that if the pre-process is running when the post-process is triggered, the same data is repeatedly processed, which is a waste of operation resources and may even cause data exception. The linux server manages a class of processes in a process management form commonly used in the current internet technology. Such processes are triggered by the same timing task, and the running time of one process may exceed the triggering interval of two times, so that blocking processing is required.
Generally, a task refers to a series of processes that perform the same code flow and achieve the same purpose; blocking refers to ensuring that only one process can process the same task in the same period, namely, when the next process starts and the previous process does not end, the previous process is ended as soon as possible by effectively controlling, and the next process is suspended.
The timing task generally has three blocking processing strategies, namely, the following processes are queued for waiting, but the processes are easily accumulated; killing the front process, wherein if the front process has unprocessed data, the progress is lost; thirdly, discarding the back process, the front process can be degenerated into a resident process, and the advantages of the timing task are lost.
Therefore, how to provide a smoother and resource-saving task process blocking method, and further realize a more efficient and safer iterative manner of the same task process, is a problem to be solved in the art.
Disclosure of Invention
The task process blocking management method aims to overcome the defects in the prior art, is smoother, saves resources, and is beneficial to realizing efficient and safe task process blocking management methods in the same task process iteration mode.
In order to achieve the above object, the task process blocking management method of the present invention includes the steps of:
starting a first process for running a task, and writing an identification of the first process into an identification file;
starting a second process of the task, and determining whether to start running the second process according to the identification file and the identification of the second process;
When the first process is still running and the second process is not started to run in a blocking state, setting that a lock file corresponding to the identification file is exclusive to the second process, and writing the identification of the second process into the identification file.
In the task process blocking management method, the steps specifically include the following steps:
Starting a first process of a task, establishing an identification file of the task, writing a first process number in the identification file, and starting to run the first process;
In the running process of the first process, regularly monitoring whether the identification file is modified; deleting the identification file when the first process is finished;
Starting a second process of the task, and judging whether an identification file of the task exists or not;
If the identification file does not exist, a new identification file of the task is established, a second process number is written in the new identification file, and the second process starts to run;
If the identification file exists, entering the blocking state, writing a second process number into the existing identification file, and monitoring the running state of the first process at regular time;
when the first process monitors that the identification file is modified, saving the processing progress and exiting the first process;
When the first process is finished, starting to run the second process, and closing the lock file;
And when the set time passes and the first process is not finished, discarding and exiting the second process, and closing the lock file.
In the task process blocking management method, the identification file comprises an absolute file path of the process.
In the task process blocking management method, the judging whether the identification file of the task exists or not specifically comprises the following steps:
searching whether an identification file exists or not, and if not, indicating that the identification file of the task does not exist; if so, acquiring an absolute file path of a process in the identification file, comparing whether the acquired absolute file path is consistent with the absolute file path of the second process, if so, indicating that the identification file of the task exists, and if not, indicating that the identification file of the task does not exist.
In the task process blocking management method, the writing of the second process number into the existing identification file specifically comprises the following steps:
And covering the first process number in the existing identification file with the second process number.
In the task process blocking management method, in the running process of the first process, whether the identification file is modified or not is monitored at regular time, specifically:
and in the running process of the first process, regularly monitoring whether the process number in the identification file is the first process number.
In the task progress blocking management method, the set elapsed time is 30 seconds.
In the task process blocking management method,
When the first process of the task is started, further comprising the step of,
Establishing a lock file associated with the identification file of the task;
When judging that the identification file exists, before writing the second process number into the existing identification file, the method further comprises the following steps:
opening the lock file in an exclusive mode;
and closing the lock file until the second process starts to run or exits.
The invention also provides a computer readable storage medium having stored thereon a computer program which, when executed by a processor, implements the task process blocking management method described above.
The invention also provides a task process blocking management device, which comprises a processor and a memory, wherein the memory is stored with a computer program, and the task process blocking management method is realized when the computer program is executed by the processor.
By adopting the task process blocking management method, when a second process of a task is started, whether the identification file exists or not is judged; if the first process is not finished, entering the blocking state, and writing the second process number into the existing identification file; after the first process monitors that the identification file is modified, the processing progress is saved and the first process exits; then the second process establishes a new identification file, writes in the second process number and starts to run. The task process blocking management method ensures that for a task triggered at regular time, only one process is processing data, a post-process can cover a pre-process to run, meanwhile, in the process of process handover, the process progress of the pre-process is not interrupted by the pick-up of the post-process, and the problem that the task triggered at regular time is easy to generate multi-process coexistence and the problem of progress interruption in the process of process handover are effectively solved. Therefore, the process switching process is smoother, resources are saved, the efficient and safe operation of the server is realized, and the task process blocking management method is simple and convenient in application mode and wide in application range.
Drawings
FIG. 1 is a flow chart of steps of a task process blocking management method of the present invention.
FIG. 2 is a flowchart illustrating steps in an embodiment of a method for task process blocking management according to the present invention in which lock files are provided.
Detailed Description
In order to make the technical contents of the present invention more clearly understood, the following examples are specifically described.
Referring to fig. 1, a flowchart of steps of a task process blocking management method according to the present invention is shown.
In practical application, the task process blocking management method of the invention can be used for selecting an implementation mode. Specific embodiments are illustrated by the following examples.
Example 1
The task process blocking management method comprises the following steps:
starting a first process for running a task, and writing an identification of the first process into an identification file;
starting a second process of the task, and determining whether to start running the second process according to the identification file and the identification of the second process;
when the first process is still running and the second process is not started to run in a blocking state, setting that a lock file corresponding to the identification file is exclusive to the second process, and writing the identification of the second process into the identification file. Processes of the same task run on the same machine (server). And designating a certain fixed file as the task identification file on the machine, writing own process number into the identification file when each process is started, and deleting the identification file before exiting.
Namely, a first process of a task is started, an identification file of the task is established, a first process number is written in the identification file, and the first process starts to run; in the running process of the first process, regularly monitoring whether the identification file is modified; and deleting the identification file when the first process is finished. .
And in the starting and running process of the latter process, continuously acquiring the content of the file, and if the file does not exist, writing the current process number into the file and starting to execute the task.
That is, a second process of the task is started later, and whether the identification file of the task exists or not is judged; if the identification file does not exist, a new identification file of the task is established, a second process number is written in the new identification file, and the second process starts to run; if the identification file exists, entering the blocking state, writing a second process number into the existing identification file, and monitoring the running state of the first process at regular time; and when the first process monitors that the identification file is modified, saving the processing progress and exiting the first process.
If the identification file exists, at this time, if the process number recorded by the file is inconsistent with the current process number, the absolute file path of the process is identified from the operation parameters of the process recorded by the file (the absolute path is obtained by using the command of 'pwdx process number' or 'lsof-a-p process number-d cwd'), and if the absolute file path is consistent with the file path of the current operation process, the absolute file path represents a different process of the same task, and then the immediately started process is still operated by the previous process.
That is, judging whether the identification file of the task exists or not, and searching whether the identification file exists or not, if not, indicating that the identification file of the task does not exist; if so, acquiring an absolute file path of the process in the identification file, wherein the identification file comprises the absolute file path of the process. Comparing whether the obtained absolute file path is consistent with the absolute file path of the second process, if so, indicating that the identification file of the task exists, and if not, indicating that the identification file of the task does not exist.
At this time, if the new process is designated to run in the overlay mode, the process number of the new process is overlaid on the old process number in the identification file, and the running state of the old process is continuously detected 30 seconds later. If the new process does not specify an override mode, the new process may be directly retired if the old process is detected.
That is to say,
Setting the lock file corresponding to the identification file to be exclusive to the second process;
when the first process is finished, starting to run the second process;
When the set time passes and the first process is not finished, the second process is abandoned and exited.
For the old process, if the change of the process number recorded by the identification file is detected, the old process safely exits after the process progress is saved and the new process is picked up for processing.
That is, in the running process of the first process, whether the identification file is modified or not is monitored regularly, which may be whether the process number in the identification file is the first process number or not is monitored regularly; and when the first process monitors that the identification file is modified, saving the processing progress and exiting the first process.
When the old process is no longer running, the new process starts to process data; if the old process is still running after 30 seconds, the new process gives up and exits.
When 30 seconds or some other predetermined time has elapsed, but the first process has not yet ended, the second process is aborted and exited.
Example 2
The difference from embodiment 1 is that to prevent another new process from entering before overwriting the old process number, a lock file (which may take the form of an identification filename plus a constant suffix name) needs to be opened in an exclusive manner until the new process enters a processing state or exits, and the lock is not released. The specific flow is shown in fig. 2. In the locking process, if a second new process tries to execute the same operation of the overlay process number as the first new process, the locking failure directly exits, so that only one new process is in a blocking state at the same time.
That is, when a first process of a task is started, an identification file and a lock file associated with the identification file of the task are simultaneously established; when judging that the identification file exists, before writing the second process number into the existing identification file, the method further comprises the following steps: opening the lock file in an exclusive mode; and closing the lock file until the second process starts to run or exits.
The lock file has the effect of preventing the second process from being updated by the third process when the second process waits for the first process to end.
Specifically, two files are used in this embodiment, one is an identification file (pid file-xxx/pid) to identify the id of the currently running process, and the other is a lock file (pid lock file xxx/pid. Lock) used when the process waits for the last process to end.
The identification file is used for checking whether the current running process needs to be switched.
The lock file is used for preventing queuing in the process of waiting, and is released after the blocking state is finished.
For example:
When the first process exits and the second process enters an operating state or the second process waits for exiting overtime, the lock file is released; if the third process is started again in the waiting process of the second process, the third process directly exits due to locking failure (lock files which are exclusively opened by the second process cannot be opened); if the second process waits for a timeout to exit, the third process starts because at this point the lock file has been released when the second process has timed out, and that third process operates as if it were the second process.
Example 3
By taking a ticketing system as an example, the task progress blocking management method of the invention is used for carrying out various scenes such as asynchronous ordering, message notification sending, asynchronous batch refund and the like, thereby not only ensuring the real-time processing performance, but also avoiding the problems of dead or memory overflow caused by long-time running of the progress.
Specifically, taking an asynchronous order task as an example, a user request is temporarily stored in a queue, then the asynchronous order task is triggered once every 1 minute, data in the queue is consumed, a process number of the user request is recorded in/[ path_of_task ]/pid when a process is started, and the content of a pid file is checked after each batch of data is processed. After one minute, the second process starts, firstly, the content of the pid file is checked, the existence of the pid is found, the process file path is judged through the process number pwdx, the process is found to be the same as the second process file path, the overlay operation is determined, then the second process locks a file/[ path_of_task ]/pid.lock in an exclusive mode, then the process number of the second process is written into the pid file, and the survival of the first process is continuously checked in the next 30 seconds. At this time, the first process checks the pid file, and finds that the process number is changed, so that the first process actively exits when the first process finishes processing the data in the memory, and the second process checks that the first process is no longer alive, releases the lock file, and enters the running state. If process three starts after 1 minute, the overlay operation is decided after the pid is checked, but the process three can exit after the pid.lock file is locked.
The invention also provides a task process blocking management device and a computer readable storage medium. The task process blocking management device comprises a processor and a memory, wherein the memory is a computer readable storage medium, and a computer program is stored on the memory. The task process blocking management method described in the above embodiments is implemented when the computer program is executed by the processor.
By adopting the task process blocking management method, when a second process of a task is started, whether the identification file exists or not is judged; if the first process is not finished, entering the blocking state, and writing the second process number into the existing identification file; after the first process monitors that the identification file is modified, the processing progress is saved and the first process exits; and then running a second process and closing the lock file. The task process blocking management method ensures that for a task triggered at regular time, only one process is processing data, a post-process can cover a pre-process to run, meanwhile, in the process of process handover, the process progress of the pre-process is not interrupted by the pick-up of the post-process, and the problem that the task triggered at regular time is easy to generate multi-process coexistence and the problem of progress interruption in the process of process handover are effectively solved. Therefore, the process switching process is smoother, resources are saved, the efficient and safe operation of the server is realized, and the task process blocking management method is simple and convenient in application mode and wide in application range.
In this specification, the invention has been described with reference to specific embodiments thereof. It will be apparent that various modifications and variations can be made without departing from the spirit and scope of the invention. The specification and drawings are, accordingly, to be regarded in an illustrative rather than a restrictive sense.

Claims (10)

1. A method for task process blocking management, the method comprising the steps of:
starting a first process for running a task, and writing an identification of the first process into an identification file;
Starting a second process of the task, if the identification file exists, entering a blocking state that the first process is still running and the second process is not started to run, writing a second process number into the existing identification file, and monitoring the running state of the first process at regular time; when the first process monitors that the identification file is modified, saving the processing progress and exiting the first process;
when entering the blocking state, setting that a lock file corresponding to the identification file is exclusive to the second process, and writing the identification of the second process into the identification file; and when the first process is finished, starting to run the second process, and closing the lock file.
2. The task process blocking management method according to claim 1, wherein,
The step of starting the first process for running the task and writing the identification of the first process into the identification file specifically comprises the following steps:
Starting a first process of a task, establishing an identification file of the task, writing a first process number in the identification file, and starting to run the first process;
In the running process of the first process, regularly monitoring whether the identification file is modified; deleting the identification file when the first process is finished;
if the identification file exists, entering a blocking state that the first process still operates and the second process does not start to operate, writing a second process number into the existing identification file, and monitoring the operation state of the first process at fixed time; when the first process monitors that the identification file is modified, the processing progress is saved, and the first process is exited specifically comprises the following steps:
Starting a second process of the task, and judging whether an identification file of the task exists or not;
If the identification file does not exist, a new identification file of the task is established, a second process number is written in the new identification file, and the second process starts to run;
If the identification file exists, entering the blocking state, writing a second process number into the existing identification file, and monitoring the running state of the first process at regular time;
when the first process monitors that the identification file is modified, saving the processing progress and exiting the first process;
When the blocking state is entered, setting that a lock file corresponding to the identification file is exclusive to the second process, and writing the identification of the second process into the identification file; when the first process is finished, starting to operate the second process, and closing the lock file, wherein the steps specifically include:
Setting the lock file corresponding to the identification file to be exclusive to the second process;
When the first process is finished, starting to run the second process, and closing the lock file;
And when the set time passes and the first process is not finished, discarding and exiting the second process, and closing the lock file.
3. A method of task process blocking management according to claim 2, wherein the identification file comprises an absolute file path of the process.
4. A method for managing task process blocking according to claim 3, wherein said determining whether the identification file of the task exists is specifically:
searching whether an identification file exists or not, and if not, indicating that the identification file of the task does not exist; if so, acquiring an absolute file path of a process in the identification file, comparing whether the acquired absolute file path is consistent with the absolute file path of the second process, if so, indicating that the identification file of the task exists, and if not, indicating that the identification file of the task does not exist.
5. The task process blocking management method according to claim 1, wherein the writing of the second process number into the existing identification file specifically includes:
And covering the first process number in the existing identification file with the second process number.
6. The task process blocking management method according to claim 5, wherein during the running of the first process, the step of periodically monitoring whether the identification file is modified is specifically:
and in the running process of the first process, regularly monitoring whether the process number in the identification file is the first process number.
7. The task process blocking management method according to claim 2, wherein the elapsed time is 30 seconds.
8. The task process blocking management method according to claim 1, wherein,
When the first process of the task is started, further comprising the step of,
Establishing a lock file associated with the identification file of the task;
When judging that the identification file exists, before writing the second process number into the existing identification file, the method further comprises the following steps:
opening the lock file in an exclusive mode;
and closing the lock file until the second process starts to run or exits.
9. A computer readable storage medium having stored thereon a computer program, which when executed by a processor, implements the task process blocking management method according to any of claims 1 to 8.
10. A task process blocking management device comprising a processor and a memory, said memory having stored thereon a computer program, characterized in that the computer program, when executed by said processor, implements the task process blocking management method according to any of claims 1 to 8.
CN201910454326.4A 2019-05-29 2019-05-29 Task process blocking management method Active CN112015525B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201910454326.4A CN112015525B (en) 2019-05-29 2019-05-29 Task process blocking management method

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201910454326.4A CN112015525B (en) 2019-05-29 2019-05-29 Task process blocking management method

Publications (2)

Publication Number Publication Date
CN112015525A CN112015525A (en) 2020-12-01
CN112015525B true CN112015525B (en) 2024-07-12

Family

ID=73501156

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201910454326.4A Active CN112015525B (en) 2019-05-29 2019-05-29 Task process blocking management method

Country Status (1)

Country Link
CN (1) CN112015525B (en)

Families Citing this family (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114020481A (en) * 2021-10-22 2022-02-08 山东浪潮科学研究院有限公司 Method for preventing multiple processes from accessing same arbitrary waveform generator
CN114327816B (en) * 2021-12-13 2025-03-18 上海万物新生环保科技集团有限公司 A method and device for processing batch tasks
CN116339956B (en) * 2023-05-29 2023-10-10 天翼云科技有限公司 Distribution method and device of configuration tasks, electronic equipment and storage medium

Citations (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106557360A (en) * 2016-11-09 2017-04-05 青岛海信电器股份有限公司 Task run method and device

Family Cites Families (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JP4022764B2 (en) * 2003-06-26 2007-12-19 日本電気株式会社 Information processing apparatus, file management method, and program
US8424017B2 (en) * 2010-04-30 2013-04-16 International Business Machines Corporation Embedding process identification information in a pathname to indicate process status
CN102693159B (en) * 2012-05-09 2015-07-01 上海晨思电子科技有限公司 Method and device for controlling hardware equipment based on user mode multitask
JP6127754B2 (en) * 2013-06-12 2017-05-17 富士通株式会社 Program, exclusive control request distribution method and system
CN103778007B (en) * 2014-02-14 2018-06-08 北京猎豹移动科技有限公司 A kind of application management method and device
CN106775980B (en) * 2016-12-15 2020-04-14 北京奇虎科技有限公司 A process ID management method, device and computer readable medium
CN108509260B (en) * 2018-01-31 2021-08-13 深圳市万普拉斯科技有限公司 Thread identification processing method, apparatus, computer equipment and storage medium
CN109359005B (en) * 2018-09-14 2022-04-19 厦门天锐科技股份有限公司 Cross-process data acquisition and processing method

Patent Citations (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106557360A (en) * 2016-11-09 2017-04-05 青岛海信电器股份有限公司 Task run method and device

Also Published As

Publication number Publication date
CN112015525A (en) 2020-12-01

Similar Documents

Publication Publication Date Title
CN112015525B (en) Task process blocking management method
CN109240765B (en) Fusing method, device and equipment for service resources and computer readable storage medium
US6978303B1 (en) Monitoring of computer usage
KR102098581B1 (en) Terminal-based control method, device, and terminal for WAKELOCK
CN113835985B (en) Method, device and equipment for monitoring and analyzing jamming reason
CN112991635B (en) Storage and taking method and device for intelligent cabinet
CN101373505A (en) Method, device and file deletion system for releasing handle
CN111124761B (en) Equipment restarting method, device, equipment and medium
CN111787256B (en) Management method, device, medium and electronic equipment for pre-alarm video
CN108733465A (en) The control method of service processes, the control device of service processes and terminal
CN114003396A (en) A kernel soft deadlock processing method, device, device and storage medium
CN112463348B (en) Timed task processing method and device
CN117707798A (en) Deadlock elimination method, device, equipment and medium in multi-thread concurrency state
CN107967181B (en) Method and device for controlling critical section
KR101478327B1 (en) Apparatus, method and computer readable recording medium of monitoring and preventing a false operation for a system optimization function
CN112804108B (en) Signaling execution method and device, electronic equipment and machine-readable storage medium
CN117555717B (en) Application exception handling method, terminal and computer storage medium
CN113515353A (en) Long transaction processing method, system, electronic device and storage medium
CN111831449A (en) Process processing method and device
CN113342511A (en) Distributed task management system and method
CN110308943B (en) Program running method and device, computing equipment and storage medium
CN111158880A (en) Distributed timed task scheduling method, system, device and storage medium
WO2023178915A1 (en) Method and system for auditing process behavior, and storage medium and computer device
CN112181609B (en) Service daemon method, device and readable storage medium
JP2545856B2 (en) Memory dump collection device

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant