CS计算机代考程序代写 file system 60-256 System Programming: Pipes II

60-256 System Programming: Pipes II

Content

COMP-2560 System Programming:
Pipes II
Courtesy of Dr. B. Boufama

School of Computer Science University of Windsor

Instructor: Dr. Dan Wu

Pipes II
1

Copyright @ 2019, 2020, 2021 all rights reserved
Content

Content

1

FIFOs or Named Pipes
The mkfifo() library function Example

Pipes II
2

Copyright @ 2019, 2020, 2021 all rights reserved
FIFOs or Named Pipes

The mkfifo() library function Example

Named pipes
FIFOs or Named Pipes
FIFOs(First In First Out), also called named pipes, offer the following advantages over pipes:
They have a name that exists in the file system. They can be used by unrelated processes.
They exist until explicitly deleted.

Pipes II
3

Copyright @ 2019, 2020, 2021 all rights reserved
FIFOs or Named Pipes

The mkfifo() library function Example

The mkfifo() library function
mkfifo()
Synopsis: int mkfifo(const char *path, mode t mode)
mkfifo() returns 0 if OK, -1 otherwise. Creating a FIFO is similar to creating a file.
Example: mkfifo(“/tmp/channel.fif”, 0755);

Note
mkfifo() is actually a library function (#include
) that invokes the system call mknod(), used to create special files.

Pipes II
4

Copyright @ 2019, 2020, 2021 all rights reserved
FIFOs or Named Pipes

The mkfifo() library function Example

FIFO manipulation

Once a FIFO has been created, it can be treated as a file. In particular, the system calls open(), close(), read(), write() and unlink()(to delete a file) can be used on a FIFO.

To create a FIFO: use mkfifo library function.
To remove a FIFO: use unlink system call unlink(fifoname)
To read from a FIFO: use open(fifoname, O RDONLY), then use read system call.
To Write to a FIFO: use open(fifoname, O WRONLY), then use write system call.

Pipes II
5

Copyright @ 2019, 2020, 2021 all rights reserved
FIFOs or Named Pipes

The mkfifo() library function Example

FIFO manipulation
By default, we have:
Calling open() for read-only blocks the caller until some other process opens the FIFO for writing.
Calling open() for write-only blocks the caller until some other process opens the FIFO for reading.
If a process writes to a FIFO that no process has opened for reading, the signal SIGPIPE will be generated.
When the last writer for a FIFO closes the FIFO, an end-of-file will be generated for the reader.
Like pipes, FIFOS are one-way communication channels.

Note
In case of multiple processes writing to the same pipe, a write
of up to PIPE_BUF bytes is guaranteed to be atomic.

Pipes II
6

Copyright @ 2019, 2020, 2021 all rights reserved
FIFOs or Named Pipes

The mkfifo() library function Example

Example 5

The goal of the following example (example 5) is to write a client/server application where a server accepts data from clients using the FIFO ~/myserver.

Pipes II
7

Copyright @ 2019, 2020, 2021 all rights reserved
FIFOs or Named Pipes

The mkfifo() library function Example

Example 5: a client/server application…(Server part)

#include
#include
#include
#include
#include

// This is the server , server.c
int main(int argc, char *argv[]){ int fd;
char ch;
unlink(“/tmp/server”); // delete it if it exists if(mkfifo(“/tmp/server”, 0777)!=0)
exit(1);

while(1){
fprintf(stderr, “Waiting for a client\n”); fd = open(“/tmp/server”, O_RDONLY); fprintf(stderr, “Got a client: “); while(read(fd, &ch, 1) == 1)
fprintf(stderr, “%c”, ch);
}
}

Pipes II
8

Copyright @ 2019, 2020, 2021 all rights reserved
FIFOs or Named Pipes

The mkfifo() library function Example

Example 5: a client/server application…(Client part)

//client.c
#include
#include
#include
#include
#include

int main(int argc, char *argv[]){ int fd;
char ch;
while((fd=open(“/tmp/server”, O_WRONLY))==-1){ fprintf(stderr, “trying to connect\n”); sleep(1);
}
printf(“Connected: type in data to be sent\n”); while((ch=getchar()) != -1) // -1 is CTRL-D
write(fd, &ch, 1); close(fd);
}

Pipes II
9

Copyright @ 2019, 2020, 2021 all rights reserved