Introduction
:
#include
#include
#include
int getd()
{
char ch;
ch=malloc(sizeof(char)2);
int ans=0;
int tmp=1;
int i;
do{
gets(ch);
i=0;
while(ch[i]>=’0’ch[i]<=’9’)
{
ans=ans+tmp(ch[i]-‘0’);
tmp=tmp10;
i++;
}
}while(ch[i]!=’#’);
return(ans);
}
void putd(int tmp)
{
char ch;
ch=malloc(sizeof(char)2);
ch[1]=’\0’;
do{
ch[0]=’0’+tmp%10;
puts(ch);
tmp=tmp/10;
}while(tmp!=0);
puts(“#\n”);
}
int main()
{
int zips;
int n,m=0;
int i;
int min,max;
n=getd();
zips=malloc(sizeof(int)n);
for(i=0;i=min zips[i]<=max)
{
m++;
}
}
putd(m);
for(i=0;i=min zips[i]<=max)
{
putd(zips[i]);
}
}
return(0);
}
Requirement
CPS 536 Homework 3
Deadline: in class, at class time, Thursday, 16 March, 2017
You are to develop code in C that involves 3 processes of which one is designated the
server and the other two are identical clients. The overall goal is for the server to first
distribute some data to the clients and then to query each client on the data it has and
report the outcomes.
Here, “data” refers to a set of zipcodes.
Invocation
Your program will be invoked from the command line as follows:
$ server-program-name data-file-name
General description
Your program should involve a server process and two client processes. The server and
client are to be implemented with two independent C programs. The client processes
must execute the same executable code (as they are indistinguishable from each other, as
far as the work done is concerned); this is effected by the server by making each client
process “exec” (you may use execlp()) the same executable for the client code.
The data file has a list of distinct zipcodes (refer to the description appearing later for
actual format of input file) in some random order. The job of the server process is to set
up the clients, set up all the communication channels needed, and then to distribute
(roughly) half of the raw data to each client; you may send “first half of data” to one
client and the rest of the data to the other client.
The server is to then take values for low-zipcode and high-zipcode from
standard input, and send those to each of the clients.
The clients are to respond by sending back to the server all the zipcodes whose values are
at least low-zipcode and at most high-zipcode.
The server is then to report to the standard output how many zipcodes were sent by each
client followed by a list of those zipcodes (8 per line).
You may consult the sample session provided.
Input format
The first integer in the input file is the integer numZip (number of zipcodes). Then there
will always be at least numZip zipcodes. Also, assume that numZip is at least 2.
Processes
Your program should involve a server process and two client processes. The client
processes must execute identical executable code.
Server process
This process is responsible for reading the input, setting up proper communication
channels, creating clients, and making each client “exec” the client code. The server then
works as explained above.
Client process
This process is to read (using system call read) from STDIN_FILENO and write (using
system call write) to STDOUT_FILENO. It should be obvious that communication
channels need to be set up properly so that this behavior. of client is feasible.
Communication between the server and the client
I am leaving some of the details out as that gives you flexibility in design; you should
think about what information needs to go between server/client and how. What I have
outlined is the information that must be passed back and forth. You are welcome to
design your own protocol on how this exchange is done between the server and client.
For example, it may make it easier to send some appropriate extra information also.
General information, assumptions, and restrictions
• You may assume the following holds: low-zipcode ≤ high-zipcode.
• Output 8 zipcodes per line (except, possibly, in the last line)
• You must use pipes for inter process communication.
• You may use as many pipes as needed.
• You may assume that the input data has at least 2 zipcodes.
• The order of the zipcodes in the output is not important.
• The code for server must not use any arrays (statically or dynamically allocated)
except for file-descriptors for pipes.
• Any array(s) used by the client must be dynamically allocated.
• Code for the client must not use any library routines for input/output (of course, you
may do so while developing and testing the client as an independent program).
• Note that client code should be implemented as an independent program. Your
overall system is supposed to create a process and exec the client-code whenever a
client is needed.
• Note that you can test your client code and server code independent of each other
using appropriate stubs before they are integrated. (How would you do that?)
What to turn-in?
Hardcopy of source code, enough of sample input/output combinations, and a brief
schematic showing how the processes are interconnected for communication, i.e., the
different pipes used and how they connect the related processes.
What to leave on the system?
Create a directory with the name hwk3 in your home directory. Give the group r-x
permission for directory hwk3 and also for all the files in the directory hwk3. Leave your
files there along with a makefile (will discuss makefile in class).
Note
If for some reason, your code does not function fully correctly, and only in that case,
insert enough diagnostic output statements to generate output that capture the extent to
which your code is functioning.
Sample session
$ cat data
23
45458 34125 76124 10012 42678 60056 23981 32007 72321 44444
95478 35127 16124 19015 62676 28059 36986 10997 45469 44429
77777 43210 55555
$
$ server data
Client 1 was sent the first 11 zipcodes.
Client 2 was sent the remaining 12 zipcodes.
Low zip? 10000
High zip? 99999
Client 1 found 11.
45458 34125 76124 10012 42678 60056 23981 32007
72321 44444 95478
Client 2 found 12.
35127 16124 19015 62676 28059 36986 10997 45469
44429 77777 43210 55555
$
$ server data
Client 1 was sent the first 11 zipcodes.
Client 2 was sent the remaining 12 zipcodes.
Low zip? 55555
High zip? 55555
Client 1 found none.
Client 2 found 1.
55555
$
$ server data
Client 1 was sent the first 11 zipcodes.
Client 2 was sent the remaining 12 zipcodes.
Low zip? 20000
High zip? 80000
Client 1 found 9.
45458 34125 76124 42678 60056 23981 32007 72321
44444
Client 2 found 9.
35127 62676 28059 36986 45469 44429 77777 43210
55555