gpt4 book ai didi

与 fork 过程混淆?

转载 作者:太空宇宙 更新时间:2023-11-04 00:03:15 25 4
gpt4 key购买 nike

我正在尝试执行第一个子函数,例如,打印出一个字符串:hello!,然后返回到父进程。然后,我为第二个子函数 fork 了另一个 n 个进程,它将计算共享内存数。我想最后只显示一个“全部完成”,但显示两个?感谢您的帮助!

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/wait.h>
#include <fcntl.h>

#define NUM_LINES 5

char * shm;

void child_func(char *shm)
{
while (atoi(shm) != NUM_LINES)
{
// P(sem);
printf("now reading word:%d\n", atoi(shm) );
*shm+=1;
// V(sem);
}

exit(0);
}

void parent_func(pid_t pid)
{
int status;
pid_t pid_wait;
do
{
pid_wait = waitpid(pid, &status, WNOHANG);
}while (pid_wait != pid);
// printf("Process %d done\n", getppid());
}

int main(int argc, char const *argv[])
{
/* declarations */
pid_t pid[8];
int ret, shmid, status,corpse, i, ave, n;
char fn[20];
key_t key = 123;
char *string_back;

/* create share memory */
if ((shmid = shmget(key, SHMSIZE, IPC_CREAT|666)) <0)
{
perror("shmget");
exit(1);
}
/* attach shm */
if ((shm = shmat(shmid, NULL, 0)) == (char*)-1)
{
perror("shmat");
exit(1);
}
/* init shm value */
*shm = '0';

/* input */
printf("please enter n:\n");
scanf("%d", &n);

/* section 1 */
pid[0] = fork();
if (pid[0] == 0) // child processes
{
printf("hello !\n");
}
else if (pid[0] >0)
{
parent_func(pid[0]);
}

/* section 2 */
for (i=0;i<n;i++)
{
pid[i] = fork();
if (pid[i] == 0) // child processes
{
child_func(shm);
}
else if (pid[i] >0)
{
parent_func(pid[i]);
}
}

printf("all done\n");
/* detach shm */
shmdt(shm);
/* destroy shm */
int retval = shmctl(shmid, IPC_RMID, NULL);
if (retval < 0) perror("remove shm");

return 0;
}

最佳答案

在第一部分添加 exit(0) 后,我最后只得到一个“全部完成”,这是我想要的答案。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/wait.h>
#include <fcntl.h>

#define NUM_LINES 5

char * shm;

void child_func(char *shm)
{
while (atoi(shm) != NUM_LINES)
{
// P(sem);
printf("now reading word:%d\n", atoi(shm) );
*shm+=1;
// V(sem);
}

exit(0);
}

void parent_func(pid_t pid)
{
int status;
pid_t pid_wait;
do
{
pid_wait = waitpid(pid, &status, WNOHANG);
}while (pid_wait != pid);
// printf("Process %d done\n", getppid());
}

int main(int argc, char const *argv[])
{
/* declarations */
pid_t pid[8];
int ret, shmid, status,corpse, i, ave, n;
char fn[20];
key_t key = 123;
char *string_back;

/* create share memory */
if ((shmid = shmget(key, SHMSIZE, IPC_CREAT|666)) <0)
{
perror("shmget");
exit(1);
}
/* attach shm */
if ((shm = shmat(shmid, NULL, 0)) == (char*)-1)
{
perror("shmat");
exit(1);
}
/* init shm value */
*shm = '0';

/* input */
printf("please enter n:\n");
scanf("%d", &n);

/* section 1 */
pid[0] = fork();
if (pid[0] == 0) // child processes
{
printf("hello !\n");
/* problem solved here */
exit(0);
}
else if (pid[0] >0)
{
parent_func(pid[0]);
}

/* section 2 */
for (i=0;i<n;i++)
{
pid[i] = fork();
if (pid[i] == 0) // child processes
{
child_func(shm);
}
else if (pid[i] >0)
{
parent_func(pid[i]);
}
}

printf("all done\n");
/* detach shm */
shmdt(shm);
/* destroy shm */
int retval = shmctl(shmid, IPC_RMID, NULL);
if (retval < 0) perror("remove shm");

return 0;
}

关于与 fork 过程混淆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34129038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com