再给你一个多进程的例子:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <time.h>
#define P_ALIVE 0
#define P_STOP 1
#define MAXTIMES 3
#define PROCNUM 3
#define TRUE 0
#define FALSE -1
struct processcfg{
int id; /* 编号 */
pid_t tPid; /* 进程id */
int tRunTimes; /* 运行次数 */
long tStartTime;
long tEndTime;
int tStatus;
};
struct processcfg *stProcess = NULL;
int ChildMain(struct processcfg *pstProcess)
{
while(1)
{
if(pstProcess->tRunTimes >= MAXTIMES)
{
time(&(pstProcess->tEndTime));
printf("child process <%d> stop\n", pstProcess->tPid);
exit(0);
}
pstProcess->tRunTimes ++;
printf("i am child <%d> run <%d> times\n",
pstProcess->tPid, pstProcess->tRunTimes);
sleep(1);
}
return TRUE;
}
int MakeChild(struct processcfg *pstProcess, int tId)
{
pid_t pid;
int status;
pid = fork();
if(pid < 0)
{
printf("\nfork child error\n");
return FALSE;
}
if(pid == 0)
{
pstProcess->id = tId;
pstProcess->tPid = getpid();
pstProcess->tRunTimes = 0;
pstProcess->tStatus = P_ALIVE;
setpgrp();
sleep(1);
ChildMain(pstProcess);
}
else
{
pstProcess->id = tId;
pstProcess->tPid = pid;
pstProcess->tRunTimes = 0;
pstProcess->tStatus = P_ALIVE;
time(&(pstProcess->tStartTime));
}
return pid;
}
/* 创建进程共享内存结构 */
void *initProcShm(int *id)
{
int i = 0, shmid;
FILE *fp = NULL;
key_t shmkey;
size_t shmsize;
struct processcfg *stProcess = NULL;
void *shmAddr;
char sFile[256];
memset(sFile, 0x00, sizeof(sFile));
strcpy(sFile, "PROCSHM");
if((fp = fopen(sFile, "a")) == NULL)
{
printf("\nopen file <%s> error\n", sFile);
return NULL;
}
fclose(fp);
shmkey = ftok("PROCSHM", 2);
shmsize = PROCNUM*sizeof(struct processcfg);
shmid = shmget(shmkey, shmsize, IPC_CREAT|0666);
if(shmid < 0)
{
printf("\nget share memory error\n");
return NULL;
}
shmAddr = (void *)shmat(shmid, (void *)0, 0);
if((long)shmAddr == -1)
{
printf("\nattach share memory error\n");
return NULL;
}
*id = shmid;
stProcess = (struct processcfg *)shmAddr;
for(i = 0; i < PROCNUM; i++)
{
memset(stProcess, 0x00, sizeof(struct processcfg));
stProcess->id = 0;
stProcess->tPid = 0;
stProcess->tRunTimes = 0;
stProcess->tStatus = P_ALIVE;
stProcess->tStartTime = 0;
stProcess->tEndTime = 0;
stProcess ++;
}
return shmAddr;
}
int main(int argc, char *argv[])
{
int i = 0, num = 0, shmid = 0;
pid_t pid = 0, cpid = 0;
struct processcfg *pstProcess = NULL;
pid = fork();
if(pid < 0)
{
printf("fork main err\n");
return FALSE;
}
if(pid == 0)
{
printf("main process begin to run\n");
return TRUE;
}
/* get process config share memory */
stProcess = (struct processcfg *)initProcShm(&shmid);
if(stProcess == NULL)
return FALSE;
num = 0;
pstProcess = stProcess;
for(i = 1; i <= PROCNUM; i++)
{
memset(pstProcess, 0x00, sizeof(struct processcfg));
if(MakeChild(pstProcess, num+1) < 0)
break;
num ++;
pstProcess ++;
}
printf("\n%d process created ...\n", num);
/* 检查是否所有子进程都退出 */
for(;;)
{
cpid = 0;
cpid = wait(NULL); /* wait a child process exit */
num = 0;
pstProcess = stProcess;
for(i = 0; i < PROCNUM; i++)
{
/* 当前退出的进程 */
if(pstProcess->tPid == cpid)
{
printf("%d %d %d\n",
pstProcess->tPid, pstProcess->tStartTime, pstProcess->tEndTime);
pstProcess->tStatus = P_STOP;
}
if(pstProcess->tStatus == P_ALIVE)
num ++;
pstProcess ++;
}
/* 所有子进程都结束,父进程退出 */
if(num == 0)
break;
}
/* detach share memory */
shmdt((char *)stProcess);
/* delete share memory */
shmctl(shmid, IPC_RMID, (struct shmid_ds *)0);
printf("\nparent process <%s %d> stop\n", argv[0], getpid());
return TRUE;
}
[[it] 本帖最后由 josen0205 于 2008-11-25 21:48 编辑 [/it]]