问题描述
我创建了以下程序,该程序需要在父进程与其子进程之间“打乒乓球”。
即使在处理它和编译时,我也注意到我的 PC 开始越来越努力地工作,但我并不认为这是一个问题。
最后,我发现自己的 Linux 崩溃了,创建了许多不同的进程,这些进程降低了 PC 的速度,还创建了大量临时/日志文件,导致我的磁盘有 200MB 的可用空间而不是 35GB。
我该如何解决这些问题?
但这并没有帮助。由于某种原因,我的磁盘空间仍然有 200MB 的可用空间和近 40GB 的 /var/log
目录。
我不想删除位于 /var/log
目录中的必要文件,因此我不会删除整个目录。
为什么我的程序甚至创建了这么多临时或日志文件?
谢谢。
/******************************** Header Files ********************************/
#define _GNU_SOURCE
#include <signal.h>
#include <stdio.h> /* printf */
#include <stdlib.h> /* exit */
#include <unistd.h> /* fork,getpid,sleep */
/**************************** Forward Declarations ****************************/
/* exits the program with the given 'ret status' if 'is_bad' is true */
void ExitIfBad(int is_bad,char *msg_to_print,int ret_stat);
void handle_siguser1(int signal_id);
void handle_siguser2(int signal_id);
enum {CHILD = 0};
static int is_parent_turn = 1;
/************************* Functions Implementations *************************/
int main()
{
/* create a child process */
pid_t pid = fork();
ExitIfBad(pid < 0,"Error: Could not create a process\n",1);
signal(SIGUSR1,handle_siguser1);
signal(SIGUSR2,handle_siguser2);
/* returned to the newly created child process. */
if (CHILD == pid)
{
while (1)
{
/* pause child process and let parent to begin */
sleep(2);
kill(getppid(),SIGUSR2);
}
}
/* returned to parent */
else
{
while (1)
{
kill(getpid(),SIGUSR1);
}
}
return 0;
}
/******************************************************************************/
void handle_siguser1(int signum)
{
if (is_parent_turn)
{
printf("\n\nPING\n\n");
is_parent_turn = 0;
}
else
{
sleep(2);
}
}
/******************************************************************************/
void handle_siguser2(int signum)
{
if (!is_parent_turn)
{
printf("\n\nPONG\n\n");
is_parent_turn = 1;
}
else
{
sleep(2);
}
}
/******************************************************************************/
void ExitIfBad(int is_bad,int ret_stat)
{
if (is_bad)
{
puts(msg_to_print);
exit(ret_stat);
}
}
/******************************************************************************/
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)