Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Practical Case Study C Operating Systems Programming – 300698 1 Introduction In this case study you will implement a simple logging service built on top of amessage queue. 2 Specification The task is...

1 answer below »

Practical Case Study C
Operating Systems Programming – 300698
1 Introduction
In this case study you will implement a simple logging service built on top of amessage queue.
2 Specification
The task is
oken into two parts, a message logging server, and a li
ary to log messages.
The message server listens to a message queue and extracts the log messages from it. The
li
ary provides a more convenient way access the message queue.
You have been provided with a program to test that your li
ary communicates with you
server. You will need to review the lecture notes, and the documentation supplied in Section 5
to implement these programs.
2.1 Message Logging Serve
The message logging server should attempt to create the message queue, if this fails then
it should terminate with an e
or message, it should not run if the message queue actually
exists (IPC_EXCL will help here).
Once connected to the message queue, the program should sit in a loop, receiving a mes-
sage, and printing it to the stdout. Messages should be formatted:
id: message
where id is the type from the message structure and message is the message field.
The server should shutdown cleanly (i.e. delete the message queue) on receipt of a
SIGINT (generated by pressing control and C keys at the same time).
The sample code files logservice.h and logserver.c should form the basis of
your solution for this part. There are a number of comments in this file to help you structure
your code.
2.2 Messaging li
ary
The messaging li
ary consists of two functions, both defined in logservice.h:
int initLogService()
This function should initialise the message queue to log messages to, returning
an id if successful, and -1 on e
or.
This function should not attempt to create the message queue, only attach it to
the process.
1
int logMessage(int id, char *message)
This function logs the message passed as the string message to the log
service id. It should return 0 on success and -1 on e
or.
When sending a message, the function should encode the processes pid into
the type field of the message, and the string into the message field.
It is your choice what to do if the message is too long (i.e. longer than
MSGCHARS), sample behaviours include
eaking the message up into
smaller pieces or simply rejecting it. Whatever the choice, the documentation in
the header file should reflect this choice.
The sample code files logservice.h and logservice.c should form the basis of
your solution for this part.
3 Marking Scheme
Refer to the vUWS site for a marking ru
ic.
2
4 Sample Code
In addition to the sample code files, two additional files have been provided, a makefile
that contains build rules, and a server launch script.
The make utility simplifies the build process for large projects, introductory documen-
tation for make is included in the documentation section (Sec. 5). To use make to auto-
mate compile process simply type “make” at the terminal (in the same directory as the othe
files), it will use the rules defined in the makefile to build both the logserver and
logclient executables from the source files, and it will also ensure that the
launch server.sh script is executable. If none of the source files have changed since
the last build (based on their timestamps) the make utility will not rebuild the executables.
There should be no need to modify the makefile, its format is a bit fussy so it is safer to
download the file from vUWS than type it up.
The launch server.sh script will open the logserver program in a new termi-
nal window for you. This script detects the host operating system and performs an equivalent
action after this detection. There is nop need to understand how this file achieves its goal.
4.1 logservice.h
* logservice.h -- definitions for the log service *
#ifndef LOGSERVICE_H /* prevent multiple inclusion *
#define LOGSERVICE_H
#include #include #include #include #include #include #include * key for the queue *
#define KEY ftok("logservice.h", ’a’)
* message structure *
#define MSGCHARS 255
* MSGCHARS is the number of characters in the message!*
struct message
{
long type;
char message[MSGCHARS+1]; /* allow it to be a string! *
};
* function prototypes *
int logServiceInit();
* initialises the log service client, returns a service id *
int logMessage(int serviceId, char* message);
* logs the message message to the log service serviceID *
#endif /* ifndef LOGSERVICE_H *
3
4.2 logservice.c
* logservice.c -- implementation of the log service *
#include "logservice.h"
int logServiceInit()
{
int id;
* TODO: connect to message queue here *
eturn id;
}
int logMessage(int serviceId,char *message)
{
int rv;
printf("The message is \"%s\"\n", message);
* TODO: Validate message length here *
* TODO: Send the message *
eturn rv;
}
4
4.3 logclient.c
* logclient.c -- implements a simple log service client *
#include "logservice.h"
int main(int argc,char **argv)
{
int id;
* Check if arguments are present on command line *
if (arg < 2)
{
fprintf(stde
, "Usage: %s message", argv[0]);
exit(1);
}
* connect to the log service *
if(-1 == (id = logServiceInit()))
{
pe
or("Connecting to queue");
exit(1);
}
* log the message supplied on the command line *
if(-1 == logMessage(id, argv[1]))
{
pe
or("Sending Message");
exit(1);
}
eturn 0;
}
5
4.4 logserver.c
* logserver.c -- implementation of the log server *
#include #include "logservice.h"
int queue_id; /* stores queue_id for use in signal handler *
void handler(int sig);
int main()
{
printf("Please make me useful!\n");
exit(0); /* delete this line once you start *
* TODO: initialise the message queue here *
* install signal handler to clean up queue
* do this after you have created the queue
* then you dont need to check if it is valid!
*
signal(SIGINT, handler)
while (1)
{
* TODO: receive a message *
* TODO: display the message *
}
eturn 0;
}
void handler(int sig)
{
* TODO: clean up the queue here *
exit(0);
}
6
4.5 makefile
# makefile -- rules to build OSP workshop C
# to use simply type "make"
# this will build the server and client and launcher script
# note, this is a configuration file for the MAKE utility
# do not try to run it directly
# if typing up the file, the indented lines need to be indented
# with TABS not spaces.
all: logserver logclient
chmod +x launch_server.sh
clean:
m -f *.o logserver logclient
logclient: logclient.o logservice.o
logservice.o: logservice.c logservice.h
logserver: logserver.o
logserver.o: logserver.c logservice.h
4.6 launch server.sh
#!
in
ash
### This script launches the logserver process in a new window.
### Magic is needed for OSX as I can’t rely on xterm being installed!
### Only works when logged in via the console, not Putty/SSH
### It is not necessary to understand this script!
if [ $(uname) == "Darwin" ]
then
osascript -e ’tell application "Terminal" to do script "cd ’$PWD’; \
./logserver; exit; "’
else
xterm -title "Log Server" -e ’./logserver; \
echo press enter to exit; read junk;’ &
fi
7
5 Supplementary Materials
The material on the following pages is an extract of the linux system documentation and may
prove useful in implementing this Workshop. These manual pages are taken from the Linux
man-pages Project available at:
http:
www.kernel.org/doc/man-pages/.
8
http:
www.kernel.org/doc/man-pages
SVIPC(7) Linux Programmer’s Manual SVIPC(7)
NAME
svipc − System V interprocess communication mechanisms
SYNOPSIS
# include # include # include # include # include DESCRIPTION
This manual page refers to the Linux implementation of the System V interprocess communication mecha-
nisms: message queues, semaphore sets, and shared memory segments. In the following, the word
esource means an instantiation of one among such mechanisms.
Resource Access Permissions
For each resource, the system uses a common structure of type struct ipc_perm to store information needed
in determining permissions to perform an ipc operation. The ipc_perm structure, defined by the
sys/ipc.h> system header file, includes the following members:
ushort cuid; /* creator user ID *
ushort cgid; /* creator group ID *
ushort uid; /* owner user ID *
ushort gid; /* owner group ID *
ushort mode; /*
w permissions *
The mode member of the ipc_perm structure defines, with its lower 9 bits, the access permissions to the
esource for a process executing an ipc system call. The permissions are interpreted as follows:
0400 Read by user.
0200 Write by user.
0040 Read by group.
0020 Write by group.
0004 Read by others.
0002 Write by others.
Bits 0100, 0010, and 0001 (the execute bits) are unused by the system. Furthermore, "write" effectively
means "alter" for a semaphore set.
The same system header file also defines the following symbolic constants:
IPC_CREAT Create entry if key doesn’t exist.
IPC_EXCL Fail if key exists.
IPC_NOWAIT E
or if request must wait.
IPC_PRIVATE Private key.
IPC_RMID Remove resource.
IPC_SET Set resource options.
IPC_STAT Get resource options.
Note that IPC_PRIVATE is a key_t type, while all the other symbolic constants are flag fields and can be
OR’ed into an int type variable.
Message Queues
A message queue is uniquely identified by a positive integer (its msqid) and has an associated data structure
of type struct msqid_ds, defined in , containing the following members:
Linux XXXXXXXXXX1
SVIPC(7) Linux Programmer’s Manual SVIPC(7)
struct ipc_perm msg_perm;
ushort msg_qnum; /* no of messages on queue *
ushort msg_qbytes; /* bytes max on a queue *
ushort msg_lspid; /* PID of last msgsnd() call *
ushort msg_lrpid; /* PID of last msgrcv() call *
time_t msg_stime; /* last msgsnd() time *
time_t msg_rtime; /* last msgrcv() time *
time_t msg_ctime; /* last change time *
msg_perm ipc_perm structure that specifies the access permissions on the message queue.
msg_qnum Number of messages cu
ently on the message queue.
msg_qbytes Maximum number of bytes of message text allowed on the message queue.
msg_lspid ID of the process that performed the last msgsnd() system call.
msg_lrpid ID of the process that performed the last msgrcv() system call.
msg_stime Time of the last msgsnd() system call.
msg_rtime Time of the last msgcv() system call.
msg_ctime Time of the last system call that changed a member of the msqid_ds structure.
Semaphore Sets
A semaphore set is uniquely identified by a positive integer (its semid) and has an associated data structure
of type struct semid_ds, defined in , containing the following members:
struct ipc_perm sem_perm;
time_t sem_otime; /* last operation time *
time_t sem_ctime; /* last change time *
ushort sem_nsems; /* count of sems in set *
sem_perm ipc_perm structure that specifies the access permissions on the semaphore set.
sem_otime Time of last semop() system call.
sem_ctime Time of last semctl() system call that changed a member of the above structure or of one
semaphore belonging to the set.
sem_nsems Number of semaphores in the set. Each
Answered 1 days After Dec 16, 2021

Solution

Swapnil answered on Dec 17 2021
116 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here