我有一个程序,我正在编写一个全局定义的 File 变量,我试图在它派生一个孩子后由父级访问。但是,孩子是写入文件的那个,所以当我尝试作为父母阅读它时,我得到一个Error: No such file or directory
. 只有它不会作为错误抛出,它存储在 tmpFP 文件中。我不知道如何解决这个问题。
为了便于阅读,我省略了一些代码,对套接字的引用来自自定义库,假设有效。相关评论应该全部大写,它们指出了我认为问题出现的地方。
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
#include "Socket.h"
#include "ToUpper.h" /* definitions shared by client and server */
#define LINE_SIZE 50
#define MAX_WORDS 10
#define MAX_LINE 1024
#define MAX_TMP 100
#define MAX_ARGS 4 /* allows program name + 3 positional parameters */
#define MIN_ARGS 2 /* must have at least 1 positional parameter */
#define NUM_PARMS 4 /* allows program name + 2 parameters + NULL */
#define ERR_RETURN -1
/* variables to hold socket descriptors */
ServerSocket welcome_socket;
Socket connect_socket;
char new_line[MAX_LINE];
char tmp_name[MAX_TMP]; //DECLARING FILENAME
char id_str[MAX_TMP];
char arr[LINE_SIZE]={0};
int id;
FILE *fp; //FILE USED IN CHILD PROCESS
void toupper_service(void);
int main(int argc, char* argv[])
{
FILE *tmpFP; //FILE USED IN PARENT PROCESS
pid_t spid; /* pid_t is typedef for Linux process ID */
int c=0,index=0;
id = (int) getpid();
sprintf(id_str, "%d", id);
strcpy(tmp_name,"tmp");
strcat(tmp_name, id_str);
if (argc < 2)
{
printf("No port specified\n");
return (-1);
}
welcome_socket = ServerSocket_new(atoi(argv[1]));
connect_socket = ServerSocket_accept(welcome_socket);
Socket_close(welcome_socket);
while (c!=EOF)
{
while((c=Socket_getc(connect_socket))!='\n')
{
arr[index]=c;
index++;
}
arr[index]='\0';
/* accept an incoming client connection; blocks the
* process until a connection attempt by a client.
* creates a new data transfer socket.
*/
spid = fork(); /* create child == service process */
if (spid == -1)
{
perror("fork");
exit (-1);
}
if (spid == 0)
{/* code for the service process */
toupper_service();
Socket_close(connect_socket);
exit (0);
} /* end service process */
else /* daemon process closes its connect socket */
{
waitpid(spid,NULL,0);
//PASSES THIS TEST SOMEHOW
if((tmpFP = fopen (tmp_name, "r")) == NULL)
{
fprintf(stderr, "%s\n",tmp_name);
fprintf (stderr, "error opening tmp file\n");
exit (-1);
}
while((c=fgetc(tmpFP))!=EOF)
{
//PRINTS OUT ERROR HERE A LETTER AT A TIME
fprintf(stderr, "c: %d %c\n", c, (char)c);
Socket_putc(c, connect_socket);
}
remove(tmp_name);
Socket_putc('\0', connect_socket);
Socket_close(connect_socket);
/* reap a zombie every time through the loop, avoid blocking*/
}
}/* end of infinite loop for daemon process */
fprintf(stderr, "C: %d\n",c);
}
void toupper_service(void)
{
int i=0, c, pointer,num_words=0,index=0;
int too_many_words=0;
char *word[MAX_WORDS]={NULL};
//THIS IS THE OTHER PLACE TMP_NAME IS USED
fp = freopen(tmp_name, "w", stdout);
while ((c=arr[index])!='\0')
{
if(c==' '||c=='\t'||c=='\n') //word encountered
{
if(num_words>=MAX_WORDS-1)
{
printf("Too many commands passed\n");
too_many_words=1;
exit(0);
break;
}
arr[index]='\0';
word[num_words]=&arr[pointer];
pointer=i+1;
num_words++;
}
index++;
}
word[num_words]=NULL;
if(too_many_words==0)
{
c=0;
int error=execvp(word[0],word);
}
return;
}