Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

University of Bridgeport Operating Systems CPE408/CS503 UNI !

a" #ssignment $% & C'at Program (sing IPC S(mmary Lab 2 is an exercise in interprocess communication using Message Queues mechanism found in almost all of UNIX systems such as Sun Solaris. The assignment should be written in or !! and compiled using either or !! compiler found on our UNIX systems in the laboratories. "dditionally# $nowledge of curses %or &NU ncurses' and(or X windows()otif programming may be re*uired for user interface de+elopment. Introd()tion In this assignment you are to de+elop a chat program between two users# similar to the one already a+ailable on the UNIX system called talk. ,hen two instances of your program are being run simultaneously on the system# a user typing in one instance will be able to send the message typed to the user running the other instance of the program. User interface can be a +ery simple one by using printf() and scanf() functions of the programming language %cout# cin in !!'. I- mechanism is going to be de+eloped using Message Queues I- mechanism a+ailable in Sun Solaris operating system installed on our systems. .bi+ously# you should design your code so that more than two instances can run on the same system. Important* /ou should design your code so that when the user is waiting to type a message# at the same time messages are being retrie+ed from the *ueue if they arri+e in it. This re*uires multi0threaded program# which is achi+ed in UNIX by a fork() function followed by a if0else statement to determine which copy should do what. Spe)ifi)s I- Introduction and naming Under UNIX operating system# apart from pipes and signals as primiti+e type of I- # from System 1 release new techni*ues were added2 message *ueues# semaphores and shared memory. /ou can in+estigate the status of I- facilities on the system by entering ipcs b on the command prompt. I- )essage *ueues will exist e+en after the process creating it terminates# thus it should be manually remo+ed with the ipcrm q <ID> command# since I- resources on a system are limited. I- system calls related to the )essage 3ueues that is the mechanism of choice for the I- are2 msgget for allocating an I- or gaining access to an existing *ueue. msgctl to control an I- # modify its status and(or remo+e it. Lastly# for sending and recei+ing messages msgsnd and msgrcv are used. .nce an I- facility is created using the msgget function# an I- identifier is returned by the system. The user creating the I- facility identified by the returned identifier should

specify the access permissions. 4eader 5sys(ipc.h6 defines the I- permissions structure called2 ipc_perm. "lso# <s s!t pes"#> needs to be included in e+ery program before the sys(ipc.h is included. The uni*ue system0wide identifier identifies message *ueues. To create such an identifier of type ke _t defined in the <s s!ipc"#> file# ftok() function should be used. If fto$%' is called with the same arguments from two different processes# the same $ey would be produced# thus two different processes would be able to access the same I- facility. 7or example2
key_t key; key = ftok(., 123);

7irst argument is the reference to an existing accessible file %in UNIX# file 8.9 : a reference to the current directory is always a+ailable and accessible'. Second argument is some integer# referred to as an id. There are also constants to be passed as an I;2 for example I- <-=I1"T> that assures that returned $ey is always uni*ue. reating )essage 3ueue " message *ueue is created using the msgget system call. If successful# a non0negati+e integer is returned which is a message *ueue identifier that can be used in subse*uent calls that reference it. 7irst parameter is the $ey returned by fto$%'# and the second parameter is the access permission that is of the same format as the c#mod command parameter. -assing ?@@@ would gi+e read and write permissions to e+eryone on the system. To +erify proper access permissions are made# once the *ueue is created# you can obser+e set permissions with the ipcs b command described at the beginning of this section. 7urthermore# use the I- < =>"T flag .=ed with the ?@@@ to assure that the *ueue is created if it does not exist or if it already exist# an identifier to it is simply returned. 7or example2
mq_handle = msgget(key, IPC_C !"# $ %&&&); 'f(mq_handle(=)1) * +,'ntf(-!,,o, .,eat'ng message q/e/e0n-); +e,,o,(-12 3am+le-); 45 +,'nt the e,,o, 54 ,et/,n(1); 6

.btaining information about the message *ueue Using the msgctl function and the I- <ST"T flag passed to it# information about the *ueue# its owner# number of bytes on the *ueue# number of messages# etcAcan be obtained. "n include file <s s!msg"#> needs also to be included in the program. 7or example2
st,/.t msq'd_ds 5'nfo; 'nt ,et; 7 ,et = msg.tl(mq_handle, IPC_3#"#, 'nfo); 'f(,et==)1) * +,'ntf(-8a'led to o9ta'n message q/e/e 'nfo,mat'on0n-); +e,,o,(-12 3am+le-);e:'t(1); 6

+,'ntf(-; of messages on q/e/e< =d0n-, 'nfo)>msg_qn/m);

