<!doctype html public "-//w3c//dtd
html 4.0 transitional//en">
CS 476/576
Systems
Programming
Fall
2005
Final
Exam
Time
2 & 1/2 hours
Open
Book & Notes
Name:
Login:
Question
1: (25 points)
Consider the following files:
int main(int argc, char **argv)
{
int n, in, out;
char buf[1024];
char *prefix;
if (argc == 1) {
prefix = "";
}
else
prefix = argv[1];
while ((n = read(0, buf, sizeof(buf))) > 0){
write(1, prefix, strlen(prefix));
write(1, buf, n);
}
exit(0);
}
int main(void)
{
int in;
pid_t pid;
char buf[1024];
if ((pid = fork()) < 0) {
perror("fork");
exit(1);
}
if (pid == 0) {
sleep (1);
execl("./Echo", "Echo", "son: ", 0);
}
else {
execl("./Echo",
"Echo","father: ", 0);
}
}
int main(void)
{
int in; pid_t pid;
char buf[1024]; int pfd[2];
if (pipe(pfd) < 0) {
perror("pipe");
exit(1);
}
if ((pid = fork()) < 0) {
perror("fork");
exit(1);
}
if (pid == 0) {
dup2(pfd[0], 0);
close(pfd[1]); close(pfd[0]);
execl("./Echo", "Echo", "son: ", 0);
}
else {
dup2(pfd[1], 1);
close(pfd[1]); close(pfd[0]);
execl("./Echo",
"Echo","father: ", 0);
}
}
What is the output of the following statements?
% Echo < TestInput
Hussein
Abdel-Wahab
% ExecEcho < TestInput
father: Hussein
Abdel-Wahab
% ExecEchoPipe < TestInput
son: father: [son:] Hussein
Abdel-Wahab
[son:] depends on buffering
Question
2: (25 points)
Consider the following files:
int main(int argc, char **argv)
{
int n, in, out; char buf[1024];
char *prefix;
if (argc == 1) {
prefix = "";
}
else
prefix = argv[1];
while ((n = read(0, buf, sizeof(buf))) > 0){
write(1, prefix, strlen(prefix));
write(1, buf, n);
}
exit(0);
}
struct sockaddr_in Server;
int ServerSocket;
int PrivateServerSocket;
int ClientSocket;
struct hostent *hp, *gethostbyname();
struct sockaddr_in from, addr;
int length, fromlen;
int main(void)
{
int in;
pid_t pid; char buf[1024];
CreateServerSocket();
if ((pid = fork()) < 0) {
perror("fork");
exit(1);
}
if (pid == 0) {
CreateClientSocket();
dup2(ClientSocket, 0);
close(ClientSocket);
execl("./Echo",
"Echo", "son:\n", 0);
}
else {
PrivateServerSocket = accept(ServerSocket, (SA
*)&from, &fromlen);
dup2(PrivateServerSocket,
1);
close(PrivateServerSocket);
execl("./Echo",
"Echo","father:\n", 0);
}
}
CreateServerSocket()
{
Server.sin_family = AF_INET;
hp =
gethostbyname("localhost");
bcopy ( hp->h_addr, &(Server.sin_addr),
hp-> h_length);
Server.sin_port = htons(0);
ServerSocket = socket
(PF_INET,SOCK_STREAM,0);
if (ServerSocket<0) {
perror("opening
ServerSocket");
exit(-1);
}
if ( bind( ServerSocket, (SA*)
&Server, sizeof(Server) ) ) {
close(ServerSocket);
perror("binding name to
ServerSocket");
exit(-1);
}
length = sizeof(Server);
if ( getsockname (ServerSocket,(SA
*)&Server,&length) ) {
perror("getting ServerSocket
name");
exit(0);
}
listen(ServerSocket,1);
}
CreateClientSocket()
{
ClientSocket = socket
(AF_INET,SOCK_STREAM,0);
if (ClientSocket<0) {
perror("opening
ClientSocket");
exit(-1);
}
if ( connect(ClientSocket, (SA
*)&Server, sizeof(Server)) < 0 ) {
close(ClientSocket);
perror("connecting
ClientSocket");
exit(0);
}
}
struct sockaddr_in Server; struct
sockaddr_in Client;
int ServerSocket; int
ClientSocket;
struct hostent *hp, *gethostbyname();
struct sockaddr_in from; struct sockaddr_in addr;
int length; int fromlen; int n;
int main(void)
{
int in; pid_t pid;
char buf[1024];
CreateServerSocket();
CreateClientSocket();
if ((pid = fork()) < 0) {
perror("fork");
exit(1);
}
if (pid == 0) {
while ((n = read(ServerSocket,
buf, sizeof(buf))) > 0){
write
(1, buf, n);
sendto(ServerSocket,
buf, n, 0, (SA *)&Client, sizeof(Client));
}
}
else {
while ((n = read(0, buf,
sizeof(buf))) > 0){
sendto(ClientSocket, buf, n, 0, (SA *)&Server,
sizeof(Server)) ;
n
= read(ClientSocket, buf, sizeof(buf));
write
(1, buf, n);
}
}
}
CreateServerSocket()
{
Server.sin_family = AF_INET;
hp = gethostbyname("localhost");
bcopy ( hp->h_addr,
&(Server.sin_addr), hp-> h_length);
Server.sin_port = htons(0);
ServerSocket = socket
(PF_INET,SOCK_DGRAM,0);
if (ServerSocket<0) {
perror("opening
ServerSocket"); exit(-1);
}
if ( bind( ServerSocket, (SA*)
&Server, sizeof(Server) ) ) {
perror("binding name to
ServerSocket");
exit(-1);
}
length = sizeof(Server);
if ( getsockname (ServerSocket,(SA
*)&Server,&length) ) {
perror("getting ServerSocket
name");
exit(0);
}
}
CreateClientSocket()
{
Client.sin_family = AF_INET;
hp =
gethostbyname("localhost");
bcopy ( hp->h_addr, &(Client.sin_addr),
hp-> h_length);
Client.sin_port = htons(0);
ClientSocket = socket
(PF_INET,SOCK_DGRAM,0);
if (ClientSocket<0) {
perror("opening ClientSocket");
exit(-1);
}
if ( bind( ClientSocket, (SA*)
&Client, sizeof(Client) ) ) {
perror("binding name to ClientSocket");
exit(-1);
}
length = sizeof(Client);
if ( getsockname (ClientSocket,(SA
*)&Client,&length) ) {
perror("getting ClientSocket name");
exit(0);
}
}
What is the output of the following statements?
% ExecEchoTCP < TestInput
son:
father:
[son:]
Hussein
Abdel-Wahab
[son] depends on buffering
% EchoUDP < TestInput
Hussein
Abdel-Wahab
Hussein
Abdel-Wahab
Question
3: (25 points)
Write a Java/Swing program that
creates a 50 buttons labeled as:
0, 1, …, 49
arranged as a 10x5 grid.
When the user clicks on any button,
that button’s background changes to red while all other buttons are disabled.
When the user clicks on the red
button, the interface is restored back to its initial state.
Question
4: (25 points)
Modify the following program to limit the number
of server threads to a maximum of 12 threads. Show only the modifications and
do not rewrite the whole program.
class ServeOne extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ServeOne (Socket s)
throws IOException {
socket = s;
in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())), true);
start();
}
public void run() {
try {
while (true) {
String str = in.readLine();
if (str.equals("DONE-THANKS"))
break;
out.println(str);
}
} catch(IOException e) {
System.err.println("IO
Exception");
} finally {
try {
socket.close();
} catch(IOException e) {
System.err.println("Socket not
closed");
}
}
}
}
public class MultiServer {
static final int PORT = 8008;
public static void main(String[] args)
throws IOException {
ServerSocket s = new ServerSocket(PORT);
try {
while(true) {
Socket socket = s.accept();
try {
new ServeOne(socket);
} catch(IOException e) {
socket.close();
}
}
} finally {
s.close();
}
}
}