Systems Programming
Systems Programming
SECTION A
Question 1: (30 Marks)
(a) Write the Linux command(s) to change the permissions of your file /usr/home/file.dat as
follows: (3 marks)
(i) you can read, write and execute
(ii) the users in your group can read and execute, but cannot write (iii) rest of
users can only read
(b) Write the following Linux shell script: From the processes that are currently running, find
and display the number of processes that are run by your user name and by other users.
(5 marks)
(c) Suppose the file /usr/home/students.dat contains student information as: Name, id,
Birthday, Department. Write a Linux shell script to sort the file according to Name. Then print
only the Name and Department of the last student. (5 marks)
(d) Explain the use of the following functions in memory management (6 marks)
(i) void *malloc(size_t size);
(ii) void free(void *ptr);
(i) void *realloc(void *ptr, size_t size);
(g) Compare the regular and special files in a filing system. (4 marks)
(h) Explain two major advantages and one disadvantage of using threads instead of heavy
processes. (3 marks)
SECTION B
(a) fd is an open, writable file descriptor for a zero-length file in a Unix-like file system. Its
file pointer is set to an offset of 109 bytes. What would each block written by the
following function call contain?
(c) Explain what each of the statements in the following program does. (8 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));
}
(a) Indicate next to each directory path below whether it is an absolute (A) directory or a
relative (R): (5 marks)
(i) ../var/log/messages.txt
(ii) /tmp/mary
(iii) ../../repository
(iv) /tmp/report.txt
(v) /var
(b) Write some code to show how memory can be reallocated using both malloc() and
realloc() (12 marks)
(c) Explain how the unlink() system call works. (3 marks)
(i) Use the following sketch and complete the drawing of the process tree, where
each dot represents a process and a fork results in a split, up for the parent and
down for the child. (4 marks)
(ii) How many total processes are created, including the initial parent process? How
many total processes are running after the loop? Explain (4 marks)
(iii) How many times does \Child: ..." print and how many times does “Parent: ..."
print to standard output? Explain. (3 marks)
(iv) Does the message “Process signaled" print? Why or why not based on the value
of status? (3 marks)
(v) At the end of the program, are there any zombie processes? If so, why are there
zombies and what happens to them when the parent terminates? If not, why aren't
there any zombie processes? (4 marks)