ommunicating through )essage 3ueue " message is defined in the struct msgbuf in the include file 5sys(msg.h6. 7irst field of the structure# mt pe$ is an user0defined positi+e integer id# agreed between the programs using the message *ueue to selecti+ely retrie+e the messages from it. Suppose# B programs %instances of the chat program' are using the same message *ueue# in that case the following should be done to assure proper bi0directional communication2 Instan)e + Instan)e % Instan)e 3 Instan)e 4 7or example2 Instance C will use mtypeEC2 to send a message to the Instance 2 and also Instance C will retrie+e messages that ha+e mtypeE2C. Instance 2 will use mtypeEC2 to retrie+e messages from the *ueue from the Instance C and it will use mtypeE2C to send a message to the Instance C. Thus# we assure that there is a bi0directional communication on the same channel %same message *ueue' that is shared by many instances of the same chat program and messages are not deli+ered to the wrong instance. msgsend%' msgsend%' ta$es four parameters and it returns ? on success and :C on failure setting the errno global +ariable to a pre0specified number that can be examined to retrie+e the error %or by using perror%' function to get a meaningful message'. 7irst parameter is the identifier of the message *ueue. Second parameter is the pointer to the actual message. Third parameter is the siFe of the message obtained by using the si%eof operator# and the last parameter is the flags. I- <N.,"IT is one flag# which causes a non0bloc$ing send %send would bloc$ if the *ueue was full' or a Fero can be passed if no flags are needed'. msgrc+%' msgrc+%' ta$es fi+e parameters. 7irst one is the *ueue identifier. Second one is the pointer to where the recei+ed message will be placed %recei+erGs end structure should ha+e the first member as ulong to assure message type can be correctly written'. Third one is the maximum siFe of the message that can be recei+ed. 7ourth parameter specifies which messages should be recei+ed2 an identifier used in mtype at the senderGs side or ? to retrie+e all messages. 7ifth parameter is the flags. I- <N.,"IT is to create a no0 bloc$ing recei+e and )S&<N.>==.= is not to produce any error if the message recei+ed is bigger than the maximum siFe specified. If )S&<N.>==.= is not specified and message recei+ed is larger than the maximum specified siFe an error will occur. Instan)e + 00 2C DC BC Instan)e % C2 00 D2 B2 Instan)e 3 CD 2D 00 BD Instan)e 4 CB 2B DB 00

msgrc+%' returns :C on failure or number of bytes recei+ed if it is successful. .n error# it also sets errno global +ariable able to be retrie+ed using perror(). Bon(s Honus IC 0 &UI Instead of using a simple user interface# a much more featured one can be de+eloped using curses library# so that the program will loo$ similar to pine or pico$ since curses is a library to de+elop "NSI graphical user interfaces used by the terminals or telnet sessions. There are many resources# tutorials and examples of curses programming a+ailable on the Internet and by loo$ing at the man page of curses library" 7urther# one can de+elop a window0based &UI that runs on top of Sun Solaris ;> using )otif library. " full0featured integrated de+elopment en+ironment with tools for creating &UI using 8point J clic$9 is a+ailable on our systems as well. Its name is Sun ,or$shop and can be run from the command prompt by typing2 s&s : you need to be sitting at the UNIX wor$station to use and you can not use it from the telnet session unless you ha+e a X ,indows client installed on your personal computer. Note2 curses is installed on our system. " +ery similar library# called ncurses that is free and distributed under &NU licence is also +ery popular. Tutorials and documentation for both are interchangeable apart from some small differences. ,eso(r)es 7or this assignment# similarly to the pre+ious one# you can consult US>N>T newsgroups# which are easily accessible by using http2((groups.google.com( website where you can find tons of articles# references and sample codes de+eloped by your peers# professionals# etc. /ou can refer to the SBDK : JUNIX -rograming course web site at2 http2((www.bridgeport.edu(sed(courses(csBDK( " nice web0site which is a collection of lin$s to other UNIX programming web sites is2 8;a+inLs collection of UNIX programming lin$s9 http2((www.cs.buffalo.edu(Mmilun(unix.programming.html Lastly# you can also refer to the textboo$ for this assignment# which was gi+en in the syllabus2 Nohn Shapley &ray# Interprocess 'ommunications in ()I*$ Second >dition# -rentice 4all -T=# COO@. ISHN2 ?0CD0POOKO20D and UNI man(a- pages# referred to as man. Note2 Interprocess ommunications boo$ by Nohn Shapley &ray should be on reser+e in the library. 7urther# man pages are +ery extensi+e regarding the system programming which is the type of the program de+eloped in this lab exercise# thus it is highly recommended to go o+er them.

