<!doctype html public "-//w3c//dtd html 4.0
transitional//en">
CS 471
Operating Systems
Spring 2011
Midterm Exam
Time 2 & 1/2 hours
Open Book & Notes
Name:
Unix Login:
Each Question is 20 points.
Question 1
A.
Consider
the following program:
main(int argc, char **argv)
{
int n, in, out;
char buf[1024];
char InputFile[1024];
char OutputFile[1024];
struct
stat st;
printf("enter Input File Name: ");
scanf("%s", InputFile);
if (stat(InputFile,
&st) != 0) {
perror("InputFile does not exit");
exit(1);
}
printf("enter Outut File Name:
");
scanf("%s", OutputFile);
if (stat(OutputFile,
&st) == 0) {
perror("OutputFile exists");
exit(1);
}
if ((in = open(InputFile,
O_RDONLY)) < 0) {
perror(InputFile);
exit(1);
}
if ((out = open(OutputFile,
O_CREAT | O_WRONLY, 0600)) < 0) {
perror(OutputFile);
exit(1);
}
while ((n = read(in, buf,
sizeof(buf))) > 0)
write(out, buf,
n);
close(out);
close(in);
printf("copied %s to %s\n", InputFile,
OutputFile);
exit(0);
}
For the above program, list the names of all:
1.
File system
calls:
2.
Standard
I/O library functions:
B.
Write a program that creates a file
named “cs471” and writes the word “wahab” in that
file.
If there is a file with the same
name, it is truncated.
Question 2
Consider the following four processes
submitted in the order:
P1, P2, P3 and P4 with estimated
CPU times: 8, 6, 4 and
2.
Compute the average waiting time if
these 3 processes are scheduled according to:
1)
FCFS:
2)
SJF:
3)
RR with Quantum time = 2:
Question
3
What is the output of the following programs?
main()
{
pid_t
pid = fork();
if (pid == 0) {
sleep(1);
printf("I am the
child\n");
exit(0);
}
printf("I am the parent\n");
wait(NULL);
if (pid == 0)
printf("pid == 0 after
wait\n");
else printf("pid != 0 after wait\n");
}
main()
{
pid_t
pid = fork();
if (pid == 0) {
sleep(1);
printf("I am the
child\n");
}
printf("I am the parent\n");
wait(NULL);
if (pid == 0)
printf("pid == 0 after
wait\n");
else printf("pid != 0 after wait\n");
}
main()
{
int*
var = (int *) malloc(sizeof(int));
*var = 5;
pid_t
pid = fork();
if (pid == 0) {
while (*var)-- > 0) sleep (1);
printf("I am the child,
var=%d\n", *var);
}
while (*var > 0) sleep (1);
usleep(1);
printf("I
am the parent, var=%d\n", *var);
}
main()
{
int
segment_id = shmget(IPC_PRIVATE,
sizeof(int), S_IRUSR|
S_IWUSR);
int*
var = (int *) shmat(segment_id, NULL, 0);
*var = 5;
pid_t
pid = fork();
if (pid == 0) {
while (*var)-- > 0) usleep (1);
printf("I am the child,
var=%d\n", *var);
}
while (*var > 0) usleep (1);
usleep(1);
printf("I am the parent, var=%d\n",
*var);
}
main(){
pid_t pid;
pid = fork();
if (pid == 0) {
execlp("./forkexec", "forkexec",
NULL);
}
else {
printf ("forkexec");
exit(0);
}
}
Question
4
A.
What is the output of the following program?
% clisrv
main()
{
pid_t
pid;
pid
= fork();
if (pid == 0) {
execlp("./server",
"server", NULL);
}
else {
sleep(1);
execlp("./client",
"client", NULL);
}
}
client.c
main(void)
{
int sockfd,
n;
char buf[SIZE];
struct
sockaddr_in servaddr;
struct
hostent *hp, *gethostbyname();
bzero(&servaddr, sizeof(servaddr));
hp
= gethostbyname("localhost");
bcopy(hp->h_addr,
&(servaddr.sin_addr.s_addr), hp->h_length);
servaddr.sin_family
= AF_INET;
servaddr.sin_port
= htons(2345);
sockfd
= socket(AF_INET, SOCK_STREAM, 0);
if (connect(sockfd, (SA *) & servaddr, sizeof(servaddr)) < 0) {
perror("connect
error");
exit(-1);
}
sprintf(buf, "%s", "I am
your client\n");
write(sockfd, buf, strlen(buf));
read(sockfd, buf, SIZE);
printf("Client got: %s",
buf);
}
main(void)
{
int len, listenfd, connfd;
struct
sockaddr_in servaddr, cliaddr;
char buf[SIZE];
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family
= AF_INET;
servaddr.sin_addr.s_addr
= htonl(INADDR_ANY);
servaddr.sin_port
= htons(2345);
listenfd
= socket(AF_INET, SOCK_STREAM, 0);
bind(listenfd, (SA *) & servaddr, sizeof(servaddr));
listen(listenfd, 0);
len
= sizeof(cliaddr);
connfd
= accept(listenfd, (SA *) & cliaddr,
&len);
read(connfd, buf, SIZE);
printf("Server got: %s", buf);
write(connfd, buf, strlen(buf));
}
B.
What is the output of the following program?
#define SIZE 10
char *msg1="child1\n";
char *msg2="child2\n";
int main(void)
{
int
fd[2];
char buf[SIZE];
pipe(fd);
if (fork()
== 0) {
write(fd[1], msg1, strlen(msg1));
sleep(1);
read(fd[0], buf, 10);
printf("%s",buf);
}
else if
(fork() == 0) {
read(fd[0], buf, 10);
printf("%s",buf);
sleep(1);
write(fd[1], msg2, strlen(msg2));
}
else
sleep(2);
}
Question
5
A.
Modify the following program to create at most N concurrent
echo threads.
For example:
% EchoServerThreadMax 2 10123
Will create at most 2 concurrent echo
threads.
#define SA struct sockaddr
sem_t available;
main(int argc,
char *argv[])
{
int len, listenfd, connfd;
struct
sockaddr_in servaddr, cliaddr;
char buff[512];
int nread;
pthread_t tid;
pthread_attr_t
attr;
int MaxThreads
= atoi(argv[1]);
sem_init(&available, 0, MaxThreads);
listenfd
= socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family
= AF_INET;
servaddr.sin_addr.s_addr
= htonl(INADDR_ANY);
servaddr.sin_port
= htons(atoi(argv[2]));
bind(listenfd, (SA *) & servaddr, sizeof(servaddr));
listen(listenfd, 0);
pthread_attr_init(&attr);
for (;;)
{
len = sizeof(cliaddr);
connfd = accept(listenfd, (SA *) & cliaddr,
&len);
pthread_create(&tid, &attr, &EchoToClient, (void *) connfd);
}
}
void *EchoToClient(void *sockfd)
{
int nread;
char buffer[512];
for (;;)
{
nread = read((int) sockfd, buffer, 512);
if (nread == 0) {
pthread_exit(0);
} else
write((int) sockfd, buffer, nread);
}
}
B.
Modify the following program so that each echo thread uses mutual
exclusion to save the received messages into the LogFile.
EchoServerThreadLog.c
#define SA struct sockaddr
int logfd;
pthread_mutex_t filemutex = PTHREAD_MUTEX_INITIALIZER;
main(int argc,
char *argv[])
{
int len, listenfd, connfd;
struct
sockaddr_in servaddr, cliaddr;
char buff[512];
int nread;
pthread_t tid;
pthread_attr_t
attr;
listenfd
= socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family
= AF_INET;
servaddr.sin_addr.s_addr
= htonl(INADDR_ANY);
servaddr.sin_port
= htons(atoi(argv[1]));
logfd
= open("LogFile", O_RDWR | O_TRUNC |
O_CREAT, 0600);
bind(listenfd, (SA *) & servaddr, sizeof(servaddr));
listen(listenfd, 0);
pthread_attr_init(&attr);
for (;;) {
len = sizeof(cliaddr);
connfd = accept(listenfd,
(SA *) & cliaddr, &len);
pthread_create(&tid, &attr, &EchoToClient, (void *) connfd);
}
}
void *EchoToClient(void *sockfd)
{
int nread;
char buffer[512];
for (;;)
{
nread = read((int) sockfd, buffer, 512);
if (nread == 0)
pthread_exit(0);
else
{
write((int) sockfd, buffer, nread);
}
}
}