【发布时间】:2015-10-19 05:08:16
【问题描述】:
我正在尝试获取程序每次运行的结果(父母和孩子)。结果在屏幕上打印一次,在文件中只打印一次。我似乎无法创建两个唯一文件(一个代表父级,一个代表子级)。我不确定 getpid() 是否是分离父母和孩子身份的有效方法。我可能做错了什么?
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static char *app1="/path/to/app1";
static char *app;
static pid_t forky=-1;
void otherfunction(){
int aflag=1;
//do something
if (aflag==1){
//run app 1
app=app1;
printf("Starting fork\n");
forky=fork();
}
}
int main(){
char dat[40000],test[10000];
sprintf(dat,"Exec start\nFORKY = %d\nPID = %d\nPPID = %d\n",forky,getpid(),getppid());
sprintf(test,"/TEST%d",getpid());
int h=open(test,O_WRONLY|O_CREAT);
write(1,dat,strlen(dat));
write(h,dat,strlen(dat));
close(h);
otherfunction();
return 0;
}
【问题讨论】:
标签: c file logging output fork