What is a Process A process is an











- Slides: 11

What is a Process? • A process is an executing program and its necessary overhead • a process is not a program by itself

Overview • System Calls – fork() – wait() – pipe() – write() – read() • Examples

Process Creation • fork() NAME fork()- create a new process SYNOPSIS #include <sys/types. h> #include <unistd. h> pid_t fork (void) RETURN VALUE success parent- child pid child 0 failure -1

fork()- program structure #include <sys/types. h> #include <unistd. h> #include <stdlib. h> #include <stdio. h> main( int argc, char* argv[] ) { pid_t pid; if ( ( pid = fork() ) > 0 ) { /*parent*/ } else if ( pid == 0 ) { /*child*/ } else { /*cannot fork*/ } exit( 0 ); }

wait() system call wait() - wait for the process whose pid reference is passed to finish executing SYNOPSIS #include <sys/types. h> #include <sys/wait. h> pid_t wait ( int *stat_loc ) The unsigned decimal integer process ID for which to wait. RETURN VALUE success- child pid failure- -1 and errno is set

wait()- program structure #include <sys/types. h> #include <unistd. h> #include <stdlib. h> #include <stdio. h> main( int argc, char* argv[] ) { pid_t child. PID; if ( ( child. PID = fork() ) == 0 ) { /*child*/ } else { /*parent*/ wait ( 0 ); } exit( 0 ); }

pipe() system call pipe() - to create a read-write pipe that may later be used to communicate with a process we'll fork off. SYNOPSIS int pipe( pfd ) int pfd[2]; PARAMETER: pfd is an array of 2 integers, which that will be used to save the two file descriptors used to access the pipe. RETURN VALUE: 0 - success; -1 - error.

pipe() - structure /* first, define an array to store the two file descriptors */ int pipes[2]; /* now, create the pipe */ int rc = pipe(pipes); if (rc == -1) { /* pipe() failed */ perror("pipe"); exit(1); } If the call to pipe() succeeded, a pipe will be created, pipes[0] will contain the number of its read file descriptor, and pipes[1] will contain the number of its write file descriptor.

write() system call write() - used to write data to a file or other object identified by a file descriptor. SYNOPSIS #include <sys/types. h> size_t write(int fildes, const void *buf, size_t nbyte); PARAMETER fildes is the file descriptor, buf is the base address of the area of memory that data is copied from, nbyte is the amount of data to copy. RETURN VALUE The return value is the actual amont of data written, if this differs from nbyte then something has gone wrong.

read() system call read() - read data from a file or other object identified by a file descriptor. SYNOPSIS #include <sys/types. h> size_t read(int fildes, void *buf, size_t nbyte); ARGUMENT fildes is the descriptor, buf is the base address of the memory area into which the data is read, nbyte is the maximum amount of data to read. RETURN VALUE The actual amount of data read from the file. The pointer is incremented by the amount of data read.

Questions