1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > linux 进程创建 c语言 linux下进程创建的C语言程序

linux 进程创建 c语言 linux下进程创建的C语言程序

时间:2024-04-02 12:21:17

相关推荐

linux 进程创建 c语言 linux下进程创建的C语言程序

再给你一个多进程的例子:

#include

#include

#include

#include

#include

#define P_ALIVE0

#define P_STOP 1

#define MAXTIMES3

#define PROCNUM3

#define TRUE0

#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 stop\n", pstProcess->tPid);

exit(0);

}

pstProcess->tRunTimes ++;

printf("i am child run 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 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 stop\n", argv[0], getpid());

return TRUE;

}

[[it] 本帖最后由 josen0205 于 -11-25 21:48 编辑 [/it]]

只有想不到,没有做不到

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。