PART-ONE123

Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Slide 1:

Operating system is a program that manages the computer hardware and it acts as an intermediary between a user of a computer and the computer hardware. A computer system is divided into four categories Hardware, Operating system Application programs Users. The hardware the central processing unit, the memory and the input and output devices these are basic computing resources. The application programs are such that word processors , compilers these resources are used to solve the computing problems of the users. The operating system controls and coordinates the use of the hardware among the various application programs for the various users.

Slide 2:

Kernel: The memory resident portion of Unix system. File system and process control system are two major components of Unix Kernel. OS interacts directly with the hardware .Such OS is called system kernel. Three major tasks of kernel: Process Management Device Management File Management

Slide 3:

Process: A fundamental entity in a computer network is a process. A program under execution is called a process. Process is an active entity of the program A process is born when the program starts execution and after completion of its execution process is said to die. Each process has a name, normally name of the program. Kernel is responsible for the management of these processes. Each process has unique id called process-id (pid) and it is allotted by kernel. Parent and children Every process is created by some process as file has a parent. The process which creates another process is called parent process. The created process is called child process. One parent can have number of child processes, but one child process has only one parent process. The parent process will die only after all its child process has only one parent processes are completed their execution. Some parent processes are there which are independent on their child processes. Their parent can complete its execution before its child. Process status:- Process status is the information about the process. This information is maintained by the kernel. Ps command is used to display the attributes of a process.

Slide 4:

Mechanism of process creation: Process is created in three phases. There are fork() exec() wait() Fork():- A process is created in unix by fork() system call. It creates a copy of the process that invokes it. When a process is created through fork() that is assigned a new PID. Exec():- The created copy is overwritten with the copy of the program that has to be executed by the exec() system call. Here no additional process is created and it execute the processes created by the fork() system call. Wait():- The parent the executes the wait() system call to keep waiting for the child process to complete. System processes:- system process is the process that keep on running all the time. These processes are not generated by the user. Most of the processes are generated at the time of the system startup. Background process:- It is the process which executes the job in the background.

Slide 5:

Premature termination of a process:- Once a process is created. These signals may be ignored, terminate or do something else by the process. The process can be terminated by sending signal of interruption while it is executing something. This type of termination of process is called premature termination. The command Kill is used to terminate a process. Communication:- One user can communicate with other user in multi-user environment. It is necessary to share the resources like printers etc., among all the users. Two-way communication:- One user can communicate with any person who is currently logged in by using write command in UNIX. One user writes a message and then waits for the reply from the other. In this way it is possible to continue a conversation until both decided to terminate. Draw back:- It is a slow type of conversation, and it may get disturbance to the actual work. The other person would get disturbance though he is doing important work. Shell:- shell is the command interpreter of the UNIX system, which runs at every logged-in terminal . The shell processes a user request and interacts with the kernel to execute the command. The shell makes available three files representing standard input, standard output and standard error to every command that it runs.

Slide 6:

Unix system offers a variety of shells. Bourne shell, Korn shell, Bash and C shell.

Slide 7:

UNIT-1 INTERPROCESS COMMUNICATION IPC is activity of passing the messages from one process to the other process. For interprocess communication, the process must agree each other to communicate, this agreement is provided by the operating system. The IPC can be between two processes of same system or between two processes executing on different systems. User process User process kernel User process User process kernel kernel

IPC On a Single Computer:

IPC On a Single Computer Kernel Process A Process B

IPC on Two Computers:

IPC on Two Computers Process A Process B DOS (Distributed OS) Kernel Kernel

Slide 10:

There are several methods of IPC Pipes FIFO Message queues Semaphores Shared memory LOCKING:- The networking environment provides the interprocess communication, when multiple processes want to share some resources, mutual exclusion is to be provided in order to access that resource by only one process at a time. To provide this type of mutual exclusion, a process to be able to set a lock so that no other process access the file until the process is done. Example: Consider a line printer, which is a shared resource. The jobs to be printed are placed on print queue with a unique sequence number. It maintains a file containing a single line with sequence number in ASCII. This file can be used by the number of processes at a time, so this is a shared resource, hence this file is to be locked for safe use. File locking:- File locking is a process of locking the entire file. The total records will be locked and would not allow them access by other processes. The file should be unlocked after execution is completed.

