File:  [OS/2 SDKs] / os2sdk / demos / examples / argument / argument.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 12:26:15 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: os2sdk-1988, HEAD
Microsoft OS/2 SDK 03-01-1988

/*
 * argument.c
 *
 * This program demonstrated a method of passing parameters to a thread.
 * Since Intel stacks grow down (High to low memory), and the PUSH instruction
 * decrements the pointer BEFORE writing to the stack, we do the same in C.
 *
 * The decrement before writing is important since the line:
 *
 *	threadStack += STACK_SIZE
 *
 * will place the pointer one element BEYOND the end of the stack.
 *
 * Also note that the stack is defined as a WORD array.  This is so the
 * decrement operator will adjust the stack by 2 with each push.
 *
 * One last caveat: what the thread expects upon the stack depends upon
 * how each argument is declared and the model that the program is being
 * compiled in.  KNOW YOUR C STACK STRUCTURE!
 *
 * Many of the C runtime library routines, including printf, are not
 * re-entrant.	DosEnterCritSec & DosExitCritSec guarantee serial access
 * to the screen.
 *
 * Created by Microsoft Corp. 1988
 */

#define INCL_DOSPROCESS

#include <os2def.h>
#include <bsedos.h>
#include <stdio.h>

#define STACK_SIZE 512	   /* stack size in WORDS (see malloc) */

main(ac, av)
char *av[];
int ac;
{
    void far thread();		/* address where thread gets control */
    PINT threadStack;		/* far pointer to thread stack (word)*/
    TID threadID;		/* thread ID */
    unsigned rc;		/* return code */

    /* allocate stack space for thread */
    /* since this is written in C, DosAllocSeg cannot be used here */

    if ((threadStack = (int *)malloc(sizeof(int) * STACK_SIZE)) == NULL) {
	printf("thread stack malloc failed\n");
	DosExit(1, 1);	/* terminate all threads and return error */
    }
    threadStack += STACK_SIZE;		/* since stack grows down */

    *--threadStack = 0xFFFF;		/* Arg #5 = FFFF */
    *--threadStack = 0xEEEE;		/* Arg #4 ...	 */
    *--threadStack = 0xDDDD;		/* Arg #3	 */
    *--threadStack = 0xCCCC;
    *--threadStack = 0xBBBB;

    if (rc = DosCreateThread(thread, &threadID,
	     (PBYTE)threadStack)) {
	printf("create of thread failed, error: %d\n", rc);
	DosExit(1, 1);
    }
    DosEnterCritSec();
    printf("Thread ID = %d\n", threadID);
    DosExitCritSec();

    DosExit(0, 0);		/* Terminate, Let the other thread run */
}


/*
 * Thread process expects 5 arguments on the stack (normal C calling
 * convention).
 */
void far
thread(a1,a2,a3,a4,a5)
int a1,a2,a3,a4,a5;
{
    DosEnterCritSec();
    printf("Argument 1 = %x\n", a1);
    DosExitCritSec();

    DosEnterCritSec();
    printf("Argument 2 = %x\n", a2);
    DosExitCritSec();

    DosEnterCritSec();
    printf("Argument 3 = %x\n", a3);
    DosExitCritSec();

    DosEnterCritSec();
    printf("Argument 4 = %x\n", a4);
    DosExitCritSec();

    DosEnterCritSec();
    printf("Argument 5 = %x\n", a5);
    DosExitCritSec();

    DosExit(0,0);
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.