US 6,272,518 B1Grant
System and method for porting a multithreaded program to a job model
Issue Date:2001-08-07
•13 Claims
•4 Drawing Sheets
Abstract
A system and method for porting a multi-threaded program to a job model. Data that needs to be shared between different jobs is globalized by storing data in a user space accessible to all of the jobs. Thread controls are replaced by job controls by replacing thread function calls by job commands wrappered to appear as functions.
Metadata
Assignee
- International Business Machines Corporation
Inventors
- Gary M. Blazo
- Kevin D. Hall
- Chung Meng
- Kurt A. Streifert
Application Information
Application Number:US 09/135,253
Filing Date:1998-08-17
Priority Date:1998-08-17
Art Unit:7
Classifications
IPC:
G06F 900
Field of Search:
709100709101709102709103709104709106709108709314709231710104710129717 4717 5
Patent Drawings (4 sheets)
Description
CROSS REFERENCES TO RELATED APPLICATIONS
[0002] U.S. patent applications Ser. No. 09/135,102, filed Jan. 30, 2001, now U.S. Pat. No. 6,182,228 entitled “System and Method for Very Fast IP Packet Filtering”, and Ser. No. 09/135,148, filed Aug. 17, 1998 entitled “System and Method for IP Network Address Translation and IP Filtering With Dynamic Address Resolution”, assignee docket numbers EN998066 and EN998067, respectively, filed concurrently herewith are assigned to the same assignee hereof and contain subject matter related, in certain respect, to the subject matter of the present application. The above identified patent applications are incorporated herein by reference.
BACKGROUND OF THE INVENTION
[0003] 1. Technical Field of the Invention
[0004] This invention pertains to a method and system for porting multi-threaded application programs to a computer system supporting a job model. In particular, this invention provides for globalizing variables and wrappering job related commands thereby to function as thread function calls.
[0005] 2. Background Art
[0006] In a prior art multithreaded application based on the POSIX draft standard 1003.4, threads process multiple execution environments with sharing of data as global values among threads.
[0007] Job models, such as the ILE C environment on the IBM AS/400 System, process work for application programs, but typically do not share data across jobs. There is a need in the art for a system and method for adapting code based on threading model concepts yet interfacing with an underlying job model.
[0008] It is an object of the invention to provide a system and method for implementing a job-based application on a threaded model.
[0009] It is a further object of the invention to provide a system and method for enabling sharing of data as global values among threads within a job model.
[0010] It is a further object of the invention to provide an HTTP server implemented as jobs built on a threaded model.
SUMMARY OF THE INVENTION
[0011] In accordance with the invention, a method is provided for porting a multi-threaded program to a job model, comprising the steps of globalizing data that need to be shared between different jobs by storing data in a user space accessible to all of the jobs; and replacing thread controls by job controls, including replacing thread function calls by job commands wrappered to appear as functions.
[0012] Other features and advantages of this invention will become apparent from the following detailed description of the presently preferred embodiment of the invention, taken in conjunction with the accompanying drawings.
BRIEF DESCRIPTION OF THE DRAWINGS
[0013] FIG. 1 is a high level system diagram illustrating server threading.
[0014] FIG. 2 is a high level flow diagram illustrating server threading flow.
[0015] FIG. 3 illustrates the preferred embodiment of the invention for globalizing variables and wrappering jobs for use in an application program based upon a thread model.
[0016] FIG. 4 illustrates the job structure of the HTTP server of the preferred embodiment of the invention.
BEST MODE FOR CARRYING OUT THE INVENTION
[0017] The invention relates to running a thread model on top of a job model. In a preferred embodiment of the invention, an HTTP server 190 is implemented as IBM System AS/400 jobs built on a threaded HTTP server.
[0018] In building a job model from a threading model, first the data that needs to be shared between the main server thread (parent job) and the worker threads (child jobs) are globalized. This is accomplished by storing globalized data in a user space that is accessible to all of the jobs. Second, the thread controls are replaced by job controls. This is done by rewriting thread specific function calls such as PthreadCreate, PthreadExit and PthreadMutexLock as AS/400 commands, API calls and MI instructions.
[0019] Referring to FIG. 1, in accordance with the Internet connection secure server embodiment of the invention, a server can listen on both a secure port 204 using secure sockets layer (SSL) and a non-secure port 202 using sockets for requests coming from HTTP clients on network 200. When a listening thread 206 detects a request coming into port 202 or 204 from network 200, it passes the socket descriptor for the request off to a worker thread 212 or 214 that is waiting in a thread pool 210 of initialized threads. Worker thread 212 or 214 processes the request and completes the transaction by sending a response back to the HTTP client on network 200. Service threads 220 run in the background to handle server utility functions including alarm 222 and log writer 224.
[0020] Referring to FIG. 2 in connection with FIG. 1, as part of initialization, main thread 240 processes configuration file 244 directives. The MinActiveThreads and MaxActiveThreads directives are used to set the boundaries of the pool 210 of available threads that can be used by the server 190. Main program thread 240 initializes thread pool 210 and then uses a thread 202 to bind( ) 260 and listens 262 on an IP address port 202. When server 190 is started with directives normalmode on and sslmode on there will be one thread 206, 262 listening on port 80 (the HTTP default port 202) and one thread 206, 272 listening on port 443 (the HTTPS default port 204). Hereafter, these threads may be referred to as listing threads, or parent threads. HTTP and HTTPS default ports 202, 204 can be overriden with a directives port (for (HTTP) and an sslport (for HTTPS). These threads 206 will listen on all IP addresses defined for this host unless the BindSpecific directive is in configuration file 244 and it contains a valid IP address for this host. Then threads 206 only listen on the ports for that IP address.
[0021] When a request comes in on either or both ports 202, 204, the listening thread 262, 272 does an accept 264, 274 and passes the incoming request off to a work thread 266, 276 that comes from thread pool 210. Listening thread 262, 272 then goes back to accepts mode 264, 274. The work thread 266, 276 processes the incoming request and sends a response to the client. When work thread 266, 276 completes its tasks it returns to pool 210 of available threads.
[0022] Two service threads 220, including log writer thread 224 and alarm thread 222, are spun off at server 190 initialization and wait in the background until their services are needed.
[0023] Log writer routine 224 is spun off in its own thread when server 190 starts. It spends most of its time sleeping, but wakes up periodically to write out to log files 226 anything that needs to be logged. It handles writing to the access and error logs.
[0024] At initialization, startup job 240, 250 opens the log files 226 that are specified in configuration directives 244 (or over-rides) and then passes the file descriptors to the log writer thread 224. As is represented by lines 234 and 236, server listening threads 206 and worker threads 210 add log data to log queue 228 for log writer thread 224 to log. When log writer thread 224 wakes up it checks queue 228 for data to be logged. When there is data to be logged, log writer thread 224 locks the queue with a mutually exclusive (mutex) lock on line 218 and, as is represented by line 238, copies the log data from queue 228 into list 216, zeros out queue 228, and then releases mutex 218. Log writer thread 224 can then log the data from list 216 without having other threads 206, 210 blocked from writing to queue 228.
[0025] Alarm thread 222 is spun off (spawned) in its own thread when server 190 starts. It spends most of its time sleeping. It wakes up periodically and checks a list of timers 232 that are associated with active work threads 210, such as 266, 268, 276, 278. When a timer 232 expires it indicates that a work thread 210 had a problem completing its task on time. When alarm thread 222 finds an expired timer the timer is removed from the list of timers 232, the outstanding function being processed is terminated, and the socket 202, 204 being used by the request is closed.
[0026] Hereafter, when the terms “thread” and “threading” are used they are, generally, referring to a server 190 threading model that is implemented in accordance with the invention within a job model.
[0027] Referring to FIG. 3, globalizing and wrappering in accordance with a preferred embodiment of the invention are presented. Application program 100 is based on a thread model, and includes a series of thread creation steps 104, 112 and thread calls 106, 114. Thread X 106 establishes a global variable VAR Y 108, and thread Z 114 requires, as is illustrated at 116, use of that VAR Y. Data area 120 includes a thread ID to job ID mapping table 122, and global variable space 124. Jobs 130, 132 reside outside of data area 120 at locations addressed by pointers 107, 119, respectively.
[0028] Variables 124 are globalized by being stored in data area 120, where they are accessible for use by steps 108, 116.
[0029] Jobs are wrappered to present themselves to program 100 as though they were threads by responding to create thread steps 104, 112 by mapping, through table 122, the respective thread identifier 105, 117 to corresponding job identifiers 107, 119, and placing in code string 106, 114 code 109, 121 derived from job characteristics 130, 132.
[0030] Referring to FIG. 3 in connection with Tables 1 and 2, application program code string 100 is implemented in Table 2 as the ICS server job model; create threads 104 is implemented at Table 2 line 6; pointer 105 to mapping table 122 is implemented at Table 2 line 115; (thread ID X) 106 is implemented at Table 2 line 90; pointers 107, 119 from mapping table 122 to job characteristics 130 is implemented at Table 2 lines 122, 133; VAR Y 108 is implemented at Table 2 lines 108-109; pointers 109, 115 and 121 from job characteristics 130, 132 are implemented a Table 2 lines 134-141; ? thread ID X 110 is implemented at Table 2 line 90; pointer 111 to VAR Y 126 is implemented at Table 2 line 110; create threads 112 is implemented at Table 2 line 6; pointer 113 to mapping table 122 is implemented at Table 2 line 115; (thread ID Z) 114 is implemented at Table 2 line 90; VAR Y=? 116 is implemented at Table 2 lines 108, 109; pointer 117 to mapping table 122 is implemented at Table 2 line 115; data area 120 is implemented in Table 2 lines 4, 58, and 90 as PJOBSTRUCT; mapping table 122 is implemented at Table 1 lines 28, 29; pointer 123 to VAR Y=? 116 is implmented at Table 2 line 110; global variables table 124 is implemented at Table 1 line 18; VAR Y entry 126 within table 124 is implemented at Table 1 line 28 ; and job characteristics 130, 132 is implemented as structure _HOTTHREAD at Table 1 line 29 and Table 2 lines 125, 127 and 145.
[0031] Referring to FIG. 4, the manner in which the code set forth in Tables 1 through 8A,B, is next described. HTTP server 190 (that is, QTMHHTTP) is set up in block 140 and started in block 142. These steps are implemented by Table 3 lines 21-23. QUSCRTUS 144 is implemented by Table 4 lines 1 through 40. SBM job QTMHHTTP Main 146 is implemented by Table 5. QTMHHTTP 160 is implemented by Table 6B line 1. USRSPC 150 is implemented by Table 6A. Spawn QTMHHTTP SSL 162 is implemented by Table 6B line 17. Spawn QTMHHTTP ALARM 164 is implemented by Table 6B line 27. Spawn QTMHHTTP LOG 166 is implemented by Table 6B line 81. Spawn QTMHHTTP WORKER 168 is implemented by Table 6B line 46. RSLVSP 170, 172, 174, 176, and 178 are implemented by Table 7, which also describes Table 2 line 90. QTMHHTTP SSL 180 is a main( ) routine implemented by Table 6B lines 1 and 17. QTMHHTTP ALARM 182 is a main( ) routine implemented by Table 6B lines 1 and 27. QTMHHTTP LOG 184 is a main( ) routine implemented by Table 6B lines 1 and 81. QTMHHTTP WORKER 186 is a main( ) routine implemented by Table 6B lines 1 and 46.
[0032] Referring further to FIG. 4, in operation, in accordance with a specific embodiment of the invention, various AS/400 jobs are started to support an HTTP server 190. The purpose of the jobs is analogous to those of a thread version. For example, a main job 160 is created that will then create additional jobs SSL 180, alarm 182, log 184, and worker 178. Upon properly configuring SSL, a spawn( ) 162 is issued to create job SSL 180 that will then listen 272 on port 204 for https requests. This job SSL 180 becomes a listening, or parent job to, for example, worker jobs 186. In addition, a number of worker jobs 186 are created (spawn 168) that are kept in a pool and at a later time are dispensed as appropriate with units of work (either requests for documents or to execute CGI programs). Finally, spawn log 166 creates log job 184 and spawn alarm 164 creates alarm job 182. USRSPC 150 is used to share global data across these jobs. USRSPC 150 is a system domain object that is uniquely named per server or worker job instance with the instance name and is located in the QHTTP library.
[0033] Referring to Table 1, pseudo code (in C-like syntax) sets forth the content of the ICS server thread jobs file thrdjobs.h. This file is included in Table 2 at line 3, and provides various declarations, definitions, and structures used in the server job model, as will be more fully described hereafter.
[0034] Referring to Table 2, pseudo code (in C-like syntax) sets forth the ICS server job model. At lines 6 and 7, the function pthread_create( ) is defined. This function pthread_create( ) includes the parameters passed to allow the server job model to simulate the corresponding thread function. This will be done hereafter by spawning the required jobs using the C-language spawn function. At line 58, variables are initialized and space is created in memory for the global variables that need to be shared between listener job 206 and worker jobs 210 and other jobs 220. At line 90, a spawned job (for example, HTTP work thread 1 job 266) identifies the memory location of the listener job (for this example, listen( ) job 262) to enable worker job 266 to access the global variables defined by the listener job. At line 105, * thread_malloc( ) creates a list of worker threads 210 used to dispatch threads using a job model. At lines 115-117, the listener job (say, 262) looks for an idle worker job in list, or pool, 210 and dispatches to that worker job the specific work to be done. In the specific embodiment of this server example, the work being done may be to look up a document the listening thread 262 needs to return to a client (on network 200) that has requested a web page.
[0035] Referring to Table 3, high level pseudo code describes create job, the C function main ( ), which calls the various functions discussed in connection with table 2.
[0036] Referring to Table 4, high level pseudo code describes the process for creating user space, which is the global shared space needed for sharing global variables.
[0037] Referring to Table 5, the C code used to create a batch job to start the server. The parameters defined are those needed to start the job. At line 5 both user( ) and main( ) are named qtmhhttp. Lines 6 and 7 specify that messages out to library qusersys are identified a user identifier of qtmhhttp.
[0038] Referring to Table 6A, an expanded version of Table 1 lines 18-22 provides C code definitions for all variables inside global shared space 124 used by all the jobs. Lines 2, 4-8, 242-245, 252 define the most important. Line 2 defines the structure _jobstruct{ }. Lines 4-8 use the C function mutex to define the names of the worker and server jobs, with both having the same name. Lines 242-245 define memory space where unique worker job information is kept. Line 252 defines the name given to structure jobstruct. Line 253 allows other modules access to the unique pointer, pSpacePtr, to jobstruct. Line 254 defines a unique mapping of name jobdata to pointer pSpacePtr.
[0039] Referring to Table 6B, C code for the spawn function is provided. This provides a more complete statement of the pseudo code statement of the server job model set forth in Table 2. Spawn is a C language function provided by include file qp0z1170.h.
[0040] Referring to Table 7, a high level pseudo code statement is provided for the resolve user space function of line 90 in Table 2.
[0041] Referring to Table 8A, a high level pseudo code statement is provided of the start thread function. This defines the process for starting a thread in the simulated job model, and relates to line 6 of Table 2 where code is provided to create pthread and spawn a job. Table 8 line 27 through line 37 describes line 115 of Table 2.
[0042] Referring to Table 8B, the thread wrapper function is described. In this example, the mutex function is implemented.
[0043] Referring to further and in more detail to Tables 1 and 2 in connection with FIG. 3, in Table 1 the thrdjobs.h include file is described for the ICS server jobmodel of Table 2. Tables 1 and 2 are a pseudo code representation (C-like in syntax and structure), of application program 100, together with data area 120, of FIG. 3.
[0044] Referring to Table 1, pseudo code (in C-like syntax) sets forth the content of the ICS server thread jobs file thrdjobs.h. This file is included in the ICS server job model, Table 2, at line 3, and provides various declarations, definitions, and structures used in the server job model, as follows:
[0045] Referring to Table 2 , pseudo code in C-like syntax sets forth the jobmodel of the ICS server 190, as follows:
Advantages over the Prior Art
[0046] The advantages of the system and method of the referred embodiment of this invention include the provision of: a system and method for implementing a job-based application on a threaded model; a system and method for enabling sharing of data as global values among threads within a job model; and an HTTP server implemented as jobs built on a threaded model.
Alternative Embodiments
[0047] It will be appreciated that, although specific embodiments of the invention have been described herein for purposes of illustration, various modifications may be made without departing from the spirit and scope of the invention. In particular, it is within the scope of the invention to provide a memory device, such as a transmission medium, magnetic or optical tape or disc, or the like, for storing signals for controlling the operation of a computer according to the method of the invention and/or to structure its components in accordance with the system of the invention.
[0048] Accordingly, the scope of protection of this invention is limited only by the following claims and their equivalents.
Claims
We claim:
1. A method for porting a multi-threaded program to a job model in an application code stream, comprising the steps of:
globalizing data that needs to be shared between different jobs by storing data in a user space accessible to all of the jobs; and
wrappering job commands to appear as thread functions by replacing thread controls by job controls, including replacing thread function calls by job commands.
2. The method of claim 1, comprising the further steps of:
operating a first job as a listening thread to detect a request from a client; and
upon said listening thread detecting said request, passing said request to a second job wrappered as a worker thread that is waiting in a thread pool of initialized threads.
3. The method of claim 2, comprising the further steps executed during initialization of a server of:
setting the boundaries of thread jobs available to said server;
selecting an available thread job as a listener thread job to listen on a port for said request; and
operating said listener thread job responsive to receiving said request to select and initialize an available thread job from said pool as a worker thread job.
4. The method of claim 3, further comprising the step of:
operating said worker thread job to process said request and respond to said client.
5. The method of claim 1, said globalizing step further comprising the steps of:
operating a first thread job to establish a first variable,
storing said first variable in global variable space; and
operating a second thread job to access said first variable.
6. The method of claim 5, said wrappering step further comprising the steps of:
storing job characteristics for a job identified by a job identifier;
responsive to a create thread command including a thread identifier in said application code stream, mapping said thread identifier to a corresponding job identifier; and
placing in said application code stream code derived from said job characteristics accessed by said thread identifier.
7. The method of claim 1, further comprising the steps of:
spawning a plurality of jobs as worker jobs in a pool of available worker jobs;
spawning at least one job as a listener job for listening for requests from a client; and
establishing a global data area for sharing global data across said jobs.
8. The method of claim 7, further comprising the steps of:
storing in said global data area global variables defined by said listener job;
operating said listener job to dispatch work to said worker job; and
operating said worker job to access said global variables.
9. The method of claim 8, further comprising the steps of:
providing an external jobstruct( ) structure including a thread pool variable providing a list of worker jobs available for dispatching;
providing a table structure for holding characteristics unique to each job in said pool;
said characteristics including which worker jobs are active and for each active worker job the listener job which activated it; and
operating said worker job to access said table structure for variables initialized by said listener job.
10. The method of claim 7, said spawning step being implemented as a closed spawn.
11. A system for porting a multi-threaded program to a job model, comprising:
means for globalizing data that needs to be shared between different jobs by storing data in a user space accessible to all of the jobs; and
means for replacing thread controls by job controls, including replacing thread function calls by job commands wrappered to appear as functions.
12. A program storage device readable by a machine, tangibly embodying a program of instructions executable by a machine to perform method steps for porting a multi-threaded program to a job model, said method steps comprising:
globalizing data that needs to be shared between different jobs by storing data in a user space accessible to all of the jobs; and
replacing thread controls by job controls, including replacing thread function calls by job commands wrappered to appear as functions.
13. An article of manufacture comprising:
a computer useable medium having computer readable program code means embodied therein for porting a multi-threaded program to a job model, the computer readable program means in said article of manufacture comprising:
computer readable program code means for causing a computer to effect globalizing data that needs to be shared between different jobs by storing data in a user space accessible to all of the jobs; and
computer readable program code means for causing a computer to effect replacing thread controls by job controls, including replacing thread function calls by job commands wrappered to appear as functions.
Patent Citations (6)
Non-Patent Literature (6)
- Iliev, R. et al. Discrete event simulation of distributed memory parallel computers at concurrent threads' level, Parallel Computing: State-of-the-Art Perspectives, 1996, pp. 637-640, INSM abstract No. 5614067.Applicant
- Sande, B.I. Entity-life modeling in a distributed environment, Proceedings of the 4th International Workshop on Parallel and Distributed Real-Time Systems, pp. 35-41, IEEE Compt. Soc. Press, 1996, INSM abstract No. 5437415.Applicant
- Vellejo, F. et al. Shared memory multimicroprocessor operating system with an extended Petri net model, IEEE Transactions on Parallel and Distributed Systems, Jul. 1994, pp.749-762, INSM abstract No. 4724275.Applicant
- Roos, J.F. et al. Execution replay of parallel programs, Proc. Eoromicro Workshop on Parallel and Distributed Processing, 1993, pp. 429-434, INSM abstract No. 4658032.Applicant
- Placide, P. et al. A consistent global state algorithm to debug distributed object-oriented applications, 2nd Int'l Workshop on Automated and Algorithmic Debugging, 1995, pp. 2330247, INSM abstract No. 5093601.Applicant
- Portable Applications Standards Comm. of the IEEE Computer Society, Information Technology—Portable Operating System Interface (POSIX)—part 1: System Application Program Interface (API), Rpt. No. ISO/IEC 9945-1:1996(E), Jul. 12, 1996, INSM abstract No. 5424125.Applicant