C Programming Code Examples
C > Gnu-Linux Code Examples
Show some pipe usage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* Show some pipe usage */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
/* the file descriptors */
int pipe_file_descr[2];
/* pipe creates a pair of file descriptors */
pipe(pipe_file_descr);
/* create a child process .... */
if(!fork()) {
/*
// dont need ``normal'' stdout() so...
// closing ``normal'' stdout
*/
close(1);
/*
// Now we make stdout of this process
// the same as pipe_file_descr[1]
// dup() `is duplicate ...'
*/
dup(pipe_file_descr[1]);
/*
// Don't need this pipe file descriptor
// since we have no input, only output for this process
// closing this file descriptor ...
*/
close(pipe_file_descr[0]);
/*
// Fire up `ls' to have some output
*/
execlp("ls", "ls", "-1", NULL);
} else {
/*
// Don't need ``normal'' stdin
*/
close(0);
/*
// making stdin the same as pipe_file_descr[0]
*/
dup(pipe_file_descr[0]);
/* again, don't need this one .. */
close(pipe_file_descr[1]);
/* count the output of `ls' ... */
execlp("wc", "wc", "-l", NULL);
}
/* done */
return 0;
}
Closes a file descriptor, fildes. This frees the file descriptor to be returned by future open() calls and other calls that create file descriptors. The fildes argument must represent a hierarchical file system (HFS) file. When the last open file descriptor for a file is closed, the file itself is closed. If the file's link count is 0 at that time, its space is freed and the file becomes inaccessible. When the last open file descriptor for a pipe or FIFO file is closed, any data remaining in the pipe or FIFO file is discarded. close() unlocks (removes) all outstanding record locks that a process has on the associated file. Behavior for sockets: close() call shuts down the socket associated with the socket descriptor socket, and frees resources allocated to the socket. If socket refers to an open TCP connection, the connection is closed. If a stream socket is closed when there is input data queued, the TCP connection is reset rather than being cleanly closed.
The pipe() function shall create a pipe and place two file descriptors, one each into the arguments fildes[0] and fildes[1], that refer to the open file descriptions for the read and write ends of the pipe. Their integer values shall be the two lowest available at the time of the pipe() call. The O_NONBLOCK and FD_CLOEXEC flags shall be clear on both file descriptors. (The fcntl() function can be used to set both these flags.) Data can be written to the file descriptor fildes[1] and read from the file descriptor fildes[0]. A read on the file descriptor fildes[0] shall access data written to the file descriptor fildes[1] on a first-in-first-out basis. It is unspecified whether fildes[0] is also open for writing and whether fildes[1] is also open for reading.
The exec family has many functions in C. These C functions are basically used to run a system command in a separate process that the main program and print the output. execl() does not use the PATH environment variable. So, the full path of the executable file is required to run it with execl(). execlp() uses the PATH environment variable. So, if an executable file or command is available in the PATH, then the command or the filename is enough to run it, the full path is not needed. The execlp() function replaces the current process image with a new process image specified by file. The new image is constructed from a regular, executable file called the new process image file. No return is made because the calling process image is replaced by the new process image. This function is declared in <process.h>, which <unistd.h> includes.
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming. These operators are used to perform logical operations and used with conditional statements like C if-else statements.
The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.
duplicate an open file descriptor. The dup() system call allocates a new file descriptor that refers to the same open file description as the descriptor oldfd. The new file descriptor number is guaranteed to be the lowest-numbered file descriptor that was unused in the calling process. After a successful return, the old and new file descriptors may be used interchangeably. Since the two file descriptors refer to the same open file description, they share file offset and file status flags; for example, if the file offset is modified by using lseek() on one of the file descriptors, the offset is also changed for the other file descriptor. The two file descriptors do not share file descriptor flags (the close-on-exec flag). The close-on-exec flag (FD_CLOEXEC;) for the duplicate descriptor is off. Upon successful completion a non-negative integer, namely the file descriptor, shall be returned; otherwise, -1 shall be returned and errno set to indicate the error.
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:
Program code print Armstrong Number from 1 to 1000. An Armstrong number is an n-digit base b number such that the sum of its (base b) Digits Raised to the Power n is the number
The statements inside the body of "if" only execute if the given condition returns true. If the condition returns false then statements inside "if" are skipped. Else and else..if are...
This C Program takes a Positive Integer from the user & displays all the positive factors of that number. The for loop is iterated until j<= Number is False. In each iteration, number is
In C program user is asked to enter the binary number and the program then converts that binary number to the octal number by calling a user defined function. To understand this...
Program takes a number and checks whether a given number is Prime or not. Take number as Input & Check if the number is Divisible by any of the natural numbers starting from 2. If
C language code to pick & display the largest element of an input matrix. How many rows in the matrix? How many columns in the matrix? The largest element of the matrix is
First, create a file to sort, write unsorted data to disk and now, sort the file. Sorting disk file. A Quicksort for files. First read in record i and j and then write them back in opposite slots.