每次写入特定的 FIFO 时,我都在编写一些代码来创建一个新文件夹:
int main (int argc, char* argv[]) {
if (argc != 2) {
printf("Usage: %s <directory>\n",argv[0]);
exit(0);
}
int ret = chdir(argv[1]);
if (ret < 0) {
printf("Could not chdir to %s\n",argv[1]);
perror("");
exit(0);
}
ret = mkfifo("create",0666);
if (ret < 0) {
perror("Could not make create fifo");
return 0;
}
int ctfd = open("create",O_RDWR);
if (ctfd < 0) {
perror("Could not open create");
return 0;
}
char create[1];
//socket counter
int count = 0;
char crdir[15];
for(;;ret = read(ctfd,create,1)) {
//printf("%s %d\n",create,count);
if (ret < 0) {
perror("Failed to read from create");
continue;
}
else if (ret < 1 || create[0] == '\n') {
continue;
}
sprintf(crdir,"%d",count);
ret = mkdir(crdir,0777);
if (ret < 0) {
printf("Failed to create dir");
perror("");
continue;
}
count++;
switch(fork()) {
case -1:
perror("Failed to fork");
break;
case 0:
printf("%d\n",count);
break;
default:
//char* argv[ ] = {"netclient",cte};
exit(0);
}
printf("test");
}
//execvp()
return 0;
}
但是,当运行时会发生这种情况:
arthur@dent:~/coding/netdir$ mkdir test
arthur@dent:~/coding/netdir$ ./netserver test &
[1] 2122
arthur@dent:~/coding/netdir$ echo 1 > test/create
[1]+ Done ./netserver test
1
我做错了什么?为什么它不继续循环并等待更多输入?FIFO是为读写而打开的,这样写停止时它就不会关闭,是不是不能正常工作?