You are on page 1of 2

DEDAN KIMATHI UNIVERSITY OF TECHNOLOGY

University Examinations 2019/2020

THIRD YEAR SEMESTER I EXAMINATION FOR THE DEGREE OF BACHELOR OF SCIENCE


COMPUTER SCIENCE

CCS 3105: SYSTEM PROGRAMMING

DATE: DECEMBER 2019 TIME: 2 HOURS

Instructions: Answer Question 1 and Any Other Two.

Question 1: (30 marks)

a) Define the following terms in context of system programming [4 marks]


(i) File stream
(ii) Buffer
(iii) Pipe
(iv) Garbage collection
b) Differentiate memory corruption from memory leak [2 marks]
c) Suppose you have a file named test.txt with the GNU/Linux permissions set as follows:
chmod 540 test.txt, or
chmod r_x r_ _ _ _ _ test.txt
Required: Explain these permissions [3 marks]
d) Explain the use of the following functions in memory management
(i) void *malloc(size_t size) [2 marks]
(ii) void free(void *ptr) [2 marks]
(iii) void *realloc(void *ptr, size_t size) [2 marks]
e) Explain the use of fork in the following code, and provide the output of the program [3 marks]
int main ()
{
fork();
fork();
printf("Hello world!\n");
}
f) Write a program which displays a message in the parent process and then creates a child process and
displays a different message [4 marks]
g) Explain the two general classes of I/O devices. [2 marks]
h) Compare the regular and special files in a filing system. [2 marks]
i) Differentiate between the getpid() and getppid() system calls in regard to process ID [2 marks]
j) Write a UNIX command that use head and tail in a pipeline to display lines 25 through
75 of a file [2 marks]
1
Question 2 (15 marks)
a) One way to open a file in C programming language is by calling the fopen() function that returns the
file pointer for a specific external file. Give and explain its function prototype [5 marks]
b) Explain what each of the statements in the following program does. [7 marks]
int main() {
int fd = open("foo.txt", O_CREAT|O_TRUNC|O_RDWR, 0644);
if(fork() == 0) { -
dup2(fd, 1);
execlp("echo", "echo", "hello world!", NULL);
}
wait(NULL);
lseek(fd, -7, SEEK_CUR);
write(fd, "planet!\n", 8);
printf("Curr pos: %ld\n", lseek(fd, 0, SEEK_CUR));
c) Explain the meaning of the term memory management and discuss why it is a necessary part of
computer programming. [3 marks]

Question 3 (15 marks)

a) Write a program in C to read a text file for read only and uses perror() to display presence or
absence of an error. [5 marks]
b) Write code of a section of a program to show how memory can be reallocated using both malloc() and
realloc() [7 marks]
c) umask() always sets the process umask and, at the same time, returns a copy of the old umask.
Describe the process you will use to obtain a copy of the current process umask while
leaving it unchanged. [3 marks]

Question 4 (15 marks)

a) Explain the use of the semaphores system calls used in the code below. [7 marks]
sem_t *mutex = sem_open("/mutex", O_CREAT, 0600, 1);
for(i=0; i<5; i++) {
if(fork() == 0) {
while(sem_wait(mutex) < 0) ;
...
sem_post(mutex);
exit(0);
}
}
sem_close(mutex);
sem_unlink("/mutex");
b) Compare and contrast named and unnamed pipes [5 marks]
c) Write an algorithm for a shell script to print a given Number say 10999, in reverse order such that
the input is provided using a command Line Argument only. [3 marks]

You might also like