lOMoARcPSD|20259537
Systems Programming Midterm Fall 2017, answers
Systems Programming (University of Ontario Institute of Technology)
Scan to open on Studocu
Studocu is not sponsored or endorsed by any college or university
Downloaded by Jahongir Azzamov (azzamovjahongir82@gmail.com)
lOMoARcPSD|20259537
SOFE 3200U
Systems Programming
Fall 2017
Midterm Exam
Time: 70 minutes
Instructor: Dr. Akramul Azim
Name: __________________
Student No.: _____________
Part A /6
Part B /10
Part C /4
Downloaded by Jahongir Azzamov (azzamovjahongir82@gmail.com)
lOMoARcPSD|20259537
Part A: (Each multi-choice question is worth 0.75 mark)
1- When user “sofe3200” logs into a Linux system, the shell starts off in a particular
directory which is
a- /home
b- /home/sofe3200
c- /users
d- /users/sofe3200
Answer: __b___
2- What shell command shows the current working directory?
a- ls
b- pwd
c- dir
d- None of the above
Answer: ___b__
3- In bourne again shell (bash) if a variable does not exist before assigning a value to it, the
shell will
a- Show an error message
b- Create a new one
c- Does nothing
d- Show a warning
Answer: __b_
4- Which of the following statement assigns variable a to 4 in C shell?
a- set a = 2 * 2
b- @a = 2 * 2
c- a=2*2
d- None of the above
Answer: __b__
Downloaded by Jahongir Azzamov (azzamovjahongir82@gmail.com)
lOMoARcPSD|20259537
5- printf () library function always sends its output using file descriptor
a- 0
b- 1
c- 2
d- 3
Answer: _1___
6- Which of the following interprocess communication is bi-directional?
a) socket
b) pipe
c) signal
d) none of the above
Answer: __a__
7- In the following system call:
void exit (int status)
What are the ranges of values status can have?
a) 0 to 127
b) 0 to 255
c) 1 to 128
d) 1 to 256
Answer: __b___
8- A parent process may wait for one of its children to terminate and then accept its child's
termination code by executing
a) sleep()
b) kill()
c) delay()
d) None of the above
Answer: __d___
Downloaded by Jahongir Azzamov (azzamovjahongir82@gmail.com)
lOMoARcPSD|20259537
Part B: (Each short question is worth 1.25 mark)
1- What is system call? Give an example.
Answer:
• Application programs must talk to the operating system for services like -
• file creation,
• process duplication
• interprocess communication
• They can do this via a collection of routines called system calls.
Example: open(), close(), read(), write(), fork() etc.
2- What is zombie process? How can you create a zombie process?
Answer:
If a process's parent is alive but never executes a wait(), the process's return code will never be
accepted and the process will remain a zombie.
Using an infinite loop at the parent process code or a long delay.
Downloaded by Jahongir Azzamov (azzamovjahongir82@gmail.com)
lOMoARcPSD|20259537
3- How to create a perl function? Show an example.
Answer:
Perl functions are simple to use, although the syntax can get complicated. A simple example of a
Perl function will give you the idea:
To call the function, your Perl script would look like this:
4- What is shell? Why it is useful?
Answer:
• A shell is a middleman between you and Linux Operating System.
• It allows you to
– Run programs
– Build pipeline of processes
– Save output to files
– Run more than one program at once
Downloaded by Jahongir Azzamov (azzamovjahongir82@gmail.com)
lOMoARcPSD|20259537
5- How to execute a set of commands in sequences and also in the background?
Answer:
for executing commands one by one ;
for executing commands in the background &
6- What are the differences between absolute path and relative path in Linux? Shown an
example.
Answer:
absolute path = path from /
relative path = path from current directory
• File Absolute PathName
• A /home/sofe3200/myFile
• B /home/myFile
• C /bin/myFile
• File Relative Pathname
• A myFile
• B ../myFile
• C ../../bin/myFile
7- What are different file permission specifications for the chmod command?
Answer:
Downloaded by Jahongir Azzamov (azzamovjahongir82@gmail.com)
lOMoARcPSD|20259537
8- What is the typical sequence of events associated when a child process reads the message
buffer of the parent using unnamed pipes?
Answer:
The typical sequence of events is as follows:
1. The parent process creates an unnamed pipe using pipe ().
2. The parent process forks.
3. The writer closes its read end of the pipe, and the designated reader closes its write end
of the pipe.
4. The processes communicate by using write () and read () calls.
5. Each process closes its active pipe descriptor when finished with it.
Downloaded by Jahongir Azzamov (azzamovjahongir82@gmail.com)
lOMoARcPSD|20259537
Part C: (2 marks each)
1. Write a C program to protect any pieces of your code against Control-C (SIGINT)
attacks.
#include <stdio.h>
#include <signal.h>
main ()
{
Answer:
#include <stdio.h>
#include <signal.h>
main ()
{
void (*oldHandler) (); /* To hold old handler value */
printf ("I can be Control-C'ed\n");
sleep (3);
oldHandler = signal (SIGINT, SIG_IGN); /* Ignore Control-C */
printf ("I'm protected from Control-C now\n");
sleep (3);
signal (SIGINT, oldHandler); /* Restore old handler */
printf ("I can be Control-C'ed again\n");
sleep (3);
printf ("Bye!\n");
}
Or
#include <stdio.h>
#include <signal.h>
main ()
{
void myhandler();
void (*oldHandler) (); /* To hold old handler value */
printf ("I can be Control-C'ed\n");
sleep (3);
oldHandler = signal (SIGINT, myhandler); /* Ignore Control-C */
printf ("I'm protected from Control-C now\n");
sleep (3);
signal (SIGINT, oldHandler); /* Restore old handler */
printf ("I can be Control-C'ed again\n");
sleep (3);
printf ("Bye!\n");
}
void myhandler(){
printf(“Do nothing”);
}
Downloaded by Jahongir Azzamov (azzamovjahongir82@gmail.com)
lOMoARcPSD|20259537
2. Consider a text file “news.txt” contains the following:
“The 2017 Maclean’s magazine rankings of Canadian universities illuminates the
growing impact of programs and satisfaction among students at the University of Ontario
Institute of Technology.
The new Maclean’s survey puts the university in the top 10 of Canada’s 19 primarily
undergraduate universities. Based on specific methodology, the university has risen in the
overall rankings from No. 15 in 2012 to No. 8 this year.”
Write a shell script to display the number of occurrences of the word “the” in “news.txt”.
Answer:
total = grep the news.txt | wc -l
echo $total
Downloaded by Jahongir Azzamov (azzamovjahongir82@gmail.com)