THE AMERICAN UNIVERSITY IN CAIRO

THE COMPUTER SCIENCE DEPARTMENT

 

MAY 1999

 

 

USING UNIX IPC TO IMPLEMENT A SIMPLE MASTER-WORKER MODEL

 

Term Paper

 

By

 

Nzumbe Ntoko

[email protected]

 

 

Presented to

Dr. Amr EL KADI

Lecturer

 

For

DISTRIBUTED SYSTEMS

As a Partial Fulfillment for the Degree of

MASTER OF SCIENCE

 

Master Worker Model

Project Report

 

 

Abstract

 

Since the introduction of sockets in 1982 in the BSD system for the VAX and it's extension for use over the TCP/IP and XNS protocols for IPC by the Berkeley UNIX version 4.3 the sockets interface have become a standard for network computing in the UNIX environment. This paper looks into a simple master-worker model using UDP sockets.

 

 

 

 

 

 

 

 

INTRODUCTION

 

Sockets have been used in the UNIX environment to allow two or more processes to communicate with each other, whether they are present on the same computer or connected to the internet. The most commonly used sockets are the "stream sockets" and the "Datagram sockets". Today many applications depend on these interfaces. Datagram sockets or UDP sockets use a connectionless protocol and sends each data independent of either the preceding or the following packets. Datagram services do not attempt to maintain data order and do not guarantee data delivery. They are generally used for packet-by-packet transfer of information. Typical applications that use such protocols are tftp and bootp.

 

THEORY

Sockets

These are the basic components of interprocess communication. They provide access to the network transport protocols. They are an endpoint to communication to which a name can be bound. Sockets typically used are the Datagram and Stream sockets.

 

Datagram

The UDP protocol provides a mode of communication whereby applications send packets of data, called datagrams to one another. A datagram is an independent, self contained message sent over the network whose arrival, arrival time, and content are not guaranteed. Datagram sockets are created in the same way as stream sockets (using the SOCK_DGRAM flag). They can use the same calls as stream sockets to send and receive data , except for the accept() and listen() calls. The calls sendto() and recvfrom() are used to transfer data on datagram sockets.

Protocols

Transmission Control Protocol (TCP)

TCP provides reliable, full-duplex, byte stream-oriented service. It resides directly above IP (and adjacent to UDP), and uses acknowledgments with retransmissions to achieve reliability. TCP differs from the sliding window protocols we have studied so far in the following ways:


 

 

User Datagram Protocol

This User Datagram Protocol (UDP) is defined to make available a datagram mode of packet-switched computer communication in the environment of an interconnected set of computer networks. This protocol assumes that the Internet Protocol (IP) [1] is used as the underlying protocol.

This protocol provides a procedure for application programs to send messages to other programs with a minimum of protocol mechanism. The protocol is transaction oriented, and delivery and duplicate protection are not guaranteed. Applications requiring ordered reliable delivery of streams of data should use the Transmission Control Protocol (TCP) [2].

 

 

Format

                                    
                  
                 0      7 8     15 16    23 24    31  
                 +--------+--------+--------+--------+ 
                 |     Source      |   Destination   | 
                 |      Port       |      Port       | 
                 +--------+--------+--------+--------+ 
                 |                 |                 | 
                 |     Length      |    Checksum     | 
                 +--------+--------+--------+--------+ 
                 |                                     
                 |          data octets ...            
                 +---------------- ...                 

                      User Datagram Header Format

Fields

Source Port is an optional field, when meaningful, it indicates the port of the sending process, and may be assumed to be the port to which a reply should be addressed in the absence of any other information. If not used, a value of zero is inserted.

Destination Port has a meaning within the context of a particular internet destination address.

Length is the length in octets of this user datagram including this header and the data. (This means the minimum value of the length is eight.)

Checksum is the 16-bit one's complement of the one's complement sum of a pseudo header of information from the IP header, the UDP header, and the data, padded with zero octets at the end (if necessary) to make a multiple of two octets.

The pseudo header conceptually prefixed to the UDP header contains the source address, the destination address, the protocol, and the UDP length. This information gives protection against misrouted datagrams. This checksum procedure is the same as is used in TCP.

                 
                 0      7 8     15 16    23 24    31 
                 +--------+--------+--------+--------+
                 |          source address           |
                 +--------+--------+--------+--------+
                 |        destination address        |
                 +--------+--------+--------+--------+
                 |  zero  |protocol|   UDP length    |
                 +--------+--------+--------+--------+

If the computed checksum is zero, it is transmitted as all ones (the equivalent in one's complement arithmetic). An all zero transmitted checksum value means that the transmitter generated no checksum (for debugging or for higher level protocols that don't care).

User Interface

A user interface should allow

IP Interface

The UDP module must be able to determine the source and destination internet addresses and the protocol field from the internet header. One possible UDP/IP interface would return the whole internet datagram including all of the internet header in response to a receive operation. Such an interface would also allow the UDP to pass a full internet datagram complete with header to the IP to send. The IP would verify certain fields for consistency and compute the internet header checksum.

Protocol Application

