📄 smpfork.c
字号:
/* $Id: smpfork.c,v 1.3 2003/09/02 11:44:16 pefo Exp $ *//* * Copyright (c) 2002 Opsycon AB (www.opsycon.se) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Opsycon AB, Sweden. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * *//* * This is a simple program that demonstrate the SMP fork * functionality for the purpose of starting up additional CPUs. * The semaphore locking around the cpu1done variable access * is not really necessary since only one thread modifies it. * Instead it's there to show how access to shared variables * can be protected withing the realm of PMON2000. Once CPU 1 * has been scheduled the client program can take full control * of the system. */typedef long long off_t;struct callvectors { int (*open) (char *, int, int); int (*close) (int); int (*read) (int, void *, int); int (*write) (int, void *, int); off_t (*lseek) (int, off_t, int); int (*printf) (const char *, ...); void (*cacheflush) (void); char *(*gets) (char *); int *(*smpfork) (int, char *); int *(*semlock) (int); int *(*semunlock) (int);};struct callvectors *callvec;#define printf (*callvec->printf)#define gets (*callvec->gets)#define smpfork (*callvec->smpfork)#define semlock (*callvec->semlock)#define semunlock (*callvec->semunlock)volatile int cpu1done;char str[256];intmain(int argc, char **argv, char **env, struct callvectors *cv){ /* Note, declarations here are private */ int i; callvec = cv; printf("\n\nSimple SMP FORK demo program!\n\n"); printf("\nCPU 0 is running\n"); printf("CPU 0 schedules CPU 1\n"); /* * Fork off CPU 1. Arg 1 is stack copy size, Arg 2 is * the location for the new stack used by CPU 1. */ if (smpfork(1024, (char *)((long)&argc & ~7) - 16384) == 0) { /* * This code is executed by CPU 1 since * the smpfork() call returns 0 for the * new CPU and cpu number for the caller. */ printf("This is CPU 1 reading input! "); printf("Type something: "); gets(str); while(!semlock(0)); printf("Telling CPU 0 that input is done\n"); cpu1done = 1; semunlock(0); printf("CPU 1 is done. Bye!\n"); } else { /* * This code is executed by CPU 0. */ printf("This is CPU 0 waiting for CPU 1!\n"); do { i = 0; if(semlock(0)) { i = cpu1done; semunlock(0); } } while (!i); printf("CPU 0 recognized that CPU 1 is done.\n"); printf("You typed '%s' which was read in by CPU 1\n", str); printf("CPU 0 is done. Bye!\n"); } return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -