home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
progjorn
/
pj_6_6.arc
/
LISTING.4
< prev
next >
Wrap
Text File
|
1988-08-30
|
2KB
|
53 lines
1 /*
2 * client.c ----- A potential customer for the background process
3 * (i.e., "daemon"). All it does is send a string
4 * and its process id to the daemon.
5 * Note that read() and write() are atomic,
6 * uninterruptable system calls. This guarantees
7 * that our data arrive in one piece and not
8 * interleaved with some other client's data.
9 */
10
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <sys/signal.h>
14 #include "local.h"
15
16 int fd; /* File descriptor for the Named Pipe (FIFO) */
17 PACKET msg; /* Information to be sent over a FIFO to a server process */
18
19 main(argc,argv)
20 char **argv;
21 {
22 int quit(); /* What to do when killed */
23
24 srand(getpid());
25
26 msg.pid = getpid(); /* Setup the data to be sent */
27 strcpy(msg.string, "Message from a Client");
28
29
30 /* Open the FIFO (Named Pipe) for writing */
31
32 if ( (fd = open(PIPE, O_WRONLY)) == -1 )
33 fprintf(stderr, "Cannot open PIPE in client\n"), exit(1);
34
35 signal(SIGINT, quit); /* Prepare for a Break */
36
37 while(1)
38 {
39 write(fd, &msg, sizeof msg); /* Write to server */
40
41 sleep(rand() % 10); /* Nice and slow, so we can see msgs */
42 }
43 }
44
45 /*****************************************************************************/
46
47 quit()
48 {
49 strcpy(msg.string, "Done"); /* Tell server to kill itself */
50 write(fd, &msg, sizeof msg);
51 exit(0); /* Kill this particular client */
52 }