|
|
1.1 root 1: /*
2: * argument.c
3: *
4: * This program demonstrated a method of passing parameters to a thread.
5: * Since Intel stacks grow down (High to low memory), and the PUSH instruction
6: * decrements the pointer BEFORE writing to the stack, we do the same in C.
7: *
8: * The decrement before writing is important since the line:
9: *
10: * threadStack += STACK_SIZE
11: *
12: * will place the pointer one element BEYOND the end of the stack.
13: *
14: * Also note that the stack is defined as a WORD array. This is so the
15: * decrement operator will adjust the stack by 2 with each push.
16: *
17: * One last caveat: what the thread expects upon the stack depends upon
18: * how each argument is declared and the model that the program is being
19: * compiled in. KNOW YOUR C STACK STRUCTURE!
20: *
21: * Many of the C runtime library routines, including printf, are not
1.1.1.2 ! root 22: * re-entrant. DosEnterCritSec & DosExitCritSec guarantee serial access
1.1 root 23: * to the screen.
24: *
1.1.1.2 ! root 25: * Created by Microsoft Corp. 1988
1.1 root 26: */
1.1.1.2 ! root 27:
! 28: #define INCL_DOSPROCESS
! 29:
! 30: #include <os2def.h>
! 31: #include <bsedos.h>
1.1 root 32: #include <stdio.h>
33:
1.1.1.2 ! root 34: #define STACK_SIZE 512 /* stack size in WORDS (see malloc) */
1.1 root 35:
36: main(ac, av)
37: char *av[];
38: int ac;
39: {
40: void far thread(); /* address where thread gets control */
1.1.1.2 ! root 41: PINT threadStack; /* far pointer to thread stack (word)*/
! 42: TID threadID; /* thread ID */
1.1 root 43: unsigned rc; /* return code */
44:
45: /* allocate stack space for thread */
1.1.1.2 ! root 46: /* since this is written in C, DosAllocSeg cannot be used here */
1.1 root 47:
48: if ((threadStack = (int *)malloc(sizeof(int) * STACK_SIZE)) == NULL) {
49: printf("thread stack malloc failed\n");
1.1.1.2 ! root 50: DosExit(1, 1); /* terminate all threads and return error */
1.1 root 51: }
52: threadStack += STACK_SIZE; /* since stack grows down */
53:
54: *--threadStack = 0xFFFF; /* Arg #5 = FFFF */
55: *--threadStack = 0xEEEE; /* Arg #4 ... */
56: *--threadStack = 0xDDDD; /* Arg #3 */
57: *--threadStack = 0xCCCC;
58: *--threadStack = 0xBBBB;
59:
1.1.1.2 ! root 60: if (rc = DosCreateThread(thread, &threadID,
! 61: (PBYTE)threadStack)) {
1.1 root 62: printf("create of thread failed, error: %d\n", rc);
1.1.1.2 ! root 63: DosExit(1, 1);
1.1 root 64: }
1.1.1.2 ! root 65: DosEnterCritSec();
1.1 root 66: printf("Thread ID = %d\n", threadID);
1.1.1.2 ! root 67: DosExitCritSec();
1.1 root 68:
1.1.1.2 ! root 69: DosExit(0, 0); /* Terminate, Let the other thread run */
1.1 root 70: }
71:
72:
73: /*
74: * Thread process expects 5 arguments on the stack (normal C calling
75: * convention).
76: */
77: void far
78: thread(a1,a2,a3,a4,a5)
79: int a1,a2,a3,a4,a5;
80: {
1.1.1.2 ! root 81: DosEnterCritSec();
1.1 root 82: printf("Argument 1 = %x\n", a1);
1.1.1.2 ! root 83: DosExitCritSec();
1.1 root 84:
1.1.1.2 ! root 85: DosEnterCritSec();
1.1 root 86: printf("Argument 2 = %x\n", a2);
1.1.1.2 ! root 87: DosExitCritSec();
1.1 root 88:
1.1.1.2 ! root 89: DosEnterCritSec();
1.1 root 90: printf("Argument 3 = %x\n", a3);
1.1.1.2 ! root 91: DosExitCritSec();
1.1 root 92:
1.1.1.2 ! root 93: DosEnterCritSec();
1.1 root 94: printf("Argument 4 = %x\n", a4);
1.1.1.2 ! root 95: DosExitCritSec();
1.1 root 96:
1.1.1.2 ! root 97: DosEnterCritSec();
1.1 root 98: printf("Argument 5 = %x\n", a5);
1.1.1.2 ! root 99: DosExitCritSec();
1.1 root 100:
1.1.1.2 ! root 101: DosExit(0,0);
1.1 root 102: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.