Samp-e )ode
(Q ->B?P(K?D 0 UNIX "ssignment I2 Q( (Q Uni+ersity of Hridgeport (Q Sample ode Q( (Q % ' 7ran NarnRa$ Q( Iinclude 5stdio.h6 (Q 7or printf# scanf# etc...Q( Iinclude 5sys(types.h6 Iinclude 5sys(ipc.h6 Iinclude 5sys(msg.h6 Iinclude 5unistd.h6 (Q 7or getpid%' function Q( Iinclude 5string.h6 (Q 7or strcpy%' function Q( Idefine )<SIS> C?? (Q SiFe of the )essage Q(

(Q ;efining our own message# up to )<SIS> charLs in length Q( typedef struct T ulong msg<toU ulong msg<fromU char msg<contentV)<SIS>WU X )>SS"&>U int main%' T $ey<t $eyU int m*<handleU struct ms*id<ds QinfoU int retU int mypid E getpid%'U (Q To uni*uely identify oursel+es Q( )>SS"&> msg# ret<msgU (Q msg 0 messge to send. ret<msg 0 to retrie+e message (Q reate Yey Q( $ey E fto$%Z.Z# C2DB'U printf%ZYey generated E [?PX\nZ# $ey'U (Q reate message *ueue Q( m*<handle E msgget%$ey# I- < =>"T ] ?@@@'U (Q reate it if it doesnLt exist Q( if%m*<handle5E0C' T printf%Z>rror creating message *ueue\nZ'U perror%Z)3 SampleZ'U (Q print the error Q( return%C'U X printf%Z)essage 3ueue created\nZ'U (Q .btain information Q( (Q To demonstrate I- <ST"T flag Q( ret E msgctl%m*<handle# I- <ST"T# info'U if%retEE0C' T printf%Z7ailed to obtain message *ueue information\nZ'U perror%Z)3 SampleZ'U exit%C'U X

printf%Z)3 Information2\nZ'U printf%Z.wner UI;2 [d\nZ# info06msg<perm.uid'U printf%Z.wner &I;2 [d\nZ# info06msg<perm.gid'U printf%ZI of bytes on *ueue2 [d\nZ# info06msg<cbytes'U printf%ZI of messages on *ueue2 [d\nZ# info06msg<*num'U printf%ZI of bytes on *ueue2 [d\nZ# info06msg<*bytes'U (Q Send a message Q( msg.msg<to E mypidU (Q send it to oursel+es 0 loo$ at the msgrc+ call belowQ( msg.msg<from E mypidU memset%msg.msg<content# ?x?# )<SIS>'U (Q clear the msg buffer Q( strcpy%msg.msg<content# ZTesting the )3Z'U ret E msgsnd%m*<handle# Jmsg# siFeof%msg'# ?'U if%retEE0C' T perror%Z)3 Sample msgsendZ'U exit%C'U X printf%Z)essage sent\nZ'U (Q =etrie+e a message Q( ret E msgrc+%m*<handle# Jret<msg# siFeof%ret<msg'# mypid#?'U if%retEE0C' T perror%Z)3 Sample msgrc+Z'U exit%C'U X printf%Z)essage recei+ed2 [s\nZ# ret<msg.msg<content'U (Q ;elete message *ueue Q( msgctl%m*<handle# I- <=)I;# %struct ms*id<ds Q' ?'U printf%Z)essage 3ueue deleted\nZ'U return%?'U X

Sample code output


Yey generated E ;2@D@@;B )essage 3ueue created )3 Information2 .wner UI;2 @^K? .wner &I;2 KCB I of bytes on *ueue2 ? I of messages on *ueue2 ? I of bytes on *ueue2 B?O^ )essage sent )essage recei+ed2 Testing the )3 )essage 3ueue deleted

You might also like