The major uses of this protocol is the Internet Name Server [3], and the Trivial File Transfer [4].

Protocol Number

This is protocol 17 (21 octal) when used in the Internet Protocol. Other protocol numbers are listed in [5].

 

Port Addresses

 

 

MASTER/WORKER MODEL


The client: requests an operation or service that some other application, the server, provides.

The server: receives a client request, performs the requested service and returns to the client any results.


Advantages:

It offers simplicity in closely matching the flow of data with the control flow. It promotes modular, flexible, and extensive system designs. Data resources and computing services can be organized, integrated and used as a service. Widely used in distributed systems.

Communication:

Servers communicate over a network. In Unix and Windows machines the network uses: TCP/IP & UDP.

 

 

 

 

 

 

SUBJECT ANALYSIS

 

The aim of the project is to construct a simple master worker model using UNIX IPC. Data will be sent through packets utilizing UDP sockets. The project aims at parallel computations of the factorials of numbers sent to the workers by the master. After which the workers send the result back to the master who then sums up the results sent to him and gives the answer.

 

 

 

THE MASTER CODE

#include < stdio.h> #include < stdlib.h> #include < errno.h> #include < string.h> #include < sys/types.h> #include < netinet/in.h> #include < netdb.h> #include < sys/socket.h> #include < sys/wait.h> #include < sys/time.h> #include < unistd.h> #define MYPORT 4950 /* the port users will be sending to*/ #define MAXBUFLEN 100 int main(int argc, char *argv[]) { int sockfd, addr_len,val1,val2,val3,sum[4]; struct sockaddr_in addr1,addr2,addr3; /*connector's address information */ struct hostent *he1, *he2,*he3; int numbytes; char buf[MAXBUFLEN],buf1[MAXBUFLEN],buf2[MAXBUFLEN]; printf("\n");printf("\n"); printf("*****Starting the Master-worker model program******\n"); if (argc != 7) { fprintf(stderr,"usage: master host1 host2 host3 msg1 msg2 msg3\n"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0))== -1) { /*get socket*/ perror("socket"); /*descriptor*/ exit(1); } /*Getting worker's IP and Address and sending them Data */ printf("My Group is composed of: \n"); if ((he1=gethostbyname(argv[1])) == NULL) { /* get host1 info */ perror("gethostbyname"); exit(1); } printf("\n"); printf("Host name : %s\n", he1->h_name); printf("IP Address: %s\n",inet_ntoa(*((struct in_addr *)he1->h_addr))); addr1.sin_family = AF_INET; /* host byte order */ addr1.sin_port = htons(MYPORT); /* short, network byte order */ addr1.sin_addr = *((struct in_addr *)he1->h_addr);/*host address*/ bzero(&(addr1.sin_zero), 8); /* zero the rest of the struct */ if ((numbytes=sendto(sockfd, argv[4], strlen(argv[4]), 0, \ (struct sockaddr *)&addr1, sizeof(struct sockaddr))) == -1) { perror("sendto"); exit(1); } if ((he2=gethostbyname(argv[2])) == NULL) { /* get host2 info */ perror("gethostbyname"); exit(1); } printf("Host name : %s\n", he2->h_name); printf("IP Address: %s\n",inet_ntoa(*((struct in_addr *)he2->h_addr))); addr2.sin_family = AF_INET; /* host byte order */ addr2.sin_port = htons(MYPORT); /* short, network byte order */ addr2.sin_addr = *((struct in_addr*)he2->h_addr);/*host address*/ bzero(&(addr2.sin_zero), 8); /* zero the rest of the struct */ if ((numbytes=sendto(sockfd, argv[5],strlen(argv[5]), 0, \ (struct sockaddr *)&addr2, sizeof(struct sockaddr))) == -1) { perror("sendto"); exit(1); } if ((he3=gethostbyname(argv[3])) == NULL) { /* get host3 info */ perror("gethostbyname"); exit(1); } printf("Host name : %s\n", he3->h_name); printf("IP Address: %s\n",inet_ntoa(*((struct in_addr *)he3->h_addr))); addr3.sin_family = AF_INET; /* host byte order */ addr3.sin_port = htons(MYPORT); /* short, network byte order */ addr3.sin_addr = *((struct in_addr*)he3->h_addr);/*host address*/ bzero(&(addr3.sin_zero), 8); /* zero the rest of the struct */ if ((numbytes=sendto(sockfd, argv[6],strlen(argv[6]), 0, \ (struct sockaddr *)&addr3, sizeof(struct sockaddr))) == -1) { perror("sendto"); exit(1); } printf("\n"); printf("sent %d bytes to %s\n",numbytes,inet_ntoa(addr1.sin_addr)); printf("Package contains %d\n",atoi(argv[4])); printf("sent %d bytes to %s\n",numbytes,inet_ntoa(addr2.sin_addr)); printf("Package contains %d\n",atoi(argv[5])); printf("sent %d bytes to %s\n",numbytes,inet_ntoa(addr3.sin_addr)); printf("Package contains %d\n",atoi(argv[6])); printf("\n");printf("\n"); printf("**********Receiving data from workers**********\n"); addr_len = sizeof(struct sockaddr); if ((numbytes=recvfrom(sockfd, buf,MAXBUFLEN, 0, (struct sockaddr *)&addr1,&addr_len)) == -1) { perror("recvfrom"); exit(1); } val1 =atoi(buf);sum[0]=val1; printf("got packet from %s\n",inet_ntoa(addr1.sin_addr)); printf("packet is %d bytes long\n",numbytes); buf[numbytes] = '\0'; printf("packet contains \"%s\"\n",buf); if ((numbytes=recvfrom(sockfd, buf1, MAXBUFLEN, 0, (struct sockaddr *)&addr2,&addr_len)) == -1) { perror("recvfrom"); exit(1); } val2=atoi(buf1); sum[1]=val2; printf("got packet from %s\n",inet_ntoa(addr2.sin_addr)); printf("packet is %d bytes long\n",numbytes); buf[numbytes] = '\0'; printf("packet contains \"%s\"\n",buf1); if ((numbytes=recvfrom(sockfd, buf2, MAXBUFLEN, 0, (struct sockaddr *)&addr3,&addr_len)) == -1) { perror("recvfrom"); exit(1); } val3=atoi(buf2); sum[2]=val3; printf("got packet from %s\n",inet_ntoa(addr3.sin_addr)); printf("packet is %d bytes long\n",numbytes); buf[numbytes] = '\0'; printf("packet contains \"%s\"\n",buf2); sum[4] =sum[0]+sum[1]+sum[2]; printf("The sum of the parallel computation is %d\n",sum[4]); printf("**********Ending Master-WorkersModel**********\n"); printf("\n"); close(sockfd); return 0; }

THE WORKER CODE

#include < stdio.h> #include < stdlib.h> #include < errno.h> #include < string.h> #include < sys/types.h> #include < netinet/in.h> #include < sys/socket.h> #include < sys/wait.h> #define MYPORT 4950 /* the port users will be sending to */ #define MAXBUFLEN 100 FILE *valuedata; main() { int sockfd; int i,j; int count; struct sockaddr_in my_addr; /* my address information */ struct sockaddr_in their_addr; /* connector's address information */ int addr_len, numbytes; char buf[MAXBUFLEN], value[10]; if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0))== -1) { perror("socket"); exit(1); } my_addr.sin_family = AF_INET; /* host byte order */ my_addr.sin_port = htons(MYPORT); /*short, network byte order */ my_addr.sin_addr.s_addr = INADDR_ANY; /*auto-fill with my IP */ bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */ if (bind(sockfd, (struct sockaddr *)&my_addr,sizeof(struct sockaddr)) \== -1) { perror("bind"); exit(1); } addr_len = sizeof(struct sockaddr); if (getsockname(sockfd,(struct sockaddr *)&my_addr,&addr_len) == -1){ perror("Getting socket name");printf("Error:Getting socket name"); exit(1); } printf("Socket port# %d\n",ntohs(my_addr.sin_port)); if ((numbytes=recvfrom(sockfd, buf,MAXBUFLEN, 0, (struct sockaddr *)&their_addr,&addr_len)) ==-1) { perror("recvfrom"); exit(1); } printf("got packet from%s\n",inet_ntoa(their_addr.sin_addr)); printf("packet is %d bytes long\n",numbytes); buf[numbytes] = '\0'; printf("packet contains \"%s\"\n",buf); i=atoi(buf); j=1; for(count = i; count > 1; count--) j*=count; /* convert the answer to string format so as to send it as an argument in sendto */ valuedata=fopen("listen.dat","a"); fprintf(valuedata,"%d",j); fclose(valuedata); valuedata=fopen("listen.dat","r"); fscanf(valuedata,"%s",value); fclose(valuedata); printf("result of factorial of %d is %d\n",atoi(buf),j); printf("the value read is %s\n",value); /*Send the result back to the master*/ if ((numbytes=sendto(sockfd,value,strlen(value), 0, \ (struct sockaddr *)&their_addr,sizeof(struct sockaddr))) == -1) { perror("sendto"); exit(1); } printf("\n"); printf("sent %d bytes to%s\n",numbytes,inet_ntoa(their_addr.sin_addr)); close(sockfd); }

 

 

 


References

[1]

Postel, J., "Internet Protocol," RFC 760, USC/Information Sciences Institute, January 1980.

[2]

Postel, J., "Transmission Control Protocol," RFC 761, USC/Information Sciences Institute, January 1980.

[3]

Postel, J., "Internet Name Server," USC/Information Sciences Institute, IEN 116, August 1979.

[4]

Sollins, K., "The TFTP Protocol," Massachusetts Institute of Technology, IEN 133, January 1980.

[5]

Postel, J., "Assigned Numbers," USC/Information Sciences Institute, RFC 762, January 1980.

 

Internet sources

http://www.estc.csuchico.edu/~beej/guide.html

 

http://www.cs.panam.edu/~meng/Course/CS6345/Notes/chpt-6/

 

http://cie.bilkent.edu.tr/RFC/768/index.htm

 

Hosted by www.Geocities.ws

1