Beruflich Dokumente
Kultur Dokumente
Outline
Big picture
Client server paradigm
Remote file server
Linux operations
Server
Stdout
socket
Internet
(Cloud)
socket
Stdin Server
> ls
a.out
hello.c
hello
University of Hawaii EE 367L
Issues commands
for service
Request service
(send command)
e.g., ls, cat, mov
Server
(e.g., wiliki)
Replies to request
e.g., data that satisfies
Listening for requests
the request
and then replies
Linux commands
Linux commands are executed by software provided by
Linux
System calls: examples: ls, rm, cat, ....
execl
Example
main()
{
execl(/bin/ls, ls, (char*) NULL
exit(EXIT_SUCCESS);
}
Overwrite
ls
ant a program to
all a UNIX command
se data from the command and continue processing
University of Hawaii EE 367L
Solution
Original program (process) creates another child
program
The child program executes execl
The original is not overwritten and continues to run
This is implemented using the fork( ) system call
It creates a child process which is identical to the original
process
If theyre identical copies, how can a process tell whether its
the child or parent?
University of Hawaii EE 367L
Processes
Process is a running program sort of a virtual computer
Executable code
Memory for data, e.g., global variables, stack, heap (for dynamic
memory allocation)
CPU
Registers, such as general purpose registers, program counter, status control
registers, ...
Processes
main()
{
while(1) {
if (fork( ) == 0) { /* child */
task1( ):
exit(EXIT_SUCCESS);
}
else { /* parent */
task2( );
wait( );
}
}
}
main()
main()
{
{
while(1) {
while(1) {
if (fork( ) == 0) { /* child */if (fork( ) == 0) { /* child */
task1( ): // child code
task1( ):
exit(EXIT_SUCCESS); // child code
exit(EXIT_SUCCESS);
}
}
else { /* parent */
else { /* parent */
task2( );
task2( ); // parent code
wait( );
wait( ); // parent code
}
}
}
}
}
}
k( ) creates an identical copy of the process,
called a child process. So now there is await(
child ) will stop the process until a child
process terminates
and the parent (original) process
running together
eturns
University of Hawaii EE 367L
0 if its a child process
10
PID of child if its the parent
Processes
/*
/*
Just one process
Multiple processes
*/
*/
main()
{
while(1) {
task1( ):
task2( );
}
}
P
C
main()
P
{
while(1) {
if (fork( ) == 0) { /* child */
P
task1( ):
exit(EXIT_SUCCESS); /* terminate the child. You can also just return */
}
P
else { /* parent */
task2( );
wait( ); /* wait for child to terminate */
}
}
}
University of Hawaii EE 367L
C
C
11
main()
{
if (fork( ) == 0) { /* child */
execl(/bin/ls, ls, (char*) NULL
exit(EXIT_SUCCESS);
}
else { /* parent */
task2( );
}
}
Processes
Parent
main()
{
f (fork( ) == 0) { /* child */
execl(/bin/ls, ls, (char*) NULL
exit(EXIT_SUCCESS);
}
else { /* parent */
task2( );
}
}
Child
main()
{
if (fork( ) == 0) { /* child */
execl(/bin/ls, ls, (char*) NULL
exit(EXIT_SUCCESS);
}
else { /* parent */
task2( );
}
}
University of Hawaii EE 367L
Overwrite
ls program
12
main()
{
if (fork( ) == 0) { /* child */
execl(/bin/ls, ls, (char*) NULL
exit(EXIT_SUCCESS);
}
else { /* parent */
task2( );
}
}
Processes
Parent
Child
main()
{
f (fork( ) == 0) { /* child */
execl(/bin/ls, ls, (char*) NULL
exit(EXIT_SUCCESS);
}
else { /* parent */
task2( );
}
}
ls
Overwrite
ls program
13
Pipes
We still have a problem
The parent process can continue to run while ls is executing
So thats good
14
Pipe
pipe(fd);
fd[0] = read-end of the pipe
fd[1] = write-end of the pipe
Process
Process
15
execl
Program X
Example 1: without pipe
16
Example 1
main()
{
fork( )
}
Parent
main()
{
Child
main()
{
Overwrite
ls program
Output
17
Example 2
Parent
main()
{
Listen to pipe
Process what comes
}
main()
{
Make a pipe
Bind file descriptors
fork( )
}
pipe
Child
main()
{
Redirect output from stdio to pipe
using dup2
execl program, e.g., ls
}
18
Example 2
Parent
main()
{
Listen to pipe
Process what comes
}
main()
{
Make a pipe
Bind file descriptors
fork( )
}
pipe
Child
main()
{
Redirect output from stdio to pipe
using dup2
execl program, e.g., ls
}
19
Example 2
Parent
main()
{
Listen to pipe
Process what comes
}
main()
{
Make a pipe
Bind file descriptors
fork( )
}
pipe
Child
main()
{
Redirect output from stdio to pipe
using dup2
execl program, e.g., ls
}
Complete
Overwrite
ls program
20
execl, etc
21
Server
> ls
a.out
hello.c
hello
Client X
socket
Internet
(Cloud)
Server Y
socket
22
File Transfer
23
Sockets
Two types
Stream sockets
Long lived connection between two entities
Based on the TCP protocol
Well concentrate on this type of socket
Datagram sockets
One message (datagram)
Based on the UDP protocol
University of Hawaii EE 367L
24
Sockets
Applications
TCP Ports
Client
Server
TCP protocol
TCP protocol
Internet Network
(Network Layer IP protocol)
port = IP address
128.15.20.0
somewhere.com
ain name for the
dress
ain Name System (DNS) is used to convert a domain name into an IP address
University of Hawaii EE 367L
25
Server
TCP port number
Type of socket
26
Examples
hints.ai_family = AF_UNSPEC;
// Dont specify IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM;
// or SOCK_DATAGRAM
hints.ai_flags = AI_PASSIVE;
// Assign local host to IP address
structure
ee http://beej.us/guide/bgnet/output/html/multipage/syscalls.html
res
isand
thethere
output
or results
and
this that
is inyou
a can
linked
or these system
calls
is an example
called
showip.c
run
s in section ouput
5.1
University of Hawaii EE 367L
Sockets
28
Client-Server
Request
Client
Initiates Transaction
Server
Reply
Waits for
requests
Request connection
to a port X / IP address
Provides its own port#/IP addr
Client
Initiates connection
Reply
send
recv
University of Hawaii EE 367L
Waits for
requests to
Server
connect to
port X
no IP addresses needed
send
recv
29
Client Socket
getaddrinfo(www.example.com ,3490, &hints, &res);
sockfd = socket(res->ai_family, res->ai_socktype, res>ai_protocol);
Now sockfd is the socket file descriptor, similar to a file descriptor
Client
connect(sockfd, res->ai_addr, res->ai_addrlen);
This will connect to the remote address www.example.com specified
in res->ai_addr and on port 3490
30
Server Socket
getaddrinfo(www.example.com ,http, &hints, &res);
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
Now sockfd is the socket file descriptor, similar to a file descriptor
Server:
hints.ai_flags = AI_PASSIVE;
// use local address
getaddrinfo(NULL, 3490, &hints, &res);
socketfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
bind(int sockfd, res->ai_addr, res->ai_addrlen);
Binds sockfd data structure to the local IP address and port 3490
sockfd = socket file descriptor
res->ai_addr = pointer to address information: port number
res->ai_addrlen = length in bytes of the address
31
listen( ) for
connection
requests at
connect( ) port 4000
port 4000
Client-Server
Process
Client
Process
(Server)
Client
okay
Client
is busy
port 6059
okay
connect( )
port 4000
Process
(Server)
connect( )
port 4000
accept( )
Child
new port 6059
new sockfd
Process
Client
is not busy
32
Sockets
Client will connect to a server
connect(sockfd, res->ai_addr, res->ai_addrlen);
This will connect to the remote IP address specified in res->ai_addr.
Port # from sockfd
33
Sockets
34
Client Server
Client
Server
Create socket
Connect
Create socket
Call queuelisten
send
recv
University of Hawaii EE 367L
accept
Get a new socket fd
Create a child process
to use this new fd
35
Ideas
Break up the programming problem into smaller chunks so that you can
test it
Example:
Part 1: Use the client server programs from Beejs guide. Run them to make sure they
work
Part 2: Modify the server so that it executes ls every time it gets a connection request
from the client. Note the output of ls goes to the console of the server. The server then
terminates the connection.
This will give you experience with execl and processes (and also maybe pipes)
Part 3: Modify the server so that it executes ls every time it gets a connection request
from the client but the output of ls goes through the socket back to the client. The server
will terminate the connection. The client will display the output
This will give you experience with pipes as well as sockets
Part 4: Modify the client so that it accepts the three commands (list, check
<filename>, and get <filename>) from the user, starts a connection per command,
sends a message to indicate the command to the server (e.g., list, check <file name>,
get <file name>). The server is modified so that it accepts the connection, prints the
command on its own console, executes ls, sends the output of ls back to the client.
Part 5: Complete the project
University of Hawaii EE 367L
36
Ideas
Hint:
Model: client sends request -> server process replies
Have each transaction correspond to a new connect, i.e.,
terminate a connection after a reply
37