<!doctype html public
"-//w3c//dtd html 4.0 transitional//en">
CS
471
Operating Systems
Spring 2013
Midterm Exam
Time
2 & 1/2 hours
Open Book & Notes
Name:
Unix Login:
Each
Question is 25 points.
Question 1
What is the
print out of the following programs:
A.
int main()
{
fork();
printf("Norfolk\n");
fork();
printf("Virginia Beach\n");
fork();
printf("Newport News\n");
}
B.
int main()
{
if
(fork()){
printf("Hampton\n");
exit(0);
}
if
(fork())
printf("Suffolk\n");
if
(fork())
printf("Chesapeake\n");
}
C.
int main()
{
if
(fork()) execl("q1cexec", "q1cexec",
ODU, 0);
if
(fork()) execl("q1cexec", "q1cexec",
UVA, 0);
if
(fork()) execl("q1cexec", "q1cexec",
NSU, 0);
sleep(1);
printf("Q1c cs471w\n");
}
Q1cexec:
int main(int argc, char *argv[])
{
printf("%s: q1cexec cs471w\n", argv[1]);
}
Question 2
Consider the following 3 processes submitted in the order:
P1, P2 and P3 with estimated CPU
times: 9, 3, 6
Compute the average
waiting time if these 3 processes are scheduled according to:
1) FCFS:
2) SJF:
3) LJF (Longest
Job First)
4) RR with
Quantum time = 3. How many context switches are needed?
Question 3
A.
Complete the
following code:
The parent sends A and B to the child via
pipe1.
The child
adds the two received values and sends the result back to the parent via
pipe2.
The parent prints the results and exits.
int main(void)
{
int pipe1[2];
int pipe2[2];
int pid;
int A , B, C;
char buf[SIZE];
bzero(buf, SIZE);
pipe(pipe1);
pipe(pipe2);
pid = fork();
if (pid==0) { /* Child Code */
}
else { /* Parent Code */
srand((unsigned) time(0));
A = rand() % 100;
B = rand() % 100;
}
}
B.
What is the output of the following program:
int main()
{
int segment_id = shmget(IPC_PRIVATE, sizeof(int), S_IRUSR | S_IWUSR);
int *sharedvariable = (int *) shmat(segment_id, NULL, 0);
*sharedvariable = 0;
int pid;
pid = fork();
if (pid==0) {
usleep(10);
while (1) {
printf("Child got: %d\n", *sharedvariable);
(*sharedvariable)++;
sleep(1);
}
}
else {
while (*sharedvariable < 10) {
(*sharedvariable)++;
(*sharedvariable)++;
sleep(1);
printf("Parent got: %d\n", *sharedvariable);
}
kill (pid, 9);
}
}
C.
What is the output of the following program.
int sfd;
int main()
{
char buf[SIZE];
bzero(buf, SIZE);
int pid;
sfd = open("tmpfile", O_RDWR | O_TRUNC | O_CREAT, 0600);
int i = 0;
pid = fork();
if (pid==0) {
usleep(10);
while (1) {
lseek(sfd, 0, SEEK_SET);
read(sfd, buf,
SIZE);
printf("Child got: %s\n", buf);
i = atoi(buf);
i++;
sprintf(buf, "%d", i);
lseek(sfd, 0, SEEK_SET);
write(sfd, buf, strlen(buf));
sleep(1);
}
}
else {
while (i < 10) {
sprintf(buf, "%d", i);
lseek(sfd, 0, SEEK_SET);
write(sfd, buf, strlen(buf));
sleep(1);
lseek(sfd, 0, SEEK_SET);
read(sfd, buf,
SIZE);
printf("Parent got: %s\n", buf);
i = atoi(buf);
i++;
}
kill (pid, 9);
}
}
Question 4
What is the
output of the following programs?
A.
#define SA struct sockaddr
#define SIZE 10
void
*Child();
void
*Parent();
pthread_t
thread1, thread2;
main(int argc,
char *argv[])
{
pthread_create(&thread1, NULL, &Parent, NULL);
pthread_create(&thread2, NULL, &Child, NULL);
pthread_join(thread1, NULL);
exit(0);
}
void *Child()
{
int sockfd, n;
struct sockaddr_in servaddr;
struct hostent *hp, *gethostbyname();
char buf[100];
int i;
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(10294);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
printf("Child connecting\n");
connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
printf("Child connected\n");
while (1) {
read(sockfd, buf,
SIZE);
printf("Child got: %s\n", buf);
i = atoi(buf);
i++;
sprintf(buf, "%d", i);
write(sockfd, buf, strlen(buf));
sleep(1);
}
}
void *Parent()
{
int len, listenfd, connfd;
struct sockaddr_in servaddr, cliaddr;
char buf[512];
int i = 0;
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(10294);
listenfd = socket(AF_INET, SOCK_STREAM, 0);
bind(listenfd, (SA *) & servaddr, sizeof(servaddr));
listen(listenfd, 0);
len = sizeof(cliaddr);
printf("Parent accepting\n");
connfd = accept(listenfd, (SA *)
& cliaddr, &len);
printf("Parent accepted\n");
while (i < 10) {
sprintf(buf, "%d", i);
write(connfd, buf, strlen(buf));
sleep(1);
read(connfd, buf,
SIZE);
printf("Parent got: %s\n", buf);
i = atoi(buf);
i++;
}
printf("Parent thread is done\n");
pthread_exit(0);
}
B.
void
*function1();
void
*function2();
pthread_mutex_t mutexA =
PTHREAD_MUTEX_INITIALIZER;
pthread_t
thread1, thread2;
int sharedvariable
= 0;
main(int argc,
char *argv[])
{
pthread_create(&thread1, NULL, &function1, NULL);
pthread_create(&thread2, NULL, &function2, NULL);
pthread_join(thread2, NULL);
exit(0);
}
void
*function1()
{
usleep(10);
while
(1) {
pthread_mutex_lock(&mutexA);
printf("Child got: %d\n", sharedvariable);
sharedvariable++;
pthread_mutex_unlock(&mutexA);
sleep(1);
}
}
void
*function2()
{
while (sharedvariable
< 10) {
pthread_mutex_lock(&mutexA);
sharedvariable++;
printf("Parent got: %d\n", sharedvariable);
pthread_mutex_unlock(&mutexA);
sleep(1);
}
printf("Parent
thread is done\n");
pthread_exit(0);
}