Slide 11:

Record locking:- Record locking is process of locking a specified portion of a file. Through this locking, the other records, which are not locked can be accessed by other process. In UNIX, there is no record system, because UNIX kernel does not support the concept of records and every thing is treated as a file. Lockf() function is used to lock a file. Syntax:- #include<unistd.h> int lockf(int fd, int function, long size); This system call is successfully executed it can return 0 otherwise it return -1. fd is a file descriptor, size is the length of the file Function has the following arguments 1.F_ULOCK() 2.F_LOCK() 3.F_TEST() 4.F_TLOCK() 1. F_ULOCK():- This argument is used to unlock a file when a file is locked. 2.F_LOCK():- This argument is used to lock a file. 3.F_TEST():- This argument is used to test a file to see whether the file is locked or not. 4.F_TLOCK():- This argument is used to test and lock a file.

Slide 12:

The lockf function provides both the ability to set a lock and to test if a lock is set. When the function is F_LOCK and the region is already locked by another process, the calling process is put to sleep until the region is available. This is termed as blocking . The F_TLOCK() operation, is termed a nonblocking call if the region is not available, lockf returns immediately with a value of -1. Type of locking files and records:- There are two types of locking Advisory locking 2. Mandatory locking 1. Advisory locking:- It is one type of locking process. This locking would not prevent from writing to a file by another process. i.e., a process can ignore an advisory lock and can write to a file that is locked. This ignorance is possible if and only if that process has adequate permissions. This type of locking is suitable for cooperating processes i.e., one process depends on other process. The operation system maintains an information which files has been locked by which processes. This locking is maintained by kernel. UNIX VERSIONS:- There are various versions and flavors of Unix in use today. There are three versions 1. AT & T Unix system V. 2. Berkeley Software Distributions. 3. Microsoft Xenix, system V

Slide 13:

1. AT & T Unix system V:- The first release of system V was in 1983. IPC facilities were provided with system V. Message queues, semaphores and shared memory. There have been various releases of system V. Unix system V release 2.0 in April 1984. Unix system V release 2.0 enhancement release in November 1984. it contained some important features advisory file and record locking and demand paging. Unix system V release 3.0 in 1986. and it provided RFS( remote file sharing), streams , TLI (transport layer interface) and TPI (transport provider interface) etc. Unix system V release 3.1 in 1987. Unix system V release 3.2 in mid 1988 Unix system V release 4.0 in late1989. this release was a merging of AT * T system V with sun Microsystems (4.xBSD derivate). The IPC facilities and the networking features of system V release 3 and 4.3BSD. Mandatory locking:- In this one process locked by resource any other process cannot performed any read, write operations. It is provided by some systems, this lock feature is that the kernel checks every read and write request to verify that the operation does not interfere with a lock held by a process.

Slide 14:

