December 1998
CA208: 'C' PROGRAMMING

QUESTION 2

Total Marks: 20 Marks

Click here to access other questions

Click to access
SUGGESTED SOLUTIONS
for Question 2

 

 

 

In Unix, a file descriptor set (fd_set) is a data-structure in which each bit represents the availability of a different device in the system. In a system that only has a maximum of 32 distinct devices, a file descriptor set can be represented by data-type fd_set below. For example, if devices zero, three, and four becomes available, then this will be represented by an fd_set value of 25 (i.e., the binary value 00000000000000000000000000011001). The following question will require the definition of the following C functions:

typedef unsigned int fd_set;

fd_set FD_SET(fd_set p, int n);
fd_set FD_CLR(fd_set p, int n);
int    FD_ISSET(fd_set p, int n);
int    FDS_ACTIVE(fd_set p);

 

(a) What would be returned if the following expressions were evaluated: [5]
(i) 2 & 3
(ii) 3 | 9
(iii) 21 & (~4)
(iv) 1 << 3
(v) 21 >> 2

 

(b) Define a macro FD_ZERO, that takes a single argument that represents a fd_set, which initialise each bit of the file descriptor set to 0.

 

[2]
(c) Define a function FD_SET, that returns as it's result a fd_set that augments the value represented by p such that the nth bit (the rightmost bit is bi 0) of fd_set is set to one, i.e., fd_set(5,1) should return 7.

 

[4]
(d) Define a function FD_CLR, that returns as it's result a fd_set that augments the value represented by p such that the nth bit (the rightmost bit is bit 0) of the file descriptor set to one, i.e., fd_clr(5,0) should return 4.

 

[3]
(e) Define a function FD_ISSET, that returns zero if the  nth bit (the rightmost bit is bit 0) of the file descriptor set is zero, and a non-zero value otherwise, i.e., fd_isset(5,2) returns an integer that is not zero, whereas fd_isset(5,1) return 0.

 

[2]
(f) Define a function FDS_ACTIVE, that returns the number of bits that are set within the file descriptor set.

 

[4]