218 lines
11 KiB
HTML
218 lines
11 KiB
HTML
|
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
<!DOCTYPE html
|
||
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<html lang="en-us" xml:lang="en-us">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
<meta name="security" content="public" />
|
||
|
<meta name="Robots" content="index,follow" />
|
||
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
||
|
<meta name="DC.Type" content="reference" />
|
||
|
<meta name="DC.Title" content="Example: Use signals with blocking socket APIs" />
|
||
|
<meta name="abstract" content="Signals allow you to be notified when a process or application becomes blocked." />
|
||
|
<meta name="description" content="Signals allow you to be notified when a process or application becomes blocked." />
|
||
|
<meta name="DC.Relation" scheme="URI" content="example.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="csignals.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="asynchi0.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="xasynchi0.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/accept.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/listen.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/close.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/socket.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="../apis/bind.htm" />
|
||
|
<meta name="copyright" content="(C) Copyright IBM Corporation 2001, 2006" />
|
||
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2001, 2006" />
|
||
|
<meta name="DC.Format" content="XHTML" />
|
||
|
<meta name="DC.Identifier" content="xsignals" />
|
||
|
<meta name="DC.Language" content="en-us" />
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
||
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
||
|
<title>Example: Use signals with blocking socket APIs</title>
|
||
|
</head>
|
||
|
<body id="xsignals"><a name="xsignals"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Example: Use signals with blocking socket APIs</h1>
|
||
|
<div><p>Signals allow you to be notified when a process or application
|
||
|
becomes blocked.</p>
|
||
|
<div class="section"><div class="p">Signals provide a time limit for blocking processes.
|
||
|
In this example the signal occurs after five seconds on the <span class="apiname">accept()</span> call.
|
||
|
This call normally blocks indefinitely, but because there is an alarm set,
|
||
|
the call only blocks for five seconds. Because blocked programs can hinder
|
||
|
performance of an application or server, they can be used to diminish this
|
||
|
impact. The following example shows you how to use signals with blocking socket
|
||
|
APIs. <div class="note"><span class="notetitle">Note:</span> Asynchronous I/O used in a threaded server model is preferable
|
||
|
over the more conventional model. </div>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><p><br /><img src="rzab6505.gif" alt="This graphic shows the socket calls that are used in the example program that uses signals." /><br /></p>
|
||
|
</div>
|
||
|
<div class="section"></div>
|
||
|
<div class="section"><h4 class="sectiontitle">Socket flow of events: Use signals with blocking socket</h4><p>The
|
||
|
following sequence of function calls shows how you can use signals to alert
|
||
|
the application when the socket has been inactive:</p>
|
||
|
<ol><li>The <span class="apiname">socket()</span> function returns a socket
|
||
|
descriptor representing an endpoint. The statement also identifies that the
|
||
|
AF_INET (Internet Protocol) address family with the TCP transport (SOCK_STREAM)
|
||
|
being used for this socket. </li>
|
||
|
<li>After the socket descriptor is created, a <span class="apiname">bind()</span> function
|
||
|
gets a unique name for the socket. In this example, a port number is not specified
|
||
|
because the client application does not connect to this socket. This code
|
||
|
snippet can be used within other server programs that use blocking APIs, such
|
||
|
as <span class="apiname">accept()</span>.</li>
|
||
|
<li>The <span class="apiname">listen()</span> function indicates a willingness to accept
|
||
|
client connection requests. After the <span class="apiname">listen()</span> function
|
||
|
is issued, an alarm is set to go off in five seconds. This alarm or signal
|
||
|
alerts you when the <span class="apiname">accept()</span> call blocks.</li>
|
||
|
<li>The <span class="apiname">accept()</span> function accepts a client
|
||
|
connection request. This call normally blocks indefinitely, but because there
|
||
|
is an alarm set, the call only blocks for five seconds. When the alarm goes
|
||
|
off, the accept call is completed with -1 and with an errno value
|
||
|
of EINTR. </li>
|
||
|
<li>The <span class="apiname">close()</span> function ends any open socket descriptors. </li>
|
||
|
</ol>
|
||
|
</div>
|
||
|
<div class="section"><div class="note"><span class="notetitle">Note:</span> By using the code examples, you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
|
||
|
<pre>/******************************************************************/
|
||
|
/* Example shows how to set alarms for blocking socket APIs */
|
||
|
/******************************************************************/
|
||
|
|
||
|
/******************************************************************/
|
||
|
/* Include files */
|
||
|
/******************************************************************/
|
||
|
#include <signal.h>
|
||
|
#include <unistd.h>
|
||
|
#include <stdio.h>
|
||
|
#include <time.h>
|
||
|
#include <errno.h>
|
||
|
#include <sys/socket.h>
|
||
|
#include <netinet/in.h>
|
||
|
|
||
|
/******************************************************************/
|
||
|
/* Signal catcher routine. This routine will be called when the */
|
||
|
/* signal occurs. */
|
||
|
/******************************************************************/
|
||
|
void catcher(int sig)
|
||
|
{
|
||
|
printf(" Signal catcher called for signal %d\n", sig);
|
||
|
}
|
||
|
|
||
|
/******************************************************************/
|
||
|
/* Main program */
|
||
|
/******************************************************************/
|
||
|
int main(int argc, char *argv[])
|
||
|
{
|
||
|
struct sigaction sact;
|
||
|
struct sockaddr_in addr;
|
||
|
time_t t;
|
||
|
int sd, rc;
|
||
|
|
||
|
/******************************************************************/
|
||
|
/* Create an AF_INET, SOCK_STREAM socket */
|
||
|
/******************************************************************/
|
||
|
printf("Create a TCP socket\n");
|
||
|
sd = socket(AF_INET, SOCK_STREAM, 0);
|
||
|
if (sd == -1)
|
||
|
{
|
||
|
perror(" socket failed");
|
||
|
return(-1);
|
||
|
}
|
||
|
|
||
|
/******************************************************************/
|
||
|
/* Bind the socket. A port number was not specified because */
|
||
|
/* we are not going to ever connect to this socket. */
|
||
|
/******************************************************************/
|
||
|
memset(&addr, 0, sizeof(addr));
|
||
|
addr.sin_family = AF_INET;
|
||
|
printf("Bind the socket\n");
|
||
|
rc = bind(sd, (struct sockaddr *)&addr, sizeof(addr));
|
||
|
if (rc != 0)
|
||
|
{
|
||
|
perror(" bind failed");
|
||
|
close(sd);
|
||
|
return(-2);
|
||
|
}
|
||
|
|
||
|
/******************************************************************/
|
||
|
/* Perform a listen on the socket. */
|
||
|
/******************************************************************/
|
||
|
printf("Set the listen backlog\n");
|
||
|
rc = listen(sd, 5);
|
||
|
if (rc != 0)
|
||
|
{
|
||
|
perror(" listen failed");
|
||
|
close(sd);
|
||
|
return(-3);
|
||
|
}
|
||
|
|
||
|
/******************************************************************/
|
||
|
/* Set up an alarm that will go off in 5 seconds. */
|
||
|
/******************************************************************/
|
||
|
printf("\nSet an alarm to go off in 5 seconds. This alarm will cause the\n");
|
||
|
printf("blocked accept() to return a -1 and an errno value of EINTR.\n\n");
|
||
|
sigemptyset(&sact.sa_mask);
|
||
|
sact.sa_flags = 0;
|
||
|
sact.sa_handler = catcher;
|
||
|
sigaction(SIGALRM, &sact, NULL);
|
||
|
alarm(5);
|
||
|
|
||
|
/******************************************************************/
|
||
|
/* Display the current time when the alarm was set */
|
||
|
/******************************************************************/
|
||
|
time(&t);
|
||
|
printf("Before accept(), time is %s", ctime(&t));
|
||
|
|
||
|
/******************************************************************/
|
||
|
/* Call accept. This call will normally block indefinitely, */
|
||
|
/* but because we have an alarm set, it will only block for */
|
||
|
/* 5 seconds. When the alarm goes off, the accept call will */
|
||
|
/* complete with -1 and an errno value of EINTR. */
|
||
|
/******************************************************************/
|
||
|
errno = 0;
|
||
|
printf(" Wait for an incoming connection to arrive\n");
|
||
|
rc = accept(sd, NULL, NULL);
|
||
|
printf(" accept() completed. rc = %d, errno = %d\n", rc, errno);
|
||
|
if (rc >= 0)
|
||
|
{
|
||
|
printf(" Incoming connection was received\n");
|
||
|
close(rc);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
perror(" errno string");
|
||
|
}
|
||
|
|
||
|
/******************************************************************/
|
||
|
/* Show what time it was when the alarm went off */
|
||
|
/******************************************************************/
|
||
|
time(&t);
|
||
|
printf("After accept(), time is %s\n", ctime(&t));
|
||
|
close(sd);
|
||
|
return(0);
|
||
|
}</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="example.htm" title="These examples provide many sample programs that illustrate the more advanced socket concepts. You can use these sample programs to create your own applications that complete a similar task.">Examples: Socket application designs</a></div>
|
||
|
</div>
|
||
|
<div class="relconcepts"><strong>Related concepts</strong><br />
|
||
|
<div><a href="csignals.htm" title="An application program can request to be notified asynchronously (request that the system send a signal) when a condition that the application is interested in occurs.">Signals</a></div>
|
||
|
<div><a href="asynchi0.htm" title="Asynchronous I/O APIs provide a method for threaded client server models to perform highly concurrent and memory efficient I/O.">Asynchronous I/O</a></div>
|
||
|
</div>
|
||
|
<div class="relref"><strong>Related reference</strong><br />
|
||
|
<div><a href="xasynchi0.htm" title="An application creates an I/O completion port using the QsoCreateIOCompletionPort() API.">Example: Use asynchronous I/O</a></div>
|
||
|
</div>
|
||
|
<div class="relinfo"><strong>Related information</strong><br />
|
||
|
<div><a href="../apis/accept.htm">accept()</a></div>
|
||
|
<div><a href="../apis/listen.htm">listen()</a></div>
|
||
|
<div><a href="../apis/close.htm">close()</a></div>
|
||
|
<div><a href="../apis/socket.htm">socket()</a></div>
|
||
|
<div><a href="../apis/bind.htm">bind()</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|