Birla Institute of Technology & Science, Pilani
Work-Integrated Learning Programmes Division
Second Semester 2016-2017
Mid-Semester Test
(EC-2 Make-up-SOLUTION)
Course No. : IS ZC462
Course Title : NETWORK PROGRAMMING
Nature of Exam : Close Book
Weightage : 30%
Duration : 2 Hours
Date of Exam : 19/03/2017 (AN)
Q.1. Explain the control operations that can be performed on Message queues? [4]
Message Queue Control Operations :
The msgctl() system call performs control operations on the message queue identified
by msqid.
#include <sys/types.h>
#include <sys/msg.h>
int msgctl (int msqid, int cmd, struct msqid_ds *buf);
- Returns 0 on success, or –1 on error-
The cmd argument specifies the operation to be performed on the queue. It can be one of the following:
- IPC_RMID: Immediately remove the message queue object and its associated msqid_ds data structure. All messages remaining in the queue are lost, and any blocked reader or writer processes are immediately awakened, with msgsnd () or msgrcv () failing with the error EIDRM. The third argument to msgctl() is ignored for this operation.
- IPC_STAT : Place a copy of the msqid_ds data structure associated with this message queue in the buffer pointed to by buf. We describe the msqid_ds structure.
- IPC_SET : Update selected fields of the msqid_ds data structure associated with this message queue using values provided in the buffer pointed to by buf.
Q.2. Explain the socket primitives to exchange the data with stream socket? [6]
The Socket Primitives to exchange the data with stream socket:
Socket Primitives
|
Meaning
|
Socket
|
The socket() system call creates a new socket.
int socket(int domain, int type, int protocol);
- Returns file descriptor on success, or –1 on error
|
Bind
|
The bind() system call binds a socket to an address. Usually, a server employs this call to bind its socket to a well-known address so that clients can locate the socket.
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
- Returns 0 on success, or –1 on error
|
Listen
|
The listen() system call allows a stream socket to accept incoming connections from other sockets.
int listen(int sockfd, int backlog);
- Returns 0 on success, or –1 on error
|
Accept
|
The accept() system call accepts a connection from a peer application on a listening stream socket, and optionally returns the address of the peer socket.
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
- Returns file descriptor on success, or –1 on error
|
Connect
|
The connect() system call establishes a connection with another socket.
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
- Returns 0 on success, or –1 on error
|
Read
|
The read() system call receives some data from the connection.
ssize_t read(int fd, void *buffer, size_t count);
- Returns number of bytes read, 0 on EOF, or –1 on error
|
Write
|
The write() system call sends some data over the connection.
ssize_t write(int fd, void *buffer, size_t count);
- Returns number of bytes written, or –1 on error
|
Close
|
The close() system call releases the connection.
int close (int sockfd);
- Returns: 0 if OK, -1 on error
|
Q.3. Explain the algorithm used by kernel to check the permission on the file? [6]
File permissions :
File permissions mask divides the permissions into three categories:
- Owner (also known as user): The permissions granted to the owner of the file.
- Group: The permissions granted to users who are members of the file’s group.
- Other: The permissions granted to everyone else
Three permissions may be granted to each category:
- Read: The contents of the file may be read.
- Write: The contents may be changed.
- Execute: The file may be executed (i.e., it is a program or a script). In order to execute a script file (e.g., a bash script), both read and execute permissions are required.
The algorithm used by the kernel to check the permission on the file are as follows:
- If the process is privileged, all access is granted.
- If the effective user ID of the process is the same as the user ID (owner) of the file, then access is granted according to the owner permissions on the file. For example, read access is granted if the owner-read permission bit is turned on in the file permissions mask; otherwise, read access is denied.
- If the effective group ID of the process or any of the process supplementary group IDs matches the group ID (group owner) of the file, then access is granted according to the group permissions on the file.
- Otherwise, access is granted according to the other permissions on the file.
Q.4. Explain the difference between named pipe and unnamed pipe: [4]
named pipe
|
unnamed pipe
|
1. A named type has a specific name which can be given to it by the user. Named pipe if referred through this name only by the reader and writer. All instances of a named pipe share the same pipe name.
|
1. An unnamed pipes is not given a name. It is accessible through two file descriptors that are created through the function pipe(fd[2]), where fd[1] signifies the write file descriptor, and fd[0] describes the read file descriptor.
|
2. A named pipe can be used for Communication between two unnamed process as well. Processes of different ancestry can share data through a named pipe.
|
2. An unnamed pipe is only used for communication between a child and it’s parent process
|
3. A named pipe exists in the file system. After input-output has been performed by the sharing processes, the pipe still exists in the file system independently of the process, and can be used for communication between some other processes.
|
3. An unnamed pipe vanishes as soon as it is closed, or one of the process (parent or child) completes execution.
|
4. Named pipes can be used to provide communication between processes on the same computer or between processes on different computers across a network, as in case of a distributed system.
|
4. Unnamed pipes are always local; they cannot be used for communication over a network.
|
5. A Named pipe can have multiple process communicating through it, like multiple clients connected to one server.
|
5. An unnamed pipe is a one-way pipe that typically transfers data between a parent process and a child process.
|
Q.5. Explain 4 way handshake procedure in TCP connection termination? [5]
The 4-way handshake procedure in TCP connection termination:
1. One application calls close first, and we say that this end performs the active close. This end's TCP sends a FIN segment, which means it is finished sending data.
2. The other end that receives the FIN performs the passive close. The received FIN is acknowledged by TCP. The receipt of the FIN is also passed to the application as an endof-file (after any data that may have already been queued for the application to receive), since the receipt of the FIN means the application will not receive any additional data on the connection.
3. Sometime later, the application that received the end-of-file will close its socket. This causes its TCP to send a FIN.
4. The TCP on the system that receives this final FIN (the end that did the active close) acknowledges the FIN.
Q.6. Explain the mechanism to attach a shared memory to process? [5]
The mechanism to attach a shared memory to process :
1. Call shmget() to create a new shared memory segment or obtain the identifier of an existing segment (i.e., one created by another process). This call returns a shared memory identifier for use in later calls.
int shmget(key_t key, size_t size, int shmflg);
- Returns shared memory segment identifier on success, or –1 on error
2. Use shmat() to attach the shared memory segment; that is, make the segment part of the virtual memory of the calling process.
void *shmat(int shmid, const void *shmaddr, int shmflg);
- Returns address at which shared memory is attached on success, or (void *) –1 on error
***********
No comments:
Post a Comment