Hi,
I have a really confusing question. I call fprintf to send some output from my program to a file in my function customer()
This fprintf statements works fine, however, when i call the same command from a different function teller() i do not get any output into the log file.
This is my declarations for my functions, followed by the function Customer and the function Teller.
Code:
void teller(FILE *fin, FILE *fout, message_buf rbuf, int msqid, int i, int Td, int Tw);
void customer(FILE *fin, FILE *fout, int msqid, message_buf sbuf, size_t buf_length, int timer);
Calling of functions
Code:
if(i == 0) //using the parent process
{
customer(fptr2, fptr1, msqid, sbuf, buf_length, atoi(argv[1]));
exit(1);
}
else //uses the children processes
{
teller(fptr2, fptr1, rbuf, msqid,i, atoi(argv[5]), atoi(argv[6]));
exit(1);
}
customer function
Code:
void customer(FILE *fin, FILE *fout, int msqid, message_buf sbuf, size_t buf_length, int timer)
{
//declarations
int j;
time_t now;
char buff[MAX_LEN];
//read from customer_file, adding customers to queue every TIMER time
while (fgets(buff, MAX_LEN, fin) != NULL)
{
char custAction[MAX_LEN];
char custRef[MAX_LEN];
int i=0;
int x=0;
while((buff[i] != ' ') && (i <= MAX_LEN))
{ //read from buffer storing in custRef values up till
//a ' ' is found
custRef[i] = buff[i];
i++;
}
i++; //increment i past the space
while((buff[i] != ' ') && (i <=MAX_LEN) && (buff != '\0'))
{ //read the rest of buffer till next space or EOF
custAction[x] = buff[i];
i++;
x++;
}
//printf("%s\n", buff);
time(&now);
fprintf(fout,"--------------------------------------------------\n");
fprintf(fout,"Customer %s: %s\rArrival time: %s\r", custRef, custAction, ctime(&now));
//fprintf(fout,"--------------------------------------------------\n");
//Send the customer to the message queue
(void) strcpy(sbuf.mtext, buff);
buf_length = strlen(sbuf.mtext) + 1;
/*
* Send a message.
*/
if (msgsnd(0, &sbuf, buf_length, IPC_NOWAIT) < 0) {
printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buf_length);
perror("msgsnd");
exit(1);
}
else
{} //do nothing
memset(buff, 0, MAX_LEN); //reset buffer
memset(custRef, 0, MAX_LEN); //reset CustRef
sleep(timer); //sleep for timer time (specified in command line)
}
teller function
Code:
void teller(FILE *fin, FILE *fout, message_buf rbuf, int msqid, int i, int Td, int Tw)
{
int e;
char service[MAX_LEN];
char custNum[MAX_LEN];
char custAct[MAX_LEN];
time_t current;
time_t arrival;
int p=0;
int x=0;
while(1)
{
/*
* Receive an answer of message type 1.
*/
if (msgrcv(0, &rbuf, MSGSZ, 1, 0) < 0) {
perror("msgrcv");
exit(1);
}
/*
* Allow teller to service the request.
*/
strcpy(service, rbuf.mtext);
while((rbuf.mtext[p] != ' ') && (i <= MAX_LEN))
{ //read from buffer storing in custNum values up till
//a ' ' is found
custNum[p] = rbuf.mtext[p];
p++;
}
p++; //increment i past the space
while((rbuf.mtext[p] != ' ') && (p <=MAX_LEN) && (rbuf.mtext != '\0'))
{ //read the rest of buffer till next space or EOF
custAct[x] = rbuf.mtext[p];
p++;
x++;
}
fprintf(fout, "Customer statistics for Teller %d\n", getpid());
fprintf(fout, "Servicing customer %s - Action required %s\n", custNum, custAct);
fprintf(fout, "Customer statistics for Teller %d\n",getpid());
I'm totally confused on this. And I apologise for the length of the message.
Matt