System V release 2 provides advisory locking. System V release 3 provides both advisory locking and mandatory locking. By default, system V release 3 provides advisory locking. To enable mandatory locking for a particular file, we must turn the group-execute bit off and turn the set group-id bit on for the file. System V release 2 advisory locking:- The enhanced version of system V release 2 provided advisory file and record locking. System V release 2 using two routines for file record locking My_lock() My_unlock() My_lock() can implemented as my_lock(fd) int fd; { lseek(fd, 0L,0);//rewind before locking if(lock(fd,F_LOCK,0L)== -1)//0L lock entire file err_sys(“cannot F_LOCK”); } my_unlock(fd) int fd; { lseek(fd, 0L,0); if(lockf(fd,F_ULOCK, 0L)== -1) err_sys(“cannot F_ULOCK”): }

Slide 15:

4.3BSD advisory locking:- This locking is provided by a flock system call to lock and unlock a file. #include<sys/file.h> int flock(int fd, int operation); fd is a file descriptor of an open file and operation is built from the following constants: LOCK_SH Shared lock LOCK_EX Exclusive lock LOCK_UN Unlock LOCK_NB Don’t block when locking LOCK_SH Shared lock:- It is an advisory locking i.e., one process can lock a resource another process performed read, write operations. LOCK_EX Exclusive lock:- It is a mandatory locking i.e., one process can lock a resource another process cannot performed operations from that resource. LOCK_UN Unlock:- This argument is used to unlocked a file. LOCK_NB Don’t block when locking:- This argument is used to test a file either block or unblocking.

Slide 16:

OTHER LOCKING TECHNIQUES:- Other locking techniques are when system call is failed are represented. These techniques create and use an ancillary file as an indicator that the process has the shared resource locked. If the ancillary file exists, the resource is locked by some other process, other wise the calling process must create the ancillary file to lock the resource. The three techniques are used to for locking. Link () Create() Open() The link system call fails the name of the new link to the file already exists. Create system call fails if the file already exists and if the caller does not have write permission for the file. Open system call fails if the file already exists. The first technique is with link() system call. This system call takes an existing file and creates another directory link to that file. If the new pathname specified by the link() which already exists, an error is returned and the global errno is set to EEXIST. int link (char *existing path, char *new path); By this technique, can create a unique temporary file, the name is based on the process ID of the process. Once this file is created, we try to form another link to it, under the “well-known” name of the ancillary lock file is formed. If the link is succeeded, then the process has the lock and the lock file pointed to by two directory entries.

Slide 17:

The temporary file name based on the process ID. The well-known name of the lock file. The second technique is create system call returns an error if the file exists and write permission is denied. If the file already exists the it truncates to zero-length file. If the file access permissions do not permit to write the file for process, create fails and return errno with EACCES. If the create succeeds then the calling process knows it has the lock. The unlock operation removes the temporary lock file. The caller (calling process) does not need either read or write permission for the file being unlinked. The third technique uses the option flags for the open system call that are provided by most recent versions of the UNIX both 4.3 BSD and system V. These flags are defined in <fcntl.h> header file. The open() system call is int open(char *pathname, int oflag,[int mode]); Open fails if the file already exists, other wise the file is created. The flags are O_CREAT|O_EXCL.

Slide 18:

A simple client-server model Client :- A device or application program that makes use of the services provided by a user. A client may be a PC or a work station on a network using services provided from the network file server. Server:- In networking, an computer that makes access to file, printing, communication or other services available to users of the network. In large networks, a server may run a special network operating system, and in small networks a server may run a personal computer operating system. Client-server architecture:- A computing architecture that distributes processing between clients and servers on the network. Clients request information from the servers, while the servers store the data and program and provide network-wide services to the clients. The process of communication between a client and a server can be explained in steps as follows: The client reads a filename from the standard input and writes it to the IPC channel. The server read this filename from the IPC channel and tries to open the file for reading. If the server finds the file, immediately it responds by reading the file and writing it to the IPC channel. If not server, returns with ASCII error message stating that it cannot open the file. The client reads from the IPC channel, writing what it receives to the standard output. If the file cannot read by the server, the client reads an error message from the IPC channel. Other wise, the client reads the contents of the file.

Slide 19:

IPC METHODS IPC is the activity of transferring the data from one process to other process. These processes may be on single system or may be on different systems. The IPC can be performed though some channels, there are mainly five IPC methods Pipes Fifos Message queues Semaphores Shared memories PIPES:- It is a form of the IPC channel. It provides a one way flow of data. These are typically communicate between two different processes. It is a unidirectional flow of data. Pipe can be created by using pipe() system call. Syntax:- int pipe (int *filedes); It returns two types of file descriptor filedes[0]: pipe is open for reading filedes[1]: pipe is open for writing. Pipes can used within single process also, but rarely requires

Slide 20:

Pipe cannot have any name and it identified by the its file descriptor. Pipe in a single process has a finite size always at least 4096 bytes. The rules for reading and writing a pipe when there is either no data in the pipe or when the pipe is full. Pipes are typically used to communicate between two different processes. The following are the steps involved in creation and usage of pipes. A process creates a pipe It forks to create a copy of itself. Parent process closes the read end of the pipe. Child process closes the write end of the pipe. This provides a one-way flow of data between the two processes. The following example shows, how the pipes are used for a piped command . Consider the Unix command which gives the output of all the login names are moved to the line printer in sorted order. When a user enters a command such as $who| sort| lpr The above command creates two pipes, one pipe is to transfer data of all logged on name and another is to pass the names in sorted order to the line printer. Who is a program that outputs the login names, terminal names and login times of all users on the system. The sort program orders this list by login names, and lpr that sends the result to the line printer.

UNIX Pipe Implementation:

Pipes are typically used to communicate between two different (but related) processes. They are implemented using UNIX domain sockets. Two types of sockets in UNIX: 1. internet -- for two processes on different machines 2. UNIX domain -- for two processes on a single machine UNIX Pipe Implementation

Pipe Creation:

Pipe Creation First, a process creates a pipe, and then forks to create a copy of itself. pipe flow of data kernel parent process readfd writefd child process readfd writefd fork

Pipe Examples:

Pipe Examples Parent opens file, child reads file parent closes read end of pipe child closes write end of pipe pipe flow of data kernel parent process writefd child process readfd fork

Slide 24:

who | sort | lpr who process writes to pipe1 sort process reads from pipe1, writes to pipe2 lpr process reads from pipe2 pipe1 flow of data kernel who process writefd lpr process readfd sort process readfd writefd pipe2 flow of data

Slide 25:

Parent sends file name to child. Child opens and reads file, then returns contents to parent. parent creates pipe 1, pipe 2 fork parent closes read end of pipe1 parent closes write end of pipe2 child closes write end of pipe1 child closes read end of pipe2 pipe2 flow of data kernel parent process writefd child process readfd fork pipe1 flow of data readfd writefd

Slide 26:

Limitations of pipes:- Pipes can be applied to only related processes, i.e., pipe can be created between parent child processes only, not related process. Pipes provide only uni-directional data flow. When a two-way flow of data is desired, we must create two pipes and use one for each direction. The actual steps are Create pipe1, pipe2 Fork a child process Parent closes read end of pipe1( perform write onto pipe1 by parent) Parent closes write end of pipe2(perform read from pipe 2 by parent) Child closes write end of pipe1(perform read from pipe1 by child) Child closes read end of pipe2(peform write onto pipe 2 by the child) The main function creates the pipe and forks. The client then runs in the parent process and the server runs in the child process. FIFOS:- FIFO stands for first in first out. A UNIX FIFO is similar to a pipe. It is a one-way flow of data, the transformation is serial, first byte written to FIFO is first byte read from it. A UNIX FIFO is similar to PIPE but FIFO is associated with a name, Hence FIFOs are some times referred as “named pipes.” FIFO can be used by un-related processes also. FIFOs are used in system V, not used in 4.3 BSD.

Slide 27:

A FIFO is created by mknod() system call. INT MKNOD(CHAR *PATHNAME, INT MODE, INT DEV); It returns an integer value that specifies the FIFOs. Pathname specifies the name of the FIFO. Mode specifies the file mode, access mode and S_IFIFO flag. Dev specifies the device name, it is ignored by the FIFO. Once FIFO is created, it must be opened for reading or writing by open() system call or standard I/O functions fopen() or freopen(). So, three system calls to create and use a FIFO is needed. mknod(), open() for reading, open() for writing A pipe or FIFO follows these rules for reading and writing: Reading: If the data requested is less than the data in FIFO, then FIFO returns only the requested amount of data, the remainder is left for next read. If the data requested is more than the data in FIFO, then FIFO returns only the amount of data which is currently available. The requesting process must prepare for this situation. If there is no data in FIFO, then read returns zero with indication end-of-file. Writing:- If a process writes less than the capacity of a FIFO, then the write is guarantee i.e., there is no loss of data. If a process writes more than the capacity of a FIFO, then there is no guarantee write. If a process writes to a FIFO, but there is no process to read it, then SIGPIPE signal is generated and write returns zero with errno set to EPIPE.

Slide 28:

STREAMS AND MESSAGES A stream is collection of data bytes with no interpretation done by the system. This is referred as stream of bytes. The pipes and FIFOs use this stream I/O model. There is no record boundaries in this system. i.e., a process reading 100 bytes from a pipe, it is not sure that other process written that 100 bytes at a time into the pipe. This is OK when fixed length of streams are being transferred. The stream of data is interpreted when writing process and reading process agreed to do. Messages:- When the data streams consists of variable-length, then the sending process can impose a structure on data which it is transferring by a message structure. The many Unix processes does this by new line character to separate each message. The writing process appends a new line to each message and the reading process reads one line at a time from the IPC channel. NAME SPACES Name space is set of possible names for a given type of IPC. Pipe has no name, FIFO has a Unix path name to identify it, similarly all other IPC forms also have names as pathnames. The name space is important because for all forms of IPC in order to exchange messages from client to server and vice-versa.

Slide 29:

The name space for IPC types can be: - IPC TYPE NAME SPACE IDENTIFICATION PIPE NO NAME FILE DESCRIPTOR FIFO PATH NAME FILE DESCRIPTOR MESSAGE QUEUE Key_t key Identifier Shared memory Key_t key Identifier Semaphore Key_t key Identifier Socket-Unix domain Path name FILE DESCRIPTOR Socket-other domain (domain dependent) FILE DESCRIPTOR

Slide 30:

Key_t keys:- key_t is a data type which is defined in <sys/ types.h >. it is a 32-bit integer. The key_t keys are the keys used to identify message queues, semaphores and shared memory IPC. These keys are called System V IPC keys. ftok (): The function ftok () convert a path name and project identifier into a system V IPC key. Syntax:- key_t ftok (char * pathname, char proj ); Path name must be single between server and client for an IPC. Proj argument to ftok () is an 8 bit character. SYSTEM V IPC METHODS System V supports the interprocess communication in three types: Message queues Semaphores Shared memory The above three types collectively referred as “ system V IPC”.

Slide 31:

The kernel maintains a structure of information for each IPC channel. Structure of IPC channel is defined in <sys/ipc.h> struct ipc_perm { ushort uid; /* owners user id */ ushort gid; /* owners group id */ ushort cuid; /* creator’s user id */ ushort cgid; /* creator’s group id */ ushort mode; /* access mode */ ushort seq; /*slot usage sequence number*/ key_t key; /* key */ }; The above structure is access and modified by using three type of ctl system calls msgctl(), semctl(), shmctl() To create or open IPC channel three types of get system calls msgget(), semget(), shmget() All these take key_t key as input. Creating an IPC ids using fork() is as follows ftok() convert and project pathname into key_t key The key_t key is taken by get system call to create/open IPC channel.

Slide 32:

Creation of new IPC or open an existing IPC channel An IPC channel can be created, open an existing IPC channel by following some sequence of steps with the get system calls i.e., msgget(), semget(), shmget(). There are some rules to know whether new IPC channel is created or whether an existing one is referenced. These rules are:- Here IPC_private() generate unique key value, that key value already existed set flag bits. If the key is not created it returns the errno=ENOENT i.e.,error is returned and IPC_CREAT set. All permissions are supported that key value automatically generated a key as identified and return that id. When created key value system table is full it generated error no space other wise generate ID. Flag argument Key does not exist Key already exists No special flags Error, errno =ENOENT Ok IPC_CREAT OK, creates a new entry OK IPC_CREAT| IPC_EXCL OK, creates a new entry Errno , Errno =EEXIST

Slide 33:

MESSAGE QUEUES Message queues are the form of the IPC channel of system V, the messages can be sent to any process from any process i.e., some operating system restrict the passing of messages such that a process can only send a message to another specific process. But system V has such restriction. In the system V implementation of messages, all messages are stored in the kernel and have an associated message queue identifier. The message queue identifier is called as msqid. It identifies a particular queue of messages in this implementation of IPC. There is no requirement that any process be waiting for a message to arrive on a queue before some other process is allowed to write a message to that queue.

Slide 34:

It is possible to read the messages in the queue need not be in the order as they write. It is possible to write some messages to a queue, then exit, and have the messages read by another process at a later time. Each message on a queue has the following attributes:- Long integer type Length of the data portion of the message (can be zero) Data (if the length is greater than zero). Creation of message queue: msgget() system call A new message queue is created, or an existing message queue is accessed with the msgget system call. #include<sys/types.h> #include<sys/ipc.h> #include<sys/msg.h> int msgget(key_t key, int msgflag); This system call is executed successfully it return ‘0’ otherwise ‘-1’. Here key_t key value taken as a name for a message queue, it generated an id called msgid.

Slide 35:

Flag value represented as Numeric Symbolic Description 0400 MSG_R READ BY OWNER 0200 MSG_W WRITE BY OWNER 0040 MSG_R>>3 READ BY GROUP 0020 MSG_W>>3 WRITE BY GROUP 0004 MSG_R>>6 READ BY WORLD 0002 MSG_W>>6 WRITE BY WORLD

Slide 36:

Write a message on queue: msgsnd() system call Once a message queue is opened with msgget, we put a message on the queue using the msgsnd system call. #include<sys/types.h> #include<sys/ipc.h> #include<sys/msg.h> int msgsnd(int msqid, struct msgbuf *ptr, int length, int flag); It returns zero if the system call is successful otherwise returns ‘-1’ on error. The ptr argument is a pointer to a structure with the following template struct msgbuf { long mtype; /* MESSAGE TYPE, MUST BE > 0 */ char mtext [1]; /* MESSAGE DATA */ }; The length argument msgsnd specifies the length of the message in bytes. The flag argument can be specified as either IPC_NOWAIT or as zero.

Slide 37:

Read messages from queue : msgcrv() system call A message is read from a message queue with msgcrv() system call. Syntax:- int msgcrv(int msgid, struct msgbuf *ptr, int length, long msgtype, int flag); The ptr argument is like the one for msgsnd and specifies where the received message is stored. The length specifies the size of the data portion of the structure pointed to by ptr. This system call is executed successfully it return ‘0’ otherwise ‘-1’. msgcrv() system call returns the number of bytes of data in the received message. Msgctl():- To control message queue or to remove existing message queue using msgctl(). Multiplexing messages:- When number of messages with different types placed on single queue, the processes must allow to multiplex (many to one) messages onto a single queue with the help of type of the message. This concept can be explained by considering a server-client process which can use two IPC channels (FIFOs or PIPEs ) to exchange data in both directions or can use a message queue having type of each message signify if the message is from the client to the server or vice-versa.

Slide 38:

If the there are multiple clients and a server processes, then Type 1 can be used to indicate a message from any client to the server. If the client passes its process ID as part of the message, the server can be send its messages to the client processes by using the clients process ID as the message type. Each client process specifies the msgtype as its process ID in msgrcv(). Message queue limitations:- There are certain system limits on message queues. Some of them can be changed by a system administrator, by configuring a new kernel. For system V Maximum size of messages is 8192 bytes. Maximum number of bytes on any one message queue is 16384. Maximum number of message queues is 50 Maximum number of messages on system wide is 40

Slide 39:

STRUCTURE OF SEMAPHORE TO CREATE A SEMAPHORE KERNAL PROVIDED INTERNAL STRUCTURE #include<sys/types.h> #include<sys/ipc.h> struct semid_ds { struct ipc_perm sem_perm; /* operation permission struct*/ struct sem *sem_base; ushort sem_nsems; /* # of semaphores in set */ time_t sem_otime; /* time of last semop*/ time_t sem_ctime; /* time of last change*/ }; The ipc_perm structure contains the access permissions for this particular semaphore. The sem structure is the internal data structure used by the kernel to maintain the set of values for a given semaphore.

Slide 40:

Every member of a semaphore set is described by the following structure: Struct sem { ushort semval; /* semaphore value, nonnegative */ short sempid; /* pid of last operation */ ushort semcnt; /* #awaiting semval>cval*/ ushort semzcnt; /* #awaiting semval =0*/ };

Slide 41:

The kernel maintains the complete information with semid_ds data structure and sem data structure as in the following diagram kernel maintains the semaphore which has two members in its set. System V provides the following system calls in handling the semaphores. Creation/ accessing of semaphore To create a semaphore or open a semaphore by using semget() system call, it defined in <sys/sem.h> header file. Syntax:- int semget(key_t key, int nsems, int semflag); The value returned by semget is the semaphore identifier, semid or -1 if an error occurred. The nsems argument specifies the number of semaphores in the set. The semflag argument specifies values of permissions with numbers 0400,0200,0040,0004 etc.,

Slide 42:

Manipulation of semaphore values:- Once a semaphore set is opened with semget() system call, the semval can be manipulated with semop() system call, it defined in <sys/sem.h>. Syntax:- int semop( int semid, struct sembuf *opsptr, unsigned int nops); semop returns zero if all ok, -1 if an error encountered. opsptr pointer points to an array of sembuf structure.

Slide 43:

The pointer opsptr pointer an array of the following structures: struct sembuf { ushort sem_num; /* semaphore #*/ short sem_op; /* semaphore operation*/ short sem_flag; /*operation flags*/ }; The array of operations in the semop() are done by the kernel, each particular operation specified by the sem_op value can be negative, zero or positive. If sem_op is positive, The value of sem_val is added to the semaphore’s current value. Released resource. If sem_op is zero, The caller wants to wait until the semaphore's value becomes zero. If sem_op is negative, The caller eants to wait until the semaphores value becomes greater than or equal to the absolute value of sem_op. Allocation of reource.

Slide 44:

Control operation on semaphore:- The semctl system call provides control operations on a sempahore. syntax:- int semctl(int semid, int semnum, int cmd, union semun arg); Union semun { int val; /* used for SETVAL only*/ struct semid_ds *buff; /* used for IPC_STAT and IPC_SET*/ ushort *array; /* used for GETALL and SETALL*/ }arg; Cmd to specify the command for example IPC_RMD to remove a semaphore from the system. SETVAL, IPC_SET, GETVAL are the command to control the semaphore.

Slide 45:

LIMITATIONS OF SEMPAHORES:- There are certain system limits with semaphores, these limitations depends on the architectures. These limitations can be changed by reconfiguring the kernel. System V release 2 has the following limitations with semaphores Maximum number of Unique semaphore sets is 10 Semaphores is 60 Semaphores per semaphore set is 25 Operations per semop() call 10 Max val of any semaphore is 32767 Max value of any semaphores adjust on exist value.

Slide 46:

SHARED MEMORY Consider a client-server file copying process it involve the following steps normally The server reads the data file from the input file through the kernel and copies into the servers buffer. Server writes (copies ) the data in messages into IPC channel(pipe, fifo , message queue). The client reads the data from the IPC channel and copies the data into clients buffer. Finally, the data is copied from client buffer to the output file. CLIENT OUT FILE IN FILE PIPE, FIFO AND MESSAGE QUEUE SERVER 3 4 2 1

Slide 47:

The data movements are specified and copied at 1,2,3 ,4. Problem with the above approach In above, 4 copies of data file are required, which more expensive. The problem with IPC forms PIPES, FIFOS, MESSAGE QUEUES is that for two processes to exchange information. The information has to go through the kernel. Kernel uses the kernel buffers to store the online data. The above problem of making unnecessary copies can be avoided by using the concept of shared memory. Shared memory provides a way to share a memory segment by two or more processes. Shared memory is a memory segment. This can be shared by multiple processes. Mutual exclusion must be provided for this shared memory. This can be achieved by using semaphores for synchronization.

Slide 48:

The above example with shared memory implementation: The server gets access to a shared memory segment using a semaphore. The server reads from the input file into the shared memory segment. When the read is complete the server notifies the client, again by using a semaphore. The client write the data from the shared memory segment to the output file. CLIENT OUT FILE IN FILE SHARED MEMORY SERVER KERNEL In this figure the data is only twice from the input file into shared memory and from shared memory to the output file.

Slide 49:

STRUCTURE OF SHARED MEMORY:- The kernel maintains the following structure of information: #include <sys/types.h> #include<sys/ipc.h> Struct shmid_ds { Struct ipc_perm shm_perm; /* operation permission struct*/ int shm_segsz; /*segemnt size*/ struct XXX shm_YYY; /* implemetation dependent info*/ ushort shm_lpid; /*pid of last operation*/ ushort shm_cpid; /*creator pid*/ ushort shm_nattch; /*current # attached*/ ushort shm_cnattch; /*in-core #attached*/ time_t shm_atime; /* last attach time*/ time_t shm_dtime; /* last detach time*/ time_t shm_ctime; /*last change time*/ }; In the above structure, struct xxx shm_yyy; specifies the unknown structure, the actual data structures could not be described which is used by the kernel to point to the shared memory segment, since it is hardware and implementation dependent.

Slide 50:

Creation / opening of shared memory :- A shared memory segment is created, or an existing one is accessed with the shmget system call. int shmget(key_t key, int size, int shmflag); It is defined in <sys.shm.h> header file, shmget returns the shared memory identifier shmid on error -1 is returned size specifies size of the segment. Shmflag specifies the read/write permissions in number/symbolic representation. Accessing shared memory segment:- Once a shared memory segment is created or an existing one is opened with the shmget() system call, it is accessed by the calling process with shmat() system call. We must attach the shared memory segment by calling the shmat system call. It is defined in <sys/shm.h> as char *shmat(int shmid, char *shmaddr. Int shmflag); This system call returns the starting address of the shared memory segment. If the shmaddr argument is zero, the system selects the address for the caller. If the shmaddr argument is nonzero, the returned address depends whether the caller Specifies the SHM_RND value for the shmflag argument. If the shmaddr argument is nonzero, the returned address is depended on the shmflag argument.

Slide 51:

If the value is specified, the shared memory segment is attached at the address specified by the shmaddr argument. If the value is not specified, the shared memory segment is attached at the address specified by the shmaddr argument. By default, the shared memory segment is attached for both reading and writing by the calling process. The SHM_RD only value can also be specified for the shmflag argument specifying “read-only” access.

Slide 52:

Detaching the shared memory segment from a process:- When a process is finished with a shared memory segment, it detaches the segment by calling the shmdt() system call. Syntax:- int shmdt(char *shmaddr); Deleting the shared memory segment:- To remove a shared memory segment, shmctl() system call is used as int shmctl(int shmid, int cmd, struct shmid_ds *buf); cmd argument with IPC_RMID which removes a shared memory segment from the system. Shared memory limitations:- There are certain system limits on shared memory. Some of these can be changed by a system administrator by configuring a new kernel. Portable code can not be possible with these shared memory as compared to the other IPC methods.

Slide 53:

Socket :- A socket implemented by 4.3 BSD it can provided interface between processes on a single system and between processes on different systems. One type of socket the Unix domain socket is used for IPC between processes on a single system. Pipes are implemented in 4.3 BSD using a Unix domain socket. TLI:- TLI stands for Transport Layer Interface and is form of IPC provided with system V Release 3.0. TLI is similar to Berkeley sockets in that it provides communication between processes on the same system or on different systems.