Did you know some place in internet where there is good C programming code? because i want to research those code to learn C programming language, and practice.
and how to join in the C newsgroup?
thank you.
question2 :
"Many system calls relate to the file system.To create a new file, the CREAT call is used. It's parameters provide the name of the file and the protection mode. Thus
fd = creat("abc", 0751);
create a file called abc with mode 0751 octal .
CREAT not only create a new file but also opens it for writing, regardless of the file's mode. The file descriptor returned, fd, can be used to write the file. ......"
"To read or write an existing file, the file must first be opened using OPEN.
This call specifies the file name to be opened, either as an absolute path name or
relative to the working directory, and a code of 0-RDONLY. 0-WRONLY, or
0-RDWR, meaning open for reading, writing, or both. The file descriptor
returned can then be used for reading or writing. Afterward, the file can he closed
by CLOSE, which makes the file descriptor available for reuse on a subsequent
CREAT or OPEN."
" When manipulating file descriptors, the DUP call is occasionally helpful. Consider,
for example, a program that needs to close standard output (file descriptor
l), substitute another file as standard output, call a function that writes some output
onto standard output, and then restore the original situation. Just closing file
descriptor 1 and then opening a new file will make the new file standard output
(assuming standard input, file descriptor 0, is in use), but it will be impossible to
restore the original situation later.
The solution is first to execute the statement
fd = dup(1);
which uses the DUP system call to allocate a new file descriptor, fd, and arrange
for it to correspond to the same file as standard output. Then standard output can
be closed and a new file opened and used. When it is time to restore the original
situation, file descriptor 1 can be closed, and then
n = dup(fd)
executed to assign the lowest file descriptor, namely, 1, to the same file as jd.
Finally, fd can be closed and we are back where we started.
The DUP call has a variant that allows an arbitrary unassigned file descriptor
to be made to refer to a given open file. It is called by
dup2(fd, fd2)
where fd refers to an open file and fd2 is the unassigned file descriptor that is to be
made to refer to the same file as fd. Thus if fd refers to standar$ input (file
descriptor 0) and fd2 is 4, after the call, file descriptors 0 and 4 will both refer to
standard input. "
Interprocess communication in MINIX uses pipes, as described earlier. When
a user types
cat file1 file2 | sort
the shell creates a pipe and arranges for standard output of the first process to
write to -the pipe, so standard input of the second process can read from it. The
PWE system call creates a pipe and returns two file descriptors, one for writing and
one for reading. The call is
pipe(&fd[0]);
where fd is an array of two integers and fd[O) is the file descriptor for reading and
fd [I] is the one for writing. Typically, a FORK comes next, and the parent closes
the file descriptor for reading and the child closes the file descriptor for writing
(or vice versa), so when they are done, one process can read the pipe and the other
can write on it.
Figure 1- 13 depicts a skeleton procedure that creates two processes, with the
output of the first one piped into the second one.
First a pipe is created, and then the procedure
forks, with the parent eventually becoming the first process in the pipeline
and the child process becoming the second one. Since the files to be executed,
process] and process2, do not know that they are part of a pipeline, it is essential
that the file descriptors be manipulated so that the first process' standard output be.
the pipe and the second one's standard input be the pipe. The parent first closes
off the file descriptor for reading from the pipe. Then it closes standard output
and does a DUP call that allows file descriptor 1 to write on the pipe. It is important
to realize that DUP always returns the lowest available file descriptor, in this
case, 1 . Then the program closes the other pipe file descriptor.
#define STD_INPUT 0 /* file descriptor for standard input */
#define STD_OUTPUT 1 /* file descriptor for standard output */
pipeline(process1, process2)
char *processl, *process2; /* pointers to program names */
{
int fd[2];
pipe(&fd[0]); /* create a pipe */
if (fork() != 0) {
/* The parent process executes these statements. */
close(fd[0]); /* process 1 does not need to read from pipe */
close(STD_OUTPUT); /* prepare for new standard output */
dup(fd[1]); / * set standard output to fd[l] */
close(fd[1]); /* this file descriptor n d needed any more */
execl(process1, process1 ,0);
) else {
/* The child process executes these statements. */
close(fd[1]); /* process 2 does not need to write to pipe */
close(STD_INPUT); /* prepare for new standard input */
dup(fd[0]); /* set standard input to fd[0] */
close(fd[0]); /* this file descriptor not needed any more */
execl(process2, process2,0);
}
}
Figure 1-13. A skeleton for setting up a two-process pipeline.
/* what is the file descriptor? */
/* what is system calls ? */
/* what is the meaning of dup(1) , dup(fd), dup2? */
thank you.