Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
disk-based files
terminals
printers
interprocess communication facilities, such as pipes and
sockets
Value Meaning
0 standard input
1 standard output
2 standard error
Introduction to Unix System Programming
- For Example,
the “printf()” library function always sends its output
using file descriptor 1.
Fd1
File
Fd2
A single file may be opened several times and, thus may have several
file descriptors associated with it:
A file pointer that records the offset in the file where it is reading and
or writing.
Name Function
System Call: int open( char* fileName, int mode[, int permissions])
“open()” allows you to open or create a file for reading and/or writing.
FLAG MEANING
O_RDONLY Open for read only.
O_WRONLY Open for write only.
O_RDWR Open for both read and write.
FLAG MEANING
O_APPEND Position the file pointer at the end of the file
before each “write()”.
- To create a file, use the O_CREAT flags as part of the mode flags and
supply the initial file-permission flag settings as an octal value.
For example: fd=open("new.txt", O_CREAT,0600);
Introduction to Unix System Programming
System Call: ssize_t read( int fd, void* buf, size_t count)
System Call: ssize_t write( int fd, void* buf, size_t count)
“write()” copies count bytes from a buffer buf to the file referenced
by the file descriptor fd.
If the O_APPEND flag was set for fd, the file position is set to the end
of the file before each “write”.
System Call: off_t lseek( int fd, off_t offset, int mode )
VALUE MEANING
- uses the “close()” system call to free the file descriptor of the inp
- stored in a special area at the start of the disk called the inode
list.
Introduction to Unix System Programming
Duplicating a File Descriptor: dup() and dup2()
- “dup()”, “dup2()”
to duplicate file descriptors, and they work like this:
the original and copied file descriptors share the same file
pointer and access mode.
IPC
Pipes
$ who | wc -l
Pipes
who pipe wc
A simple pipe
Introduction to Unix System Programming
Pipes
- It’s important to realize that both the writer process and the read
process of a pipeline execute concurrently;
- All versions of UNIX support unnamed pipes, which are the kind o
pipes that shells use.
The descriptor associated with the “read” end of the pipe is store
in fd[0],
and the descriptor associated with the “write” end of the pipe is
stored in fd[1].
If a process reads from a pipe whose “write” end has been close
the “read()” call returns a value of zero, indicating the end of in
If a process tries to read more bytes from a pipe than are presen
all of the current contents are returned and “read()” returns
the number of bytes actually read.
int fd[2];
pipe(fd);
5. Each process closes its active pipe descriptor when it’s finished
with it.
Introduction to Unix System Programming
Here’s a small program that uses a pipe to allow the parent to rea
a message from its child:
- The child included the phrase’s NULL terminator as part of the mes
so that the parent could easily display it.
Named Pipes
Named Pipes
- C code that creates a name pipe with read and write permissions
for the owner and group:
$ gcc producer.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
do {
if ((num = read(fd, s, 300)) == -1)
perror("read");
else {
s[num] = '\0';
printf("tick: read %d bytes: \"%s\"\n", num,
s);
}
} while (num > 0);
return 0;
}
Introduction to Unix System Programming
WRITER END
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
int main()
{
int fd1;