Hi,
I tried to post this to the Neutrino newsgroup, but it would not post
and there was no clear reason why ?? – Any ideas on the following:
I am working on getting 2 programs to talk to each other, and have
implemented time outs on the MsgSend and MsgReceive. I can get it all
to work except for STATE_REPLY blocking on the sender. It will time out
on MsgSend if I don’t receive, but if I receive and don’t reply, then
the sending program hangs in the Reply State and does not time out.
Source code follows: (sorry the indenting has gone all to %&$@#)
Makefile for 2 program that talk to each other
LDFLAGS= -lm
CFLAGS= -c -I. -g
CC= qcc
all::client
client: client.o
$(CC) -o client client.o $(LDFLAGS)
client.o: client.c
$(CC) client.c $(CFLAGS)
all:: server
server: server.o
$(CC) -o server server.o $(LDFLAGS)
server.o: server.c
$(CC) server.c $(CFLAGS)
//
// Client.c program that talks to a simple server!
//
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <sys/neutrino.h>
#include <sys/types.h>
#include <time.h>
#include <spawn.h>
#include <process.h>
#include <unistd.h>
int commchan, commcon;
#define TWOSECS (2.0e9) // 2 seconds
int main(int argc, char** argv)
{
int i;
struct sigevent event;
pid_t server_pid;
int message_id;
struct inheritance inherit;
_uint64 timeout = 1.0e6; // In Nanoseconds!
iov_t sndiov;
iov_t rcviov;
unsigned flags =
_NTO_CHF_FIXED_PRIORITY |
_NTO_CHF_SENDER_LEN |
_NTO_CHF_UNBLOCK ;
char* serv_args[2] = {“server”, };
serv_args[1] = NULL;
printf(“Hello, I am the Client\n”);
timeout = TWOSECS;
inherit.flags = 0;
server_pid = spawn(“server”, 0, NULL, &inherit, serv_args,
NULL);
if (server_pid != -1)
printf(“Server has been spawned %d\n”, server_pid);
else
{
perror("Server not spawned ");
exit(1);
}
// Give the server a moment to start running !
sleep(1);
commchan = 1;
commcon = ConnectAttach(0, server_pid, commchan, 0, flags);
if (commcon >= 0)
printf(“Connect Attach worked %d \n”, commcon);
else
{
perror("Connect Attach did not work: ");
if (kill(server_pid, SIGKILL) == -1)
perror(“Could not kill the server:”);
else
printf(“Killed the server\n”);
exit(1);
}
for (i = 0; i < 100; i++)
{
printf(“Hello from Client %d\n”, i);
// Tel the server a weird number.
message_id = i * 27;
event.sigev_notify = SIGEV_UNBLOCK;
TimerTimeout(CLOCK_REALTIME, _NTO_TIMEOUT_SEND |
_NTO_TIMEOUT_REPLY, &event,
&timeout, NULL);
SETIOV(&sndiov, &message_id, sizeof(int));
// if (MsgSend(commcon, (void )&message_id,
sizeof(int), (void)&timeis,
sizeof(float)) == -1)
if (MsgSendv(commcon, &sndiov, 1, NULL, 0) == -1)
{
if (errno != 260) // Timeout is “somewhat”
expected
{
perror("Client MsgSend failed: ");
break;
}
else
printf(“Client time out\n”);
}
sleep(1);
}
printf(“Client Exits\n”);
exit(0);
}
// Server.c program that replies to client!
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/neutrino.h>
#define TWOSECS (2.0e9) // 2 seconds in nanoseconds
int main(int argc, char** argv)
{
int i, rcvid;
struct sigevent event;
int msgcode;
int tickchan;
struct _msg_info info;
_uint64 timeout = 1.0e8; // In Nanoseconds! = 1 millisecond
unsigned flags =
_NTO_CHF_FIXED_PRIORITY |
_NTO_CHF_SENDER_LEN |
_NTO_CHF_UNBLOCK ;
timeout = TWOSECS;
printf(“Hello, I am the Server %d, I was created by %d\n”, getpid(),
getppid());
tickchan = ChannelCreate(flags);
printf(“Server channel created %d \n”, tickchan);
sleep(1); // Just to get (roughly) in time with the client
for (i = 0; i < 10; i++)
{
printf(“Hello from Server %d\n”, i);
/* Now process input if any */
event.sigev_notify = SIGEV_UNBLOCK;
TimerTimeout(CLOCK_REALTIME, _NTO_TIMEOUT_RECEIVE,
&event, &timeout, NULL);
if ((rcvid = MsgReceive(tickchan, (void *)&msgcode,
sizeof(msgcode), &info)) == -1)
{
if (errno != 260) // Timeout is (somewhat) expected
{
perror("Server MsgReceive failed: ");
}
else
printf(“Server time out\n”);
}
else
{
printf(“Message receive %d \n”, msgcode);
// Let’s see if we can trigger a STATE_REPLY timeout
if (i == 4)
printf(“Receiving message %d, no reply \n”, i);
else
MsgReply(rcvid, EOK, NULL, 0);
}
sleep(1);
}
sleep(8);
printf(“Server exits\n”);
exit(0);
}
\
Simon Wakley