Docsity

Understanding Sockets and Datagrams in Unix/Linux Systems, Slides of Advanced Operating Systems

Partial preview of the text

Download Understanding Sockets and Datagrams in Unix/Linux Systems and more Slides Advanced Operating Systems in PDF only on Docsity!

CS 620 Advanced Operating

Systems

Lecture 3 – UNIX IPC Programming

Review

UNIX Programming Review

• UNIX manages multiple concurrent processes.

– Each process is represented by an entry in the

process table.

– A process goes through a number of states before

running to completion

• Running

• Asleep

• Ready

• Zombie

Process Creation

#include <stdio.h> #include <unistd.h> #include <stdlib.h> main() { int child_id; child_id = fork(); if ( child_id == 0 ) /* process creation/* child code begin // { printf("Child getpid(), says my pid = %d and my parent pid = %d\n", getppid()); } _exit(0);^ /* child terminates (i) // child code end / / remaining parent code */ if ( child_id < 0 ) { fprintf(stderr, "fork failed\n"); exit(1); } printf("Parent says child pid = %d\n", child_id); }

Program Execution: exec

• The child process can also run a different

program than the parent by overlaying a new

executable file on itself.

– This is done using one of the exec routines: execl,

execv, execve, etc.

– The execed file can either be an executable binary

file or an executable text file (e.g. a shell script).

– The various exec routines vary in the type and

number of arguments they take.

Exec Example

#define WHITE "\t \n" #define MAXARG 20 void background(char *cmd) { char argv[MAXARG]; int id, i = 0; / to fill in argv / argv[i++] = strtok(cmd, WHITE); while ( i < MAXARG && if ( (id = fork()) == 0)^ (argv[i++] = strtok(NULL, WHITE)) != NULL ); / child executes background job / { execv(argv[0], argv); _exit(1); / execv failed */ } else if ( id < 0 ) { fprintf(stderr, "fork failed\n"); perror("background:"); }^ }

Synchronization of Parent and Child

  • After creating a child process using fork, the

parent may run independently, or it may wait for

the child process to terminate before proceeding

further.

  • The wait call searches for a terminated child (in

the zombie state) of the calling process.

  • If there are no child processes, wait returns with a value of -
  • If one or more child processes are in the zombie state, waitselects an arbitrary child, frees its process table slot, stores its

exit status and returns it PID

  • Otherwise, wait blocks until one of the child processesterminates.

Synchronization of Parent and Child

printf("first pid = wait(&status); wait\n"); printf("pid=%d, printf("2nd wait\n"); status=%d\n", pid, status); pid printf("pid=%d, = wait(&status); status=%d\n", pid, status);

}^ return(0);

More on Wait

• The waitpid system call can be used to search

for a particular child

– Another system call – waitid – provides even more

flexibility.

• The ‘status’ returned can be examined more

closely to get info about how the process

terminated by using one of several macros

  • WEXITSTATUS(status), WIFSIGNALED(status),

WCOREDUMP(status), WSTOPSIG(status), etc.

Interrupts and Signals

– From the shell, a signal can be sent using the kill

command with the signal to be sent along with

the PID.

– From a C program, the function raise sends a

signal to the same process, while the function kill

sends a signal to some other process.

– If a signal is not currently blocked by a process,

further occurrences of the signal are blocked

during signal handling.

Interrupts and Signals

– The function is suspended and the handler

function for the signal is called, and finally if the

handler function returns, the signal is unblocked

and normal execution of the process resumes

from the point of interrupt.

– After receiving a signal, a process normally either

exits or stops. This default behavior can be

changed using the signal function, specifying a

handler function for a specific signal. When that

signal is received, the handler function will be

invoked.

IPC and Network Communication

  • Two processes residing either on the same

computer or on different computers may need to

exchange data. This is known as Inter Process

Communication ( IPC ).

  • If the two processes are related by a fork, IPC can

be done through the pipe mechanism (the

processes are on the same machine).

  • If the two processes are not related, IPC can be

performed using the socket mechanism.

Pipes

– A pipe is a direct (in memory) I/O channel

between processes.

– It is used with the calls fork, exec, wait, and exit to

make multiple processes cooperate.

– To establish a pipe use the system callpipe(int fildes[2]) int

• This establishes a buffer and two descriptors fildes[0]

for reading the pipe and fildes[1] for writing the pipe.

• The pipe is created before the fork, so both parent andchild have copies of the pipe.

Pipes

#include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/wait.h>

int main(int argc, char argv[]) { int p[2]; int i, pid, status; char buffer[20]; pipe(p); if ((pid = fork()) == 0) / setting up the pipe / / in child / { close(p[1]); / child closes p[1] / while ((i = read(p[0], buffer, 6)) != 0) { buffer[i] = '\0'; / string terminator / }^ printf("%d chars :%s: received by child\n", i, buffer); }^ _exit(0);^ / child terminates^ */

Pipes

/* in parent / close(p[0]); / parent writes p[1] / write(p[1], "Hello there,", sizeof("Hello write(p[1], " from me.", sizeof(" from me.")); there,")); close(p[1]); while (wait(&status)!=pid); / finished writing p[1] // waiting for pid */ if (status == 0) else printf("child failed\n"); printf("child finished\n");

}^ return(0);